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/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/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: