Skip to content

Instantly share code, notes, and snippets.

@baskarp
Created October 29, 2012 07:43
Show Gist options
  • Save baskarp/3972177 to your computer and use it in GitHub Desktop.
Save baskarp/3972177 to your computer and use it in GitHub Desktop.
A script to quickly ack a PagerDuty incident by incident number
#
#
# How to use this script:
#
# 1) You'll need to have Ruby installed on your machine.
# Follow these instructions to install Ruby on your machine: https://docs.google.com/document/pub?id=1Eo3f77CC1Flkb2f71ro-NZCYK2VuA6ajxvcuTGG_c6Q
#
# 2) Once Ruby is installed, run the following from your command prompt:
# gem install faraday --no-ri --no-rdoc
#
# 3) Save this script as quick_ack.rb in a directory.
# 4) From the command prompt, navigate to the directory where the script was saved.
# 5) Run: ruby quick_ack.rb subdomain email password incident_number
#
require 'rubygems'
require 'faraday'
require 'json'
class PagerDutyAgent
attr_reader :email
attr_reader :user_id
def initialize(subdomain, email, password)
@connection = Faraday.new(:url => "https://#{subdomain}.pagerduty.com",
:ssl => {:verify => false}) do |c|
c.basic_auth email, password
# c.response :logger
c.adapter :net_http
end
@email = email
end
def get_my_user_id
@user_id ||= begin
response = @connection.get("/api/v1/users") do |request|
request.params[:query] = email
end
doc = JSON.parse(response.body)
doc["users"].first["id"]
end
end
def get_incident_id(incident_number)
response = @connection.get("/api/beta/incidents") do |request|
request.params[:status] = "triggered,acknowledged"
end
doc = JSON.parse(response.body)
# TODO support for pagination
incident = doc["incidents"].find do |incident|
incident["incident_number"] == incident_number
end
incident["id"] if incident
end
def change_status(incident_number, status)
# Get incident id from incident number
incident_id = get_incident_id(incident_number)
return false unless incident_id
body_hash = {
:incidents => [{:id => incident_id, :status => status}]
}
response = @connection.put("/api/v1/incidents") do |request|
request.headers['Content-Type'] = 'application/json'
request.body = JSON.generate(body_hash)
end
doc = JSON.parse(response.body)
incident = doc["incidents"].find do |incident|
incident["id"] == incident_id
end
return response.status == 200
end
def ack(incident_number)
change_status(incident_number, "acknowledged")
end
def resolve(incident_number)
change_status(incident_number, "resolved")
end
end
def main(subdomain, email, password, incident_number)
pd = PagerDutyAgent.new(subdomain, email, password)
success = pd.ack(incident_number)
puts "Operation #{success ? "SUCCEEDED" : "FAILED"}"
end
if $PROGRAM_NAME == __FILE__
if ARGV.length != 4
puts "Usage: ruby #{__FILE__} subdomain email password incident_number"
exit
end
subdomain, email, password, incident_number = ARGV
main(subdomain, email, password, incident_number.to_i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment