alert_tweeter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .yardoc
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2@alert_tweeter --create
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+ source "http://localhost:50000"
3
+ source "http://rubygems"
4
+
5
+ # Specify your gem's dependencies in alert_tweeter.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Jonathan D. Simms
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # Alert tweeter
2
+
3
+ Hate paying _[insert provider here]_ the __OUTRAGEOUS__ texting rates you need if you're sending Nagios alerts via SMS?
4
+
5
+ Yeah, so do I.
6
+
7
+ Here's a fairly simple way to send your nagios alerts to your mobile device via Twitter. The iOS twitter app will alert when you get direct messages (you may need to run it first, so that it's in "sleep mode"), so instead of paying [$150/MB](https://twitter.com/#!/slyphon/status/160849711937490944) in SMS rates, you're piggybacking off your somewhat more generous data plan.
8
+
9
+ Unless you *love* your wireless carrier, or perhaps in the case of AT&T [feel sorry for them][whining babies], use alert tweeter.
10
+
11
+
12
+ [whining babies]: http://www.pcworld.com/businesscenter/article/184179/atandt_now_blaming_customers_for_its_problems.html
13
+
14
+ ## Configuration
15
+
16
+ * You will probably want to set up a dedicated twitter account that represnts your nagios.
17
+ * You'll need to [register an app](https://dev.twitter.com/apps).
18
+ * People who need to receive tweets will likely need to follow the tweeter.
19
+ * You'll need a YAML file like:
20
+
21
+ ```
22
+ ---
23
+ app_name: your-clever-app-name
24
+ notify_users:
25
+ - annoy
26
+ - all_of_these
27
+ - twitter_uesrs
28
+ auth:
29
+ consumer_key: YOUR_CONSUMER_KEY
30
+ consumer_secret: YOUR_CONSUMER_SECRET
31
+ oauth_token: YOUR_OAUTH_TOKEN
32
+ oauth_token_secret: YOUR_OAUTH_TOKEN_SECRET
33
+ ```
34
+
35
+ then in your nagios config, you'll need something like:
36
+
37
+ ```
38
+ define command {
39
+ command_name service-notify-by-twitter
40
+ command_line /usr/local/rvm/bin/rvm-exec 1.9.2 tweet-alert \
41
+ --config=/path/to/the/config.yml \
42
+ --notification_type=$NOTIFICATIONTYPE$ \
43
+ --event_state=$SERVICESTATE$ \
44
+ --event_state_type=$SERVICESTATETYPE$ \
45
+ --host_name=$HOSTALIAS$ \
46
+ --host_address=$HOSTADDRESS$ \
47
+ --service_desc=$SERVICEDESC$ \
48
+ --service_attempt=$SERVICEATTEMPT$ \
49
+ --service_duration_sec=$SERVICEDURATIONSEC$ \
50
+ --service_output="$SERVICEOUTPUT$"
51
+ }
52
+
53
+ define command {
54
+ command_name host-notify-by-twitter
55
+ command_line /usr/local/rvm/bin/rvm-exec 1.9.2 tweet-alert \
56
+ --config=/path/to/the/config.yml \
57
+ --notification_type=$NOTIFICATIONTYPE$ \
58
+ --event_state=$HOSTSTATE$ \
59
+ --event_state_type=$HOSTSTATETYPE$ \
60
+ --host_name=$HOSTALIAS$ \
61
+ --host_address=$HOSTADDRESS$ \
62
+ --host_attempt=$HOSTATTEMPT$ \
63
+ --host_duration_sec=$HOSTDURATIONSEC$ \
64
+ --host_output="$HOSTOUTPUT$"
65
+ }
66
+
67
+ define contact {
68
+ name twitter-contact
69
+ service_notification_period 24x7
70
+ host_notification_period 24x7
71
+ service_notification_options w,u,c,r,f,s
72
+ host_notification_options d,u,r,f,s
73
+ service_notification_commands service-notify-by-twitter
74
+ host_notification_commands host-notify-by-twitter
75
+ register 0
76
+ }
77
+
78
+ define contactgroup {
79
+ contactgroup_name admins-twitter
80
+ alias Sysamdin Twitter
81
+ members twitter-dummy
82
+ }
83
+
84
+ # as of alert_tweeter-0.0.2 the contacts are determined by the alert_tweeter.yml file
85
+ # managed by chef in the icinga recipe
86
+ define contact {
87
+ use twitter-contact
88
+ contact_name twitter-dummy
89
+ service_notification_options c,r,f
90
+ host_notification_options d,u,r,f
91
+ # ^^^^^^^--- ha! twitter dummy! durf! get it!?
92
+ }
93
+ ```
94
+
95
+ (GAH! I *hate* nagios! could they make it MORE verbose, error-prone, and annoying?!)
96
+
97
+ Obviously replace `--config=/path/to/the/config.yml` with the path to the *actual* config.
98
+
99
+ I use rvm. If you don't, or if you're weird and like rbenv, you'll need to adjust the above commands.
100
+
101
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "alert_tweeter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "alert_tweeter"
7
+ s.version = AlertTweeter::VERSION
8
+ s.authors = ["Jonathan D. Simms"]
9
+ s.email = ["slyphon@gmail.com"]
10
+ s.homepage = "https://github.com/slyphon/alert_tweeter"
11
+ s.summary = %q{Nagios alerts go to twitter}
12
+ s.description = s.summary + "\n"
13
+
14
+ s.rubyforge_project = "alert_tweeter"
15
+
16
+ s.files = `git ls-files`.split("\n").reject{|n| n =~ /\.gem$/}
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'twitter', '~> 2.0.2'
22
+ s.add_runtime_dependency 'slop', '~> 2.4.2'
23
+ s.add_runtime_dependency 'activesupport', '~> 3.1.0'
24
+ s.add_runtime_dependency 'i18n'
25
+ end
data/bin/tweet-alert ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../../lib/alert_tweeter', __FILE__)
4
+
5
+ AlertTweeter::Command.run!(*ARGV)
6
+
@@ -0,0 +1,136 @@
1
+ $LOAD_PATH.unshift(File.expand_path('..', __FILE__)).uniq!
2
+
3
+ require "alert_tweeter/version"
4
+
5
+ require 'slop'
6
+ require 'twitter'
7
+
8
+ %w[ core_ext/module/attribute_accessors
9
+ core_ext/class/attribute_accessors
10
+ core_ext/string/strip
11
+ core_ext/module/aliasing
12
+ ].each do |p|
13
+ require "active_support/#{p}"
14
+ end
15
+
16
+ module AlertTweeter
17
+ mattr_accessor :root, :instance_writer => false
18
+
19
+ DEFAULT_CONFIG_PATH = '/etc/alert_tweeter.yml'
20
+
21
+ class Command
22
+ attr_reader :opts
23
+
24
+ def self.run!(*a, &b)
25
+ new.run!(*a, &b)
26
+ end
27
+
28
+ def config
29
+ @config ||= YAML.load_file(opts[:config])
30
+ end
31
+
32
+ def notify_users
33
+ @config.fetch('notify_users')
34
+ end
35
+
36
+ def configure_client!(path)
37
+ require 'yaml'
38
+
39
+ hash = config.fetch('auth')
40
+
41
+ Twitter.configure do |c|
42
+ %w[consumer_key consumer_secret oauth_token oauth_token_secret].each do |k|
43
+ c.__send__(:"#{k}=", hash.fetch(k))
44
+ end
45
+ end
46
+ end
47
+
48
+ def timestamp
49
+ Time.at(timet.to_i).strftime('%Y-%m-%d %H:%M:%S')
50
+ end
51
+
52
+ def send_notification(message)
53
+ notify_users.each do |user|
54
+ Twitter.direct_message_create(user, message)
55
+ end
56
+ end
57
+
58
+ def service_notify!
59
+ send_notification <<-EOS.strip_heredoc[0...140]
60
+ #{notification_type} #{host_name}/#{service_desc} #{event_state}
61
+ #{timestamp}
62
+ #{service_output}
63
+ EOS
64
+ end
65
+
66
+ def host_notify!
67
+ send_notification <<-EOS.strip_heredoc[0...140]
68
+ #{notification_type} #{host_name} #{event_state}
69
+ #{timestamp}
70
+ #{host_output}
71
+ EOS
72
+ end
73
+
74
+ def run!(*args)
75
+ args << '-h' if args.empty?
76
+
77
+ @opts = Slop.parse!(args, :strict => true, :help => true) do
78
+ banner <<-EOS.strip_heredoc
79
+ Usage: #{File.basename($0)} [opts]
80
+
81
+ Will direct-message the configured users about service problems. Command-line options
82
+ are provided that more-or-less match their nagios macro counterparts.
83
+ EOS
84
+
85
+ on :f, :config, 'path to the config file to use', true, :default => DEFAULT_CONFIG_PATH
86
+
87
+ on :n, :notification_type, 'string describing the type of notification being sent', true
88
+
89
+ on :s, :event_state, 'string indicating the state of event-generating object', true, :default => 'UNKNOWN'
90
+ on :y, :event_state_type, 'HARD or SOFT', true
91
+
92
+ on :H, :host_name, 'host name where the alert is triggered', true, :required => true
93
+ on :a, :host_address, 'IP address of the host', true
94
+ on :A, :host_attempt, 'number of the current re-check', true, :as => :integer
95
+ on :O, :host_output, 'output from host check', true
96
+ on :U, :host_duration_sec, 'number of seconds the host has spent in the current state', true, :as => :integer
97
+
98
+ on :d, :service_desc, 'description of the service', true
99
+ on :a, :service_attempt, 'number of the current re-check', true, :as => :integer
100
+ on :u, :service_duration_sec, 'number of seconds the service has been in the current state', true, :as => :integer
101
+ on :o, :service_output, 'first line of text output from the last service check', true
102
+
103
+ on :t, :timet, 'seconds since unix epoch', true, :as => :integer, :default => Time.now.to_i
104
+ end
105
+
106
+ if opts.help?
107
+ $stderr.puts opts.help
108
+ exit 1
109
+ end
110
+
111
+ unless opts[:service_output] or opts[:host_output]
112
+ $stderr.puts "ERROR: You must specify either a --service_output or --host_output option"
113
+ exit 1
114
+ end
115
+
116
+ configure_client!(opts[:config])
117
+
118
+ if opts[:service_output]
119
+ service_notify!
120
+ else
121
+ host_notify!
122
+ end
123
+ end
124
+
125
+ private
126
+ def method_missing(name, *a, &b)
127
+ namesym = name.to_sym
128
+ if val = opts[namesym]
129
+ return val
130
+ else
131
+ super
132
+ end
133
+ end
134
+ end
135
+ end
136
+
@@ -0,0 +1,3 @@
1
+ module AlertTweeter
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alert_tweeter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jonathan D. Simms
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: twitter
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 2
31
+ - 0
32
+ - 2
33
+ version: 2.0.2
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: slop
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 27
45
+ segments:
46
+ - 2
47
+ - 4
48
+ - 2
49
+ version: 2.4.2
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: activesupport
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 3
63
+ - 1
64
+ - 0
65
+ version: 3.1.0
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: i18n
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ type: :runtime
81
+ version_requirements: *id004
82
+ description: |
83
+ Nagios alerts go to twitter
84
+
85
+ email:
86
+ - slyphon@gmail.com
87
+ executables:
88
+ - tweet-alert
89
+ extensions: []
90
+
91
+ extra_rdoc_files: []
92
+
93
+ files:
94
+ - .gitignore
95
+ - .rvmrc
96
+ - Gemfile
97
+ - LICENSE
98
+ - README.md
99
+ - Rakefile
100
+ - alert_tweeter.gemspec
101
+ - bin/tweet-alert
102
+ - lib/alert_tweeter.rb
103
+ - lib/alert_tweeter/version.rb
104
+ homepage: https://github.com/slyphon/alert_tweeter
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project: alert_tweeter
133
+ rubygems_version: 1.8.15
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Nagios alerts go to twitter
137
+ test_files: []
138
+