added service and deploy script
This commit is contained in:
26
upsmon.py
26
upsmon.py
@@ -1,15 +1,17 @@
|
||||
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 environ
|
||||
from os import path
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import paho.mqtt.enums as mqtt_enums
|
||||
|
||||
# Logger options
|
||||
LOGFILE = environ.get('LOGFILE','/mnt/logs/UPSmon.log')
|
||||
LOGFILE = '/mnt/logs/UPSmon.log'
|
||||
if not path.exists("/mnt/logs"):
|
||||
LOGFILE = '/tmp/UPSmon.log'
|
||||
|
||||
#CONSOLE_DEBUG = logging.DEBUG
|
||||
CONSOLE_DEBUG = logging.INFO
|
||||
@@ -49,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})
|
||||
))
|
||||
|
||||
@@ -74,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
|
||||
@@ -102,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)
|
||||
@@ -112,4 +114,4 @@ if __name__ == "__main__":
|
||||
while main() != 0:
|
||||
LOGGER.warning("Restarting...")
|
||||
sleep(30.0)
|
||||
sys.exit(0)
|
||||
sysexit(0)
|
||||
|
||||
Reference in New Issue
Block a user