9 from email.utils import parsedate
10 logging.basicConfig(level=logging.INFO)
12 HOSTS = os.environ['MIRROR_CHECK_HOSTS'].split()
13 OUTPUT_DIR = "/run/dsa-mirror-health-{}".format(os.environ['MIRROR_CHECK_SERVICE'])
14 HEALTH_FILE = os.path.join(OUTPUT_DIR, "health")
15 URL = os.environ['MIRROR_CHECK_URL']
16 INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60'))
20 def retrieve_from_host(host, url):
22 'http': 'http://{}:80'.format(host),
23 'https': 'http://{}:443'.format(host),
25 return requests.get(url, timeout=5, proxies=proxies, allow_redirects=False)
27 def last_modified(response):
29 if response.status_code == 200 and response.headers.get('last-modified'):
30 lm = calendar.timegm(parsedate(response.headers['last-modified']))
34 if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
35 logging.info("considering myself unhealthy, shutdown scheduled")
43 lm = last_modified(retrieve_from_host(host, URL))
44 logging.debug("lm for host %s: %s", host, lm)
47 except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout):
49 local_lm = last_modified(retrieve_from_host('localhost', URL))
50 logging.debug("lm for localhost: %s", local_lm)
51 if local_lm < latest_ts:
52 logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
58 if check_shutdown() and check_uptodate():
59 logging.info("considering myself healthy")
60 open(HEALTH_FILE, 'w').write("OK")
63 os.remove(HEALTH_FILE)
66 sleep_time = start + INTERVAL - time.time()
67 logging.debug("sleeping for %d seconds", sleep_time)
68 time.sleep(sleep_time)