From 219bcf8a12a347e7131430ec1adeb3aa1e01b668 Mon Sep 17 00:00:00 2001 From: askiiart Date: Tue, 6 Feb 2024 12:37:19 -0600 Subject: [PATCH] Add default extensions --- README.md | 18 +++++++++--------- docs/README.md | 29 +++++++++++++++++++--------- helpers.py | 44 ++++++++++++++++++++++++++++++++++++------- services-example.json | 13 +++++++------ updog.py | 2 ++ 5 files changed, 75 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index ff6b357..d30eb4c 100644 --- a/README.md +++ b/README.md @@ -20,21 +20,22 @@ Docs are in the `./docs/` folder, but here's a quick overview on how to use this ```json { + "global-args": { + "default-checker": "CheckerTemplate", + "default-alerts": "AlertsTemplate", + "default-logging": "LoggingTemplate" + }, "site": { "name": "A Website", - "checker": "CheckerTemplate", + "checker": "HttpChecker", "checker-args": { - "url": "https://example.net", - "port": 443, - "lol": "CheckerTemplate ignores these options lol" + "url": "https://example.com" }, - "rate": 60, - "alerts": "AlertsTemplate", + "rate": 1, "alerts-args": { "url": "https://example.com/webhook-url-or-whatever-goes-here", "lol": "irrelevant, AlertsTemplate ignores these options lol" }, - "logging": "LoggingTemplate", "logging-args": { "file": "/home/askiiart/Documents/updog/logs/site-log" } @@ -45,8 +46,7 @@ Docs are in the `./docs/` folder, but here's a quick overview on how to use this ## To-do - Add maintenance windows (optionally recurring) -- Add ability to set default extensions that can be overridden on a per-service basis - - Also add groups +- Add groups - Add ability for checkers to have preferred alerts and logging extensions - will print a warning if the preferred extension(s) are not used - Add "keywords" to be replaced in the config (for current directory, service name, maybe some others) diff --git a/docs/README.md b/docs/README.md index 48b1bfb..29b8682 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,21 +14,22 @@ Then create a `services.json` file for your config, like this: ```json { + "global-args": { + "default-checker": "CheckerTemplate", + "default-alerts": "AlertsTemplate", + "default-logging": "LoggingTemplate" + }, "site": { "name": "A Website", - "checker": "CheckerTemplate", + "checker": "HttpChecker", "checker-args": { - "url": "https://example.net", - "port": 443, - "lol": "CheckerTemplate ignores these options lol" + "url": "https://example.com" }, "rate": 1, - "alerts": "AlertsTemplate", "alerts-args": { "url": "https://example.com/webhook-url-or-whatever-goes-here", "lol": "irrelevant, AlertsTemplate ignores these options lol" }, - "logging": "LoggingTemplate", "logging-args": { "file": "/home/askiiart/Documents/updog/logs/site-log" } @@ -36,15 +37,25 @@ Then create a `services.json` file for your config, like this: } ``` +#### Global config + +Optional arguments: + +- `default-checker`: The default checker for all services, can be overriden per-service using `checker` +- `default-alerts`: The default alerter extension for all services, can be overriden per-service using `alerts` +- `default-logging`: The default logging extension for all services, can be overriden per-service using `logging` + +#### Service config + Required arguments: - The key (in this case `site`): A unique string to identify the service in logs, also used internally. -- `checker`: The name of the checker extension's *class*. +- `checker`: The name of the checker extension's *class* - optional if `default-checker` is specified in the global arguments. - `checker-args`: Arguments for the checker. Check the checker's docs for what to put here. - `rate`: How often to check if the service is up (every *x* seconds) -- `alerts`: The name of the alerting extension's *class*. +- `alerts`: The name of the alerting extension's *class* - optional if `default-alerts` is specified in the global arguments. - `alerts-args`: Arguments for the alerting extension. Check the alerter's docs for what to put here. -- `logging`: The name of the logging extension's *class*. +- `logging`: The name of the logging extension's *class* - optional if `default-logging` is specified in the global arguments. - `logging-args`: Arguments for the logging extension. Check the logger's docs for what to put here. Optional arguments: diff --git a/helpers.py b/helpers.py index 10c4294..80184e7 100644 --- a/helpers.py +++ b/helpers.py @@ -66,15 +66,45 @@ def create_instances(config, checkers, alerts, logging): example: {'site': {'checker': instanceOfCheckerTemplate}} ''' instances = {} + + # global arguments + default_checker = '' + default_alerter = '' + default_logger = '' for service in config.keys(): + + if service == 'global-args': + default_checker = config['global-args'].get('default-checker', '') + default_alerter = config['global-args'].get('default-alerts', '') + default_logger = config['global-args'].get('default-logging', '') + continue instances[service] = {} - # just creates an instance of the checker with the arguments for it - instances[service]['checker'] = checkers[config[service] - ['checker']](config[service]['checker-args']) - instances[service]['alerts'] = alerts[config[service] - ['alerts']](config[service]['alerts-args']) - instances[service]['logging'] = logging[config[service] - ['logging']](config[service]['logging-args']) + # create an instance of checker with the arguments for them, including the optional `default-checker` + checker_args = config[service]['checker-args'] + if config[service].get('checker', '') != '': + checker = config[service]['checker'] + else: + checker = default_checker + + instances[service]['checker'] = checkers[checker](checker_args) + + # and for alerts + alerter_args = config[service]['alerts-args'] + if config[service].get('alerts', '') != '': + alerter = config[service]['alerts'] + else: + alerter = default_alerter + + instances[service]['alerts'] = alerts[alerter](alerter_args) + + # and for logging + logger_args = config[service]['logging-args'] + if config[service].get('logging', '') != '': + logger = config[service]['logging'] + else: + logger = default_logger + + instances[service]['logging'] = logging[logger](logger_args) return instances diff --git a/services-example.json b/services-example.json index d0dfd1c..6c3703f 100644 --- a/services-example.json +++ b/services-example.json @@ -1,19 +1,20 @@ { + "global-args": { + "default-checker": "CheckerTemplate", + "default-alerts": "AlertsTemplate", + "default-logging": "LoggingTemplate" + }, "site": { "name": "A Website", - "checker": "CheckerTemplate", + "checker": "HttpChecker", "checker-args": { - "url": "https://example.net", - "port": 443, - "lol": "CheckerTemplate ignores these options lol" + "url": "https://askiiart.net" }, "rate": 1, - "alerts": "AlertsTemplate", "alerts-args": { "url": "https://example.com/webhook-url-or-whatever-goes-here", "lol": "irrelevant, AlertsTemplate ignores these options lol" }, - "logging": "LoggingTemplate", "logging-args": { "file": "/home/askiiart/Documents/updog/logs/site-log" } diff --git a/updog.py b/updog.py index 7e5d45f..2cbadfe 100644 --- a/updog.py +++ b/updog.py @@ -78,6 +78,8 @@ timers = {} try: for service in config.keys(): + if service == 'global-args': + continue timers[service] = RepeatedTimer(config[service]['rate'], run_check, service, instances[service]['checker'], instances[service]['alerts'], instances[service]['logging']) except: