This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.py
366 lines (294 loc) · 11.5 KB
/
master.py
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
from os import getenv
from sys import exit
from datetime import datetime
from time import time
import asyncio
from dotenv import load_dotenv
import logging, disnake
from disnake.ext import commands, tasks
VERSION = "1.1.3"
load_dotenv()
log_file = getenv("LOG_FILE")
token = getenv("DISCORD_TOKEN")
guild_id = getenv("GUILD_ID")
public_id = getenv("PUBLIC_CHANNEL")
alert_id = getenv("ALERT_CHANNEL")
log_id = getenv("LOG_CHANNEL")
admin_id = getenv("ADMIN_ROLE")
frequency = int(getenv("CHECK_FREQUENCY"))
delay_threshold = int(getenv("DELAY_ALERT_THRESHOLD"))
latency_threshold = int(getenv("LATENCY_ALERT_THRESHOLD"))
ping_threshold = int(getenv("ALERTS_PING_THRESHOLD"))
logger = logging.getLogger("disnake")
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename=log_file, encoding="utf-8", mode="w")
handler.setFormatter(
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
)
logger.addHandler(handler)
bot = commands.Bot(
intents=disnake.Intents(guild_messages=True, message_content=True),
help_command=None,
test_guilds=[int(guild_id)],
)
class PingMonitoringBot(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.checks = {}
self.public_ch = self.alert_ch = self.log_ch = None
self.status_msg = self.alert_msg = self.admin_ping_msg = None
self.last_checked = self.last_ping = self.last_speedtest = None
self.to_ping = False
self.checker.start()
@commands.Cog.listener()
async def on_ready(self):
print(f"Logged in as MASTER as '{bot.user}'")
@commands.Cog.listener()
async def on_message(self, msg):
if msg.author.id != self.bot.user.id:
return
data = msg.content.split(":")
if len(data) < 3:
return
i = int(data[0])
key = data[1]
value = data[2]
if key == "MASTER":
return
if self.checks.get(i) is None:
self.checks[i] = {}
if value == "PONG":
speedtesting = (
self.last_speedtest is not None and time() - self.last_speedtest < 75
)
self.checks[i]["node"] = key
self.checks[i]["online"] = True
self.checks[i]["delay"] = (
self.checks[i].get("delay", 0)
if speedtesting
else int(
(
(
msg.created_at.replace(tzinfo=None) - self.last_checked
).total_seconds()
- round(self.bot.latency)
)
* 1000
)
)
self.checks[i]["latency"] = (
self.checks[i].get("latency", 0) if speedtesting else int(data[3])
)
elif value == "ST-RESULT":
self.checks[i]["node"] = key
self.checks[i]["online"] = True
self.checks[i]["download"] = float(data[3])
self.checks[i]["upload"] = float(data[4])
@commands.Cog.listener()
async def on_message_delete(self, _):
try:
if await self.public_ch.fetch_message(self.status_msg.id) is None:
self.status_msg = None
except Exception:
self.status_msg = None
finally:
return
try:
if await self.alert_ch.fetch_message(self.alert_msg.id) is None:
self.alert_msg = None
except Exception:
self.alert_msg = None
finally:
return
try:
if await self.alert_ch.fetch_message(self.admin_ping_msg.id) is None:
self.admin_ping_msg = None
except Exception:
self.admin_ping_msg = None
finally:
return
@tasks.loop(seconds=frequency)
async def checker(self):
for index in self.checks.keys():
self.checks[index]["online"] = False
try:
msg = await self.log_ch.send("0:MASTER:PING")
except Exception as e:
print(e)
await asyncio.sleep(3)
await self.checker()
return
self.last_checked = msg.created_at.replace(tzinfo=None)
await asyncio.sleep(int(frequency / 2))
embed = disnake.Embed(
title="Node Status", color=disnake.Color.fuchsia(), timestamp=datetime.now()
)
embed.set_footer(
text=f"Alaister.net Ping Monitoring Bot v{VERSION}",
icon_url="https://alaister.net/hotlink-ok/alaister_net_icon.png",
)
self.to_ping = False
for index in sorted(self.checks):
check = self.checks[index]
embed.add_field(
check.get("node", "Unnamed"),
f"`STATUS` **{':green_circle: ONLINE' if check.get('online') else ':warning: DISCORD API ERROR'}**"
+ f"\n`MESSAGE RESPONSE DELAY` **{check.get('delay', '?') if check.get('online') else '?'}** ms"
+ f"\n`DISCORD API LATENCY` **{check.get('latency', '?') if check.get('online') else '?'}** ms"
+ f"\n`DOWNLOAD/UPLOAD SPEED` **{check.get('download', '?')}** / **{check.get('upload', '?')}** Mbps",
inline=False,
)
alerts = check.get("alerts") or {"online": 0, "delay": 0, "latency": 0}
alerts["online"] = (
alerts["online"] + 1 if not check.get("online", False) else 0
)
alerts["delay"] = (
alerts["delay"] + 1 if check.get("delay", 0) >= delay_threshold else 0
)
alerts["latency"] = (
alerts["latency"] + 1
if check.get("latency", 0) >= latency_threshold
else 0
)
alert_msg_body = ""
if alerts["online"] >= ping_threshold:
alert_msg_body += f"- **Discord API error** <t:{int(time())}:R>\n"
if alerts["delay"] >= ping_threshold:
alert_msg_body += (
f"- **High message response delay** <t:{int(time())}:R>\n"
)
if alerts["latency"] >= ping_threshold:
alert_msg_body += (
f"- **High Discord API latency** <t:{int(time())}:R>\n"
)
self.to_ping = self.to_ping or alert_msg_body != ""
self.checks[index]["alerts"] = alerts
self.checks[index]["alert_msg_body"] = alert_msg_body or check.get(
"alert_msg_body", ""
)
view = StatusView(self.start_speedtest)
try:
self.status_msg = (
await self.public_ch.send(embed=embed, view=view)
if self.status_msg is None
else await self.status_msg.edit(embed=embed)
)
except Exception as e:
print(e)
await asyncio.sleep(5)
await self.checker()
return
await self.send_alerts()
@checker.before_loop
async def before_check(self):
await self.bot.wait_until_ready()
if self.status_msg is None:
self.public_ch = await self.bot.fetch_channel(public_id)
self.alert_ch = await self.bot.fetch_channel(alert_id)
self.log_ch = await self.bot.fetch_channel(log_id)
if self.public_ch is None or self.alert_ch is None or self.log_ch is None:
exit("One or more text channels cannot be found!")
await self.public_ch.purge()
await self.alert_ch.purge()
async def start_speedtest(self):
if self.last_speedtest and (time() - self.last_speedtest) < 60 * 15:
return int(self.last_speedtest + 60 * 15)
await self.log_ch.send(f"0:MASTER:SPEEDTEST")
self.last_speedtest = time()
return True
async def send_alerts(self):
embed = disnake.Embed(
title="Status Alerts",
color=disnake.Color.dark_red(),
timestamp=datetime.now(),
)
embed.set_footer(
text=f"Alaister.net Ping Monitoring Bot v{VERSION}",
icon_url="https://alaister.net/hotlink-ok/alaister_net_icon.png",
)
for index in sorted(self.checks):
check = self.checks.get(index, {})
embed.add_field(
check.get("node", "Unnamed"),
check.get("alert_msg_body")
or "Hurray! There are no system issues currently.",
inline=False,
)
try:
self.alert_msg = (
await self.alert_ch.send(
embed=embed, view=AlertsView(self.clear_alerts)
)
if self.alert_msg is None
else await self.alert_msg.edit(embed=embed)
)
except Exception as e:
print(e)
await asyncio.sleep(5)
await self.send_alerts()
if self.to_ping and (
not self.last_ping or (time() - self.last_ping) > (60 * 5 - 10)
):
try:
if self.admin_ping_msg:
await self.admin_ping_msg.delete()
self.admin_ping_msg = None
self.admin_ping_msg = await self.alert_ch.send(f"<@&{admin_id}>")
self.last_ping = time()
except Exception as e:
print(e)
async def clear_alerts(self):
for index in self.checks.keys():
self.checks[index]["alerts"] = self.checks[index]["alert_msg_body"] = None
if self.admin_ping_msg:
await self.admin_ping_msg.delete()
self.admin_ping_msg = None
await self.send_alerts()
class StatusView(disnake.ui.View):
def __init__(self, st_func: PingMonitoringBot.start_speedtest):
super().__init__()
self.timeout = None
self.st_func = st_func
self.add_item(
disnake.ui.Button(label="System Status", url="https://status.alaister.net")
)
self.add_item(
disnake.ui.Button(
label="GitHub",
url="https://github.com/alaister-net/ping-monitoring-bots",
)
)
@disnake.ui.button(
label="Run Speedtest", emoji="▶️", style=disnake.ButtonStyle.green
)
async def run_speedtest(self, _, i: disnake.MessageInteraction):
st = await self.st_func()
if st is True:
embed = disnake.Embed(
title="Running speedtest...",
description=f"The result will be updated after a few minutes.",
color=disnake.Color.green(),
)
else:
embed = disnake.Embed(
title="A speedtest has just recently finished.",
description=f"Please try again <t:{st}:R>",
color=disnake.Color.red(),
)
await i.response.send_message(embed=embed, ephemeral=True)
class AlertsView(disnake.ui.View):
def __init__(self, clear_func: PingMonitoringBot.clear_alerts):
super().__init__()
self.timeout = None
self.clear_func = clear_func
@disnake.ui.button(label="Clear Alerts", emoji="❎", style=disnake.ButtonStyle.red)
async def clear_alerts(self, _, i: disnake.MessageInteraction):
await self.clear_func()
embed = disnake.Embed(
title="Done!",
description=f"All alerts and mentions have been cleared.",
color=disnake.Color.green(),
)
await i.response.send_message(embed=embed, ephemeral=True)
bot.add_cog(PingMonitoringBot(bot))
bot.run(token)