bbd4c2787768291ecc37ed7b748c309e3dc18d02
[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     headers = {'User-Agent': 'mirror-health'}
25     return requests.get(url, headers=headers, timeout=5, proxies=proxies, allow_redirects=False)
26
27 def last_modified(response):
28     lm = 0
29     if response.status_code == 200 and response.headers.get('last-modified'):
30         lm = calendar.timegm(parsedate(response.headers['last-modified']))
31     return lm
32
33 def healthy(response):
34     if response.status_code == 200:
35         return True
36     return False
37
38 def check_shutdown():
39     if subprocess.call(['dsa-is-shutdown-scheduled']) == 0:
40         logging.info("considering myself unhealthy, shutdown scheduled")
41         return False
42     return True
43
44 def check_uptodate():
45     latest_ts = 0
46     for host in HOSTS:
47         try:
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):
53             pass
54     try:
55         local_lm = last_modified(retrieve_from_host('localhost', URL))
56     except (requests.exceptions.ProxyError, requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
57         return False
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)
61         return False
62     return True
63
64 while True:
65     start = time.time()
66     if check_shutdown() and check_uptodate():
67         logging.info("considering myself healthy")
68         open(HEALTH_FILE, 'w').write("OK")
69     else:
70         try:
71             os.remove(HEALTH_FILE)
72         except OSError:
73             pass
74     sleep_time = start + INTERVAL - time.time()
75     logging.debug("sleeping for %d seconds", sleep_time)
76     time.sleep(sleep_time)