Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Twitter plugin. Use twitter.json to configure. See example for requir…

…ed values.
  • Loading branch information...
commit 5d42f049688e1655a4487be0031dddf26ae877c7 1 parent c0056be
Joe Crim authored
Showing with 74 additions and 0 deletions.
  1. +17 −0 handlers/twitter/twitter.json
  2. +57 −0 handlers/twitter/twitter.rb
View
17 handlers/twitter/twitter.json
@@ -0,0 +1,17 @@
+{
+ "twitterhandle": {
+ "sensusub": "webservers",
+ "consumer_key": "",
+ "consumer_secret": "",
+ "oauth_token": "",
+ "oauth_token_secret": ""
+ },
+
+ "twitterhandle2": {
+ "sensusub": "databases",
+ "consumer_key": "",
+ "consumer_secret": "",
+ "oauth_token": "",
+ "oauth_token_secret": ""
+ }
+}
View
57 handlers/twitter/twitter.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/env 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
+#
+# Released under the same terms as Sensu (the MIT license); see LICENSE
+# for details.
+
+require 'sensu-handler'
+require 'twitter'
+require 'timeout'
+
+class TwitterHandler < Sensu::Handler
+
+ def initialize
+ @settings = read_config('/etc/sensu/twitter.json')
+ end
+
+ def read_config(config_file)
+ if File.readable?(config_file)
+ begin
+ JSON.parse(File.open(config_file, 'r').read)
+ rescue JSON::ParserError => e
+ puts 'configuration file must be valid JSON: ' + e
+ end
+ else
+ puts 'configuration file does not exist or is not readable: ' + config_file
+ end
+ end
+
+ def handle(event)
+ puts @settings
+ @settings.each do |account|
+ if event['client']['subscriptions'].include?(account[1]["sensusub"])
+ Twitter.configure do |config|
+ config.consumer_key = account[1]["consumer_key"]
+ config.consumer_secret = account[1]["consumer_secret"]
+ config.oauth_token = account[1]["oauth_token"]
+ config.oauth_token_secret = account[1]["oauth_token_secret"]
+ end
+ if event['action'].eql?("resolve")
+ Twitter.update("RESOLVED - #{short_name(event)}: #{event['check']['notification']} Time: #{Time.now()} ")
+ else
+ Twitter.update("ALERT - #{short_name(event)}: #{event['check']['notification']} Time: #{Time.now()} ")
+ end
+ end
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.