Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/allow custom user defined checks #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ index.html
history.html
history.json
nohup.out
.env
.env
venv
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion tinystatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import logging
import platform
import importlib

# Load environment variables
load_dotenv()
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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:
Expand Down