Created
May 26, 2016 00:07
-
-
Save lfepp/8cb74ae2a779b1088b5a69127d4f6e61 to your computer and use it in GitHub Desktop.
Sample script to pull all PagerDuty incidents from a service within a given time range and print the output to the file incidents_in_service.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# 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 script to pull all the incidents from a service within a given time range and print the output to the file incidents_in_service.txt | |
# CLI Usage: ./incidents_in_service api_key service_id [since] [until] | |
# api_key: PagerDuty API access token | |
# service_id: Unique ID for the service you want to pull | |
# since: Start date of incidents you want to pull in YYYY-MM-DD format | |
# until: End date of incidents you want to pull in YYYY-MM-DD format | |
require 'json' | |
filename = "incidents_in_service.txt" | |
url = "https://api.pagerduty.com/incidents/" | |
def curl_command_string(api_key, service_id, url, start='', stop='') | |
curl_command = 'curl -H "Accept: application/vnd.pagerduty+json;version=2" -H "Authorization: Token token=' + api_key + '" -X GET -G \ | |
--data-urlencode "since=' + start + '" \ | |
--data-urlencode "until=' + stop + '" \ | |
--data-urlencode "service_ids[]=' + service_id + '" \ | |
"' + url + '"' | |
end | |
def print_results_to_file(curl_get_all, filename) | |
file = File.open(filename, "a") | |
IO.popen(curl_get_all).each do |line| | |
parsed = JSON.parse(line) | |
puts parsed | |
parsed["incidents"].each do |hash| | |
hash.each_pair {|key,value| file.puts "#{key},#{value}"} | |
file.puts | |
end | |
end | |
file.close | |
end | |
if !ARGV[0] | |
puts "Error: You did not enter any parameters" | |
puts "Usage: ./incidents_in_service api_key service_id [since] [until]" | |
puts "\tapi_key: PagerDuty API access token" | |
puts "\tservice_id: Unique ID for the service you want to pull" | |
puts "\tsince: Start date of incidents you want to pull in YYYY-MM-DD format" | |
puts "\tuntil: End date of incidents you want to pull in YYYY-MM-DD format" | |
elsif !ARGV[1] | |
puts "Error: You did not enter a service_id" | |
puts "Usage: ./incidents_in_service api_key service_id [since] [until]" | |
puts "\tapi_key: PagerDuty API access token" | |
puts "\tservice_id: Unique ID for the service you want to pull" | |
puts "\tsince: Start date of incidents you want to pull in YYYY-MM-DD format" | |
puts "\tuntil: End date of incidents you want to pull in YYYY-MM-DD format" | |
elsif !ARGV[2] | |
r = curl_command_string(ARGV[0], ARGV[1], url) | |
print_results_to_file(r, filename) | |
elsif !ARGV[3] | |
r = curl_command_string(ARGV[0], ARGV[1], url, ARGV[2]) | |
print_results_to_file(r, filename) | |
else | |
r = curl_command_string(ARGV[0], ARGV[1], url, ARGV[2], ARGV[3]) | |
print_results_to_file(r, filename) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment