lego-monitoring/send_ups_alert.py
2024-09-01 14:26:15 +03:00

41 lines
1.2 KiB
Python

#!/usr/bin/env python3
import asyncio
from os import environ
from sys import argv
from alerting import alerts
type_priority_map = {
"ONLINE": alerts.Severity.INFO,
"ONBATT": alerts.Severity.WARNING,
"LOWBATT": alerts.Severity.CRITICAL,
"FSD": alerts.Severity.CRITICAL,
"COMMOK": alerts.Severity.INFO,
"COMMBAD": alerts.Severity.WARNING,
"SHUTDOWN": alerts.Severity.CRITICAL,
"REPLBATT": alerts.Severity.WARNING,
"NOCOMM": alerts.Severity.WARNING,
"NOPARENT": alerts.Severity.CRITICAL,
"CAL": alerts.Severity.INFO,
"NOTCAL": alerts.Severity.INFO,
"OFF": alerts.Severity.CRITICAL,
"NOTOFF": alerts.Severity.INFO,
"BYPASS": alerts.Severity.WARNING,
"NOTBYPASS": alerts.Severity.INFO,
None: alerts.Severity.CRITICAL,
}
async def main():
"""Sends an alert about a UPS event. Meant to be used from upsmon with NOTIFYCMD"""
if len(argv) != 2:
raise Exception("provide exactly one argument: alert message")
message = argv[1]
typestr = environ.get("NOTIFYTYPE", None)
severity = type_priority_map[typestr]
alert = alerts.Alert(alert_type=alerts.AlertType.UPS, message=message, severity=severity)
await alerts.send_alert(alert)
if __name__ == "__main__":
asyncio.run(main())