Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active August 29, 2015 13:58
Show Gist options
  • Save julianeon/10432854 to your computer and use it in GitHub Desktop.
Save julianeon/10432854 to your computer and use it in GitHub Desktop.
Check your Gmail inbox for CLEARED emails, then go to PagerDuty and clear any incidents with a matching subject line. Please note that this script will require some customization and probably cannot be used 'as is.'
require 'rubygems'
require 'gmail'
require 'curb'
require 'json'
#credentials to login to PagerDuty and resolve incidents
subdomain="CHANGE_THIS"
api_token="CHANGE_THIS"
user_id_resolver = "CHANGE_THIS"
# Credentials to login to Gmail email address & a separate email address to check_from as the source for CLEARED emails
check_from_email="CHANGE_THIS"
username="CHANGE_THIS"
password="CHANGE_THIS"
# The part you may need to customize: it selects the unique part of your gmail-subject line that needs to be matched to identify the corresponding incident in PagerDuty
# In this example, it checks the gmail-subject line for two things - RESOLVED and also a multiple digit ID from the monitoring system
def match_test(subject,body)
if subject.match('RESOLVED')
puts "Searching for the monitoring ID within the email subject line."
id_block = subject.match(/#(\d+):/)
id_block=id_block.to_s
id_number = id_block.match(/(\d+)/)
end
end
puts "Connecting to Gmail"
gmail=Gmail.new(username,password)
# Get CLEARED message Alert ID from Gmail
# Exits if there are no new CLEARED messages
# You need to enter the logic to identify these CLEARED messages and tag them through a Gmail filter, set up separately inside Gmail
puts ("Checking messages for CLEARED notificatons")
count="0"
count=gmail.label("cleared").count(:from => check_from_email)
if count == 0
puts ("No CLEARED messages found. Exiting...")
exit
else
puts "Clear label email count is: #{count}"
end
puts ("Getting Alert ID Number.")
cleared_ids=Array.new
cleared_emails=gmail.label("cleared").emails(:from => check_from_email)
cleared_emails.each do |email|
subject_line= email.subject
email_body = email.multipart? ? (email.text_part ? email.text_part.body.decoded : nil) : email.body.decoded
issue_id = match_test(subject_line,email_body)
unless issue_id.nil?
cleared_ids.push issue_id
end
end
gmail.logout
puts "Monitoring system's CLEARED email notification ID's from your gmail inbox."
puts cleared_ids
# Check PagerDuty for open incidents & resolve them
# Step 1: hit the PagerDuty API for all open PagerDuty incident numbers and save any numbers that match Gmail CLEARED ID's in alert-subject line
# Gets PagerDuty open incidents based on matching subject line ID in CLEARED notifications and saves their number (not ID) & subject line
incident_keys=Hash.new
your_site_trigger_number_subject="https://#{subdomain}.pagerduty.com/api/v1/incidents?fields=incident_number,status,incident_key&status=triggered,acknowledged"
c = Curl::Easy.http_post(your_site_trigger_number_subject) do |curl|
curl.headers['Content-Type'] = 'application/json'
curl.headers['Authorization'] = "Token token=#{api_token}"
curl.encoding = ''
curl.perform
string = curl.body_str
parsed = JSON.parse(string)
puts parsed
parsed["incidents"].each do |line|
subject = line["incident_key"]
unless subject.nil?
cleared_ids.each do |id|
if subject.match(id.to_s)
puts "match: #{subject}"
number = line["incident_number"]
incident_keys[number] = subject
end
end
end
end
end
puts "These incident numbers are open or unresolved in PagerDuty and have subject-line matches with the CLEARED's ID's."
puts incident_keys
# Check PagerDuty for open incidents & resolve them
# Step 2: hit the PagerDuty API and save each individual PagerDuty incident ID (needed to resolve)
# Gets your PagerDuty open incident ID's based off their incident number
pd_ids=Array.new
incident_keys.each_pair do |number, subject|
puts number
your_site_trigger_id="https://#{subdomain}.pagerduty.com/api/v1/incidents/#{number}"
c = Curl::Easy.http_post(your_site_trigger_id) do |curl|
curl.headers['Content-Type'] = 'application/json'
curl.headers['Authorization'] = "Token token=#{api_token}"
curl.encoding = ''
curl.perform
string = curl.body_str
parsed = JSON.parse(string)
puts parsed
pd_id = parsed["id"]
pd_ids.push pd_id
end
end
# Check PagerDuty for open incidents & resolve them
# Step 3: hit the PagerDuty API and resolve those open PagerDuty incident ID's also CLEARED in your monitoring system's emails
puts "These incident ID's are open or unresolved in PagerDuty."
puts pd_ids
puts "Marking the above incident(s) as resolved."
your_site_resolve="https://#{subdomain}.pagerduty.com/api/v1/incidents"
pd_ids.each do |key|
put_data = '{"requester_id":"'+user_id_resolver+'","incidents":[{"id":"' + key + '","status":"resolved"}]}'
puts put_data
pd = Curl::Easy.http_put(your_site_resolve,put_data) do |curl|
curl.headers['Content-Type'] = 'application/json'
curl.headers['Authorization'] = "Token token=#{api_token}"
end
puts pd.body_str
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment