From 864f3526d12efdc3818c20f28a80e060ee586c02 Mon Sep 17 00:00:00 2001 From: abdulkaderf Date: Thu, 21 Nov 2024 20:47:27 +0100 Subject: [PATCH 1/2] add capability to run custom python scripts --- .gitignore | 3 ++- checks.yaml | 7 +++++++ tinystatus.py | 14 +++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 61456a0..58207c5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ index.html history.html history.json nohup.out -.env \ No newline at end of file +.env +venv diff --git a/checks.yaml b/checks.yaml index cb26892..f140ef2 100644 --- a/checks.yaml +++ b/checks.yaml @@ -80,3 +80,10 @@ host: https://homeserver.local ssc: True expected_code: 200 + + # - name: My Custom Check + # type: user_defined + # module_name: external_script + # function_name: check_custom + # variables: + # custom_var: custom_value diff --git a/tinystatus.py b/tinystatus.py index 327ff76..5185bb2 100644 --- a/tinystatus.py +++ b/tinystatus.py @@ -12,6 +12,7 @@ import json import logging import platform +import importlib # Load environment variables load_dotenv() @@ -62,6 +63,16 @@ async def check_port(host, port): except: return False +async def check_user_defined(module_name, function_name, variables=None): + try: + module = importlib.import_module(module_name) + function = getattr(module, function_name) + return function(**variables) + except Exception as e: + print(f"Error running user defined check: {e}") + return False + + async def run_checks(checks): background_tasks = {} @@ -76,7 +87,8 @@ async def run_checks(checks): task = tg.create_task( check_http(check['host'], check['expected_code'], selfcert) if check['type'] == 'http' else check_ping(check['host']) if check['type'] == 'ping' else - check_port(check['host'], check['port']) if check['type'] == 'port' else None, + check_port(check['host'], check['port']) if check['type'] == 'port' else + check_user_defined(check['module_name'], check['function_name'], check['variables']) if check['type'] == 'user_defined' else None, name=check['name'] ) if task: From 728b8d368dd353e9d5b23dcfb49be621ac479ab3 Mon Sep 17 00:00:00 2001 From: abdulkaderf Date: Thu, 21 Nov 2024 20:59:44 +0100 Subject: [PATCH 2/2] update readme --- README.md | 10 ++++++++++ checks.yaml | 7 ------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b98a243..3b0f269 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,16 @@ In order to run the script using Docker: - Adjust the configuration variables in the `.env` file to customize the behavior of TinyStatus. - Customize the appearance of the status page by editing the CSS in `index.html.theme` and `history.html.theme`. - Add or remove services by modifying the `checks.yaml` file. +- Add your own custom checks by adding a check of type `user_defined` to the `checks.yaml` file. The check can be any Python function that takes the specified variables as input and returns a boolean value. + Example: + ```yaml + - name: My Custom Check + type: user_defined + module_name: my_module_name + function_name: my_function_name + variables: + custom_var: custom_value + ``` ## Porting TinyStatus diff --git a/checks.yaml b/checks.yaml index f140ef2..cb26892 100644 --- a/checks.yaml +++ b/checks.yaml @@ -80,10 +80,3 @@ host: https://homeserver.local ssc: True expected_code: 200 - - # - name: My Custom Check - # type: user_defined - # module_name: external_script - # function_name: check_custom - # variables: - # custom_var: custom_value