Created
March 5, 2014 18:19
-
-
Save marianogg9/9373214 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env /opt/sensu/embedded/bin/ruby | |
# | |
# Sensu Twitter Handler | |
# === | |
# | |
require 'rubygems' if RUBY_VERSION < '1.9.0' | |
require 'sensu-handler' | |
require 'twitter' | |
require 'timeout' | |
class TwitterHandler < Sensu::Handler | |
# var definitions: here we define the var we're using accoss the script. | |
def corte | |
@event['check']['output'][0,90] | |
end | |
def alert | |
@event['check']['output'].split(' |').first # in order to cut the message, we apply a split function and keep just the first part of the text. | |
end | |
def alert_load | |
@event['check']['output'].split('|').first | |
end | |
def alert_disk | |
@event['check']['output'].split(' - ').first # same point than before, but spliting with "/". | |
end | |
def event_name | |
@event['client']['name'] + '/' + @event['check']['name'] | |
end | |
def event_output | |
@event['check']['output'] | |
end | |
# handle definition: we need to define how and what we're handling. | |
def handle | |
puts settings['twitter'] | |
puts alert_load | |
puts corte | |
# we publish on console screen two of the var values in order to check if the script is being correctly executed so far. Debug porpouse mainly. | |
# now we define and assign the settings for sender account. | |
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") | |
if @event['check']['output'].eql?("Resolving on request of the API") | |
foo.create_direct_message("@my_twitter_account", "RESOLVED - #{event_name}: #{corte}") | |
else | |
foo.create_direct_message("@my_twitter_account", "RESOLVED - #{event_name}: #{corte}") | |
end | |
else | |
foo.create_direct_message("@my_twitter_account", "#{event_name}: #{alert}") | |
end | |
begin | |
rescue | |
foo.create_direct_message("@my_twitter_account", "There's been an error") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment