-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
181 lines (152 loc) · 6.23 KB
/
Program.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
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StatusChecker
{
class Program
{
private static readonly HttpClient httpClient = new HttpClient();
private static string discordWebhookUrl = "";
static async Task Main(string[] args)
{
InitializeSettings(args);
string servicesFilePath = Path.Combine("docs", "services.json");
if (!File.Exists(servicesFilePath))
{
Console.WriteLine("services.json file not found.");
return;
}
string servicesJson = await File.ReadAllTextAsync(servicesFilePath);
JObject apps = JObject.Parse(servicesJson);
string statusFilePath = Path.Combine("docs", "status.json");
JObject statusData = new JObject();
// Load existing status data or create a new file if it does not exist
if (File.Exists(statusFilePath))
{
string existingStatusJson = await File.ReadAllTextAsync(statusFilePath);
statusData = JObject.Parse(existingStatusJson);
}
else
{
Console.WriteLine("status.json file not found. Creating a new one.");
statusData = new JObject();
}
foreach (var app in apps["apps"])
{
string appName = app["name"]?.ToString() ?? "Unknown";
Console.WriteLine($"Checking services for app: {appName}");
// Create or get app's status array
if (statusData[appName] == null)
{
statusData[appName] = new JArray();
}
JArray appStatusArray = (JArray)statusData[appName];
// Check website status
await CheckAndUpdateService(app["website"], appStatusArray);
// Check each service, indexer, and relay status
foreach (var service in app["services"] ?? new JArray())
{
await CheckAndUpdateService(service, appStatusArray);
}
foreach (var indexer in app["indexers"] ?? new JArray())
{
await CheckAndUpdateService(indexer, appStatusArray);
}
foreach (var relay in app["relays"] ?? new JArray())
{
await CheckAndUpdateService(relay, appStatusArray);
}
// Maintain only last 600 entries for the app
if (appStatusArray.Count > 600)
{
appStatusArray = new JArray(appStatusArray.Skip(appStatusArray.Count - 600));
statusData[appName] = appStatusArray;
}
}
// Save status data to the file
await File.WriteAllTextAsync(statusFilePath, statusData.ToString(Formatting.Indented));
Console.WriteLine("Status check completed and saved.");
}
private static async Task CheckAndUpdateService(JToken service, JArray appStatusArray)
{
string serviceName = service["name"]?.ToString() ?? "Unknown";
string serviceUrl = service["url"]?.ToString() ?? string.Empty;
string serviceType = service["type"]?.ToString() ?? "Unknown";
if (string.IsNullOrEmpty(serviceUrl))
{
Console.WriteLine($"URL for service '{serviceName}' is missing.");
return;
}
bool isActive = await CheckServiceStatus(serviceUrl);
var statusEntry = new JObject
{
["name"] = serviceName,
["url"] = serviceUrl,
["type"] = serviceType,
["isActive"] = isActive,
["timestamp"] = DateTime.UtcNow
};
appStatusArray.Add(statusEntry);
if (!isActive)
{
await SendToDiscord(serviceName, serviceUrl, serviceType);
}
}
private static async Task<bool> CheckServiceStatus(string url)
{
try
{
HttpResponseMessage response = await httpClient.GetAsync(url);
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
Console.WriteLine($"Error checking status for {url}: {ex.Message}");
return false;
}
}
private static async Task SendToDiscord(string serviceName, string serviceUrl, string serviceType)
{
if (string.IsNullOrEmpty(discordWebhookUrl))
{
Console.WriteLine("Discord webhook URL is not provided.");
return;
}
var messageContent = new
{
content = $"Service **{serviceName}** (Type: {serviceType}) at {serviceUrl} is currently **down**. Time: {DateTime.UtcNow}"
};
var jsonContent = new StringContent(JsonConvert.SerializeObject(messageContent), Encoding.UTF8, "application/json");
try
{
var response = await httpClient.PostAsync(discordWebhookUrl, jsonContent);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Discord notification sent for {serviceName}.");
}
else
{
Console.WriteLine($"Failed to send Discord notification for {serviceName}. Status code: {response.StatusCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while sending Discord notification: {ex.Message}");
}
}
private static void InitializeSettings(string[] args)
{
discordWebhookUrl = ExtractArgument(args, "-webhook");
}
private static string? ExtractArgument(string[] args, string key)
{
int index = Array.IndexOf(args, key);
return index >= 0 && index + 1 < args.Length ? args[index + 1] : null;
}
}
}