From 609e4b50d5056bd134d19f2804b37a6a9ff12706 Mon Sep 17 00:00:00 2001 From: askiiart Date: Mon, 29 Jan 2024 10:45:39 -0600 Subject: [PATCH] Initial commit - basics of checker extensions added --- .gitignore | 3 + .../checker_template/checker_template.py | 15 ++++ main.py | 31 +++++++ readme.md | 88 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 extensions/checkers/checker_template/checker_template.py create mode 100644 main.py create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74c9223 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +extensions/__pycache__ +extensions/checkers/__pycache__ +extensions/checkers/checker_template/__pycache__ \ No newline at end of file diff --git a/extensions/checkers/checker_template/checker_template.py b/extensions/checkers/checker_template/checker_template.py new file mode 100644 index 0000000..82f9069 --- /dev/null +++ b/extensions/checkers/checker_template/checker_template.py @@ -0,0 +1,15 @@ +import random + + +class CheckerTemplate: + def __init__(arguments): + pass + + def get_status(): + latency = int(random.random() * 3000) + if latency > 2500: # "simulate" a timeout + return 0 + elif latency > 1000: # "simulate" a very slow application - a partial outage + return 50 + else: + return 100 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..4d192d4 --- /dev/null +++ b/main.py @@ -0,0 +1,31 @@ +import sys +import os +import importlib +import inspect + +DEBUG = False + +# relative imports suck - https://gideonbrimleaf.github.io/2021/01/26/relative-imports-python.html +path = os.path.realpath(__file__) +path = path[:path.rfind('/')] +sys.path.insert(1, f'{path}/extensions/checkers') + +# importlib used to import stuff programmatically, rather than using the hardcoded import keyword, basically the same but it seems it can't import *just* a class +uptime_extension_imports = [] +for ext_name in os.listdir(f'{path}/extensions/checkers'): + if ext_name[0] != '.': + uptime_extension_imports.append(f'{ext_name}.{ext_name}') + +# uptime_checkers contains all the classes for the checker extensions +# e.g. {'CheckerTemplate': checker_template.checker_template.CheckerTemplate} +checkers = dict() +for ext in [importlib.import_module(ext) for ext in uptime_extension_imports]: + for name, obj in inspect.getmembers(ext): + if inspect.isclass(obj): + checkers[name] = obj + +if DEBUG: + print('uptime_extension_imports:', uptime_extension_imports) + print('checkers:', checkers) + +print(checkers['CheckerTemplate'].get_status()) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..706ed51 --- /dev/null +++ b/readme.md @@ -0,0 +1,88 @@ +# askiiart/updog + +## What's updog? + +Not much, you? + +(it's a simple, extensible uptime monitor) + +## Warning + +**Extensions will be executed with no safety checks!** Make sure an extension isn't malicious before adding it. + +## Uptime Checker Extensions + + + +Updog doesn't do any monitoring by itself. Instead, extensions are used to check the status of whatever thing, and are used + +### Metadata + +Extensions need to be put in the `./extensions/checkers` folder and the name of the file must match the name of the folder. In the future, `alerts` and `logging` folders will be added for those extensions. For now, there is only support for checkers, and Updog itself will have very basic logging. + +### Methods + +`*`: indicates a method is required + +#### `__init__()`* + +**Arguments**: + +- a list of strings from `checker-args` in `services.json` - for no arguments an empty list will be used + +**Return**: None + +#### `get_status()`* + +**Arguments**: None + +**Return**: An integer from 0 to 100 indicating status; defaults listed in [`get_return_codes()`](#get_return_codes) + +These values can be overriden by providing [`get_return_codes()`](#get_return_codes) + +#### `get_return_codes()` + +**Arguments**: None + +**Return**: A `dict` containing integers and their associated statuses. + +Default: + +```py +{ + 0: "Down", + 50: "Partial outage", + 100: "Up" +} +``` + +## To-do + +- Add basic functionality + - Read `services.json` file: + +```json +{ + "site" { + "name": "A Website", + "uptime-checker": "CheckerTemplate", + "checker-args": { + "url": "https://example.net", + "port: "443", + }, + "rate": 60, + "alerts": "AlertsTemplate", + "alerts-args" { + "url": "https://example.com/webhook-url-or-whatever-goes-here" + } + } +} +``` + +The args are just passed to the extension as a `dict`, the extension handles it from there. + +- Add support for logging and alert extensions + +--- + +***All specs are still a work-in-progress, breaking changes are likely!***