updated routermon e upsmon, ora funzionano

This commit is contained in:
2025-05-30 23:26:22 +02:00
parent 27170ef12a
commit 5f34f3169a
6 changed files with 140 additions and 38 deletions

View File

@@ -7,17 +7,23 @@ import logging
import signal
import json
from influxdb_client.client.write.point import Point
from influxdb_client.client.influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS
# Get environment variables
env = dict(os.environ)
LOGGER: logging.Logger
class Runner:
running: bool = True
def __init__(self):
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
class SignalHandler:
running: bool
def stop(self, _):
def __init__(self):
self.running: bool = True
signal.signal(signal.SIGINT, self._handle_sigint)
signal.signal(signal.SIGTERM, self._handle_sigint)
def _handle_sigint(self, signum, frame):
self.running = False
def send(port: serial.Serial, d: str):
@@ -47,9 +53,14 @@ def bruteforceCommands(port: serial.Serial):
def main():
INTERVAL = int(env['INTERVAL'])
run = Runner.running
LOGGER.debug(json.dumps(env, indent=2))
run: SignalHandler = SignalHandler()
port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1)
while run:
write_client = InfluxDBClient(url=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'],
org=env['INFLUXDB_ORG'])
write_api = write_client.write_api(write_options=ASYNCHRONOUS)
while run.running:
try:
send(port, "Q1")
data = receive(port, "Q1").lstrip('(').split()
@@ -66,9 +77,13 @@ def main():
'onLine': True if str(data[7]).endswith('1') else False,
}
LOGGER.debug(f"UPS Status: \n{json.dumps(values, indent=2)}")
if values['onBatt']:
LOGGER.info(f"OnBattery\n{json.dumps(values,indent=2)}")
p = Point('ups')
for k,v in values.items():
p.field(k,v)
write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'], record=p)
time.sleep(INTERVAL)
except KeyboardInterrupt:
run = False
except Exception as e:
print(f"Unexpected exception: [{e}]")
return 1

9
upsmon/upsmon.Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM python:3.12-alpine
RUN apk update && apk upgrade --no-cache
RUN pip install --no-cache-dir pyserial RouterOS-API influxdb-client
COPY ./ups.py /home/ups.py
CMD [ "python", "/home/ups.py" ]