-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerakiHA.cs
91 lines (78 loc) · 3.94 KB
/
MerakiHA.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using MerakiProbe;
using System.Configuration;
using MerakiHAWatch.Az;
using System.Collections.Generic;
using MerakiHAWatch.Model;
using Newtonsoft.Json;
namespace MerakiHAWatch
{
public static class MerakiHA
{
[FunctionName("MerakiHA")]
public static void Run([TimerTrigger("0 */2 * * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation($"Function executed at: {DateTime.Now}");
// Meraki dashboard parameters
var apiKey = Environment.GetEnvironmentVariable("MerakiApiKey");
var networkId = Environment.GetEnvironmentVariable("MerakiNetworkId");
var targetIp = Environment.GetEnvironmentVariable("MerakiTargetIp");
var timespan = Environment.GetEnvironmentVariable("MerakiProbeTimespan");
// UDR parameters
AadCredential cred = new AadCredential()
{
TenantId = Environment.GetEnvironmentVariable("AadTenantId"),
ClientId = Environment.GetEnvironmentVariable("AadClientId"),
ClientSecret = Environment.GetEnvironmentVariable("AadClientSecret")
};
var subscription = Environment.GetEnvironmentVariable("AzSubscriptionId");
// route table parameters
var udrrg = Environment.GetEnvironmentVariable("AzRouteTableResourceGroup");
var routeTableName = Environment.GetEnvironmentVariable("AzRouteTableName");
var routeNames = JsonConvert.DeserializeObject<List<object>>(Environment.GetEnvironmentVariable("AzRouteNames"));
// Blob Storage Parameters
var blobCnString = Environment.GetEnvironmentVariable("BlobCnString");
var container = Environment.GetEnvironmentVariable("BlobContainerName");
var blobname = Environment.GetEnvironmentVariable("BlobName");
// Log Analytics parameters
var workspaceId = Environment.GetEnvironmentVariable("LogAnalyticsWorkspaceId");
var workspaceKey = Environment.GetEnvironmentVariable("LogAnalyticsWorkspaceKey");
// max loss percent
var maxLossPercent = Environment.GetEnvironmentVariable("MaxLossPercent");
// create all dependency injection objects
IGetLossLatency probe = new ProbeAgent(apiKey, targetIp);
IDefineRoute router = new RouteAgent(cred, subscription);
IPersistFailover blob = new BlobAgent(blobCnString, container, blobname);
ILog logger = new AzMonLogger(workspaceId, workspaceKey);
// constructor
List<RouteInfo> routes = new List<RouteInfo>();
foreach (var routeName in routeNames)
{
routes.Add(new RouteInfo()
{
ResourceGroup = udrrg,
RouteTableName = routeTableName,
RouteName = routeName.ToString()
});
}
MerakiHaAgent agent = new MerakiHaAgent(probe, router, blob, logger, routes);
// Probe and failover if needed
var activeProbe = agent.CanProbeActive(int.Parse(maxLossPercent));
var standbyProbe = agent.CanProbeStandby(int.Parse(maxLossPercent));
// Don't failover if both nodes are unreachable
if (!activeProbe && standbyProbe)
{
var result = agent.Failover(int.Parse(maxLossPercent));
log.LogInformation("Loss={0}, Failover={1}, Message={2}", result.lossPercent, result.Failover, result.Message ?? "OK");
}
// Both nodes are down, don't failover but alert
if (!activeProbe && !standbyProbe)
{
log.LogCritical("Loss={0}, Failover={1}, Message={2}", "100", "false", "Both nodes unreachable, will not failover.");
}
}
}
}