-
Notifications
You must be signed in to change notification settings - Fork 24
/
pull_alerts.py
237 lines (203 loc) · 8.15 KB
/
pull_alerts.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
from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals
import argparse
import logging
import urllib
from collections import Counter, OrderedDict, defaultdict, namedtuple
from datetime import datetime, timedelta
from dateutil import tz
import dateutil.parser
import pygerduty.v2
from prettytable import PrettyTable
try:
import settings
except ImportError:
print("*** Error: Follow setup instructions in README.md to create settings.py")
raise SystemExit(1)
logger = logging.getLogger(__name__)
pagerduty_service = pygerduty.v2.PagerDuty(settings.PAGERDUTY_API_TOKEN)
LOCAL_TZ = tz.tzlocal()
Tag = namedtuple("Tag", ["tag", "display_name"])
TAGS = [
Tag(tag="#a", display_name="Actionable (#a)"),
Tag(tag="#na", display_name="Non Actionable (#na)"),
Tag(tag="#t", display_name="Transient (#t)"),
Tag(tag="#s", display_name="Seasonal (#s)"),
Tag(tag="#abot", display_name="Actionable By Other Team (#abot)"),
]
class FormattedIncident(object):
def pretty_output(self):
return u'Time: {}\nService: {}\nDescription: {}\nURL: {}\nNotes:\n{}\n'.format(
self.created_on.strftime('%A, %B %-d - %-I:%M %p %z'),
self.service,
self.description,
self.url,
self.notes,
)
def recent_incidents_for_services(services, time_window):
service_ids = [service.id for service in services]
try:
recent_incidents = list(pagerduty_service.incidents.list(
service_ids=service_ids,
since=datetime.now(tz=LOCAL_TZ) - time_window
))
return recent_incidents
except urllib.error.HTTPError as e:
if e.reason == 'URI Too Long':
mid_point = int(len(services)/2)
return recent_incidents_for_services(
services[:mid_point],
time_window,
) + recent_incidents_for_services(
services[mid_point:],
time_window,
)
raise
def print_all_incidents(
silent,
time_window_days,
group_by_description=False,
group_by_service=False,
include_stats=False,
include_incidents_as_blockquote=False,
):
services = []
for escalation_policy in settings.ESCALATION_POLICIES:
services.extend(list(pagerduty_service.escalation_policies.show(escalation_policy).services))
recent_incidents = recent_incidents_for_services(services, timedelta(days=time_window_days))
formatted_incidents = get_formatted_incidents(recent_incidents)
all_incidents, sorted_description_to_incident_list, sorted_service_to_incident_list = sort_incidents(
formatted_incidents,
group_by_description,
group_by_service
)
print_stats(all_incidents, include_stats)
if include_incidents_as_blockquote:
print("""# Raw incident log
```
""")
if group_by_service:
sorted_group_to_incident_list = sorted_service_to_incident_list
elif group_by_description:
sorted_group_to_incident_list = sorted_description_to_incident_list
if group_by_service or group_by_description:
for group, incident_list in sorted_group_to_incident_list.items():
print("########### {}: {} ##########\n".format(len(incident_list), group))
if not silent:
for incident in incident_list:
print(incident.pretty_output())
else:
for incident in all_incidents:
print(incident.pretty_output())
print('Total Pages: {}'.format(len(all_incidents)))
if include_incidents_as_blockquote:
print("```")
def get_formatted_incidents(recent_incidents):
formatted_incidents = []
for incident in recent_incidents:
formatted_incident = FormattedIncident()
formatted_incident.service = incident.service.summary
formatted_incident.url = incident.html_url
if hasattr(incident, 'title'):
formatted_incident.description = incident.title
elif hasattr(incident, 'summary'):
formatted_incident.description = incident.summary
elif hasattr(incident, 'id'):
formatted_incident.description = incident.id
else:
logger.warning('action=get_description status=not_found incident={}'.format(incident))
formatted_incident.created_on = dateutil.parser.parse(incident.created_at).astimezone(LOCAL_TZ)
notes = list(incident.notes.list())
formatted_notes = []
for note in notes:
formatted_notes.append(u'{}: {}'.format(note.user.summary, note.content))
formatted_incident.notes = formatted_notes
formatted_incidents.append(formatted_incident)
return formatted_incidents
def _tag_incident(incident, tag_stats):
tagged = False
for tag in TAGS:
found_tag = any(tag.tag in note for note in incident.notes)
if not found_tag:
continue
tagged = True
tag_stats[tag] += 1
return tagged
def print_stats(all_incidents, include_stats):
if not include_stats:
return
stats_table = PrettyTable()
stats_table.field_names = ["Incidents", "Number"]
stats_table.align["Incidents"] = "l"
stats_table.align["Number"] = "r"
tag_stats = Counter()
not_tagged = 0
for i in all_incidents:
tagged = _tag_incident(i, tag_stats)
not_tagged += not tagged
for tag in TAGS:
stats_table.add_row([tag.display_name, tag_stats[tag]])
stats_table.add_row(["Not Tagged", not_tagged])
stats_table.add_row(["Total", len(all_incidents)])
print(stats_table)
def sort_incidents(all_incidents, group_by_description, group_by_service):
description_to_incident_list = defaultdict(list)
service_to_incident_list = defaultdict(list)
for incident in all_incidents:
description_to_incident_list[incident.description].append(incident)
for incident in all_incidents:
service_to_incident_list[incident.service].append(incident)
# Sort by desc count
sorted_description_to_incident_list = OrderedDict(sorted(
description_to_incident_list.items(),
key=lambda x: len(x[1]),
reverse=True
))
sorted_service_to_incident_list = OrderedDict(sorted(
service_to_incident_list.items(),
key=lambda x: len(x[1]),
reverse=True
))
if group_by_description:
all_incidents = []
for incident_list in sorted_description_to_incident_list.values():
all_incidents += incident_list
else:
all_incidents = sorted(all_incidents, key=lambda i: i.created_on)
return all_incidents, sorted_description_to_incident_list, sorted_service_to_incident_list
if __name__ == '__main__':
logging.basicConfig()
parser = argparse.ArgumentParser()
parser.add_argument("--silent",
action="store_true",
default=False,
help="Do not print each description")
parser.add_argument("--group-by-description",
action="store_true",
default=False,
help="Group PD incidents by description")
parser.add_argument("--group-by-service",
action="store_true",
default=False,
help="Group PD incidents by service")
parser.add_argument("--include-stats",
action="store_true",
default=False,
help="Include incidents stats")
parser.add_argument("--include-incidents-as-blockquote",
action="store_true",
default=False,
help="Include raw incident log as markdown blockquote")
parser.add_argument('--days',
type=int,
default=7,
help='time window days')
args = parser.parse_args()
print_all_incidents(
silent=args.silent,
group_by_description=args.group_by_description,
group_by_service=args.group_by_service,
include_stats=args.include_stats,
include_incidents_as_blockquote=args.include_incidents_as_blockquote,
time_window_days=args.days
)