Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active August 29, 2015 14:00
Show Gist options
  • Save julianeon/11300111 to your computer and use it in GitHub Desktop.
Save julianeon/11300111 to your computer and use it in GitHub Desktop.
require 'json'
#where to print your file output to
filename="CHANGE_THIS.txt"
subdomain="CHANGE_THIS"
service_id="CHANGE_THIS"
api_key="CHANGE_THIS"
endpoint="https://#{subdomain}.pagerduty.com/api/v1/incidents/"
def curl_command_string(tokennum,start,stop,endpoint,service_number)
start_time="2014-#{start}"
stop_time="2014-#{stop}"
curl_command='curl -H "Content-type: application/json" -H "Authorization: Token token='+tokennum+'" -X GET -G \
--data-urlencode "since='+start_time+'" \
--data-urlencode "until='+stop_time+'" \
--data-urlencode "service='+service_number+'" \
"'+endpoint+'"'
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|
incident,date,subject,state=""
hash.each_pair do |key,value|
if key=="incident_number"
incident=value
elsif key=="created_on"
date=value
elsif key=="trigger_summary_data"
subject=value["subject"]
elsif key=="status"
state=value
end
end
file.puts "#{subject},#{state},#{date},#{incident},"
#file.puts
end
end
file.close
end
def hash_loader(starter,stopper)
time_hash=Hash.new
time_hash["start"]=starter
time_hash["stop"]=stopper
time_hash
end
times_array = Array.new
times_array << hash_loader("01-01","01-10")
times_array << hash_loader("01-10","01-20")
times_array << hash_loader("01-20","02-01")
times_array << hash_loader("02-01","02-10")
times_array << hash_loader("02-10","02-20")
times_array << hash_loader("02-20","03-01")
times_array << hash_loader("03-01","03-10")
times_array << hash_loader("03-10","03-20")
times_array << hash_loader("03-20","04-01")
times_array << hash_loader("04-01","04-10")
times_array << hash_loader("04-10","04-20")
puts times_array
times_array.each do |hash|
start= hash["start"]
stop = hash["stop"]
puts "#{start} to #{stop}"
curl_interval=curl_command_string(api_key,start,stop,endpoint,service_id)
print_results_to_file(curl_interval,filename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment