Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add heartbeat monitoring plugin #251

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/monitoring/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ce programme est régi par la licence CeCILL soumise au droit français et
# respectant les principes de diffusion des logiciels libres. Vous pouvez
# utiliser, modifier et/ou redistribuer ce programme sous les conditions
# de la licence CeCILL diffusée sur le site "http://www.cecill.info".

monitoring_enabled: false
monitoring_push_url: "https://uptimekuma.url/api/push/"
monitoring_push_monitor: ""
9 changes: 9 additions & 0 deletions plugins/monitoring/credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright © ZRunner 2021
Copyright © Leirof 2021 - 2022
Copyright © Aeris One 2022
Copyright © ascpial 2023

Ce programme est régi par la licence CeCILL soumise au droit français et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL diffusée sur le site "http://www.cecill.info".
77 changes: 77 additions & 0 deletions plugins/monitoring/monitoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Ce programme est régi par la licence CeCILL soumise au droit français et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL diffusée sur le site "http://www.cecill.info".
"""
import time

import aiohttp
from discord.ext import tasks, commands

import core
from utils import Gunibot


async def setup(bot: Gunibot = None):
await bot.add_cog(Monitoring(bot))


class Monitoring(commands.Cog):
def __init__(self, bot: Gunibot):
self.bot = bot
self.file = "monitoring"
self.logger = core.setup_logger(self.file)
self.config = core.config.get(self.file)
self.error_counter = 0
self.session = aiohttp.ClientSession()

async def cog_load(self) -> None:
if self.config["monitoring_enabled"]:
for i in range(5):
if await self.ping_monitoring():
self.logger.info("Monitoring test ping successful")
self.logger.info("Monitoring enabled")
self.loop.start() #pylint: disable=no-member
return
self.logger.warning("Monitoring ping failed %s times", i + 1)
time.sleep(5)
self.bot.dispatch("error", RuntimeError("Monitoring disabled due to ping failure"))

async def ping_monitoring(self):
# retrieve Discord Ping
ping = round(self.bot.latency * 1000, 0)

# build URL
url = (self.config["monitoring_push_url"] +
self.config["monitoring_push_monitor"] +
"?status=up&msg=OK&ping=" + str(ping))

# send request
async with self.session.get(url) as resp:
if resp.status != 200:
self.logger.error("Monitoring ping failed with status %s", resp.status)
return False
json = await resp.json()
try:
if not json["ok"]:
self.logger.error("Monitoring ping failed with error : %s", json["msg"])
return False
return True
except KeyError:
self.logger.error("Monitoring ping failed")
return False

@tasks.loop(seconds=20)
async def loop(self):
if await self.ping_monitoring():
self.error_counter = 0
return
self.error_counter += 1
if self.error_counter >= 6:
self.bot.dispatch("error", RuntimeError("Monitoring disabled due to multiple ping failure"))
self.loop.stop() #pylint: disable=no-member

@loop.before_loop
async def before_ping_monitoring(self):
await self.bot.wait_until_ready()
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
psutil
aiohttp
psutil~=5.9.5
aiohttp~=3.8.5
gitpython >= 3.1.8
python-i18n
pyyaml
pyyaml~=6.0.1
emoji >= 2.0
feedparser >= 6.0
async-timeout >= 3.0
Expand Down
Loading