Created
July 11, 2011 13:43
-
-
Save nstielau/1075871 to your computer and use it in GitHub Desktop.
A Sinatra app that for Cloudkick webhooks.
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
{ | |
"node": { | |
"provider_id": 999, | |
"name": "i-12345678", | |
"state": 2, | |
"details": { | |
"status": ["running"], | |
"productcode": ["[]"], | |
"instanceId": ["i-12345678"], | |
"dns_name": ["ec2-174-123-210-12.compute-1.amazonaws.com"], | |
"instancetype": ["m1.small"], | |
"launchdatetime": ["2010-03-30T01:23:45.000Z"], | |
"imageId": ["ami-12345678"], | |
"kernelid": ["aki-12345678"], | |
"ownerId ": ["123456789012"], | |
"keyname": ["None"], | |
"lcid": ["i-12345678"], | |
"groups ": ["default"], | |
"launchindex": ["0"], | |
"reservationId": ["r-12345678"], | |
"private_dns": ["domU-12-34-56-78-90-AB.compute-1.internal"], | |
"availability": ["us-east-1b"], | |
"monitor:host": ["Thu, 01 Jan 1970 00:00:00 GMT", "Tue, 13 Apr 2010 12:34:56 GMT", "0"], | |
"ramdiskid": ["ari-12345678"] | |
}, | |
"provider_name": "EC2 (us-east-1)", | |
"provider_id": "p827a7", | |
"active": 1, | |
"ipaddress": "174.123.210.12", | |
"id": 12345, | |
"tags": ["web", "agent"], | |
"color": "#fff", | |
"agent_state": "DISCONNECTED", | |
"provider_type": 3 | |
}, | |
"account": "YourAccount", | |
"severity": "ERROR", | |
"url": "http://example.com/web/hook/", | |
"state_change": { | |
"current": { | |
"status": "Error", | |
"duration": 50.0, | |
"status_details": "GET on http://127.0.0.1:80/server-status?auto returned connection refused", | |
"time": "2010-04-14T01:16:22" | |
}, | |
"previous": { | |
"status": "Ok", | |
"duration": 49.0, | |
"status_details": "ReqPerSec: 0.02", | |
"time": "2010-04-14T01:13:52" | |
} | |
}, | |
"method": "POST", | |
"type": "APACHE", | |
"check": "APACHE - 127.0.0.1:80" | |
} |
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 'sinatra' | |
require 'socket' | |
require 'json' | |
require 'xmpp4r-simple' | |
get '/' do | |
'Welcome to your cloudkick webhook handlers!' | |
end | |
post "/statsd_handler" do | |
STATSD_HOST = 'graphite.example.com' | |
STATSD_PORT = '8125' | |
request.body.rewind # in case someone already read it | |
body = request.body.read | |
content = JSON.parse(body) | |
['alerts', content['severity'] || 'unknown'].each do |metric| | |
UDPSocket.new.send "cloudkick.#{metric.downcase}:1|c", 0, STATSD_HOST, STATSD_PORT | |
end | |
"Recorded event <br/><pre>#{content.inspect}</pre>" | |
end | |
post "/jabber_handler" do | |
JABBER_USERNAME = '[email protected]' | |
JABBER_PASSWORD = 'sekret' | |
JABBER_DESTINATION = '[email protected]' | |
request.body.rewind # in case someone already read it | |
body = request.body.read | |
content = JSON.parse(body) | |
message = "Cloudkick alert (#{content['severity']}) for #{content['node']['name']}:" | |
messagte += " #{content['state_change']['current']['status_details']}" | |
im = Jabber::Simple.new(JABBER_USERNAME, JABBER_PASSWORD) | |
im.deliver(JABBER_DESTINATION, message) | |
# wait a little for relay to complete | |
sleep(2) | |
"Sent message for event <br/><pre>#{content.inspect}</pre>" | |
end |
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
# Use curl to test the webhook: | |
curl -X POST -d @alert_payload.json http://cloudkickwebhooks.example.com/jabber_handler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment