async def monitor_uptime(self) -> None: """Checks the status of each server and sends up/down notifications""" await self.bot.wait_until_ready() channel = self.bot.get_channel(get_config("notification_channel")) for i in get_servers(): if i["type"] == "ping": if ping(i["address"]) is False: await self.notify_down(i, channel, "Host unknown") elif ping(i["address"]) is None: await self.notify_down(i, channel, "Timed out") else: await self.notify_up(i, channel) else: address = i["address"] timeout = get_config("http_timeout") if not address.startswith("http"): address = f"http://{address}" async with aiohttp.ClientSession( timeout=aiohttp.ClientTimeout(total=timeout) ) as session: try: async with session.get(address) as res: if res.ok: await self.notify_up(i, channel) else: await self.notify_down(i, channel, res.reason) except asyncio.TimeoutError: await self.notify_down(i, channel, "Timed out") except aiohttp.ClientError: await self.notify_down(i, channel, "Connection failed")
async def monitor_uptime(self) -> None: """Checks the status of each server and sends up/down notifications""" await self.bot.wait_until_ready() channel = self.bot.get_channel(get_config("notification_channel")) timeout = get_config("timeout") for i in get_servers(): if i["type"] == "ping": if ping(i["address"], timeout=timeout) is False: await self.notify_down(i, channel, "Host unknown") elif ping(i["address"], timeout=timeout) is None: await self.notify_down(i, channel, "Timed out") else: await self.notify_up(i, channel) elif i["type"] == "tcp": host, port = i["address"].split(":") conn = asyncio.open_connection(host, port) try: reader, writer = await asyncio.wait_for(conn, timeout) writer.close() await writer.wait_closed() except asyncio.TimeoutError: await self.notify_down(i, channel, "Timed out") except ConnectionRefusedError: await self.notify_down(i, channel, "Connection failed") else: await self.notify_up(i, channel) else: address = i["address"] if not address.startswith("http"): address = f"http://{address}" async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout( total=timeout)) as session: try: async with session.get(address) as res: if res.ok: await self.notify_up(i, channel) else: await self.notify_down(i, channel, res.reason) except asyncio.TimeoutError: await self.notify_down(i, channel, "Timed out") except aiohttp.ClientError: await self.notify_down(i, channel, "Connection failed")
async def status(self, ctx) -> None: """Returns an embed showing the status of each monitored server""" embed = discord.Embed( title="**Monitor Status**", color=16711680 if self.currently_down else 65287 ) for i in get_servers(): if i["address"] in self.currently_down: downtime = str(timedelta(seconds=self.currently_down[i["address"]])) embed.add_field( name=f"{i['name']} ({i['type']})", value=f":red_circle: {i['address']} ({downtime})", inline=False, ) else: embed.add_field( name=f"{i['name']} ({i['type']})", value=f":green_circle: {i['address']}", inline=False, ) await ctx.send(embed=embed)