Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active May 1, 2025 15:47
Show Gist options
  • Save ZiTAL/75bfc3b3e484dd7f7637f463a92bb7b4 to your computer and use it in GitHub Desktop.
Save ZiTAL/75bfc3b3e484dd7f7637f463a92bb7b4 to your computer and use it in GitHub Desktop.
python: salicru ups telegram alert
*/5 * * * * /usr/bin/python3 /home/zital/scripts/python/ups.py >> /dev/null 2>> /home/zital/scripts/python/ups.log
# /etc/nut/nut.conf
MODE=standalone
# /etc/nut/ups.conf
maxretry = 3
[salicru]
driver = blazer_usb
port = auto
desc = "Salicru UPS"
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)
# /etc/nut/upsd.users
[monuser]
password = secret
upsmon master
@ZiTAL
Copy link
Author

ZiTAL commented May 1, 2025

su
upsdrvctl start
systemctl restart nut-server
systemctl restart nut-client
exit
upsc salicru

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment