Last active
May 1, 2025 15:47
-
-
Save ZiTAL/75bfc3b3e484dd7f7637f463a92bb7b4 to your computer and use it in GitHub Desktop.
python: salicru ups telegram alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/5 * * * * /usr/bin/python3 /home/zital/scripts/python/ups.py >> /dev/null 2>> /home/zital/scripts/python/ups.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/nut/nut.conf | |
MODE=standalone |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/nut/ups.conf | |
maxretry = 3 | |
[salicru] | |
driver = blazer_usb | |
port = auto | |
desc = "Salicru UPS" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
import re | |
import requests | |
UPS_NAME = "salicru" | |
def readFile(path): | |
with open(path, 'r') as file: | |
content = file.read() | |
return content | |
def tele(msg): | |
token = readFile(sys.path[0]+"/ups/telegram.credentials").strip() | |
channel = readFile(sys.path[0]+"/ups/telegram.channel").strip() | |
requests.get("https://api.telegram.org/bot"+token+"/sendMessage", params={ | |
"chat_id": channel, | |
"text": msg | |
}) | |
def get_ups_status(ups_name): | |
try: | |
output = subprocess.check_output( | |
["upsc", ups_name], | |
stderr=subprocess.DEVNULL, | |
universal_newlines=True) | |
return dict(line.split(": ", 1) for line in output.strip().split("\n") if ": " in line) | |
except subprocess.CalledProcessError as e: | |
print("Error getting UPS status:", e) | |
return {} | |
def log_status(status): | |
ups_status = status.get("ups.status", "UNKNOWN") | |
ups_info = ( | |
f"SALICRU UPS:\n\n" | |
f"OL = On Line (normal, power is good)\n" | |
f"OB = On Battery (power outage — likely beeping)\n" | |
f"LB = Low Battery\n" | |
f"RB = Replace Battery\n\n" | |
f"Status: {ups_status}\n" | |
f"Charge: {status.get('battery.charge', '?')}%\n" | |
f"Load: {status.get('ups.load', '?')}%\n" | |
) | |
print(ups_info.strip()) | |
if "OL" not in ups_status: | |
tele(ups_info) | |
""" | |
OL = On Line (normal, power is good) | |
OB = On Battery (power outage — likely beeping) | |
LB = Low Battery | |
RB = Replace Battery | |
""" | |
if __name__ == "__main__": | |
status = get_ups_status(UPS_NAME) | |
if status: | |
log_status(status) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/nut/upsd.users | |
[monuser] | |
password = secret | |
upsmon master |
Author
ZiTAL
commented
May 1, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment