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']))
35 lm = last_modified(retrieve_from_host(host, URL))
36 logging.debug("lm for host %s: %s", host, lm)
39 local_lm = last_modified(retrieve_from_host('localhost', URL))
40 logging.debug("lm for localhost: %s", lm)
41 if latest_tz <= local_lm:
43 logging.debug("considering myself unhealthy")
44 os.remove(HEALTH_FILE)
48 logging.debug("considering myself healthy")
49 open(HEALTH_FILE, 'w').write("OK")
50 sleep_time = start + INTERVAL - time.time()
51 logging.debug("sleeping for %d seconds", sleep_time)
52 time.sleep(sleep_time)