move checks to upsmon, where possible

This commit is contained in:
Alex 2024-09-01 14:26:15 +03:00
parent 9518527beb
commit 09a9cfe310
3 changed files with 53 additions and 51 deletions

View file

@ -123,36 +123,12 @@ async def ups_check() -> list[alerts.Alert]:
)
for status in sensor.ups_status:
if IS_TESTING or status == UPSStatus.BATTERY_REPLACE:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS,
message=f"UPS battery needs to be replaced ASAP!",
severity=alerts.Severity.CRITICAL,
)
)
elif IS_TESTING or status == UPSStatus.UPS_OVERLOAD:
if IS_TESTING or status == UPSStatus.UPS_OVERLOAD:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS, message=f"UPS is overloaded!", severity=alerts.Severity.CRITICAL
)
)
elif IS_TESTING or status == UPSStatus.UPS_BYPASS:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS,
message=f"BYPASS MODE: Battery and connected devices are not protected from power outage!",
severity=alerts.Severity.WARNING,
)
)
elif IS_TESTING or status == UPSStatus.UPS_CALIBRATION:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS,
message=f"UPS is currently performing runtime calibration.",
severity=alerts.Severity.INFO,
)
)
elif IS_TESTING or status == UPSStatus.ON_BATTERY:
alert_list.append(
alerts.Alert(
@ -161,12 +137,6 @@ async def ups_check() -> list[alerts.Alert]:
severity=alerts.Severity.INFO,
)
)
elif IS_TESTING or status == UPSStatus.UPS_OFFLINE:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS, message=f"UPS seems to be offline.", severity=alerts.Severity.INFO
)
)
elif IS_TESTING or status == UPSStatus.UPS_TRIM:
alert_list.append(
alerts.Alert(
@ -183,13 +153,5 @@ async def ups_check() -> list[alerts.Alert]:
severity=alerts.Severity.INFO,
)
)
elif IS_TESTING or status == UPSStatus.UPS_FSD:
alert_list.append(
alerts.Alert(
alert_type=alerts.AlertType.UPS,
message=f"Shutdown imminent!",
severity=alerts.Severity.CRITICAL,
)
)
return alert_list

View file

@ -3,9 +3,10 @@ from dataclasses import dataclass
from psutil import cpu_percent, sensors_temperatures, virtual_memory
from .enums import UPSStatus
from alerting import alerts
from .enums import UPSStatus
@dataclass
class TemperatureSensor:
@ -116,11 +117,13 @@ class Sensors:
try:
raw_data = subprocess.run(["upsc", "cp1300"], stdout=subprocess.PIPE, encoding="utf-8")
except FileNotFoundError:
await alerts.send_alert(alerts.Alert(
alert_type=alerts.AlertType.ERROR,
message="upsc is not installed!",
severity=alerts.Severity.CRITICAL
))
await alerts.send_alert(
alerts.Alert(
alert_type=alerts.AlertType.ERROR,
message="upsc is not installed!",
severity=alerts.Severity.CRITICAL,
)
)
return None
sensor_data = UPSSensor()
@ -131,13 +134,9 @@ class Sensors:
case "battery.charge":
sensor_data.battery_charge_percentage = int(value)
case "battery.charge.low":
# ? in case we need to evaluate critical% from sensor
# sensor_data.battery_critical_percentage = int(value)
sensor_data.battery_critical_percentage = 25
sensor_data.battery_critical_percentage = int(value)
case "battery.charge.warning":
# ? in case we need to evaluate warning% from sensor
# sensor_data.battery_warning_percentage = int(value)
sensor_data.battery_warning_percentage = 50
sensor_data.battery_warning_percentage = int(value)
case "battery.runtime":
sensor_data.battery_runtime = int(value)
case "ups.status":