Last active
August 29, 2015 13:57
-
-
Save julianeon/9820092 to your computer and use it in GitHub Desktop.
This service checks an gmail address that you specify for all emails from your monitoring service. If it find ones, it then locates its ID, then hits the PagerDuty API to resolve that incident.
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
require 'rubygems' | |
require 'gmail' | |
require 'curb' | |
require 'json' | |
# Gmail Credentials | |
username="CHANGE_THIS" | |
password="CHANGE_THIS" | |
puts ("Connecting to Gmail") | |
gmail=Gmail.new(username,password) | |
puts ("Connected") | |
# Get CLEARED message Alert ID, then delete the email | |
puts ("Checking messages for CLEARED notificatons") | |
# Exits if there are no new CLEARED messages | |
count="0" | |
check_email="change_this" | |
count=gmail.label("cleared").count(:from => check_email) | |
if count == 0 | |
puts ("No CLEARED messages found. Exiting...") | |
exit | |
end | |
puts ("Getting Alert ID Number:") | |
gm=gmail.label("cleared").emails(:from => check_email) | |
value="none" | |
gm.each do |email| | |
subject_line= email.subject | |
if subject_line.match('CLEARED') | |
email_body = email.multipart? ? (email.text_part ? email.text_part.body.decoded : nil) : email.body.decoded | |
$incident=email_body[/.*lert ID: (.*?) /,1] | |
puts $incident | |
# email.delete! | |
end | |
end | |
gmail.logout | |
# Check PagerDuty for Open Incidents | |
puts ("Checking PagerDuty for Incidents") | |
your_site_trigger_api="https://change_this.pagerduty.com/api/v1/incidents?fields=incident_number,status,incident_key&status=triggered,acknowledged" | |
c = Curl::Easy.http_post(your_site_trigger_api) do |curl| | |
curl.headers['Content-Type'] = 'application/json' | |
curl.headers['Authorization'] = 'Token token=CHANGE_THIS' | |
curl.encoding = '' | |
curl.perform | |
string = curl.body_str | |
parsed = JSON.parse(string) | |
# Match Alert ID to incident_key, then return incident_key | |
puts ("Matching Alert ID to corresponding Incident Key") | |
puts ("Incident Key:") | |
# Using fixed_incident to test a known positive | |
$fixed_incident = 'change_this' | |
$i = "0" | |
$n = parsed['incidents'].length | |
def find_mine(one_incident) | |
raw = one_incident['incident_key'] | |
re1='((?:[a-z][a-z]*[0-9]+[a-z0-9]*))' | |
re=(re1) | |
m=Regexp.new(re,Regexp::IGNORECASE); | |
if m.match(raw) | |
alphanum1=m.match(raw)[1]; | |
key = "" << alphanum1 << "" << "\n" | |
if alphanum1 == $incident | |
$number = one_incident['incident_key'] | |
puts $number | |
end | |
end | |
end | |
for $i in (0..$n-1) | |
find_mine(parsed['incidents'][$i]) | |
end | |
end | |
# Connect to PagerDuty again and mark the Incident as | |
# Resolved using the incident_key found above | |
puts ("Marking the Incident as Resolved") | |
service_key="change_this" | |
pd = Curl.put("https://events.pagerduty.com/generic/2010-04-15/create_event.json") do |pd| | |
pd.headers['Content-Type'] = 'application/json' | |
pd.put_data = '[{"service_key":service_key,"incident_key":"' + $fixed_incident + '","event_type":"resolve"}]' | |
end | |
puts pd.body_str | |
puts ("Thanks for playing") | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment