2021-05-13 16:32:05 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Uptime check, at the moment HTTPS only domains
|
2021-10-23 14:36:49 +02:00
|
|
|
Usage of own healthchecks.io instance and config file
|
2021-05-13 16:32:05 +02:00
|
|
|
"""
|
|
|
|
|
2021-05-13 17:49:30 +02:00
|
|
|
from requests import get, post
|
2021-10-23 14:36:49 +02:00
|
|
|
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:
|
2021-12-19 17:30:03 +01:00
|
|
|
return (e, 0)
|
2021-11-02 19:11:22 +01:00
|
|
|
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)
|
2021-05-13 17:49:30 +02:00
|
|
|
except Exception as e:
|
2021-05-13 16:32:05 +02:00
|
|
|
print("Ping failed: %s" % e)
|
|
|
|
|
2021-10-23 14:36:49 +02:00
|
|
|
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 = []
|
2021-10-23 14:36:49 +02:00
|
|
|
for domain in check_item['domains']:
|
2021-11-02 19:11:22 +01:00
|
|
|
(status, timed) = check_domain(domain)
|
2021-05-13 16:32:05 +02:00
|
|
|
if not fail and status != 200:
|
|
|
|
fail = True
|
2021-12-19 17:30:03 +01:00
|
|
|
if isinstance(status, Exception):
|
|
|
|
data.append('%s : %s' % (domain, status))
|
|
|
|
else:
|
|
|
|
data.append('%s : %s (%sms)' % (
|
|
|
|
domain, status, round(timed.total_seconds() * 1000.0)
|
|
|
|
))
|
2021-10-23 14:36:49 +02:00
|
|
|
url = '%s/ping/%s' % (HC_ROOT, check_item['uuid'])
|
|
|
|
ping_with_info(url, fail=fail, data='\n'.join(data))
|