X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=modules%2Froles%2Ffiles%2Fmirror_health%2Fmirror-health;h=bbd4c2787768291ecc37ed7b748c309e3dc18d02;hb=0c067639ec7d7050b69249b85e6aa83ab91820b5;hp=49808b316f9599af5159df4e30e12b3bee28479d;hpb=819869cfb2d6c09600d0ac2aa51ace34669af685;p=mirror%2Fdsa-puppet.git diff --git a/modules/roles/files/mirror_health/mirror-health b/modules/roles/files/mirror_health/mirror-health index 49808b316..bbd4c2787 100755 --- a/modules/roles/files/mirror_health/mirror-health +++ b/modules/roles/files/mirror_health/mirror-health @@ -13,16 +13,16 @@ HOSTS = os.environ['MIRROR_CHECK_HOSTS'].split() OUTPUT_DIR = "/run/dsa-mirror-health-{}".format(os.environ['MIRROR_CHECK_SERVICE']) HEALTH_FILE = os.path.join(OUTPUT_DIR, "health") URL = os.environ['MIRROR_CHECK_URL'] +HEALTH_CHECK_URL = os.environ['MIRROR_CHECK_HEALTH_URL'] INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60')) -latest_ts = 0 - def retrieve_from_host(host, url): proxies = { 'http': 'http://{}:80'.format(host), 'https': 'http://{}:443'.format(host), } - return requests.get(url, timeout=5, proxies=proxies, allow_redirects=False) + headers = {'User-Agent': 'mirror-health'} + return requests.get(url, headers=headers, timeout=5, proxies=proxies, allow_redirects=False) def last_modified(response): lm = 0 @@ -30,6 +30,11 @@ def last_modified(response): lm = calendar.timegm(parsedate(response.headers['last-modified'])) return lm +def healthy(response): + if response.status_code == 200: + return True + return False + def check_shutdown(): if subprocess.call(['dsa-is-shutdown-scheduled']) == 0: logging.info("considering myself unhealthy, shutdown scheduled") @@ -37,16 +42,19 @@ def check_shutdown(): return True def check_uptodate(): - global latest_ts + latest_ts = 0 for host in HOSTS: try: lm = last_modified(retrieve_from_host(host, URL)) logging.debug("lm for host %s: %s", host, lm) - if lm > latest_ts: - latest_ts = lm - except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout): + if healthy(retrieve_from_host(host, HEALTH_CHECK_URL)): + latest_ts = max(latest_ts, lm) + except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError): pass - local_lm = last_modified(retrieve_from_host('localhost', URL)) + try: + local_lm = last_modified(retrieve_from_host('localhost', URL)) + except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError): + return False logging.debug("lm for localhost: %s", local_lm) if local_lm < latest_ts: logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)