minitor/minitor/main.py

43 lines
1.1 KiB
Python
Raw Normal View History

2018-02-14 23:37:15 +00:00
from subprocess import CalledProcessError
from subprocess import check_call
2018-02-15 01:54:42 +00:00
from time import sleep
2018-02-14 23:37:15 +00:00
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
2018-02-15 01:54:42 +00:00
config = get_config('config.yml')
2018-02-14 23:37:15 +00:00
alerts = config.get('alerts', {})
2018-02-15 01:54:42 +00:00
while True:
for monitor in config.get('monitors', []):
try:
check_monitor(monitor)
except CalledProcessError:
alert_for_monitor(monitor, alerts)
sleep(config.get('interval', 1))
2018-02-14 23:37:15 +00:00
if __name__ == '__main__':
main()