mirror of
https://forgejo.altau.su/lego/lego-monitoring.git
synced 2026-03-10 12:45:19 +00:00
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from socket import gethostname
|
|
|
|
from psutil import cpu_percent
|
|
|
|
from lego_monitoring.alerting.alert import Alert
|
|
from lego_monitoring.alerting.enum import AlertType, Severity
|
|
from lego_monitoring.core import cvars
|
|
|
|
from .utils import format_for_healthchecks_slug
|
|
|
|
IS_TESTING = False
|
|
|
|
|
|
def cpu_check() -> list[Alert]:
|
|
percentage = cpu_percent()
|
|
config = cvars.config.get().checks.cpu
|
|
slug = f"{format_for_healthchecks_slug(gethostname())}-cpu"
|
|
if config.critical_percentage and (IS_TESTING or percentage >= config.critical_percentage):
|
|
return [
|
|
Alert(
|
|
alert_type=AlertType.CPU,
|
|
message=f"CPU load: {percentage:.2f}% >= {config.critical_percentage:.2f}%",
|
|
severity=Severity.CRITICAL,
|
|
healthchecks_slug=slug,
|
|
)
|
|
]
|
|
elif config.warning_percentage and (IS_TESTING or percentage >= config.warning_percentage):
|
|
return [
|
|
Alert(
|
|
alert_type=AlertType.CPU,
|
|
message=f"CPU load: {percentage:.2f}% >= {config.warning_percentage:.2f}%",
|
|
severity=Severity.WARNING,
|
|
healthchecks_slug=slug,
|
|
)
|
|
]
|
|
else:
|
|
return [
|
|
Alert(
|
|
alert_type=AlertType.CPU,
|
|
message=f"CPU load: {percentage:.2f}% (nominal)",
|
|
severity=Severity.OK,
|
|
healthchecks_slug=slug,
|
|
)
|
|
]
|