-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_report.py
executable file
·348 lines (301 loc) · 11.9 KB
/
build_report.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "python-dotenv",
# "numpy",
# "pandas",
# "requests",
# "jinja2",
# "premailer",
# ]
# ///
# Usage: uv run build_report.py [OPTIONS]
import argparse
import datetime
import os
from dotenv import load_dotenv
import requests
import pandas as pd
from premailer import transform
load_dotenv()
PINGDOM_API_TOKEN = os.getenv("PINGDOM_API_TOKEN")
PINGDOM_API_URL = "https://api.pingdom.com/api/3.1"
PARENT_DIR = "reports/"
HEADERS = {"Authorization": f"Bearer {PINGDOM_API_TOKEN}", "Accept-Encoding": "gzip"}
def get_checks(tags):
payload = {"tags": tags}
r = requests.get(f"{PINGDOM_API_URL}/checks", headers=HEADERS, params=payload)
r.raise_for_status()
if not r.json()["checks"]:
print(f"No checks found using tags: {tags}")
exit()
return [
[check["id"], check["name"], check["hostname"], check["status"]]
for check in r.json()["checks"]
]
def downtime_outages(check_id, from_date, to_date):
"""Calculate the number of downtimes and downtime seconds given a pingdom check_id and date range
Args:
check_id (integer): pingdom check_id
from_date (float): UNIX timestamp, beginning of time period
to_date (float): UNIX timestamp, end of time period
Returns:
tuple: downtime (integer, minutes), outages (integer, count)
"""
payload = {"from": from_date, "to": to_date}
r = requests.get(
f"{PINGDOM_API_URL}/summary.outage/{check_id}", headers=HEADERS, params=payload
)
outages = 0
downtime = 0
for state in r.json()["summary"]["states"]:
if state["status"] == "down":
outages += 1
downtime += state["timeto"] - state["timefrom"]
return downtime, outages
def average_uptime(check_id, from_date, to_date):
"""Get response_time, and calculate uptime given a pingdom check_id and date range
Args:
check_id (integer): pingdom check_id
from_date (float): UNIX timestamp, beginning of time period
to_date (float): UNIX timestamp, end of time period
Returns:
tuple: response_time (integer, ms), uptime_percent (integer, percent)
"""
payload = {"from": from_date, "to": to_date, "includeuptime": "true"}
r = requests.get(
f"{PINGDOM_API_URL}/summary.average/{check_id}", headers=HEADERS, params=payload
)
response_time = r.json()["summary"]["responsetime"]["avgresponse"]
status = r.json()["summary"]["status"]
uptime_percent = 100 - (status["totaldown"] / sum(status.values()) * 100)
return response_time, uptime_percent
def get_time_hh_mm_ss(seconds):
mm, ss = divmod(seconds, 60)
hh, mm = divmod(mm, 60)
return f"{hh}h {mm}m {ss}s"
def generate_html_report(
checks, from_date, to_date, days, report_name, file_name, tags
):
df_checks = pd.DataFrame(checks)
df_checks.columns = [
"CHECK_ID",
"CHECK_NAME",
"CHECK NAME",
"HOSTNAME",
"STATUS",
"DOWNTIME_MINUTES",
"DOWNTIME",
"OUTAGES",
"RESPONSE TIME",
"UPTIME",
]
# Create overview data from checks data
overview_count = len(checks)
overview_uptime = df_checks["UPTIME"].mean()
overview_outages = df_checks["OUTAGES"].sum()
overview_response_times = df_checks["RESPONSE TIME"].mean()
overview_downtime = get_time_hh_mm_ss(df_checks["DOWNTIME_MINUTES"].sum())
df_overview = pd.DataFrame(
columns=["UPTIME", "OUTAGES", "DOWNTIME", "RESPONSE TIME", "NUMBER OF CHECKS"],
data=[
[
overview_uptime,
overview_outages,
overview_downtime,
overview_response_times,
overview_count,
]
],
)
# create dataframes for checks w/o downtime and add unit labels
df_checks_y_downtime = df_checks[df_checks.OUTAGES != 0].sort_values(
by="OUTAGES", ascending=False
)
df_checks_n_downtime = df_checks[df_checks.OUTAGES == 0].sort_values(
by="CHECK_NAME"
)
header_text = f"{days} Uptime Report"
page_title_text = header_text
header_date = f'{from_date.strftime("%Y-%m-%d")} to {to_date.strftime("%Y-%m-%d")}'
overview_text = f"OVERVIEW: {report_name} - {days}"
checks_y_downtime_text = "CHECKS WITH DOWNTIME"
checks_y_downtime_hidden_columns = [
"CHECK_ID",
"CHECK_NAME",
"STATUS",
"DOWNTIME_MINUTES",
]
checks_n_downtime_text = "CHECKS WITHOUT DOWNTIME"
check_n_downtime_hidden_columns = [
"CHECK_ID",
"CHECK_NAME",
"STATUS",
"DOWNTIME",
"DOWNTIME_MINUTES",
"OUTAGES",
]
pd.set_option("colheader_justify", "left")
html = f"""
<html>
<head>
<title>{page_title_text}</title>
<style>
table {{width:100%;border-collapse:collapse;margin-left:auto;margin-right:auto}}
th {{color:#fff;background-color:#222;border:1px solid #000;padding:5px 10px;font-size:12px;font-family:Arial,sans-serif;text-align:left}}
td {{color:#000;background-color:#fff;border:1px solid #ddd;padding:10px 10px;font-size:13px;font-family:Arial,sans-serif}}
h2 {{margin-bottom:10px;font-family:Arial,sans-serif;font-size:16px}}
h3 {{margin-bottom:10px;font-family:Arial,sans-serif;font-size:13px}}
</style>
</head>
<body>
<table style=width:100%;margin-left:auto;margin-right:auto;border:0px>
<tr>
<td>
<table style="width:100%;height:100%;margin:0px;padding:0px;border:0px;background:#222;border-collapse:collapse" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td style="border:0px;background:#000;padding-left:30px;padding-right:5px;padding-top:20px;padding-bottom:20px">
<img style="display:block" src="https://mozilla.design/files/2019/06/mozilla-logo-bw-rgb.png" width="150" border="0">
</td>
<td style="border:0px;background-color:#000;padding-top:20px;padding-bottom:20px" width="60%"></td>
<td style="border:0px;background:#000;color:white;font-size:12px;font-family:Arial,sans-serif;padding-top:20px;padding-bottom:20px">
<td style="border:0px;background:#000;color:white;font-size:14px;font-family:Arial,sans-serif;text-align:right;padding-right:30px">
<span style="margin-bottom:3px;color:#fff333;font-size:14px">{header_text}</span>
<br>
{header_date}
</td>
</td>
</tr>
</tbody>
</table>
</td>
<tr>
<td style="background:#f7f7f7;border-left:0px solid #ddd;border-right:0px solid #ddd;border-bottom:0px solid #ddd;padding:30px">
<h2>{overview_text}</h2>
{df_overview .style
.format({'UPTIME': "{:.3f}%", "RESPONSE TIME": "{:.0f} ms"})
.hide()
.to_html()}
</td>
</tr>
<tr>
<td style="background:#f7f7f7;border-left:0px solid #ddd;border-right:0px solid #ddd;border-bottom:0px solid #ddd;padding:30px">
<h3>{checks_y_downtime_text}</h2>
{df_checks_y_downtime.style
.format({'UPTIME': "{:.3f}%", "RESPONSE TIME": "{:.0f} ms"})
.bar(color="#00ff00", subset=["RESPONSE TIME"], align="mid")
.bar(color="#ff4500", subset=["OUTAGES"], align="mid")
.hide()
.hide(checks_y_downtime_hidden_columns, axis='columns')
.to_html()}
</td>
</tr>
<tr>
<td style="background:#f7f7f7;border-left:0px solid #ddd;border-right:0px solid #ddd;border-bottom:0px solid #ddd;padding:30px">
<h3>{checks_n_downtime_text}</h2>
{df_checks_n_downtime.style
.format({"UPTIME": "{:.0f}%","RESPONSE TIME": "{:.0f} ms"})
.bar(color="#00ff00", subset=["RESPONSE TIME"], align="mid")
.hide(check_n_downtime_hidden_columns, axis="columns")
.hide()
.to_html()}
</td>
</tr>
<tr>
<td style="background:#f7f7f7;border-left:0px solid #ddd;border-right:0px solid #ddd;border-bottom:0px solid #ddd;padding:3px">
<span style="float:left;font-size:8pt">Made with 💚 by sre: green; maintained by obs-team</span>
<span style="float:right;font-size:8pt">tags used to generate this report: {tags}</span>
</td>
</tr>
</table>
</html>
</body>
"""
try:
os.mkdir(PARENT_DIR)
except FileExistsError:
pass
fixed_html = transform(html, pretty_print=True)
with open(f"{PARENT_DIR}/{file_name}", "w") as f:
f.write(fixed_html)
def main(days, tags, report_name):
try:
from_date = datetime.datetime.now() - datetime.timedelta(days)
to_date = datetime.datetime.now()
days = f"{days} day"
file_name = f"{days}_pingdom_report.html"
except TypeError:
# produce monthly report
# last day of previous month
to_date = datetime.datetime.combine(
datetime.date.today().replace(day=1) - datetime.timedelta(days=1),
datetime.time(23, 59, 59),
)
# first day for previous month
from_date = datetime.datetime.combine(
datetime.date.today().replace(day=1) - datetime.timedelta(days=to_date.day),
datetime.time(0, 0, 0),
)
days = to_date.strftime("%B, %Y")
file_name = f'{to_date.strftime("%B")}_pingdom_report.html'
finally:
unix_from_date = from_date.timestamp()
unix_to_date = to_date.timestamp()
# collect check data for checks with specific tags
checks = get_checks(tags)
for check in checks:
link_text = f'<a href="https://my.pingdom.com/app/reports/uptime#check={check[0]}&daterange={days}days">{check[1]}</a>'
check.insert(2, link_text)
downtime_minutes, outages = downtime_outages(
check[0], unix_from_date, unix_to_date
)
response_time, uptime_percent = average_uptime(
check[0], unix_from_date, unix_to_date
)
downtime_formatted = get_time_hh_mm_ss(downtime_minutes)
check.extend(
(
downtime_minutes,
downtime_formatted,
outages,
response_time,
uptime_percent,
)
)
# 0 1 2 3 4 5 6 7 8 9
# checks [check_id, name, link_text, hostname, status, downtime_minutes, downtime, total_outages, response_time, uptime_percent]
generate_html_report(checks, from_date, to_date, days, report_name, file_name, tags)
# TODO: handle missing checks?
# TODO: print list of available tags
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="pingdom_report",
description="Create a custom pingdom report for the previous month from tagged checks",
)
parser.add_argument(
"-d",
"--days",
metavar="DAYS",
action="store",
type=int,
help="Specify previous n days to cover instead of previous month",
)
parser.add_argument(
"-t",
"--tags",
metavar="TAGS",
action="store",
default="bug_bounty_site",
help="comma separated list of tags",
)
parser.add_argument(
"-r",
"--report-name",
metavar="REPORT_NAME",
action="store",
default="Critical Sites Monthly Uptime",
)
args = parser.parse_args()
main(args.days, args.tags, args.report_name)