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 headers = {'User-Agent': 'mirror-health'}
25 return requests.get(url, headers=headers, 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']))
33 def healthy(response):
34 if response.status_code == 200:
39 if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
40 logging.info("considering myself unhealthy, shutdown scheduled")
48 lm = last_modified(retrieve_from_host(host, URL))
49 logging.debug("lm for host %s: %s", host, lm)
50 if healthy(retrieve_from_host(host, HEALTH_CHECK_URL)):
51 latest_ts = max(latest_ts, lm)
52 except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
55 local_lm = last_modified(retrieve_from_host('localhost', URL))
56 except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
58 logging.debug("lm for localhost: %s", local_lm)
59 if local_lm < latest_ts:
60 logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts)
66 if check_shutdown() and check_uptodate():
67 logging.info("considering myself healthy")
68 open(HEALTH_FILE, 'w').write("OK")
71 os.remove(HEALTH_FILE)
74 sleep_time = start + INTERVAL - time.time()
75 logging.debug("sleeping for %d seconds", sleep_time)
76 time.sleep(sleep_time)