Skip to content

Instantly share code, notes, and snippets.

@jmdawson
Last active May 3, 2018 00:38
Show Gist options
  • Save jmdawson/ededf9a8a6be88decf77c2f367b752fc to your computer and use it in GitHub Desktop.
Save jmdawson/ededf9a8a6be88decf77c2f367b752fc to your computer and use it in GitHub Desktop.
A quick script to get an idea of what users in PagerDuty have interacted with incidents. In this case, over the last 180 days.
import requests,configparser,json
from dateutil.relativedelta import *
from dateutil.parser import *
from datetime import date,timedelta
def getToken(filename):
config = configparser.ConfigParser()
config.read(filename)
return config['default']['token']
def getUsers(token,headers):
params = {"limit" : 100}
url = 'https://api.pagerduty.com/users'
r = requests.get(url,headers=headers,params=params)
jsonResponse = json.loads(r.text)
return jsonResponse["users"]
def getIncidents(token,headers,startDate,endDate,incidentDates):
url = "https://api.pagerduty.com/incidents"
incidents =[]
more = True
offset = 0
limit = 100
#Like a do-while, only serpentine
while(more):
params={"limit" : limit, "since":"{}".format(startDate), "until":"{}".format(endDate), "offset": offset}
r = requests.get(url,headers=headers,params=params)
if r.status_code is 200:
jsonResponse = json.loads(r.text)
batchIncidents = jsonResponse["incidents"]
for incident in batchIncidents:
createdDate = parse(incident["created_at"]).date()
incidentDates.add(createdDate)
incidents.extend(batchIncidents)
limit = jsonResponse["limit"]
offset = jsonResponse["offset"] + limit
more = jsonResponse["more"]
else:
print("Error: {}".format( r.status_code))
print(r.text)
print("Url: {}, params: {}".format(url,params))
return incidents
return incidents
def findIdInUsers(users,userid,unknowns):
for user in users:
if user["id"] == userid:
return {"id":user["id"],"name":user["name"],"email":user["email"]}
unknowns.add(userid)
def main():
endDate = date.today()
# startDate = endDate - relativedelta(years=+1)
startDate = endDate - relativedelta(days=+180)
token = getToken("pdCreds.ini")
headers = {'Authorization' : "Token token="+token}
usersArray = getUsers(token,headers)
incidentDates = set()
incidentsArray = getIncidents(token,headers,startDate,endDate,incidentDates)
print("Total Users: {}".format(len(usersArray)))
print("Number of incidents since start date({}): {}\n".format(startDate,len(incidentsArray)))
incidentChangerIds = set()
statusChangeField = "last_status_change_by"
for incident in incidentsArray:
incidentChanger=incident[statusChangeField]["id"]
incidentChangerIds.add(incidentChanger)
print("Number of users listed in {} field: {}".format(len(statusChangeField,incidentChangerIds)))
changerUsers = []
unknownUsers = set()
for changer in incidentChangerIds:
changerUsers.append(findIdInUsers(usersArray,changer,unknownUsers))
for user in unknownUsers:
print ("Unknown user: " + user)
print("\nActive Users found: {}".format(len(changerUsers)))
for user in changerUsers:
if user is not None:
print("{}, {}, {}".format(user["id"],user["name"],user["email"]))
unknownUserIncidents = []
for incident in incidentsArray:
if incident["last_status_change_by"]["id"] in unknownUsers:
unknownUserIncidents.append(incident)
print("\n{} Incidents with last status change by unknown user:".format(len(unknownUserIncidents)))
for incident in unknownUserIncidents:
print("Incident #{} at {}, last changed by user #{} ({})".format(incident["id"],incident["created_at"],incident["last_status_change_by"]["id"],incident["summary"]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment