-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cs
536 lines (512 loc) · 39.8 KB
/
Configuration.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Authentication;
using System.Text;
using System.Text.Json;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;
namespace cachet_monitor
{
class Configuration
{
public static void ConfigurationDialog()
{
string userInputLevel1;
bool exit = false;
while (!exit)
{
Dictionary<string, string> optionslevel1 = new Dictionary<string, string>()
{
{ "API Key", currentConfiguration.APIKey },
{ "Base URL", currentConfiguration.BaseURL },
{ "Interval", currentConfiguration.Interval.ToString() },
{ "Hosts", "Host List"},
{ "Save and Exit", "" }
};
KeyValuePair<string, string> selectedOptionLevel1 = RenderConfigurationDialogMenu(optionslevel1);
switch (selectedOptionLevel1.Key)
{
case "Save and Exit":
SaveConfiguration(currentConfiguration);
exit = true;
break;
case "Base URL":
Console.Write("Base URL(the URL to Cachet): ");
userInputLevel1 = Console.ReadLine();
currentConfiguration.BaseURL = userInputLevel1;
break;
case "Interval":
Console.Write("Interval (in seconds): ");
userInputLevel1 = Console.ReadLine();
currentConfiguration.Interval = Convert.ToInt32(userInputLevel1);
break;
case "API Key":
Console.Write("API Key: ");
userInputLevel1 = Console.ReadLine();
currentConfiguration.APIKey = userInputLevel1;
break;
case "Hosts": //LEVEL2STARTER Hosts
bool backlevel2 = false;
string userInputLevel2;
while (!backlevel2)
{
Dictionary<string, string> optionslevel2 = new Dictionary<string, string>();
optionslevel2.Add("Add", "");
if (currentConfiguration.Hosts == null)
{
currentConfiguration.Hosts = new List<Expectation>();
}
List<List<Expectation>> groupedhostlist = new List<List<Expectation>>();
foreach (Expectation host in currentConfiguration.Hosts)
{
List<Expectation> samehosts = new List<Expectation>();
if (groupedhostlist.Where(h => h.Contains(host)).Count() < 1)
{
foreach (Expectation host_ in currentConfiguration.Hosts)
{
if (host_.path == host.path)
{
samehosts.Add(host_);
}
}
groupedhostlist.Add(samehosts);
}
}
foreach (List<Expectation> groupedhost in groupedhostlist)
{
int hostindex = 0;
foreach (Expectation host in groupedhost)
{
hostindex++;
optionslevel2.Add(host.path + "|" + hostindex, host.type);
}
}
KeyValuePair<string, string> selectedOptionLevel2 = RenderConfigurationDialogMenu(optionslevel2);
switch (selectedOptionLevel2.Key)
{
case "Add":
Console.Write("Path (URL for HTTP and IP for ping): ");
userInputLevel2 = Console.ReadLine();
if (currentConfiguration.Hosts == null)
{
currentConfiguration.Hosts = new List<Expectation>();
}
currentConfiguration.Hosts.Add(new Expectation { path = userInputLevel2 });
break;
case "Exit":
backlevel2 = true;
break;
default: //LEVEL3STARTER Host Details
bool backlevel3 = false;
string userInputLevel3;
int SelectedHost = currentConfiguration.Hosts.FindIndex(host => host == currentConfiguration.Hosts.FindAll(host => host.path == selectedOptionLevel2.Key.Split("|")[0])[Convert.ToInt32(selectedOptionLevel2.Key.Split("|")[1])-1]); //Find index of userselected host
while (!backlevel3)
{
Dictionary<string, string> optionslevel3 = new Dictionary<string, string>()
{
{ "Delete", "" },
{ "Path", currentConfiguration.Hosts[SelectedHost].path },
{ "Type", currentConfiguration.Hosts[SelectedHost].type },
{ "Expected Status Code Range", currentConfiguration.Hosts[SelectedHost].ExpectedStatusCodeRange },
{ "Verify SSL", currentConfiguration.Hosts[SelectedHost].verifySSL.ToString() },
{ "Actions", "Actions List" }
};
KeyValuePair<string, string> selectedOptionLevel3 = RenderConfigurationDialogMenu(optionslevel3);
switch (selectedOptionLevel3.Key)
{
case "Delete":
currentConfiguration.Hosts.Remove(currentConfiguration.Hosts[SelectedHost]);
backlevel3 = true;
break;
case "Path":
Console.Write("Path (URL for HTTP and IP for ping): ");
userInputLevel3 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].path = userInputLevel3;
backlevel3 = true;
break;
case "Type":
Console.Write("Type (http): ");
userInputLevel3 = Console.ReadLine();
if (userInputLevel3 == "http")
{
currentConfiguration.Hosts[SelectedHost].type = userInputLevel3;
}
break;
case "Expected Status Code Range":
Console.Write("Expected Status Code Range (from-to): ");
userInputLevel3 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].ExpectedStatusCodeRange = userInputLevel3;
break;
case "Verify SSL":
Console.Write("Verify SSL (true/false): ");
userInputLevel3 = Console.ReadLine();
try
{
currentConfiguration.Hosts[SelectedHost].verifySSL = Convert.ToBoolean(userInputLevel3);
} catch (FormatException)
{
Console.WriteLine($"{selectedOptionLevel3.Key} is not a valid option.");
break;
}
break;
case "Actions": //LEVEL4STARTER Host Actions
bool backlevel4 = false;
string userInputLevel4;
while (!backlevel4)
{
Dictionary<string, string> optionslevel4 = new Dictionary<string, string>();
optionslevel4.Add("Add", "");
int actionindex = 0;
if (currentConfiguration.Hosts[SelectedHost].Actions != null)
{
foreach (Expectation.Action action in currentConfiguration.Hosts[SelectedHost].Actions)
{
actionindex++;
optionslevel4.Add($"{action.actiontype}-{actionindex}", $"");
}
}
KeyValuePair<string, string> selectedOptionLevel4 = RenderConfigurationDialogMenu(optionslevel4);
switch (selectedOptionLevel4.Key)
{
case "Add":
Console.Write("Type (create_incident/update_component): ");
userInputLevel4 = Console.ReadLine();
string userInputLevel4incident_title = "";
string userInputLevel4incident_message = "";
string userInputLevel4failed_count = "";
string userInputLevel4incident_solvedmessage = "";
string userInputLevel4incident_status = "";
string userInputLevel4incident_component_id = "";
string userInputLevel4incident_component_status = "";
string userInputLevel4component_id = "";
string userInputLevel4component_status = "";
if (currentConfiguration.Hosts[SelectedHost].Actions == null)
{
currentConfiguration.Hosts[SelectedHost].Actions = new List<Expectation.Action>();
}
if (userInputLevel4 == "create_incident")
{
Console.Write("Incident title (Title of incident): ");
userInputLevel4incident_title = Console.ReadLine();
Console.Write("Incident message (Message of incident): ");
userInputLevel4incident_message = Console.ReadLine();
Console.Write("How many times the host has failed before the action is ran: ");
userInputLevel4failed_count = Console.ReadLine();
Console.Write("Incident solved message (Message of incident when solved): ");
userInputLevel4incident_solvedmessage = Console.ReadLine();
Console.Write("Incident status (Status of created incident (fixed=4/investigating=1/watching=3/identified=2)): ");
userInputLevel4incident_status = Console.ReadLine();
Console.Write("Incident component id (Optional component ID):");
userInputLevel4incident_component_id = Console.ReadLine();
if (userInputLevel4component_id != "")
{
Console.Write("Incident component status (Status of component (Operational=1/Watching=3/PartialOutage=2/MajorOutage=4)): ");
userInputLevel4incident_component_status = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions.Add(new Expectation.Action()
{
actiontype = userInputLevel4,
failed_count = Convert.ToInt32(userInputLevel4failed_count),
incident_parameters = new Expectation.Action.IncidentParameters()
{
title = userInputLevel4incident_title,
message = userInputLevel4incident_message,
solvedmessage = userInputLevel4incident_solvedmessage,
componentstatus = Convert.ToInt32(userInputLevel4incident_component_status),
component_id = Convert.ToInt32(userInputLevel4incident_component_id),
status = Convert.ToInt32(userInputLevel4incident_status)
}
}) ;
} else
{
currentConfiguration.Hosts[SelectedHost].Actions.Add(new Expectation.Action()
{
actiontype = userInputLevel4,
failed_count = Convert.ToInt32(userInputLevel4failed_count),
incident_parameters = new Expectation.Action.IncidentParameters()
{
title = userInputLevel4incident_title,
message = userInputLevel4incident_message,
solvedmessage = userInputLevel4incident_solvedmessage,
status = Convert.ToInt32(userInputLevel4incident_status)
}
});
}
} else if (userInputLevel4 == "update_component")
{
Console.Write("Component id (component ID):");
userInputLevel4component_id = Console.ReadLine();
Console.Write("Component status (Status of component (Operational=1/Watching=3/PartialOutage=2/MajorOutage=4)):");
userInputLevel4component_status = Console.ReadLine();
Console.Write("How many times the host has failed before the action is ran: ");
userInputLevel4failed_count = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions.Add(new Expectation.Action()
{
actiontype = userInputLevel4,
failed_count = Convert.ToInt32(userInputLevel4failed_count),
component_paramters = new Expectation.Action.ComponentParamters()
{
component_id = Convert.ToInt32(userInputLevel4component_id),
component_status = Convert.ToInt32(userInputLevel4component_status)
}
}) ;
}
break;
case "Exit":
backlevel4 = true;
break;
default: //LEVEL5STARTER
bool backlevel5 = false;
string userInputLevel5;
int SelectedAction = Convert.ToInt32(selectedOptionLevel4.Key.Split("-")[1])-1;
while (!backlevel5)
{
Dictionary<string, string> optionslevel5 = new Dictionary<string, string>();
if (currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype == "create_incident")
{
if (currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters == null)
{
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters = new Expectation.Action.IncidentParameters();
}
optionslevel5 = new Dictionary<string, string>()
{
{ "Delete", "" },
{ "actiontype", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype},
{ "failed_count", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].failed_count.ToString()},
{ "incident_title", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.title},
{ "incident_message", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.message},
{ "incident_status", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.status.ToString()},
{ "incident_solvedmessage", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.solvedmessage},
{ "incident_component_id", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.component_id.ToString()},
{ "incident_component_status", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.componentstatus.ToString()},
};
} else if (currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype == "update_component")
{
if (currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters == null)
{
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters = new Expectation.Action.ComponentParamters();
}
optionslevel5 = new Dictionary<string, string>()
{
{ "Delete", "" },
{ "actiontype", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype},
{ "failed_count", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].failed_count.ToString()},
{ "component_id", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters.component_id.ToString()},
{ "component_status", currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters.component_status.ToString()}
};
} else
{
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype = "create_incident";
backlevel5 = true;
}
KeyValuePair<string, string> selectedOptionLevel5 = RenderConfigurationDialogMenu(optionslevel5);
switch (selectedOptionLevel5.Key)
{
case "Delete":
currentConfiguration.Hosts[SelectedHost].Actions.Remove(currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction]);
backlevel5 = true;
break;
case "failed_count":
Console.Write("How many times the host has failed before the action is ran: ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].failed_count = Convert.ToInt32(userInputLevel5);
backlevel5 = true;
break;
case "incident_solvedmessage":
Console.Write("Incident solved message (Message of incident when solved): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.solvedmessage = userInputLevel5;
backlevel5 = true;
break;
case "actiontype":
Console.Write("Action Type (create_incident/update_component): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].actiontype = userInputLevel5;
backlevel5 = true;
break;
case "incident_title":
Console.Write("Incident title (Title of an incient): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.title = userInputLevel5;
break;
case "incident_message":
Console.Write("Incident message (Description of an incient): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.message = userInputLevel5;
break;
case "incident_status":
Console.Write("Incident status (fixed=4/investigating=1/watching=3/identified=2): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.status = Convert.ToInt32(userInputLevel5);
break;
case "incident_component_id":
Console.WriteLine("(Optional. Leave blank if using an update_component action!)");
Console.Write("Incident component id (id of component): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.component_id = Convert.ToInt32(userInputLevel5);
break;
case "incident_component_status":
Console.WriteLine("(Optional. Use only if component id is specified)");
Console.Write("Incident component status (Operational=1/Watching=3/PartialOutage=2/MajorOutage=4): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].incident_parameters.componentstatus = Convert.ToInt32(userInputLevel5);
break;
case "component_id":
Console.Write("Component ID to update (id): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters.component_id = Convert.ToInt32(userInputLevel5);
break;
case "component_status":
Console.Write("Component status (Operational=1/Watching=3/PartialOutage=2/MajorOutage=4): ");
userInputLevel5 = Console.ReadLine();
currentConfiguration.Hosts[SelectedHost].Actions[SelectedAction].component_paramters.component_status = Convert.ToInt32(userInputLevel5);
break;
case "Exit":
backlevel5 = true;
break;
default:
Console.WriteLine($"{selectedOptionLevel5.Key} is not a valid option.");
break;
}
}
break;
}
}
break;
case "Exit":
backlevel3 = true;
break;
default:
Console.WriteLine($"{selectedOptionLevel3.Key} is not a valid option.");
break;
}
}
break;
}
}
break;
case "Exit":
exit = true;
break;
default:
Console.WriteLine($"{selectedOptionLevel1.Key} is not a valid option.");
break;
}
}
}
public static KeyValuePair<string, string> RenderConfigurationDialogMenu(Dictionary<string, string> options)
{
KeyValuePair<string, string> selectedOption;
int DrawCount = 0;
foreach (KeyValuePair<string, string> menuOption in options)
{
DrawCount++;
if (menuOption.Value != "")
{
Console.WriteLine($"[{DrawCount}] {menuOption.Key} ({menuOption.Value})");
} else
{
Console.WriteLine($"[{DrawCount}] {menuOption.Key}");
}
}
DrawCount++;
Console.WriteLine($"[{DrawCount}] Exit");
Console.Write("Selection: ");
string userSelection = Console.ReadLine();
try
{
if (Convert.ToInt32(userSelection) > options.Count)
{
selectedOption = new KeyValuePair<string, string>("Exit", "");
return selectedOption;
}
selectedOption = options.ElementAt(Convert.ToInt32(userSelection) - 1);
return selectedOption;
}catch (FormatException) //If user inputs non-menuoption value, return that
{
selectedOption = new KeyValuePair<string, string>(userSelection,"");
return selectedOption;
}
}
private static Configuration currentConfiguration = new Configuration();
public static Configuration GetConfiguration(string path = "Config.json", bool refresh = false)
{
if (refresh || currentConfiguration == null || currentConfiguration.BaseURL == null)
{
Console.WriteLine("Reading configuration");
try
{
currentConfiguration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(path));
}
catch (Exception ex)
{
if (ex is FileNotFoundException)
{
return currentConfiguration;
}
Console.WriteLine("Error occured while loading configuration:");
throw ex;
}
}
return currentConfiguration;
}
public static void SaveConfiguration(Configuration configuration, string path = "Config.json")
{
try
{
File.WriteAllText(path, JsonSerializer.Serialize<Configuration>(configuration, new JsonSerializerOptions() { IgnoreNullValues = true }));
}
catch (Exception ex)
{
Console.WriteLine("Error occured while writing configuration:");
throw ex;
}
}
public string APIKey { get; set; }
public string BaseURL { get; set; }
public int Interval { get; set; }
public List<Expectation> Hosts { get; set; }
public class Expectation
{
[field: NonSerialized] public string id { get; set; }
public string path { get; set; }
public string type { get; set; }
public static class Types
{
public static string ping = "ping";
public static string http = "http";
}
public string ExpectedStatusCodeRange { get; set; }
public bool verifySSL { get; set; }
public List<Action> Actions { get; set; }
public class Action
{
public string actiontype { get; set; }
public static string create_incident = "create_incident";
public int failed_count { get; set; } = 1;
public IncidentParameters incident_parameters { get; set; }
public class IncidentParameters
{
public string title { get; set; }
public string message { get; set; }
public string solvedmessage { get; set; }
public int status { get; set; }
public int? component_id { get; set; }
public int? componentstatus { get; set; }
}
public static string update_component = "update_component";
public ComponentParamters component_paramters { get; set; }
public class ComponentParamters
{
public int component_id { get; set; }
public int component_status { get; set; }
}
}
public Expectation Clone()
{
return this.MemberwiseClone() as Expectation;
}
}
}
}