-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusNowService.cs
More file actions
365 lines (243 loc) · 11.2 KB
/
StatusNowService.cs
File metadata and controls
365 lines (243 loc) · 11.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Net.Http.Headers;
namespace StatusNowFancyMCP;
public class StatusNowService
{
public string _accesstoken = Environment.GetEnvironmentVariable("accesstoken") != null ? Convert.ToString(Environment.GetEnvironmentVariable("accesstoken")) : "";
public Int64 _accountid = Environment.GetEnvironmentVariable("accountid") != null ? Convert.ToInt64(Environment.GetEnvironmentVariable("accountid")) : 0;
public StatusNowService()
{
//// Correctly initialize the fields
//_accesstoken = accesstoken;
//_accountid = accountid;
Console.WriteLine("StatusNowService constructor called");
Console.WriteLine("AccessToken: " + _accesstoken);
Console.WriteLine("AccountId: " + _accountid);
}
public async Task<List<MonitorsModel>> GetMonitors()
{
Console.WriteLine("Get Monitors Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/List?accountid=" + _accountid),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
var monitors = JsonSerializer.Deserialize<List<MonitorsModel>>(body, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return monitors;
}
}
public async Task<List<AlertsModel>> GetAlerts()
{
Console.WriteLine("Get Alerts Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Alerts/List?accountid=" + _accountid + "&shownew=true&showopen=true&showclosed=false"),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
var alerts = JsonSerializer.Deserialize<List<AlertsModel>>(body, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return alerts;
}
}
public async Task<string> PauseMonitor(Int64 monitorid)
{
Console.WriteLine("Pause Monitor Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid + " and MonitorId: " + monitorid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/" + monitorid + "/Pause?accountid=" + _accountid),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
return body;
}
}
public async Task<string> ResumeMonitor(Int64 monitorid) {
Console.WriteLine("Resume Monitor Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid + " and MonitorId: " + monitorid );
//Default to 3min interval
string cronfrequency = "*/3 * * * *";
//if (cronfrequency == null || cronfrequency == "") {
// cronfrequency = "*/3 * * * *"; //Default to 3 minutes if no frequency is provided
//}
//if (cronfrequency =="0 * * * *") {
// cronfrequency = "*/3 * * * *"; //Default to 3 minutes if no frequency is provided
//}
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/" + monitorid + "/Resume?accountid=" + _accountid + "&frequency=" + Uri.EscapeDataString(cronfrequency)),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
return body;
}
}
public async Task<string> CertCheck(string urltocheck)
{
Console.WriteLine("Cert Check Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/CertCheck?url=" + urltocheck),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
if (body == "" || body == "[]"){
body = "Success: No certificate issues found";
}
return body;
}
}
public async Task<string> DomainCheck(string urltocheck)
{
Console.WriteLine("Domain Check Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/DomainCheck?url=" + urltocheck),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
if (body == "" || body == "[]"){
body = "Success: No domain issues found";
}
return body;
}
}
public async Task<string> PingCheck(string urltocheck)
{
Console.WriteLine("Ping Check Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/PingCheck?url=" + urltocheck),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
if (body == "" || body == "[]"){
body = "Success: No ping/site issues found";
}
return body;
}
}
public async Task<string> HttpCheck(string urltocheck)
{
Console.WriteLine("Http Check Called with AccessToken: " + _accesstoken + " and AccountId: " + _accountid);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", _accesstoken);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://statusnow.dev/api/Monitors/HttpCodeCheck?url=" + urltocheck),
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
if (body == "" || body == "[]"){
body = "Success: No http issues found";
}
return body;
}
}
}
public class MonitorsModel {
public Int64 id { get; set; }
public Int64? accountid { get; set; }
public string? name { get; set; }
public bool? isactive { get; set; } //1 = active, 0 = inactive
public string? monitortype { get; set; } //HTTPCODE, SSL CERT, DOMAIN EXPIRY, PING, TCP PORT
public MonitorConfiguration? configuration { get; set; }
public string? createdby { get; set; }
public DateTime? createdat { get; set; }
public string? updatedby { get; set; }
public DateTime? updatedat { get; set; }
public DateTime? lastusedat { get; set; }
public MonitorStatus? laststatus { get; set; }
}
public class MonitorStatus {
public string? laststatus { get; set; }
public string? sslstatus { get; set; }
public string? domainstatus { get; set; }
public string? pingstatus { get; set; }
public string? sslexpiry { get; set; }
public string? domainexpiry { get; set; }
public string? httpresponsecodestatus { get; set; }
public string? tcpportstatus { get; set; }
}
public class MonitorConfiguration {
public string? url { get; set; }
public string? frequency { get; set; } //Cron Expression: https://en.wikipedia.org/wiki/Cron#CRON_expression
//# * * * * * <command to execute>
//# | | | | |
//# | | | | day of the week (0–6) (Sunday to Saturday;
//# | | | month (1–12) 7 is also Sunday on some systems)
//# | | day of the month (1–31)
//# | hour (0–23)
//# minute (0–59)
public string? httpmethod { get; set; } //GET, POST, PATCH, PUT, HEAD
public bool? certexpiry { get; set; } //True or False (Will check all SSL and Cert checks and issues, including expiry)
public bool? domainexpiry { get; set; } //True or False (Will check all domain expiry dates)
public bool? pingstatus { get; set; } //True or False (Will check url ping, response status and times)
public Int32? pingresponsetarget { get; set; } //Target response time, if this is exceeded we have an issue. 3 seconds = 3000 ms
public bool? httpresponsestatus { get; set; } //True or False (Will check response code)
public string? httpresponsecode { get; set; } //What code are we expecting? 200, 201, 202, 204, 301, 302, 304, 400, 401, 403, 404, 405, 500, 501, 502, 503, 504
public bool? tcpportstatus { get; set; } //True or False - (Will check for port status on host)
public int? tcpportresponsetarget { get; set; } //Target response time, if this is exceeded we have an issue. 3 seconds = 3000 ms
public string? tcpporthost { get; set; } //Host IP or DNS name
public string? tcpports { get; set; } //TCP Port numbers
}
public class AlertsModel
{
public Int64 id { get; set; }
public Int64? accountid { get; set; }
public Int64? monitorid { get; set; }
public string? runid { get; set; }
public Int64? occurrences { get; set; } //Number of occurrences
public string? name { get; set; }
public string? alertstatus { get; set; } //NEW, OPEN, CLOSED
public string? alerttype { get; set; } //HTTPCODE, SSL CERT, DOMAIN EXPIRY, PING, TCP PORT
public DateTime? createdat { get; set; }
public string? updatedby { get; set; }
public DateTime? updatedat { get; set; }
public DateTime? closedat { get; set; }
}