Skip to content

Instantly share code, notes, and snippets.

@marianogg9
Created February 5, 2014 20:37
Show Gist options
  • Save marianogg9/8832510 to your computer and use it in GitHub Desktop.
Save marianogg9/8832510 to your computer and use it in GitHub Desktop.
#!/usr/bin/env /path/ruby
#
# Sensu Twitter Handler
# ===
#
# This handler reports alerts to a configured twitter handler.
# Map a twitter handle to a sensusub value in the twitter.json to get going!
# sensusub == subscription in the client object, not check..
# see twitter.json for required values
#
# Copyright 2011 Joe Crim <[email protected]>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
# export SSL_CERT_FILE=/etc/sensu/handlers/cacert.pem
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'twitter'
require 'timeout'
class TwitterHandler < Sensu::Handler
def event_name
@event['client']['name'] + '/' + @event['check']['name']
end
def handle
puts settings['twitter']
settings['twitter'].each do |account|
if @event['client']['subscriptions'].include?(account[1]["sensusub"])
foo = Twitter::REST::Client.new do |config|
config.consumer_key = account[1]["consumer_key"]
config.consumer_secret = account[1]["consumer_secret"]
config.access_token = account[1]["access_token"]
config.access_token_secret = account[1]["access_token_secret"]
end
if @event['action'].eql?("resolve")
#foo.update("RESOLVED - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}" )
foo.create_direct_message("@user1", "RESOLVED - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user2", "RESOLVED - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user3", "RESOLVED - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user4", "RESOLVED - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
else
#foo.update("ALERT - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}" )
foo.create_direct_message("@user1", "ALERT - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user2", "ALERT - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user3", "ALERT - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
foo.create_direct_message("@user4", "ALERT - #{event_name}: #{@event['check']['notification']} Time: #{Time.now()}")
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment