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 HEALTH_CHECK_URL = os.environ['MIRROR_CHECK_HEALTH_URL']
17 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']))
32 def healthy(response):
33 if response.status_code == 200:
38 if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
39 logging.info("considering myself unhealthy, shutdown scheduled")
47 lm = last_modified(retrieve_from_host(host, URL))
48 logging.debug("lm for host %s: %s", host, lm)
49 if healthy(retrieve_from_host(host, HEALTH_CHECK_URL)):
50 latest_ts = max(latest_ts, lm)
51 except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
54 local_lm = last_modified(retrieve_from_host('localhost', URL))
55 except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
57 logging.debug("lm for localhost: %s", local_lm)
58 if local_lm < latest_ts:
59 logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
65 if check_shutdown() and check_uptodate():
66 logging.info("considering myself healthy")
67 open(HEALTH_FILE, 'w').write("OK")
70 os.remove(HEALTH_FILE)
73 sleep_time = start + INTERVAL - time.time()
74 logging.debug("sleeping for %d seconds", sleep_time)
75 time.sleep(sleep_time)