Guest User

Untitled

a guest
Feb 18th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. require 'sinatra'
  2. require 'socket'
  3. require 'json'
  4. require 'xmpp4r-simple'
  5.  
  6. get '/' do
  7. 'Welcome to your cloudkick webhook handlers!'
  8. end
  9.  
  10. post "/statsd_handler" do
  11. STATSD_HOST = 'graphite.example.com'
  12. STATSD_PORT = '8125'
  13.  
  14. request.body.rewind # in case someone already read it
  15. body = request.body.read
  16. content = JSON.parse(body)
  17.  
  18. ['alerts', content['severity'] || 'unknown'].each do |metric|
  19. UDPSocket.new.send "cloudkick.#{metric.downcase}:1|c", 0, STATSD_HOST, STATSD_PORT
  20. end
  21.  
  22. "Recorded event <br/><pre>#{content.inspect}</pre>"
  23. end
  24.  
  25. post "/jabber_handler" do
  26. JABBER_USERNAME = 'me@jabber.org'
  27. JABBER_PASSWORD = 'sekret'
  28. JABBER_DESTINATION = 'my_room@partychapp.appspotchat.com'
  29.  
  30. request.body.rewind # in case someone already read it
  31. body = request.body.read
  32. content = JSON.parse(body)
  33.  
  34. message = "Cloudkick alert (#{content['severity']}) for #{content['node']['name']}:"
  35. messagte += " #{content['state_change']['current']['status_details']}"
  36.  
  37. im = Jabber::Simple.new(JABBER_USERNAME, JABBER_PASSWORD)
  38. im.deliver(JABBER_DESTINATION, message)
  39.  
  40. # wait a little for relay to complete
  41. sleep(2)
  42.  
  43. "Sent message for event <br/><pre>#{content.inspect}</pre>"
  44. end
Add Comment
Please, Sign In to add comment