docker_images/uptime/check/check.py

38 lines
1009 B
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
return r.status_code
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']:
2021-05-13 16:32:05 +02:00
status = check_domain(domain)
if not fail and status != 200:
fail = True
data.append('%s : %s' % (domain, status))
url = '%s/ping/%s' % (HC_ROOT, check_item['uuid'])
ping_with_info(url, fail=fail, data='\n'.join(data))