docker_images/uptime/check/check.py

43 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
"""
Uptime check, at the moment HTTPS only domains
Usage of own healthchecks.io instance and config file
"""
from requests import get, post
from config import HC_ROOT, CONFIG
def check_domain(domain, timeout):
try:
r = get('https://%s' % domain, timeout=timeout)
except Exception as e:
return (e, 0)
return (r.status_code, r.elapsed)
def ping_with_info(url, fail=False, data=None):
try:
if fail:
url = '%s/fail' % url
post(url, timeout=10, data=data)
except Exception as e:
print("Ping failed: %s" % e)
for check_item in CONFIG:
print('Checking %s : %s' % (check_item['host'], ', '.join(check_item['domains'])))
fail = False
data = []
for domain in check_item['domains']:
(status, timed) = check_domain(domain, check_item.get('timeout', 9))
if not fail and status != 200:
fail = True
if isinstance(status, Exception):
data.append('%s : %s' % (domain, status))
else:
data.append('%s : %s (%sms)' % (
domain, status, round(timed.total_seconds() * 1000.0)
))
url = '%s/ping/%s' % (HC_ROOT, check_item['uuid'])
ping_with_info(url, fail=fail, data='\n'.join(data))