From 6d0ef7b1370e14114f051e07f09d447d3d862f51 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Mon, 27 Dec 2021 16:55:54 +0100 Subject: [PATCH] Added threading to cron and foreground execution mode --- config.example.json | 6 +++--- uplink.py | 20 ++++++++++++-------- uplink/uplink.py | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/config.example.json b/config.example.json index ce1c1ac..9f8aab1 100644 --- a/config.example.json +++ b/config.example.json @@ -1,12 +1,12 @@ { "interval": 60, + "database_type": "mariadb", "database": "uplink", "database_host": "127.0.0.1", "database_user": "USER", "database_password": "PASSWORD", - "cron": false, - "daemon": false, - "log_level": "", + "mode": "cron", + "log_level": "ERROR", "uplinks": [ { "provider": "Cable Provider", "ip": "192.168.0.1", "password": "1234" }, { "provider": "DSL Provider", "ip": "192.168.1.1", "password": "1234" } diff --git a/uplink.py b/uplink.py index 32ee6ed..ccf3f6e 100755 --- a/uplink.py +++ b/uplink.py @@ -27,10 +27,11 @@ import json import sys import time +from threading import Thread from uplink.uplink import Uplink # Version format: MAJOR.FEATURE.FIXES -__version__ = "0.4.1-development" +__version__ = "0.4.2-development" # TODO: CHECK ALL ERROR HANDLING!!! # TODO: Implement logging @@ -43,7 +44,8 @@ __version__ = "0.4.1-development" def parse_args(): parser = argparse.ArgumentParser( - description="uplink is a tool to monitor the link status of AVM FRITZ!Box Cable and DSL based routers.", + description="uplink is a tool to monitor the link status of AVM FRITZ!Box Cable and DSL " + "based routers.", epilog="uplink is not some program expecting uplinks to work!", prog="uplink") @@ -59,8 +61,9 @@ def parse_args(): mode.add_argument("-f", "--foreground", default=False, dest="foreground", action="store_true", help="run looped in foreground (default: false)") - parser.add_argument("-i", "--interval", type=int, help="poll interval in seconds. this overrides config file " - "settings. (default: 60)") + parser.add_argument("-i", "--interval", type=int, help="poll interval in seconds. this " + "overrides config file settings. " + "(default: 60)") """ parser.add_argument("-b", "--database", metavar="DATABASE", help="database to use") @@ -112,7 +115,7 @@ if __name__ == "__main__": config = json.loads(config_data) except Exception as err: # TODO: Replace all lines like this with generic Python logging - print(str("Uplink: Configuration Error!" + str(err))) + print(str("uplink: Configuration Error! " + str(err))) sys.exit(1) try: @@ -129,14 +132,15 @@ if __name__ == "__main__": if args.cron: for i in range(len(config["uplinks"])): - uplink.get_data(config, i) + t = Thread(target=uplink.get_data, args=(config, i)) + t.start() elif args.daemon: uplink.start() elif args.foreground: while True: for i in range(len(config["uplinks"])): - uplink.get_data(config, i) - # TODO: print data formatted to STDOUT + t = Thread(target=uplink.get_data, args=(config, i)) + t.start() try: time.sleep(config["interval"]) except KeyboardInterrupt as err: diff --git a/uplink/uplink.py b/uplink/uplink.py index fce09b1..29a34a1 100644 --- a/uplink/uplink.py +++ b/uplink/uplink.py @@ -85,7 +85,7 @@ class Uplink(Daemon): status = "DOWN" # TODO: Replace all lines like this with generic Python logging - print(str("[" + str(d) + " " + str(t) + "] " +config["uplinks"][inc]["provider"] + " " + status)) + print(str("[" + str(d) + " " + str(t) + "] " + config["uplinks"][inc]["provider"] + " " + status)) # TODO: Fix to long lines and make the SQL statement more readable sql = 'INSERT INTO log (timestamp, date, time, uptime, internal_ip, external_ip, external_ipv6, is_linked, ' \