Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to retrieve the most recent activity for all users in your PagerDuty account
#!/usr/bin/env python
#
# Copyright (c) 2016, PagerDuty, Inc. <info@pagerduty.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of PagerDuty Inc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Script to retrieve the most recent activity for all users in your account
# CLI Usage: ./get_user_activity api_key
# api_key: Your PagerDuty API access token
import datetime
import requests
import sys
base_url = 'https://api.pagerduty.com'
def get_user_count(headers):
url = base_url + '/users'
r = requests.get(url, headers=headers)
return len(r.json()['users'])
def get_users(headers, offset):
url = base_url + '/users'
payload = {
'offset': offset
}
r = requests.get(url, headers=headers, params=payload)
print "Listing all recent user activity:"
for user in r.json()['users']:
print 'Name: ' + user['name']
print 'ID: ' + user['id']
get_log_entries(headers, user['id'])
def get_log_entries(headers, user_id):
url = base_url + '/users/' + user_id + '/log_entries'
r = requests.get(url, headers=headers)
if len(r.json()['log_entries']) > 0:
log_entry = r.json()['log_entries'][0]
print 'Log entry created at: ' + log_entry['created_at']
print 'Log entry summary: ' + log_entry['summary']
else:
print 'This user has not yet had any activity'
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Error: You did not enter any parameters\nUsage: ./get_user_activity api_key\n\tapi_key: Your PagerDuty API access token'
else:
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token=' + sys.argv[1],
'Content-type': 'application/json'
}
user_count = get_user_count(headers)
for offset in xrange(0, user_count):
if offset % 25 == 0:
get_users(headers, offset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.