This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
UptimeRobotCheck.cs
163 lines (125 loc) · 5.07 KB
/
UptimeRobotCheck.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using AnyStatus.API;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace AnyStatus
{
public class UptimeRobotCheck : ICheckHealth<UptimeRobot>
{
private const string UptimeRobotApi = "https://api.uptimerobot.com";
private const string ConstApiParams = "format=json&noJsonCallback=1";
private readonly ILogger _logger;
public UptimeRobotCheck(ILogger logger)
{
_logger = Preconditions.CheckNotNull(logger, nameof(logger));
}
[DebuggerStepThrough]
public async Task Handle(HealthCheckRequest<UptimeRobot> request, CancellationToken cancellationToken)
{
UptimeRobotMonitorStatus status;
if (string.IsNullOrEmpty(request.DataContext.MonitorName))
{
var monitors = await GetMonitors(request.DataContext).ConfigureAwait(false);
status = monitors.Max(k => k.status);
}
else
{
var monitor = await GetMonitor(request.DataContext).ConfigureAwait(false);
status = monitor.status;
}
request.DataContext.State = GetState(status);
}
private static State GetState(UptimeRobotMonitorStatus status)
{
switch (status)
{
case UptimeRobotMonitorStatus.Pause:
return State.Disabled;
case UptimeRobotMonitorStatus.NotChecked:
return State.None;
case UptimeRobotMonitorStatus.Up:
return State.Ok;
case UptimeRobotMonitorStatus.SeemsDown:
case UptimeRobotMonitorStatus.Down:
return State.Failed;
default:
return State.Unknown;
}
}
private async Task<Monitor> GetMonitor(UptimeRobot uptimeRobot)
{
var monitorsResponse = await SendMonitorsRequest(uptimeRobot.ApiKey, uptimeRobot.MonitorName).ConfigureAwait(false);
if (monitorsResponse.stat == "fail")
{
throw new Exception($"\"{uptimeRobot.Name}\" failed to update. Uptime Robot: {monitorsResponse.message}.");
}
return monitorsResponse.monitors.monitor.First();
}
private async Task<IEnumerable<Monitor>> GetMonitors(UptimeRobot uptimeRobot)
{
var total = 1;
var monitors = new List<Monitor>();
while (monitors.Count < total)
{
var monitorsResponse = await SendMonitorsRequest(uptimeRobot.ApiKey, monitors.Count).ConfigureAwait(false);
if (monitorsResponse.stat == "fail")
throw new Exception($"\"{uptimeRobot.Name}\" failed to update. Uptime Robot: {monitorsResponse.message}.");
monitors.AddRange(monitorsResponse.monitors.monitor);
total = monitorsResponse.total;
}
return monitors;
}
private async Task<MonitorsResponse> SendMonitorsRequest(string apiKey, string monitorName)
{
var uri = new Uri($"{UptimeRobotApi}/getMonitors?apiKey={apiKey}&{ConstApiParams}&limit=1&search={monitorName}");
using (var client = new HttpClient())
{
var response = await client.GetAsync(uri).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return new JavaScriptSerializer().Deserialize<MonitorsResponse>(content);
}
}
private async Task<MonitorsResponse> SendMonitorsRequest(string apiKey, int offset)
{
var uri = new Uri($"{UptimeRobotApi}/getMonitors?apiKey={apiKey}&{ConstApiParams}&offset={offset}");
using (var client = new HttpClient())
{
var response = await client.GetAsync(uri).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return new JavaScriptSerializer().Deserialize<MonitorsResponse>(content);
}
}
#region Contracts
public class Monitor
{
public UptimeRobotMonitorStatus status { get; set; }
}
public class Monitors
{
public List<Monitor> monitor { get; set; }
}
public class MonitorsResponse
{
public string stat { get; set; }
public int total { get; set; }
public string message { get; set; }
public Monitors monitors { get; set; }
}
public enum UptimeRobotMonitorStatus
{
Pause = 0,
NotChecked = 1,
Up = 2,
SeemsDown = 8,
Down = 9
}
#endregion Contracts
}
}