creds file -> config file

This commit is contained in:
Alex 2024-10-27 14:47:02 +03:00
parent c653055134
commit f199292aba
5 changed files with 70 additions and 49 deletions

View file

@ -1,13 +1,16 @@
#!/usr/bin/env python3
import asyncio
import json
import logging
import signal
from typing import Callable, Coroutine
import aiofiles
import nio
from alerting import alerts
from misc import checks
from alerting.common import CONFIG_FILE
from misc import checks, cvars
logging.basicConfig(level=logging.INFO)
@ -19,7 +22,7 @@ def stop_gracefully(signum, frame):
stopping = True
async def checker(check: Callable | Coroutine, interval_secs: int, client: nio.AsyncClient, *args, **kwargs):
async def checker(check: Callable | Coroutine, interval_secs: int, *args, **kwargs):
while True:
logging.info(f"Calling {check.__name__}")
if isinstance(check, Callable):
@ -32,7 +35,7 @@ async def checker(check: Callable | Coroutine, interval_secs: int, client: nio.A
raise TypeError(f"check is {type(check)}, neither function nor coroutine")
logging.info(f"Got {len(result)} alerts")
for alert in result:
await alerts.send_alert(alert, client)
await alerts.send_alert(alert)
await asyncio.sleep(interval_secs)
@ -43,13 +46,19 @@ async def main():
WEEK = 7 * DAY
signal.signal(signal.SIGTERM, stop_gracefully)
async with aiofiles.open(CONFIG_FILE) as f:
contents = await f.read()
cvars.config.set(json.loads(contents))
client = await alerts.get_client()
cvars.matrix_client.set(client)
checkers = (
checker(checks.temp_check, 5 * MINUTE, client),
checker(checks.cpu_check, 5 * MINUTE, client),
checker(checks.ups_check, 5 * MINUTE, client),
checker(checks.ram_check, 1 * MINUTE, client),
checker(checks.vuln_check, 1 * DAY, client),
checker(checks.temp_check, 5 * MINUTE),
checker(checks.cpu_check, 5 * MINUTE),
checker(checks.ups_check, 5 * MINUTE),
checker(checks.ram_check, 1 * MINUTE),
checker(checks.vuln_check, 1 * DAY),
)
async with asyncio.TaskGroup() as tg:
checker_tasks: set[asyncio.Task] = set()