-
Notifications
You must be signed in to change notification settings - Fork 71
/
healthcheck
executable file
·173 lines (141 loc) · 5.74 KB
/
healthcheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""Write Prometheus metrics to a textfile for a mirror.
This detects most problems caused by both failed syncs to our direct upstream
as well as cases where the upstream is out-of-date.
Ideally, the upstream url argument should refer to the *actual* upstream (e.g. Debian's
authoritative archive) and not just our upstream mirror (e.g.
mirrors.kernel.org).
"""
import argparse
import sys
import time
from collections import namedtuple
from datetime import datetime
import dateutil.parser
import requests
from prometheus_client import CollectorRegistry
from prometheus_client import Gauge
from prometheus_client import write_to_textfile
Mirror = namedtuple('Mirror', ['url', 'updated_at'])
def update_func(healthcheck):
if healthcheck == 'debian':
return get_updated_debian
elif healthcheck == 'manjaro':
return get_updated_manjaro
elif healthcheck == 'unix_timestamp':
return get_updated_unix_timestamp
elif healthcheck == 'datetime':
return get_updated_datetime
elif healthcheck == 'recursive_ls':
return get_updated_recursive_ls
elif healthcheck == 'http_last_modified':
return get_updated_http_last_modified
else:
raise ValueError('Unsupported type: {}'.format(healthcheck))
def get_updated_datetime(mirror_url):
"""Find the time the host was last synced.
The first line on the page should be a datetime, e.g. Fri Jan 19 20:15:01 UTC 2018.
>>> get_updated_datetime(mirror_url)
datetime.datetime(2018, 1, 19, 20, 15, 1, tzinfo=tzutc())
"""
req = requests.get(mirror_url)
req.raise_for_status()
return dateutil.parser.parse(req.text.splitlines()[0], fuzzy=True)
def get_updated_unix_timestamp(mirror_url):
"""Find the time the host was last synced.
The first line on the page should be a unix timestamp, e.g. 1516392227.
>>> get_updated_unix_timestamp(mirror_url)
datetime.datetime(2018, 1, 19, 20, 3, 47)
"""
req = requests.get(mirror_url)
req.raise_for_status()
return datetime.utcfromtimestamp(int(req.text.split()[0]))
def get_updated_recursive_ls(mirror_url):
"""Find the time the host was last synced.
Should contain the output of ls -lR
>>> get_updated_recursive_ls(mirror_url)
datetime.datetime(2018, 1, 24, 10, 3)
"""
req = requests.get(mirror_url)
req.raise_for_status()
# Ex: drwxr-xr-x 3 ftpadmin packager 3 2009-09-21 14:51 3.5.6
# Splits on date and time, giving us "2009-09-21 14:51"
dates = [' '.join(line.split()[5:7]) for line in req.text.splitlines()
if len(line.split()) == 8]
return dateutil.parser.parse(max(dates))
def get_updated_http_last_modified(mirror_url):
"""Find the time the host was last synced.
The header content should be of the form "Sat, 30 Apr 2022 19:16:24 GMT".
>>> get_updated_http_last_modified(mirror_url)
datetime.datetime(2022, 4, 30, 19, 16, 24, tzinfo=tzutc())
"""
req = requests.head(mirror_url)
req.raise_for_status()
return dateutil.parser.parse(req.headers['last-modified'])
def get_updated_debian(mirror_url):
"""Find the time the host was last synced.
The page of should have a line of the form "Date: Fri, 19 Jan 2018 19:26:41 UTC"
>>> get_updated_debian(mirror_url)
datetime.datetime(2018, 1, 19, 19, 26, 41, tzinfo=tzutc())
"""
req = requests.get(mirror_url)
req.raise_for_status()
updated_line, = [line for line in req.text.splitlines() if line.startswith('Date: ')]
return dateutil.parser.parse(updated_line.split(': ', 1)[1])
def get_updated_manjaro(mirror_url):
"""Find the time the host was last synced.
Line in question should be of the form "date=2018-01-25T21:52:18Z"
>>> get_updated_manjaro(mirror_url)
datetime.datetime(2018, 1, 25, 21, 52, 18, tzinfo=tzutc())
"""
req = requests.get(mirror_url)
req.raise_for_status()
updated_line, = [line for line in req.text.splitlines() if line.startswith('date=')]
return dateutil.parser.parse(updated_line.split('=', 1)[1])
def write_prometheus(project, local, upstream):
registry = CollectorRegistry()
updated_upstream = Gauge(
'mirror_updated_upstream',
'When the upstream mirror was last updated',
['project'],
registry=registry,
)
updated_upstream.labels(project=project).set(upstream.timestamp())
updated_local = Gauge(
'mirror_updated_local',
'When the local mirror was last updated',
['project'],
registry=registry,
)
updated_local.labels(project=project).set(local.timestamp())
last_run = Gauge(
'mirror_healthcheck_last_run',
'When the healthcheck last successfully ran',
['project'],
registry=registry,
)
last_run.labels(project=project).set(time.time())
write_to_textfile(
'/srv/prometheus/{}.prom'.format(project),
registry,
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('project', type=str, help='Project title')
parser.add_argument('url_local', type=str, help='URL of local mirror')
parser.add_argument('url_upstream', type=str, help='URL of upstream mirror')
parser.add_argument('-t', '--type', type=str, default='debian')
# ensure we aren't comparing a mirror against itself
args = parser.parse_args()
if args.url_local == args.url_upstream:
print('Local and upstream urls cannot be equal... Exiting!')
sys.exit(1)
get_updated = update_func(args.type)
local_mirror = Mirror(args.url_local, get_updated(args.url_local))
upstream_mirror = Mirror(args.url_upstream, get_updated(args.url_upstream))
write_prometheus(
args.project,
local_mirror.updated_at,
upstream_mirror.updated_at,
)
# vim: ft=python