checkers are now objects, lay foundation for persistent alerts

This commit is contained in:
Alex Tau 2025-06-05 21:45:01 +03:00
parent 5d2759c63c
commit eef6ec59b0
12 changed files with 162 additions and 81 deletions

View file

@ -1,18 +1,18 @@
from psutil import cpu_percent
from lego_monitoring.alerting import alerts
from lego_monitoring.alerting.alert import Alert
from lego_monitoring.alerting.enum import AlertType, Severity
from lego_monitoring.core import cvars
IS_TESTING = False
def cpu_check() -> list[alerts.Alert]:
def cpu_check() -> list[Alert]:
percentage = cpu_percent()
config = cvars.config.get().checks.cpu
if config.critical_percentage and (IS_TESTING or percentage > config.critical_percentage):
return [
alerts.Alert(
Alert(
alert_type=AlertType.CPU,
message=f"CPU load: {percentage:.2f}% > {config.critical_percentage:.2f}%",
severity=Severity.CRITICAL,
@ -20,7 +20,7 @@ def cpu_check() -> list[alerts.Alert]:
]
elif config.warning_percentage and (IS_TESTING or percentage > config.warning_percentage):
return [
alerts.Alert(
Alert(
alert_type=AlertType.CPU,
message=f"CPU load: {percentage:.2f}% > {config.warning_percentage:.2f}%",
severity=Severity.WARNING,

View file

@ -1,18 +1,18 @@
from psutil import virtual_memory
from lego_monitoring.alerting import alerts
from lego_monitoring.alerting.alert import Alert
from lego_monitoring.alerting.enum import AlertType, Severity
from lego_monitoring.core import cvars
IS_TESTING = False
def ram_check() -> list[alerts.Alert]:
def ram_check() -> list[Alert]:
percentage = virtual_memory().percent
config = cvars.config.get().checks.ram
if config.critical_percentage and (IS_TESTING or percentage > config.critical_percentage):
return [
alerts.Alert(
Alert(
alert_type=AlertType.RAM,
message=f"RAM usage: {percentage:.2f}% > {config.critical_percentage:.2f}%",
severity=Severity.CRITICAL,
@ -20,7 +20,7 @@ def ram_check() -> list[alerts.Alert]:
]
elif config.warning_percentage and (IS_TESTING or percentage > config.warning_percentage):
return [
alerts.Alert(
Alert(
alert_type=AlertType.RAM,
message=f"RAM usage: {percentage:.2f}% > {config.warning_percentage:.2f}%",
severity=Severity.WARNING,

View file

@ -1,4 +1,4 @@
from lego_monitoring.alerting import alerts
from lego_monitoring.alerting.alert import Alert
from lego_monitoring.alerting.enum import AlertType, Severity
from . import sensors
@ -6,19 +6,19 @@ from . import sensors
IS_TESTING = False
def temp_check() -> list[alerts.Alert]:
def temp_check() -> list[Alert]:
alert_list = []
temps = sensors.get_readings()
for sensor, readings in temps.items():
for r in readings:
if r.critical_temp is not None and (IS_TESTING or r.current_temp > r.critical_temp):
alert = alerts.Alert(
alert = Alert(
alert_type=AlertType.TEMP,
message=f"{sensor} {r.label}: {r.current_temp}°C > {r.critical_temp}°C",
severity=Severity.CRITICAL,
)
elif r.warning_temp is not None and (IS_TESTING or r.current_temp > r.warning_temp):
alert = alerts.Alert(
alert = Alert(
alert_type=AlertType.TEMP,
message=f"{sensor} {r.label}: {r.current_temp}°C > {r.warning_temp}°C",
severity=Severity.WARNING,

View file

@ -1,4 +1,5 @@
from lego_monitoring.alerting import alerts
from lego_monitoring.alerting.alert import Alert
from lego_monitoring.alerting.channel import send_alert
from lego_monitoring.alerting.enum import AlertType, Severity
from .vulnix import get_vulnix_output
@ -6,13 +7,13 @@ from .vulnix import get_vulnix_output
IS_TESTING = False
def vulnix_check() -> list[alerts.Alert]:
def vulnix_check() -> list[Alert]:
alert_list = []
try:
vulnix_output = get_vulnix_output(IS_TESTING)
except Exception as e:
alerts.send_alert(
alerts.Alert(
send_alert(
Alert(
alert_type=AlertType.ERROR,
message=f"Exception {type(e).__name__} while calling vulnix: {e}",
severity=Severity.CRITICAL,
@ -34,7 +35,7 @@ def vulnix_check() -> list[alerts.Alert]:
score_str = "(not scored by CVSSv3)"
message += f'\n* <a href="https://nvd.nist.gov/vuln/detail/{cve}">{cve}</a> - {finding.description[cve]} {score_str}'
alert = alerts.Alert(
alert = Alert(
alert_type=AlertType.VULN,
message=message,
severity=Severity.WARNING,