# -*- coding: utf-8 -*- """ Uptime check, at the moment HTTPS only domains Usage of own healthchecks.io instance and ENV """ from os import environ from requests import get, post, RequestException 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) except RequestException as e: 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))