sensugcm.rb
5d4c2ceb
 #!/usr/bin/env ruby
 #A small script to send piped json events from SensuAPP to Google Cloud Messaging, Frank Solli <frank@frank2.net>
 require ‘sensu-handler’
 require ‘json’
 require ‘rubygems’
 require ‘gcm’
 
 class GCMALERT < Sensu::Handler
 def event_name
 @event[‘client’][‘name’] + ‘/’ + @event[‘check’][‘name’]
 end
 
 def handle
 gcm_apikey = settings[‘gcmalert’][‘apikey’]
 gcm_regid = settings[‘gcmalert’][‘registration_ids’]
 puts gcm_regid
 
 gcm = GCM.new(gcm_apikey)
 registration_ids = [(gcm_regid)]
 
 if @event[‘action’].eql?("resolve")
 message = "RESOLVED – #{event_name} – #{@event[‘check’][‘output’]}"
 else
 message = "ALERT – #{event_name} – #{@event[‘check’][‘output’]}"
 end
 payload = {data: {info: "#{message}"}} #GCM requires a hash
 puts payload #debug
 response = gcm.send_notification(registration_ids, payload)
 puts response #debug
 end
 end