c3606de0018c6930bb1705c6599c30c332d5795e
[mirror/dsa-puppet.git] / modules / roles / files / mirror_health / mirror-health
1 #! /usr/bin/python3
2
3 import os
4 import requests
5 import time
6 import calendar
7 import logging
8 import subprocess
9 from email.utils import parsedate
10 logging.basicConfig(level=logging.INFO)
11
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'))
18
19 def retrieve_from_host(host, url):
20     proxies = {
21         'http': 'http://{}:80'.format(host),
22         'https': 'http://{}:443'.format(host),
23     }
24     return requests.get(url, timeout=5, proxies=proxies, allow_redirects=False)
25
26 def last_modified(response):
27     lm = 0
28     if response.status_code == 200 and response.headers.get('last-modified'):
29         lm = calendar.timegm(parsedate(response.headers['last-modified']))
30     return lm
31
32 def healthy(response):
33     if response.status_code == 200:
34         return True
35     return False
36
37 def check_shutdown():
38     if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
39         logging.info("considering myself unhealthy, shutdown scheduled")
40         return False
41     return True
42
43 def check_uptodate():
44     latest_ts = 0
45     for host in HOSTS:
46         try:
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):
52             pass
53     try:
54         local_lm = last_modified(retrieve_from_host('localhost', URL))
55     except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout):
56         return False
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)
60         return False
61     return True
62
63 while True:
64     start = time.time()
65     if check_shutdown() and check_uptodate():
66         logging.info("considering myself healthy")
67         open(HEALTH_FILE, 'w').write("OK")
68     else:
69         try:
70             os.remove(HEALTH_FILE)
71         except OSError:
72             pass
73     sleep_time = start + INTERVAL - time.time()
74     logging.debug("sleeping for %d seconds", sleep_time)
75     time.sleep(sleep_time)