/
scrape.py
74 lines (58 loc) · 1.99 KB
/
scrape.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
#!/usr/bin/env python
# coding: utf-8
import csv
import datetime
import os
import sys
import logging
import pytz
import requests
subdomain = os.getenv("PAGERDUTY_SUBDOMAIN")
user = os.getenv("PAGERDUTY_USER")
password = os.getenv("PAGERDUTY_PASS")
auth = (user, password)
timezone = pytz.timezone(os.getenv("TZ", "UTC"))
log = logging.getLogger()
def main():
log.addHandler(logging.StreamHandler())
log.level = logging.DEBUG
API = "https://%s.pagerduty.com/api/v1" % subdomain
since, until = sys.argv[1:3]
emails = sys.argv[3:]
out = csv.DictWriter(sys.stdout, (
"timestamp", "timezone", "utc_timestamp", "hour", "week",
"month", "day_of_week", "type", "email"),
extrasaction="ignore")
out.writeheader()
for alert in alerts(since, until):
alert.update(alert["user"])
utc_dt = strptime(alert["started_at"])
dt = timezone.localize(utc_dt)
alert["timezone"] = timezone.zone
alert["utc_timestamp"] = utc_dt.strftime("%s")
alert["timestamp"] = dt.strftime("%s")
alert["day_of_week"] = dt.strftime("%w")
alert["week"] = dt.strftime("%U")
alert["month"] = dt.strftime("%m")
alert["hour"] = dt.strftime("%H")
if not emails or alert["email"] in emails:
out.writerow(alert)
def alerts(since, until):
global auth
params={"since": since, "until": until, "offset": 0}
data = True
while data:
result = requests.get(API + "/alerts", auth=auth, params=params)
data = result.json().get("alerts", [])
for alert in data:
yield alert
params["offset"] += len(data)
def strptime(string):
"""2012-06-01T05:10:54-07:00"""
stamp, offset = string.rsplit("-", 1)
hours, minutes = [int(x) for x in offset.split(":")]
dt = datetime.datetime.strptime(stamp, "%Y-%m-%dT%H:%M:%S")
dt += datetime.timedelta(hours=hours, minutes=minutes)
return dt
if __name__ == "__main__":
main()