Skip to content

Instantly share code, notes, and snippets.

@lfepp
Created May 26, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfepp/19cdc3ca469b4d353308c84a32853fe4 to your computer and use it in GitHub Desktop.
Save lfepp/19cdc3ca469b4d353308c84a32853fe4 to your computer and use it in GitHub Desktop.
Sample shell script to pull PagerDuty incidents that were triggerd within the given time period and are in currently queue
#!/bin/sh
#
# 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.
#
# Sample shell script to pull incidents that were triggerd within the given time period and are in currently queue
# CLI Usage: ./get_recent_incidents api_key service_id num_days [user_id]
# api_key: PagerDuty API access token
# service_id: Unique ID for the service you want to pull from
# num_days: The number of days back that you would like to pull
# user_id: Unique ID of user whose assigned incidents you want to pull
# Run an HTTP GET request and parse data
get_request()
{
res=$(curl -X GET -H "Accept: application/vnd.pagerduty+json;version=2" -H "Authorization: Token token=$2" -g $1)
echo $res | python -c "import json,sys;obj=json.loads(sys.stdin.read())['incidents'];print [{'incident_number': i['incident_number'], 'summary': i['summary'], 'created_at': i['created_at'], 'html_url': i['html_url']} for i in obj]"
}
# Save data to CSV file
save_file()
{
incidents=$(get_request $1 $2)
echo "import csv\\nf=csv.DictWriter(open('test.csv', 'w'), fieldnames=['incident_number','summary','created_at','html_url'])\\nf.writeheader()\\nfor i in $incidents: f.writerow(i)" | python
}
# Define variables & throw errors for missing params
if [ $# -eq 0 ]
then
echo 'Error: You did not enter any parameters'
echo 'Usage: ./get_recent_incidents api_key service_id num_days [user_id]'
echo '\tapi_key: PagerDuty API access token'
echo '\tservice_id: Unique ID for the service you want to pull from'
echo '\tnum_days: The number of days back that you would like to pull'
echo '\tuser_id: Unique ID of user whose assigned incidents you want to pull'
elif [ $# -eq 1 ]
then
echo 'Error: You did not enter the service_id or num_days parameters'
echo 'Usage: ./get_recent_incidents api_key service_id num_days [user_id]'
echo '\tapi_key: PagerDuty API access token'
echo '\tservice_id: Unique ID for the service you want to pull from'
echo '\tnum_days: The number of days back that you would like to pull'
echo '\tuser_id: Unique ID of user whose assigned incidents you want to pull'
elif [ $# -eq 2 ]
then
echo 'Error: You did not enter the num_days parameter'
echo 'Usage: ./get_recent_incidents api_key service_id num_days [user_id]'
echo '\tapi_key: PagerDuty API access token'
echo '\tservice_id: Unique ID for the service you want to pull from'
echo '\tnum_days: The number of days back that you would like to pull'
echo '\tuser_id: Unique ID of user whose assigned incidents you want to pull'
else
num_days=$3
since=$(date -v -${num_days}d +'%Y-%m-%d')
url="https://api.pagerduty.com/incidents?since=$since&service_ids[]=$2&statuses[]=triggered"
fi
# Initiate GET request
if [ $# -eq 3 ]
then
save_file $url $1
elif [ $# -eq 4 ]
then
echo $4
url="$url&user_ids[]=$4"
save_file $url $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment