-
Notifications
You must be signed in to change notification settings - Fork 7
/
pagerduty.py
148 lines (117 loc) · 5.05 KB
/
pagerduty.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
#!/usr/bin/env python
import datetime
import calendar
import getpass
import json
import os
import requests
import subprocess
import shlex
import sys
import time
import ConfigParser
api_token = None
authenticated = False
time_format = '%Y-%m-%dT%H:%M:%SZ'
class TokenAuth(requests.auth.AuthBase):
"""Attaches PagerDuty Token Authentication to the given Request object."""
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers['Authorization'] = "Token token=%s" % self.token
return r
def get_authentication():
global domain
global api_token
global primary_schedule
global shift_start_hour
global authenticated
if authenticated:
return
configfile = os.path.join(os.path.expanduser('~'), '.pagerduty.cfg')
if not os.path.exists(configfile):
sys.stderr.write('Move pagerduty.cfg to ~/.pagerduty.cfg to begin.\n')
sys.exit(1)
else:
config = ConfigParser.RawConfigParser()
config.read(configfile)
domain = config.get('PagerDuty', 'domain') if config.has_option('PagerDuty', 'domain') else raw_input('PagerDuty Domain: ')
api_token = config.get('PagerDuty', 'api_token') if config.has_option('PagerDuty', 'api_token') else raw_input('PagerDuty API Token: ')
primary_schedule = config.get('PagerDuty', 'primary_schedule') if config.has_option('PagerDuty', 'primary_schedule') else raw_input('PagerDuty Schedule ID: ')
shift_start_hour = int(config.get('PagerDuty', 'shift_start_hour')) * -1 if config.has_option('PagerDuty', 'shift_start_hour') else int(raw_input('Schedule Start Hour: ')) * -1
authenticated = 'In Progress'
if "<error>" in get_schedule(primary_schedule):
sys.stdout.write('Authentication with "%s" failed. Please try again...' % (admin_email))
sys.exit(1)
# print 'Successfully authenticated to PagerDuty!'
authenticated = True
def get_schedule(schedule_id=False, time_period=False, offset_days=False):
get_authentication()
if not schedule_id:
schedule_id = primary_schedule
offset = datetime.timedelta(days=0)
if offset_days:
offset = datetime.timedelta(days=offset_days)
now = datetime.datetime.today() + offset + datetime.timedelta(hours=shift_start_hour)
start_date = now.strftime('%Y-%m-%d')
if time_period == 'day':
end_date = (now + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
elif time_period == 'week':
end_date = (now + datetime.timedelta(days=7)).strftime('%Y-%m-%d')
else:
end_date = (now + datetime.timedelta(days=92)).strftime('%Y-%m-%d')
return requests.get('https://%s.pagerduty.com/api/v1/schedules/%s/entries' %
(domain, schedule_id),
auth=TokenAuth(api_token),
params={'since': start_date, 'until': end_date}).json
def get_user_schedule(schedule_id=False, needle_name=False, schedule=False):
if not schedule:
schedule = get_schedule(schedule_id)
result = {}
for entry in schedule['entries'][1:]:
agent_name = entry['user']['name']
agent_email = entry['user']['email']
shift_start = entry['start']
if not needle_name or needle_name.lower() in agent_name.lower():
start_stamp = calendar.timegm(time.strptime(shift_start, time_format))
start_date = time.localtime(start_stamp)
shift_start = time.strftime('%m.%d.%Y - %A (%I%p %Z)', start_date)
result[start_date] = {
'agent_name': agent_name,
'agent_email': agent_email,
'shift_start': shift_start
}
return result
def get_daily_schedule(schedule_id=False):
schedule = get_schedule(schedule_id, time_period='day')
return get_user_schedule(schedule_id, schedule=schedule)
def get_tomorrows_schedule(schedule_id=False):
schedule = get_schedule(schedule_id, time_period='day', offset_days=1)
return get_user_schedule(schedule_id, schedule=schedule)
def get_weekly_schedule(schedule_id=False):
schedule = get_schedule(schedule_id, time_period='week')
return get_user_schedule(schedule_id, schedule=schedule)
def get_open_incidents(just_count=False):
get_authentication()
count_parameter = ''
if just_count:
count_parameter = '/count'
return requests.get('https://%s.pagerduty.com/api/v1/incidents%s' %
(domain, count_parameter),
auth=TokenAuth(api_token),
params={'status': 'triggered,acknowledged'}).json
if __name__ == "__main__":
print 'Running basic tests:'
print get_user_schedule()
print '====================='
print get_user_schedule(needle_name='joaquin')
print '====================='
print get_daily_schedule()
print '====================='
print get_tomorrows_schedule()
print '====================='
print get_weekly_schedule()
print '====================='
print get_open_incidents()
print '====================='
print get_open_incidents(just_count=True)