diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7013b0a --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +env: + virtualenv -p python3 env + ./env/bin/pip install -r requirements.txt + +run: env + ./env/bin/python -m minitor.main diff --git a/minitor/__init__.py b/minitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/minitor/main.py b/minitor/main.py new file mode 100644 index 0000000..a9a36cd --- /dev/null +++ b/minitor/main.py @@ -0,0 +1,39 @@ +from subprocess import CalledProcessError +from subprocess import check_call + +import yamlenv + +# TODO: validate on start + +def get_config(path): + """Loads config from a YAML file with env interpolation""" + with open(path, 'r') as yaml: + contents = yaml.read() + return yamlenv.load(contents) + + +def check_monitor(monitor): + cmd = monitor.get('command', []) + if cmd: + check_call(cmd, shell=isinstance(cmd, str)) + + +def alert_for_monitor(monitor, alerts): + for alert_name in monitor.get('alerts', []): + cmd = alerts.get(alert_name, {}).get('command', []) + if cmd: + check_call(cmd, shell=isinstance(cmd, str)) + + +def main(): + # TODO: get config file off command line + config = get_config('./sample-config.yml') + alerts = config.get('alerts', {}) + for monitor in config.get('monitors', []): + try: + check_monitor(monitor) + except CalledProcessError: + alert_for_monitor(monitor, alerts) + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e315c16 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +yamlenv diff --git a/sample-config.yml b/sample-config.yml new file mode 100644 index 0000000..c1f6b83 --- /dev/null +++ b/sample-config.yml @@ -0,0 +1,19 @@ +monitors: + - name: View + command: [ 'curl', 'https://localhost:5000' ] + # environment: + # PATH: ${PATH} + alerts: [ email, sms ] + # other settings from other health check apps like interval and back off + +alerts: + email: + command: echo 'Send an email' + sms: + command: echo 'Send an SMS' + +federation: + - location: https://host1.com + client_key: keyfromhost1 + server_key: keyhost1uses + alerts: [ sms ]