docker_images/uptime/check/check.py

43 lines
1.2 KiB
Python
Raw Normal View History

2021-05-13 16:32:05 +02:00
# -*- coding: utf-8 -*-
"""
Uptime check, at the moment HTTPS only domains
Usage of own healthchecks.io instance and config file
2021-05-13 16:32:05 +02:00
"""
from requests import get, post
from config import HC_ROOT, CONFIG
2021-05-13 16:32:05 +02:00
def check_domain(domain):
try:
r = get('https://%s' % domain)
except Exception as e:
return (e, 0)
return (r.status_code, r.elapsed)
2021-05-13 16:32:05 +02:00
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:
2021-05-13 16:32:05 +02:00
print("Ping failed: %s" % e)
for check_item in CONFIG:
print('Checking %s : %s' % (check_item['host'], ', '.join(check_item['domains'])))
2021-05-13 16:32:05 +02:00
fail = False
data = []
for domain in check_item['domains']:
(status, timed) = check_domain(domain)
2021-05-13 16:32:05 +02:00
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))