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 ENV
|
|
|
|
"""
|
|
|
|
|
|
|
|
from os import environ
|
2021-05-13 17:49:30 +02:00
|
|
|
from requests import get, post
|
2021-05-13 16:32:05 +02:00
|
|
|
|
|
|
|
hc_root = environ.get('HC_ROOT', '')
|
|
|
|
# HOSTS are in form hostname|uuid:hostname2|uuid...
|
|
|
|
hosts = environ.get('HOSTS', '').split(':')
|
|
|
|
# Domains are in form of example.org,example.net:hostname.org:another.net
|
|
|
|
domains = environ.get('DOMAINS', '').split(':')
|
|
|
|
|
|
|
|
config = {}
|
|
|
|
for host in zip(hosts, domains):
|
|
|
|
host_data = host[0].split('|')
|
|
|
|
config[host_data[0]] = {'url': '%s/ping/%s' % (hc_root, host_data[1]),
|
|
|
|
'domains': host[1].split(',')}
|
|
|
|
|
|
|
|
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)
|
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)
|
|
|
|
|
|
|
|
for host, settings in config.items():
|
|
|
|
print('Checking %s : %s' % (', '.join(settings['domains']), host))
|
|
|
|
fail = False
|
|
|
|
data = []
|
|
|
|
for domain in settings['domains']:
|
|
|
|
status = check_domain(domain)
|
|
|
|
if not fail and status != 200:
|
|
|
|
fail = True
|
|
|
|
data.append('%s : %s' % (domain, status))
|
|
|
|
ping_with_info(settings['url'], fail=fail, data='\n'.join(data))
|