From 6f5e1f59d0dc65eb7f75e8716754c32c96c4babe Mon Sep 17 00:00:00 2001 From: Kristian Krsnik Date: Tue, 3 Oct 2023 21:41:51 +0200 Subject: [PATCH] initial work --- outage_detector/main.py | 60 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/outage_detector/main.py b/outage_detector/main.py index ed3de06..f7b0639 100644 --- a/outage_detector/main.py +++ b/outage_detector/main.py @@ -255,12 +255,62 @@ def loadConfig() -> tuple[dict, str]: return config, mode +def parseArgs(args: list[str]): + import argparse + + parser = argparse.ArgumentParser( + prog='outage_detector', description='Log outages and print statistics.', formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + + subparsers = parser.add_subparsers() + + parser_outages = subparsers.add_parser( + 'outages', help='Print outages', formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser_outages.add_argument( + 'log_path', type=str, default='./connection.log', + help='Path to a file with connection logs.' + ) + parser_outages.add_argument( + '--time', type=int, default=3, + help='Minimum duration for a connection loss to be considered an outage.' + ) + + parser_log = subparsers.add_parser( + 'log', help='Log the connection status.', formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser_log.add_argument( + '--config', type=str, default='./config.json', + help='Path to a config file in JSON format.' + ) + parser_log.add_argument( + '--path', type=str, default='./connection.json', + help='Path to a the file where results should be appended to. Creates the file if it does not exist.' + ) + parser_log.add_argument( + '--host', type=str, default='1.1.1.1', + help='IP or hostname of a server to query.' + ) + parser_log.add_argument( + '--timeout', type=int, default=2, + help='Set the timeout in seconds to use for the connection test.' + ) + parser_log.add_argument( + '--no-stdout', action='store_true', default=argparse.SUPPRESS, + help='A flag that disables printing to stdout.' + ) + + parser.parse_args(args) + def main(): - config, mode = loadConfig() - if mode == 'log': - log(config) - elif mode == 'outages': - printOutages(config) + from sys import argv + + parseArgs(argv[1:]) + # config, mode = loadConfig() + # if mode == 'log': + # log(config) + # elif mode == 'outages': + # printOutages(config) if __name__ == '__main__':