Compare commits
2 Commits
a360090f16
...
d7872ed620
| Author | SHA1 | Date | |
|---|---|---|---|
| d7872ed620 | |||
| e414307426 |
13
deploy.sh
Executable file
13
deploy.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
sudo mkdir -p /opt/upsmon
|
||||
sudo python3 -m venv /opt/upsmon/venv
|
||||
sudo /opt/upsmon/venv/bin/pip install paho-mqtt
|
||||
sudo cp upsmon.py /opt/upsmon/
|
||||
sudo cp upsmon.service /etc/systemd/system/upsmon.service
|
||||
|
||||
sudo systemctl daemon-reexec
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable upsmon.service
|
||||
sudo systemctl start upsmon.service
|
||||
|
||||
27
upsmon.py
27
upsmon.py
@@ -1,17 +1,20 @@
|
||||
import sys
|
||||
import json
|
||||
import subprocess
|
||||
import logging
|
||||
from subprocess import check_output, TimeoutExpired, CalledProcessError
|
||||
from json import dumps
|
||||
from sys import exit as sysexit, stdout
|
||||
from time import sleep
|
||||
from os import path
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import paho.mqtt.enums as mqtt_enums
|
||||
|
||||
# Logger options
|
||||
LOGFILE = '/mnt/logs/UPSmon.log'
|
||||
if not path.exists("/mnt/logs"):
|
||||
LOGFILE = '/tmp/UPSmon.log'
|
||||
|
||||
CONSOLE_DEBUG = logging.DEBUG
|
||||
#CONSOLE_DEBUG = logging.INFO
|
||||
#CONSOLE_DEBUG = logging.DEBUG
|
||||
CONSOLE_DEBUG = logging.INFO
|
||||
#FILE_DEBUG = logging.ERROR
|
||||
#FILE_DEBUG = logging.WARNING
|
||||
FILE_DEBUG = logging.INFO
|
||||
@@ -48,18 +51,18 @@ def main() -> int:
|
||||
|
||||
while client.is_connected():
|
||||
try:
|
||||
rv: bytes = subprocess.check_output(CMD, timeout=2.0)
|
||||
rv: bytes = check_output(CMD, timeout=2.0)
|
||||
data_raw: list[list[str]] = [
|
||||
l.split(':', 1) for l in rv.decode(encoding='ascii').splitlines()
|
||||
]
|
||||
data = {
|
||||
k.strip().lower():clean_data(v.strip()) for k,v in data_raw if k.strip() in EXPORT_KEYS
|
||||
}
|
||||
LOGGER.debug(f"[{i}] {json.dumps(data, indent=2)}")
|
||||
LOGGER.debug(f"[{i}] {dumps(data, indent=2)}")
|
||||
|
||||
tag: str = str(data.pop('upsname'))
|
||||
client.publish(topic='monitoring/ups/status',
|
||||
payload=json.dumps(
|
||||
payload=dumps(
|
||||
obj=(data, {"upsname":tag})
|
||||
))
|
||||
|
||||
@@ -73,10 +76,10 @@ def main() -> int:
|
||||
except FileNotFoundError as f:
|
||||
LOGGER.error(f"{CMD} executable not found: {f}")
|
||||
return f.errno if f.errno else -1
|
||||
except subprocess.CalledProcessError as e:
|
||||
except CalledProcessError as e:
|
||||
LOGGER.error(f"{CMD} failed with: {e}")
|
||||
return e.returncode
|
||||
except subprocess.TimeoutExpired as t:
|
||||
except TimeoutExpired as t:
|
||||
LOGGER.warning(f"{CMD} timed out: {t}")
|
||||
return int(t.timeout)
|
||||
except Exception as ee: # default case catch all
|
||||
@@ -101,7 +104,7 @@ if __name__ == "__main__":
|
||||
LOGGER.addHandler(fh)
|
||||
|
||||
# Console logging
|
||||
cl = logging.StreamHandler(sys.stdout)
|
||||
cl = logging.StreamHandler(stdout)
|
||||
cl.setLevel(CONSOLE_DEBUG)
|
||||
cl.setFormatter(formatter)
|
||||
LOGGER.addHandler(cl)
|
||||
@@ -111,4 +114,4 @@ if __name__ == "__main__":
|
||||
while main() != 0:
|
||||
LOGGER.warning("Restarting...")
|
||||
sleep(30.0)
|
||||
sys.exit(0)
|
||||
sysexit(0)
|
||||
|
||||
15
upsmon.service
Normal file
15
upsmon.service
Normal file
@@ -0,0 +1,15 @@
|
||||
[Unit]
|
||||
Description=APC UPS Monitor
|
||||
After=apcupsd.service network-online.target
|
||||
Wants=network-online.target
|
||||
Requires=apcupsd.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
WorkingDirectory=/opt/upsmon
|
||||
ExecStart=/opt/upsmon/venv/bin/python /opt/upsmon/upsmon.py
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user