From ed8593e41da8208df2de36f81dd3154356923709 Mon Sep 17 00:00:00 2001 From: askiiart Date: Wed, 31 Jan 2024 17:58:26 -0600 Subject: [PATCH] Flesh out basic structure of the program, finish initial extension docs --- .gitignore | 2 +- docs/extensions/alerts.md | 39 +++++++++++++++++++++++++++++++++++++ docs/extensions/checkers.md | 8 ++++---- docs/extensions/logging.md | 39 +++++++++++++++++++++++++++++++++++++ timer.py => helpers.py | 28 ++++++++++++++++++++++++++ updog.py | 25 ++---------------------- 6 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 docs/extensions/alerts.md create mode 100644 docs/extensions/logging.md rename timer.py => helpers.py (58%) diff --git a/.gitignore b/.gitignore index 86a0441..009be08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -extensions/ +extensions/* __pycache__/ logs/ \ No newline at end of file diff --git a/docs/extensions/alerts.md b/docs/extensions/alerts.md new file mode 100644 index 0000000..d09d729 --- /dev/null +++ b/docs/extensions/alerts.md @@ -0,0 +1,39 @@ +# Alerts Extensions + +Updog doesn't do any monitoring by itself. Instead, extensions are used to check the status of whatever thing, send alerts, and log stuff. Updog just chains them together. + +Example extension: [askiiart/updog-alerts_template](https://git.askiiart.net/askiiart/updog-alerts_template) + +## Folder layout + +Extensions need to be put in the `./extensions/alerts` folder, and the name of the file must match the name of the folder. + +```txt +. +├── extensions +│ ├── alerts +│ └── alerts_template +│ └── alerts_template.py +``` + +## Methods + +`*`: indicates a method is required + +### `__init__()`* + +**Arguments**: + +- (dict) Arguments from `alerts-args` in `services.json` - For no arguments, an empty dict will be used + +**Return**: None + +### `alert()`* + +**Arguments**: + +- (str) The name of the service the alert is for +- (int) The status code returned by the checker +- (str) The status message for that status code + +**Return**: Nothing diff --git a/docs/extensions/checkers.md b/docs/extensions/checkers.md index 504b9a0..5b53494 100644 --- a/docs/extensions/checkers.md +++ b/docs/extensions/checkers.md @@ -11,9 +11,9 @@ Extensions need to be put in the `./extensions/checkers` folder, and the name of ```txt . ├── extensions -│ ├── alerts -│ └── alerts_template -│ └── alerts_template.py +│ ├── checkers +│ └── checker_template +│ └── checker_template.py ``` ## Methods @@ -24,7 +24,7 @@ Extensions need to be put in the `./extensions/checkers` folder, and the name of **Arguments**: -- A dict of arguments from `checker-args` in `services.json` - For no arguments, an empty dict will be used +- (dict) Arguments from `checker-args` in `services.json` - For no arguments, an empty dict will be used Example: diff --git a/docs/extensions/logging.md b/docs/extensions/logging.md new file mode 100644 index 0000000..1c72a4f --- /dev/null +++ b/docs/extensions/logging.md @@ -0,0 +1,39 @@ +# Alerts Extensions + +Updog doesn't do any monitoring by itself. Instead, extensions are used to check the status of whatever thing, send alerts, and log stuff. Updog just chains them together. + +Example extension: [askiiart/updog-logging_template](https://git.askiiart.net/askiiart/updog-logging_template) + +## Folder layout + +Extensions need to be put in the `./extensions/logging` folder, and the name of the file must match the name of the folder. + +```txt +. +├── extensions +│ ├── logging +│ └── logging_template +│ └── logging_template.py +``` + +## Methods + +`*`: indicates a method is required + +### `__init__()`* + +**Arguments**: + +- A dict of arguments from `logging-args` in `services.json` - For no arguments, an empty dict will be used + +**Return**: None + +### `log()`* + +**Arguments**: `service_name`, `code`, `status` + +- `service_name`: The name of the service the logger is logging - currently it's always the same value for a single instance, but this is required for compatibility when support for logging multiple things with one instance of the logger is supported. +- `code`: The status code returned by the checker +- `status`: The status message for that status code + +**Return**: Nothing diff --git a/timer.py b/helpers.py similarity index 58% rename from timer.py rename to helpers.py index 09eaa56..0aa8ddd 100644 --- a/timer.py +++ b/helpers.py @@ -52,3 +52,31 @@ class RepeatedTimer(object): def stop(self): self._timer.cancel() self.is_running = False + +def create_instances(config, checkers, alerts, logging): + ''' + Creates instances of all the extensions according to config + Parameters: + config: the dictionary containing the config + Returns: + instances (dict): A dictionary containing instances of the extensions + example: {'site': {'checker': instanceOfCheckerTemplate}} + ''' + instances = {} + for service in config.keys(): + 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']) + + return instances + +def run_check(checker, alerts, logging): + ''' + Does the logic and stuff for running the checker, calling alerts/logging, and so on + ''' + pass \ No newline at end of file diff --git a/updog.py b/updog.py index b526a8f..979c500 100644 --- a/updog.py +++ b/updog.py @@ -1,4 +1,4 @@ -from timer import RepeatedTimer +from helpers import RepeatedTimer, create_instances, run_check import sys import os import importlib @@ -73,25 +73,4 @@ elif 'services-example.json' in os.listdir(): with open(config_filename, 'rt') as config_file: config = json.loads(''.join(config_file.readlines())) - -def create_instances(config): - ''' - Creates instances of all the extensions according to config - Parameters: - config: the dictionary containing the config - Returns: - instances (dict): A dictionary containing instances of the extensions - example: {'site': {'checker': instanceOfCheckerTemplate}} - ''' - instances = {} - for service in config.keys(): - 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']) - - return instances +print(create_instances(config, checkers, alerts, logging)) \ No newline at end of file