-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerakiHaAgent.cs
90 lines (76 loc) · 2.9 KB
/
MerakiHaAgent.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
using MerakiHAWatch.Az;
using MerakiHAWatch.Model;
using MerakiProbe;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace MerakiHAWatch
{
public class MerakiHaAgent
{
private ILog _log;
private IDefineRoute _router;
private IGetLossLatency _probe;
private IPersistFailover _blob;
private List<RouteInfo> _routes;
const string LogName = "MerakiProbe";
public MerakiHaAgent(IGetLossLatency probe, IDefineRoute router, IPersistFailover blob, ILog log, List<RouteInfo> routes)
{
_log = log;
_router = router;
_probe = probe;
_blob = blob;
_routes = routes;
}
public bool CanProbeActive(int maxLossPercent)
{
// Get HA Pair from blob storage
var hapair = _blob.GetNodes();
// probe standby node
var probe = _probe.GetLossLatency(hapair.activeNode.networkId, hapair.activeNode.deviceSerial);
return ((probe.lossPercent > maxLossPercent) ? false : true);
}
public bool CanProbeStandby(int maxLossPercent)
{
// Get HA Pair from blob storage
var hapair = _blob.GetNodes();
// probe standby node
var probe = _probe.GetLossLatency(hapair.standByNode.networkId, hapair.standByNode.deviceSerial);
return ((probe.lossPercent > maxLossPercent) ? false : true);
}
public MerakiProbeResult Failover(int maxLossPercent)
{
// Get HA Pair from blob storage
var hapair = _blob.GetNodes();
// probe active node
var probe = _probe.GetLossLatency(hapair.activeNode.networkId, hapair.activeNode.deviceSerial);
var logentry = new MerakiProbeResult()
{
latencyMs = probe.latencyMs,
lossPercent = probe.lossPercent,
ActiveVmName = hapair.activeNode.vmName,
Failover=false
};
if (probe.lossPercent > maxLossPercent)
{
// failover updating all route tables
foreach(RouteInfo route in _routes)
{
_router.UpdateRoute(route.ResourceGroup, route.RouteTableName, route.RouteName, hapair.standByNode.privateIp);
}
var newhapair = new VmxHaPair()
{
activeNode = hapair.standByNode,
standByNode = hapair.activeNode
};
_blob.SaveNodes(newhapair);
logentry.Failover = true;
logentry.Message = probe.ErrorMessage;
}
var json = JsonConvert.SerializeObject(logentry);
_log.Log(LogName, json);
return logentry;
}
}
}