8 from email.utils import parsedate
9 logging.basicConfig(level=logging.INFO)
11 HOSTS = os.environ['MIRROR_CHECK_HOSTS'].split()
12 OUTPUT_DIR = "/run/dsa-mirror-health-{}".format(os.environ['MIRROR_CHECK_SERVICE'])
13 HEALTH_FILE = os.path.join(OUTPUT_DIR, "health")
14 URL = os.environ['MIRROR_CHECK_URL']
15 INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60'))
19 def retrieve_from_host(host, url):
21 'http': 'http://{}:80'.format(host),
22 'https': 'http://{}:443'.format(host),
24 return requests.get(url, timeout=5, proxies=proxies, allow_redirects=False)
26 def last_modified(response):
28 if response.status_code == 200 and response.headers.get('last-modified'):
29 lm = calendar.timegm(parsedate(response.headers['last-modified']))
36 lm = last_modified(retrieve_from_host(host, URL))
37 logging.debug("lm for host %s: %s", host, lm)
40 except requests.exceptions.ProxyError:
42 local_lm = last_modified(retrieve_from_host('localhost', URL))
43 logging.debug("lm for localhost: %s", local_lm)
44 if local_lm < latest_ts:
46 logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
47 os.remove(HEALTH_FILE)
51 logging.info("considering myself healthy")
52 open(HEALTH_FILE, 'w').write("OK")
53 sleep_time = start + INTERVAL - time.time()
54 logging.debug("sleeping for %d seconds", sleep_time)
55 time.sleep(sleep_time)