nagios_alerter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) <year> <copyright holders>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ lib/nagios/alerter.rb
5
+ lib/nagios/connection.rb
6
+ lib/nagios_alerter.rb
7
+ nagios_alerter.gemspec
8
+ spec/lib/alerter_spec.rb
9
+ spec/lib/connection_spec.rb
10
+ spec/spec.opts
11
+ tasks/rspec.rake
12
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = Nagios Alerter
2
+
3
+ Ruby library for sending heartbeat messages to a Nagios server.
4
+
5
+ This is a wrapper for kbedell's Ruby implementation of the Nagios send_nsca C libary.
6
+
7
+ == Install
8
+
9
+ gem install nagios-alerter --source http://gems.github.com
10
+
11
+ == Usage
12
+
13
+ You can create a Nagios::Alerter and call #send_alert whenever you want to send a Nagios message. There are a couple ways you can set up the Alerter:
14
+
15
+ === Option 0: Using the constructor
16
+
17
+ You can set up all of the details of your alerts when you create the Alerter:
18
+
19
+ @alerter = Nagios::Alerter.new(:hostname => "some.domain.com",
20
+ :service_name => "my-alerter",
21
+ :return_code => SendNsca::STATUS_OK,
22
+ :status => "TEST")
23
+ # ...
24
+ @alerter.send_alert(:host => "nagios.domain.com", :port => 5667)
25
+
26
+ === Option 1: Using the instance variables
27
+
28
+ You can also use Ruby's setter methods to set or tweak your Alerter details:
29
+
30
+ @alerter = Nagios::Alerter.new
31
+ @alerter.hostname = "some.domain.com"
32
+ @alerter.service_name = "my-alerter"
33
+ @alerter.return_code = SendNsca::STATUS_OK
34
+ @alerter.status = "TEST"
35
+ # ...
36
+ @alerter.send_alert(:host => "nagios.domain.com", :port => 5667)
37
+
38
+ === Option 2: Using the class method
39
+
40
+ You don't have to create an Alerter if you don't want to, you can use the class method instead:
41
+
42
+ Nagios::Alerter.send_alert(:host => "nagios.domain.com",
43
+ :port => 5667,
44
+ :hostname => "some.domain.com",
45
+ :service_name => "my-alerter",
46
+ :return_code => SendNsca::STATUS_OK,
47
+ :status => "TEST")
48
+
49
+ == Global settings
50
+
51
+ Most users have only one Nagios server, so having to explicitly pass the host and port information to every #send_alert call can be very redundant. Instead, you can use Nagios::Connection to set up a globally accessible Nagios server. For example:
52
+
53
+ Nagios::Connection.instance.host = "nagios.somedomain.com"
54
+ Nagios::Connection.instance.port = 5667
55
+
56
+ Nagios::Connection is a Singleton class, so you need to use #instance when you access it. You can set it up in your project's environment and have all further calls to #send_alert use those settings (except when using the class method).
57
+
58
+ == Making Changes
59
+
60
+ # add/remove files
61
+ rake manifest
62
+ # make changes and reinstall to local system
63
+ rake install
64
+ # update version number in Rakefile and rebuild gemspec
65
+ rake build_gemspec
66
+
67
+ == License
68
+
69
+ The MIT License
70
+
71
+ Copyright (c) 2010 Dana Merrick
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining a copy
74
+ of this software and associated documentation files (the "Software"), to deal
75
+ in the Software without restriction, including without limitation the rights
76
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
77
+ copies of the Software, and to permit persons to whom the Software is
78
+ furnished to do so, subject to the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be included in
81
+ all copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
84
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
85
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
86
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
87
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
88
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
89
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('nagios_alerter', '0.1.0') do |p|
6
+ p.description = "Send a heartbeat to a Nagios server"
7
+ p.url = "http://github.com/dmerrick/nagios_alerter"
8
+ p.author = "Dana Merrick"
9
+ p.email = "dana.merrick@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.runtime_dependencies = ["send_nsca >=0.4.11"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ # load any other rake tasks
16
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,103 @@
1
+ require 'rubygems'
2
+ require 'send_nsca'
3
+
4
+ module Nagios
5
+ class Alerter
6
+
7
+ attr_accessor :hostname
8
+ attr_accessor :service_name
9
+ attr_accessor :return_code
10
+ attr_accessor :status
11
+
12
+ def initialize(args = {})
13
+ @hostname = args[:hostname]
14
+ @service_name = args[:service_name]
15
+ @return_code = args[:return_code]
16
+ @status = args[:status]
17
+ end
18
+
19
+ def send_alert(args = {})
20
+ # host and port can be passed as parameters or set using Nagios::Connection
21
+ host, port = connection_params(args[:host], args[:port])
22
+
23
+ # check the parameters if we're missing any variables
24
+ check_args(args) if [@hostname, @service_name, @return_code, @status].any? {|var| var.nil? }
25
+
26
+ hostname = args[:hostname] || @hostname
27
+ service_name = args[:service_name] || @service_name
28
+ return_code = args[:return_code] || @return_code
29
+ status = args[:status] || @status
30
+
31
+ config = {
32
+ :nscahost => host,
33
+ :port => port,
34
+ :hostname => hostname,
35
+ :service => service_name,
36
+ :return_code => return_code,
37
+ :status => status
38
+ }
39
+
40
+ nsca_connection = SendNsca::NscaConnection.new(config)
41
+ nsca_connection.send_nsca
42
+ end
43
+
44
+ # if you don't want to create a Nagios::Alerter object, you can call Nagios::Alerter.send_alert
45
+ def self.send_alert(args)
46
+ if [:host, :port, :hostname, :service_name, :return_code, :status].any? {|key| !args.has_key?(key) }
47
+ raise ArgumentError, "You must provide all alert details"
48
+ end
49
+
50
+ config = {
51
+ :nscahost => args[:host],
52
+ :port => args[:port],
53
+ :hostname => args[:hostname],
54
+ :service => args[:service_name],
55
+ :return_code => args[:return_code],
56
+ :status => args[:status]
57
+ }
58
+
59
+ nsca_connection = SendNsca::NscaConnection.new(config)
60
+ nsca_connection.send_nsca
61
+ end
62
+
63
+ private
64
+
65
+ # check that we have the Nagios server details
66
+ def connection_params(host, port)
67
+
68
+ params = {}
69
+
70
+ if !host.nil?
71
+ params[:host] = host
72
+ elsif !Nagios::Connection.instance.host.nil?
73
+ params[:host] = Nagios::Connection.instance.host
74
+ else
75
+ raise ArgumentError, "You must provide a Nagios host or use Nagios::Connection"
76
+ end
77
+
78
+ if !port.nil?
79
+ params[:port] = port
80
+ elsif !Nagios::Connection.instance.port.nil?
81
+ params[:port] = Nagios::Connection.instance.port
82
+ else
83
+ raise ArgumentError, "You must provide a Nagios port or use Nagios::Connection"
84
+ end
85
+
86
+ return params
87
+ end
88
+
89
+ # a method to verify that all necessary requirements are satisfied
90
+ def check_args(hash)
91
+ if !hash.include? :hostname
92
+ raise ArgumentError, "You must provide a hostname"
93
+ elsif !hash.include? :service_name
94
+ raise ArgumentError, "You must provide a service name"
95
+ elsif !hash.include? :return_code
96
+ raise ArgumentError, "You must provide a return code"
97
+ elsif !hash.include? :status
98
+ raise ArgumentError, "You must provide a status"
99
+ end
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,11 @@
1
+ require 'singleton'
2
+
3
+ module Nagios
4
+ class Connection
5
+ include Singleton
6
+
7
+ attr_accessor :host
8
+ attr_accessor :port
9
+
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Nagios
2
+ autoload :Alerter, "nagios/alerter"
3
+ autoload :Connection, "nagios/connection"
4
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{nagios_alerter}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Dana Merrick"]
9
+ s.date = %q{2010-09-09}
10
+ s.description = %q{Send a heartbeat to a Nagios server}
11
+ s.email = %q{dana.merrick@gmail.com}
12
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/nagios/alerter.rb", "lib/nagios/connection.rb", "lib/nagios_alerter.rb", "tasks/rspec.rake"]
13
+ s.files = ["LICENSE", "README.rdoc", "Rakefile", "lib/nagios/alerter.rb", "lib/nagios/connection.rb", "lib/nagios_alerter.rb", "nagios_alerter.gemspec", "spec/lib/alerter_spec.rb", "spec/lib/connection_spec.rb", "spec/spec.opts", "tasks/rspec.rake", "Manifest"]
14
+ s.homepage = %q{http://github.com/dmerrick/nagios_alerter}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nagios_alerter", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{nagios_alerter}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Send a heartbeat to a Nagios server}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<send_nsca>, [">= 0.4.11"])
27
+ else
28
+ s.add_dependency(%q<send_nsca>, [">= 0.4.11"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<send_nsca>, [">= 0.4.11"])
32
+ end
33
+ end
@@ -0,0 +1,133 @@
1
+ require 'nagios/connection'
2
+ require 'nagios/alerter'
3
+
4
+ describe Nagios::Alerter do
5
+ describe "that's brand new" do
6
+
7
+ before do
8
+ @alerter = Nagios::Alerter.new
9
+ end
10
+
11
+ it "should have no defaults" do
12
+ @alerter.hostname.should be_nil
13
+ @alerter.service_name.should be_nil
14
+ @alerter.return_code.should be_nil
15
+ @alerter.status.should be_nil
16
+ end
17
+
18
+ it "should raise an error if you try to send an alert" do
19
+ lambda { @alerter.send_alert }.should raise_error(ArgumentError)
20
+ end
21
+
22
+ it "should set values using the constructor" do
23
+ @alerter = Nagios::Alerter.new(:hostname => "some.domain.com",
24
+ :service_name => "my-alerter",
25
+ :return_code => SendNsca::STATUS_OK,
26
+ :status => "TEST")
27
+
28
+ @alerter.hostname.should == "some.domain.com"
29
+ @alerter.service_name.should == "my-alerter"
30
+ @alerter.return_code.should == SendNsca::STATUS_OK
31
+ @alerter.status.should == "TEST"
32
+ end
33
+
34
+ it "should set the hostname" do
35
+ @alerter.hostname = "some.otherdomain.com"
36
+ @alerter.hostname.should == "some.otherdomain.com"
37
+ end
38
+
39
+ it "should set the service name" do
40
+ @alerter.service_name = "new-alerter"
41
+ @alerter.service_name.should == "new-alerter"
42
+ end
43
+
44
+ it "should set the return code" do
45
+ @alerter.return_code = SendNsca::STATUS_WARNING
46
+ @alerter.return_code.should == SendNsca::STATUS_WARNING
47
+ end
48
+
49
+ it "should set the status" do
50
+ @alerter.status = "NEWTEST"
51
+ @alerter.status.should == "NEWTEST"
52
+ end
53
+ end
54
+
55
+ describe "using Nagios::Connection" do
56
+ before(:all) do
57
+ Nagios::Connection.instance.host = "some.domain.com"
58
+ Nagios::Connection.instance.port = 5667
59
+ end
60
+
61
+ before(:each) do
62
+ @alerter = Nagios::Alerter.new(:hostname => "some.domain.com",
63
+ :service_name => "new-alerter",
64
+ :return_code => SendNsca::STATUS_OK,
65
+ :status => "TEST")
66
+ end
67
+
68
+ it "should not error if not given a host and port" do
69
+ @alerter.should_receive(:send_alert)
70
+ lambda { @alerter.send_alert }.should_not raise_error(ArgumentError)
71
+ end
72
+ end
73
+
74
+ describe "using the static method" do
75
+ it "should error if run without any arguments" do
76
+ lambda { Nagios::Alerter.send_alert }.should raise_error(ArgumentError)
77
+ end
78
+
79
+ it "should error if run without a host" do
80
+ lambda { Nagios::Alerter.send_alert(:port => 5667,
81
+ :hostname => "some.domain.com",
82
+ :service_name => "my-alerter",
83
+ :return_code => SendNsca::STATUS_OK,
84
+ :status => "TEST")
85
+ }.should raise_error(ArgumentError)
86
+ end
87
+
88
+ it "should error if run without a port" do
89
+ lambda { Nagios::Alerter.send_alert(:host => "nagios.domain.com",
90
+ :hostname => "some.domain.com",
91
+ :service_name => "my-alerter",
92
+ :return_code => SendNsca::STATUS_OK,
93
+ :status => "TEST")
94
+ }.should raise_error(ArgumentError)
95
+ end
96
+
97
+ it "should error if run without a hostname" do
98
+ lambda { Nagios::Alerter.send_alert(:host => "nagios.domain.com",
99
+ :port => 5667,
100
+ :service_name => "my-alerter",
101
+ :return_code => SendNsca::STATUS_OK,
102
+ :status => "TEST")
103
+ }.should raise_error(ArgumentError)
104
+ end
105
+
106
+ it "should error if run without a service name" do
107
+ lambda { Nagios::Alerter.send_alert(:host => "nagios.domain.com",
108
+ :port => 5667,
109
+ :hostname => "some.domain.com",
110
+ :return_code => SendNsca::STATUS_OK,
111
+ :status => "TEST")
112
+ }.should raise_error(ArgumentError)
113
+ end
114
+
115
+ it "should error if run without a return code" do
116
+ lambda { Nagios::Alerter.send_alert(:host => "nagios.domain.com",
117
+ :port => 5667,
118
+ :hostname => "some.domain.com",
119
+ :service_name => "my-alerter",
120
+ :status => "TEST")
121
+ }.should raise_error(ArgumentError)
122
+ end
123
+
124
+ it "should error if run without a status" do
125
+ lambda { Nagios::Alerter.send_alert(:host => "nagios.domain.com",
126
+ :port => 5667,
127
+ :hostname => "some.domain.com",
128
+ :service_name => "my-alerter",
129
+ :return_code => SendNsca::STATUS_OK)
130
+ }.should raise_error(ArgumentError)
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,27 @@
1
+ require 'nagios/connection'
2
+
3
+ describe Nagios::Connection do
4
+ it "should mixin the Singleton module" do
5
+ included_modules = (class << Nagios::Connection.instance; self; end).send :included_modules
6
+ included_modules.should include(Singleton)
7
+ end
8
+
9
+ it "should only have one instance" do
10
+ a = Nagios::Connection.instance
11
+ b = Nagios::Connection.instance
12
+ a.should == b
13
+ end
14
+
15
+ it "should have no defaults" do
16
+ Nagios::Connection.instance.host.should be_nil
17
+ Nagios::Connection.instance.port.should be_nil
18
+ end
19
+
20
+ it "should set the host and port" do
21
+ Nagios::Connection.instance.host = "some.domain.com"
22
+ Nagios::Connection.instance.port = 5667
23
+
24
+ Nagios::Connection.instance.host.should == "some.domain.com"
25
+ Nagios::Connection.instance.port.should == 5667
26
+ end
27
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
data/tasks/rspec.rake ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ task :default => :spec
5
+
6
+ desc "Run all specs in spec directory"
7
+ Spec::Rake::SpecTask.new do |t|
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ namespace :spec do
12
+ desc "Run all specs in spec directory with RCov (excluding plugin specs)"
13
+ Spec::Rake::SpecTask.new(:rcov) do |t|
14
+ t.spec_files = FileList['spec/**/*_spec.rb']
15
+ t.rcov = true
16
+ end
17
+
18
+ desc "Print Specdoc for all specs"
19
+ Spec::Rake::SpecTask.new(:doc) do |t|
20
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
21
+ t.spec_files = FileList['spec/**/*_spec.rb']
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagios_alerter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Dana Merrick
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-09 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: send_nsca
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 11
34
+ version: 0.4.11
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Send a heartbeat to a Nagios server
38
+ email: dana.merrick@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ - lib/nagios/alerter.rb
47
+ - lib/nagios/connection.rb
48
+ - lib/nagios_alerter.rb
49
+ - tasks/rspec.rake
50
+ files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/nagios/alerter.rb
55
+ - lib/nagios/connection.rb
56
+ - lib/nagios_alerter.rb
57
+ - nagios_alerter.gemspec
58
+ - spec/lib/alerter_spec.rb
59
+ - spec/lib/connection_spec.rb
60
+ - spec/spec.opts
61
+ - tasks/rspec.rake
62
+ - Manifest
63
+ has_rdoc: true
64
+ homepage: http://github.com/dmerrick/nagios_alerter
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --line-numbers
70
+ - --inline-source
71
+ - --title
72
+ - Nagios_alerter
73
+ - --main
74
+ - README.rdoc
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 11
92
+ segments:
93
+ - 1
94
+ - 2
95
+ version: "1.2"
96
+ requirements: []
97
+
98
+ rubyforge_project: nagios_alerter
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Send a heartbeat to a Nagios server
103
+ test_files: []
104
+