From 0d34d7cb0d1108591fe353f87297ce06e2e3467b Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Sun, 1 Jun 2025 10:26:11 +0200 Subject: [PATCH] Fix Build e domenticanza nel routermon --- docker/build.sh | 2 ++ routermon/routermon.py | 29 ++++++++++++++++++----------- upsmon/ups.py | 6 +++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/docker/build.sh b/docker/build.sh index 684f9ed..7d59c8b 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -2,6 +2,8 @@ GIT_HASH=$(git rev-parse --short HEAD) +docker-compose down + echo "Building UPSmon" cp ../pyutils/utils.py ../upsmon/ docker build --build-arg BUILD_VER=${GIT_HASH} -f ../upsmon/upsmon.Dockerfile -t upsmon:influx3 ../upsmon diff --git a/routermon/routermon.py b/routermon/routermon.py index e18f951..71d95ea 100644 --- a/routermon/routermon.py +++ b/routermon/routermon.py @@ -15,18 +15,25 @@ LOGGER: logging.Logger ################## ###### MAIN ###### ################## -def main(): +def main() -> int: INTERVAL = int(env['INTERVAL']) - # Init InfluxDB-v3 Client - write_client = InfluxDBClient3(host=env['INFLUXDB_URL'], - token=env['INFLUXDB_TOKEN'], - database=env['INFLUXDB_DATABASE']) - # Init routerOS API - connection = routeros_api.RouterOsApiPool(env['MIKROTIK_IP'], - username=env['MIKROTIK_USER'], - password=env['MIKROTIK_PASSWORD'], - plaintext_login=True) - api = connection.get_api() + try: + # Init InfluxDB-v3 Client + write_client = InfluxDBClient3(host=env['INFLUXDB_URL'], + token=env['INFLUXDB_TOKEN'], + database=env['INFLUXDB_DATABASE']) + # Init routerOS API + connection = routeros_api.RouterOsApiPool(env['MIKROTIK_IP'], + username=env['MIKROTIK_USER'], + password=env['MIKROTIK_PASSWORD'], + plaintext_login=True) + api = connection.get_api() + except Exception as e: + LOGGER.error(e) + return 1 + finally: + LOGGER.info(f"InfluxDB Connected: [{env['INFLUXDB_URL']}/{env['INFLUXDB_DATABASE']}]") + LOGGER.info(f"Mikrotik Connected: [{env['MIKROTIK_IP']}]") ############################## ########## MAIN LOOP ######### diff --git a/upsmon/ups.py b/upsmon/ups.py index 02580d7..835e28e 100644 --- a/upsmon/ups.py +++ b/upsmon/ups.py @@ -43,7 +43,7 @@ def bruteforceCommands(port: serial.Serial): ################## ###### MAIN ###### ################## -def main(): +def main() -> int: INTERVAL = int(env['INTERVAL']) UPS_COMMAND = "Q1" try: @@ -55,7 +55,7 @@ def main(): port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1) except Exception as e: LOGGER.error(e) - return 0 + return 1 finally: LOGGER.info(f"InfluxDB Connected: [{env['INFLUXDB_URL']}/{env['INFLUXDB_DATABASE']}]") LOGGER.info(f"Serial Port Open: [{env['PORT']}]") @@ -82,7 +82,7 @@ def main(): } LOGGER.debug(f"UPS Status: \n{json.dumps(values, indent=2)}") if values['onBatt']: - LOGGER.info(f"OnBattery\n{json.dumps(values,indent=2)}") + LOGGER.info(f"OnBattery: \n{json.dumps(values,indent=2)}") write_client.write(record=dict2Point('ups', values)) time.sleep(INTERVAL) except Exception as e: