nagios_helper 0.1.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b1d346c062988fe38d78c13e5b0acc695417d3b
4
+ data.tar.gz: 5c7225e767d1122292e7def3ce87552e946c5e76
5
+ SHA512:
6
+ metadata.gz: 282c265047ab2d6015e6eed5b85905cada8a31910af2e6cdf4a5c3c9e9395bc8cdb2537e18a743a5f0cbb0062745c278938e3a67808fa6abed4574ea1d6feb1a
7
+ data.tar.gz: 67a8344b613ffd52775aa1630c7b39f298770d1649500d3fa9ec1d98a6fbc5b036b048953e0bcedfcc3c0ec06f800c27285c0f1fd55a6b7aea2d75623590ce00
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/README.md CHANGED
@@ -2,15 +2,15 @@ NagiosHelper
2
2
  ============
3
3
 
4
4
  Gem for writing, testing, executing Nagios checks inside Rails application.
5
- Checks running throught http or binary(nrpe).
5
+ Checks running throught http or script.
6
6
 
7
7
  ```
8
- gem 'nagios_helper'
8
+ gem 'nagios_helper', :require => 'nagios'
9
9
  ```
10
10
 
11
11
  $ rails generate nagios:check some
12
-
13
- Check example:
12
+
13
+ Check example:
14
14
  --------------
15
15
 
16
16
  app/nagios/some.rb
@@ -21,22 +21,22 @@ class Nagios::Some < Nagios::Check
21
21
 
22
22
  def execute
23
23
  count = User.count + x.to_i
24
-
24
+
25
25
  warn "hmmm" if count < 10
26
26
  crit "ouch" if count < 5
27
-
27
+
28
28
  ok "good #{count}"
29
29
  end
30
30
 
31
31
  end
32
32
  ```
33
33
 
34
- Run:
34
+ Run:
35
35
 
36
36
  $ RAILS_ENV=production bundle exec nagios_check some x 1
37
37
 
38
38
  ### Nagios Check Initilizers:
39
- All files in app/nagios/initializers will auto loads.
39
+ All files in app/nagios/initializers will auto loads.
40
40
 
41
41
  Server:
42
42
  -------
@@ -58,13 +58,3 @@ end
58
58
  ```
59
59
 
60
60
  $ curl http://nagios:password@localhost:3000/nagios/check?method=some&x=1
61
-
62
- ### Outside rails server
63
-
64
- With using nonblocking EM-server [nagios_rails_server](http://github.com/kostya/nagios_rails_server)
65
-
66
- AR connections should be configured with pool: 100.
67
-
68
- $ RAILS_ENV=production bundle exec nagios_server
69
- $ curl localhost:9292/check/some?x=1
70
-
@@ -1,11 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), %w{.. lib})))
3
- require 'nagios_helper/boot'
4
-
5
- raise "RAILS_ROOT not found" unless defined?(RAILS_ROOT)
6
- require File.expand_path(File.join(RAILS_ROOT, %w{config environment}))
7
-
8
- require 'nagios_helper/runner_async'
9
2
 
10
3
  class Array
11
4
  # define an iterator over each pair of indexes in an array
@@ -27,9 +20,24 @@ end
27
20
 
28
21
  method = ARGV.shift
29
22
  params = {}
30
- ARGV.each_pair{|a, b| params[a.to_s.gsub('-', '')] = b }
23
+ ARGV.each_pair do |a, b|
24
+ if a == '-e'
25
+ ENV['RAILS_ENV'] = b
26
+ else
27
+ params[a.to_s.gsub('-', '')] = b
28
+ end
29
+ end
30
+
31
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), %w{.. lib})))
32
+ require 'nagios/boot'
33
+
34
+ raise "RAILS_ROOT not found" unless defined?(RAILS_ROOT)
35
+ require File.expand_path(File.join(RAILS_ROOT, %w{config environment}))
36
+
37
+ require 'nagios/runner'
31
38
 
32
- status, message = Nagios::RunnerAsync.check(params.merge(:method => method))
39
+ status, message = Nagios::Runner.check(params.merge(:method => method))
40
+ prefix = Nagios::STATUS_NAMES[status] + ": "
33
41
 
34
- puts message
42
+ puts prefix + message
35
43
  exit status
@@ -9,7 +9,7 @@ if Rails::VERSION::MAJOR >= 3
9
9
  template "script_class_em.rb", "app/nagios/#{file_path}.rb"
10
10
  template "spec.rb", "spec/nagios/#{file_path}_spec.rb"
11
11
  end
12
- end
12
+ end
13
13
  end
14
14
 
15
15
  end
@@ -9,7 +9,7 @@ if Rails::VERSION::MAJOR >= 3
9
9
  template "script_class.rb", "app/nagios/#{file_path}.rb"
10
10
  template "spec.rb", "spec/nagios/#{file_path}_spec.rb"
11
11
  end
12
- end
12
+ end
13
13
  end
14
14
 
15
15
  end
@@ -8,10 +8,10 @@ class Nagios::<%= class_name %> < Nagios::CheckEM
8
8
  warn "hmmm" if x == 'w'
9
9
  crit "ouch" if x == 'c'
10
10
  ok "good"
11
-
11
+
12
12
  send_result # should be!, for sending results
13
13
  end
14
14
  end
15
15
  end
16
-
16
+
17
17
  end
@@ -1,10 +1,14 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
- require 'nagios_check/spec_helper'
2
+ require 'nagios/spec_helper'
3
3
 
4
4
  describe Nagios::<%= class_name %> do
5
+ before :each do
6
+ @check = Nagios::<%= class_name %>.new({:x => 'some'})
7
+ end
8
+
5
9
  it 'should be ok' do
6
- status, message = Nagios::<%= class_name %>.check({:x => 'some'})
10
+ status, message = @check.check
7
11
  status.should == Nagios::OK
8
- message.should include('good')
12
+ message.should == 'good'
9
13
  end
10
14
  end
File without changes
@@ -2,99 +2,140 @@ class Nagios::Check
2
2
 
3
3
  TYPES = %w{ok crit other warn} unless defined?(TYPES)
4
4
 
5
- def initialize(params, &callback)
5
+ def initialize(params = {}, &callback)
6
6
  @params = params.with_indifferent_access
7
7
  @callback = callback
8
8
  @started_at = Time.now
9
9
  @tag = "#{self.class.name}/#{params.inspect}"
10
-
10
+ @check_name = self.class.name.underscore
11
+
11
12
  logger.info "=> #{@tag}"
12
-
13
+
13
14
  @ok = []
14
15
  @crit = []
15
16
  @warn = []
16
17
  @other = []
17
18
  end
18
-
19
+
19
20
  def result
20
- errors = [@crit, @warn, @other].flatten * '; '
21
-
21
+ prefix = self.respond_to?(:message_prefix) ? message_prefix : ''
22
+ errors = prefix + [@crit, @warn, @other].flatten * '; '
23
+
22
24
  if @crit.present?
23
- [Nagios::CRIT, "CRIT: " + errors]
25
+ [Nagios::CRIT, errors]
24
26
  elsif @warn.present?
25
- [Nagios::WARN, "WARN: " + errors]
27
+ [Nagios::WARN, errors]
26
28
  elsif @other.present?
27
29
  [Nagios::OTHER, errors]
28
30
  else
29
- [Nagios::OK, "OK: " + @ok * '; ']
31
+ @ok = ['OK'] if prefix.empty? && @ok.empty?
32
+ [Nagios::OK, prefix + @ok * '; ']
30
33
  end
31
34
  end
32
35
 
33
- def run
36
+ def run(additional_params = nil)
37
+ if additional_params && additional_params.is_a?(Hash)
38
+ @params.merge!(additional_params.with_indifferent_access)
39
+ end
40
+
34
41
  safe do
35
42
  execute
36
43
  send_result
37
44
  end
38
45
  end
39
-
46
+
47
+ alias check run
48
+
40
49
  # synchrony check, for manually calls
41
50
  def self.check(params = {})
42
51
  result = nil
43
-
52
+
44
53
  inst = self.new(params) do |res|
45
- result = res
54
+ result = res
46
55
  end
47
-
56
+
48
57
  inst.run
49
58
  end
50
-
59
+
51
60
  def self.default_error(mes)
52
61
  [Nagios::OTHER, mes]
53
62
  end
54
-
63
+
55
64
  protected
56
65
 
57
66
  def send_result
58
67
  st, mes = res = self.result
59
-
68
+
60
69
  logger.info "<= #{@tag} = [#{Nagios.status_name(st)}, #{mes}], time: (#{Time.now - @started_at})"
61
- @callback[ res ]
62
-
70
+ @callback[ res ] if @callback
71
+
63
72
  res
64
73
  end
65
74
 
66
75
  def execute
67
76
  raise "realize me"
68
77
  end
69
-
78
+
70
79
  TYPES.each do |m|
71
- define_method(m) do |mes|
72
- instance_variable_get("@#{m}") << mes
80
+ define_method(m) do |mes, &block|
81
+ if block
82
+ if block.call
83
+ instance_variable_get("@#{m}") << mes
84
+ end
85
+ else
86
+ instance_variable_get("@#{m}") << mes
87
+ end
73
88
  end
74
89
  end
75
90
  alias :critical :crit
76
91
  alias :error :crit
77
92
  alias :warning :warn
78
-
93
+
79
94
  def self.logger
80
95
  Nagios.logger
81
96
  end
82
-
97
+
83
98
  def logger
84
99
  Nagios.logger
85
100
  end
86
-
101
+
87
102
  def self.params(*syms)
88
- syms.each { |s| define_method(s) { @params[s] } }
103
+ syms.each { |s| define_method(s) { (r = @params[s]) && r.to_s } }
89
104
  end
90
-
105
+
91
106
  def safe
92
107
  yield
93
108
  rescue Exception, NameError, Timeout::Error => ex
94
109
  logger.info "X= #{@tag} #{ex.message} (#{ex.backtrace.inspect})"
95
-
110
+
96
111
  other "Exception: " + ex.message
97
112
  send_result
98
113
  end
99
-
100
- end
114
+
115
+ def tresholds(method, w, e, &block)
116
+ res = send(method)
117
+ msg = block[res]
118
+ if e && res >= e
119
+ crit msg
120
+ elsif w && res >= w
121
+ warn msg
122
+ else
123
+ ok msg
124
+ end
125
+ end
126
+
127
+ class << self
128
+ def check_name
129
+ @check_name ||= self.name.underscore
130
+ end
131
+
132
+ def url
133
+ "http://localhost:3000/nagios/check?method=#{check_name}"
134
+ end
135
+
136
+ def interval
137
+ 5 * 60
138
+ end
139
+ end
140
+
141
+ end
@@ -5,11 +5,11 @@ class Nagios::CheckEM < Nagios::Check
5
5
  execute
6
6
  end
7
7
  end
8
-
8
+
9
9
  def execute
10
10
  send_result
11
11
  end
12
-
12
+
13
13
  def safe_defer
14
14
  EM.defer do
15
15
  safe do
@@ -17,25 +17,25 @@ class Nagios::CheckEM < Nagios::Check
17
17
  end
18
18
  end
19
19
  end
20
-
20
+
21
21
  # synchrony check, for manually calls
22
22
  # do not call in thin!!!
23
23
  def self.check(params = {})
24
24
  result = nil
25
-
25
+
26
26
  EM.run do
27
27
  inst = self.new(params) do |res|
28
28
  begin
29
29
  result = res
30
30
  ensure
31
- EM.stop
31
+ EM.stop
32
32
  end
33
33
  end
34
34
 
35
35
  inst.run
36
36
  end
37
-
37
+
38
38
  result
39
- end
39
+ end
40
40
 
41
41
  end
@@ -4,14 +4,14 @@ class Nagios::Runner
4
4
  @params = params.with_indifferent_access
5
5
  @callback = callback
6
6
  @method = @params.delete(:method).to_s
7
- @method = @method.gsub(/[^_\.\-a-z0-9]/i, '')
7
+ @method = @method.gsub(/[^:_\.\-a-z0-9]/i, '')
8
8
  @klass_name = "Nagios::#{@method.camelize}"
9
-
9
+
10
10
  raise "method should be" if @method.blank?
11
11
 
12
- Nagios.mutex.synchronize{ load_initializers }
12
+ Nagios.load_initializers
13
13
  load_class
14
-
14
+
15
15
  run
16
16
  rescue Exception, Timeout::Error => ex
17
17
  Nagios.logger.info "T= #{params.inspect} #{ex.message} (#{ex.backtrace.inspect})"
@@ -21,47 +21,40 @@ class Nagios::Runner
21
21
  # synchrony check, for manual call
22
22
  def self.check(params = {})
23
23
  result = nil
24
-
24
+
25
25
  self.new(params) do |res|
26
26
  result = res
27
27
  end
28
-
28
+
29
29
  result
30
30
  end
31
31
 
32
- protected
32
+ protected
33
33
 
34
34
  def constantize
35
- @klass_name.constantize
36
- rescue LoadError, NameError
37
- nil
38
- end
39
-
40
- def load_initializers
41
- unless Nagios.project_initializer_loaded
42
- Dir[Nagios.rails_root + "/app/nagios/initializers/*.rb"].each do |file|
43
- require File.expand_path(file)
44
- end
45
-
46
- Nagios.project_initializer_loaded = true
35
+ c = @klass_name.constantize
36
+ if c.ancestors.detect { |an| an == Nagios::Check || an == Nagios::CheckEM }
37
+ c
47
38
  end
39
+ rescue LoadError, NameError
40
+ nil
48
41
  end
49
42
 
50
43
  def load_class
51
44
  klass = constantize
52
-
53
- unless klass
54
- Dir[Nagios.rails_root + "/app/nagios/**/#{@method}.rb"].each do |file|
45
+
46
+ unless klass
47
+ Dir[Nagios.root + "/**/#{@method}.rb"].each do |file|
55
48
  require File.expand_path(file)
56
49
  end
57
-
50
+
58
51
  klass = constantize
59
52
  end
60
-
53
+
61
54
  raise "unknown klass #{@klass_name}" unless klass
62
-
55
+
63
56
  @klass = klass
64
- @ancestor = klass.ancestors.detect{|an| an == Nagios::Check || an == Nagios::CheckEM }
57
+ @ancestor = klass.ancestors.detect { |an| an == Nagios::Check || an == Nagios::CheckEM }
65
58
  end
66
59
 
67
60
  def run
@@ -70,9 +63,9 @@ protected
70
63
  script.run
71
64
  elsif @ancestor == Nagios::CheckEM
72
65
  raise "cant run EM check in Sync Runner"
73
- else
66
+ else
74
67
  raise "unknown klass #{@klass.inspect}"
75
68
  end
76
69
  end
77
-
70
+
78
71
  end
@@ -6,22 +6,22 @@ class Nagios::RunnerAsync < Nagios::Runner
6
6
  # do not run in EM
7
7
  def self.check(params = {})
8
8
  raise "cant check sync in running EM" if EM.reactor_running?
9
-
10
- result = nil
9
+
10
+ result = nil
11
11
  EM.run do
12
12
  self.new(params) do |res|
13
13
  begin
14
14
  result = res
15
15
  ensure
16
16
  EM.stop
17
- end
17
+ end
18
18
  end
19
19
  end
20
-
20
+
21
21
  result
22
22
  end
23
23
 
24
- protected
24
+ protected
25
25
 
26
26
  def run
27
27
  if @ancestor == Nagios::Check
@@ -30,14 +30,14 @@ protected
30
30
  script = @klass.new(@params, &@callback)
31
31
  script.run
32
32
  end
33
-
33
+
34
34
  elsif @ancestor == Nagios::CheckEM
35
35
  script = @klass.new(@params, &@callback)
36
36
  script.run
37
-
38
- else
39
- raise "unknown klass #{klass.inspect}"
37
+
38
+ else
39
+ raise "unknown klass #{@klass_name}"
40
40
  end
41
41
  end
42
-
42
+
43
43
  end
@@ -0,0 +1,15 @@
1
+ if defined?(RAILS_ENV) && RAILS_ENV['test']
2
+
3
+ Nagios.load_initializers
4
+
5
+ Dir[Nagios.root + "/**/*.rb"].each do |file|
6
+ require file
7
+ end
8
+
9
+ module EM
10
+ def self.defer
11
+ yield
12
+ end
13
+ end
14
+
15
+ end
data/lib/nagios.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'active_support'
2
+ require 'active_support/inflector'
3
+ begin
4
+ require 'active_support/core_ext/object/blank'
5
+ rescue LoadError
6
+ end
7
+ require 'active_support/core_ext/hash/indifferent_access'
8
+ require 'logger'
9
+ require 'timeout'
10
+
11
+ module Nagios
12
+ OK = 0
13
+ WARN = 1
14
+ CRIT = 2
15
+ OTHER = 3
16
+
17
+ CONCURRENCY_LEVEL = 25 # for server
18
+
19
+ STATUS_NAMES = {
20
+ OK => 'OK',
21
+ WARN => 'WARN',
22
+ CRIT => 'CRIT',
23
+ OTHER => 'OTHER'
24
+ }
25
+
26
+ class << self
27
+ def logger
28
+ @logger ||= Logger.new(File.join(rails_root, "/log/nagios.log")).tap do |logger|
29
+ logger.formatter = lambda { |s, d, p, m| "#{d.strftime("%d.%m.%Y %H:%M:%S")} #{m}\n" }
30
+ end
31
+ end
32
+
33
+ attr_accessor :project_initializer_loaded
34
+
35
+ def concurrency_level
36
+ @concurrency_level
37
+ end
38
+
39
+ def mutex
40
+ @mutex ||= Mutex.new
41
+ end
42
+
43
+ def concurrency_level=(cl)
44
+ @concurrency_level = cl
45
+ EM.threadpool_size = cl
46
+ end
47
+
48
+ def status_name(status)
49
+ ":#{ STATUS_NAMES[status] || 'unknown' }"
50
+ end
51
+
52
+ def rails_root
53
+ @rails_root ||= defined?(RAILS_ROOT) ? RAILS_ROOT : (defined?(Rails) ? Rails.root : nil)
54
+ end
55
+
56
+ def rails_root=(rails_root)
57
+ @rails_root = rails_root
58
+ end
59
+
60
+ def root
61
+ @root ||= File.join(rails_root, %w{ app nagios })
62
+ end
63
+
64
+ def root=(root)
65
+ @root = root
66
+ end
67
+
68
+ def load_initializers
69
+ mutex.lock
70
+
71
+ unless project_initializer_loaded
72
+ Dir[root + "/initializers/*.rb"].each do |file|
73
+ require File.expand_path(file)
74
+ end
75
+
76
+ project_initializer_loaded = true
77
+ end
78
+
79
+ ensure
80
+ mutex.unlock
81
+ end
82
+
83
+ def url(method)
84
+ "http://localhost:3000/nagios/check?method=#{method}"
85
+ end
86
+ end
87
+
88
+ autoload :Check, 'nagios/check'
89
+ autoload :CheckEM, 'nagios/check_em'
90
+ autoload :Runner, 'nagios/runner'
91
+ autoload :RunnerAsync, 'nagios/runner_async'
92
+ end
@@ -3,12 +3,12 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = %q{nagios_helper}
6
- s.version = "0.1.2"
6
+ s.version = "0.2"
7
7
 
8
8
  s.authors = ["Makarchev Konstantin"]
9
-
10
- s.description = %q{Gem for writing, testing, executing nagios checks inside Rails application. Checks running throught http or binary(nrpe).}
11
- s.summary = %q{Gem for writing, testing, executing nagios checks inside Rails application. Checks running throught http or binary(nrpe).}
9
+
10
+ s.description = %q{Gem for writing, testing, executing nagios checks inside Rails application. Checks running throught http or script.}
11
+ s.summary = %q{Gem for writing, testing, executing nagios checks inside Rails application. Checks running throught http or script.}
12
12
 
13
13
  s.email = %q{kostya27@gmail.com}
14
14
  s.homepage = %q{http://github.com/kostya/nagios_helper}
@@ -17,11 +17,12 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
+ s.license = "MIT"
20
21
 
21
22
  s.add_dependency 'activesupport'
22
23
  s.add_dependency 'eventmachine'
23
-
24
+
24
25
  s.add_development_dependency "rspec"
25
26
  s.add_development_dependency "rake"
26
-
27
+
27
28
  end
data/spec/nagios_spec.rb CHANGED
@@ -6,62 +6,62 @@ describe "Nagios::Runner" do
6
6
  @status.should == Nagios::OK
7
7
  @message.should include('a5')
8
8
  end
9
-
9
+
10
10
  it "run ok method with indefferent access" do
11
11
  @status, @message = Nagios::Runner.check({:method => "bla"})
12
12
  @status.should == Nagios::OK
13
13
  @message.should include('a5')
14
- end
15
-
14
+ end
15
+
16
16
  it "undefined klass" do
17
17
  @status, @message = Nagios::Runner.check({'method' => "blah"})
18
18
  @status.should == Nagios::OTHER
19
19
  @message.should include('Nagios::Blah')
20
20
  end
21
-
21
+
22
22
  it "empty class" do
23
23
  @status, @message = Nagios::Runner.check({})
24
24
  @status.should == Nagios::OTHER
25
25
  @message.should include('method should')
26
26
  end
27
-
27
+
28
28
  describe "check" do
29
29
  it "crit" do
30
30
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'crit'})
31
31
  @status.should == Nagios::CRIT
32
32
  @message.should include('a1')
33
33
  end
34
-
34
+
35
35
  it "warn" do
36
36
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'warn'})
37
37
  @status.should == Nagios::WARN
38
38
  @message.should include('a2')
39
39
  end
40
-
40
+
41
41
  it "raise" do
42
42
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'raise'})
43
43
  @status.should == Nagios::OTHER
44
44
  @message.should include('a3')
45
- end
46
-
45
+ end
46
+
47
47
  it "other" do
48
48
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'other'})
49
49
  @status.should == Nagios::OTHER
50
50
  @message.should include('a4')
51
51
  end
52
-
52
+
53
53
  it "ok" do
54
54
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'ok'})
55
55
  @status.should == Nagios::OK
56
56
  @message.should include('a5')
57
57
  end
58
-
58
+
59
59
  it "crit_warn" do
60
60
  @status, @message = Nagios::Runner.check({'method' => "bla", 's' => 'crit_warn'})
61
61
  @status.should == Nagios::CRIT
62
62
  @message.should include('a1')
63
63
  @message.should include('a2')
64
- end
64
+ end
65
65
  end
66
66
 
67
67
  describe "check_em" do
@@ -70,37 +70,88 @@ describe "Nagios::Runner" do
70
70
  @status.should == Nagios::CRIT
71
71
  @message.should include('b1')
72
72
  end
73
-
73
+
74
74
  it "warn" do
75
75
  @status, @message = Nagios::RunnerAsync.check({'method' => "bla_em", 's' => 'warn'})
76
76
  @status.should == Nagios::WARN
77
77
  @message.should include('b2')
78
78
  end
79
-
79
+
80
80
  it "raise" do
81
81
  @status, @message = Nagios::RunnerAsync.check({'method' => "bla_em", 's' => 'raise'})
82
82
  @status.should == Nagios::OTHER
83
83
  @message.should include('b3')
84
- end
85
-
84
+ end
85
+
86
86
  it "other" do
87
87
  @status, @message = Nagios::RunnerAsync.check({'method' => "bla_em", 's' => 'other'})
88
88
  @status.should == Nagios::OTHER
89
89
  @message.should include('b4')
90
90
  end
91
-
91
+
92
92
  it "ok" do
93
93
  @status, @message = Nagios::RunnerAsync.check({'method' => "bla_em", 's' => 'ok'})
94
94
  @status.should == Nagios::OK
95
95
  @message.should include('b5')
96
96
  end
97
-
97
+
98
98
  it "crit_warn" do
99
99
  @status, @message = Nagios::RunnerAsync.check({'method' => "bla_em", 's' => 'crit_warn'})
100
100
  @status.should == Nagios::CRIT
101
101
  @message.should include('b1')
102
102
  @message.should include('b2')
103
- end
103
+ end
104
104
  end
105
105
 
106
- end
106
+ describe "Nagios::BlockObj" do
107
+ it "should be crit" do
108
+ st, mes = Nagios::BlockObj.new(:s => '1').check
109
+ st.should == Nagios::OK
110
+ mes.should == '1'
111
+ end
112
+
113
+ it "should be crit" do
114
+ st, mes = Nagios::BlockObj.new(:s => '2').check
115
+ st.should == Nagios::CRIT
116
+ mes.should == '2'
117
+ end
118
+ end
119
+
120
+ it "message_prefix" do
121
+ st, mes = Nagios::Prefix.new(:s => '1').check
122
+ st.should == Nagios::CRIT
123
+ mes.should == 'some 1'
124
+ end
125
+
126
+ it "message_prefix" do
127
+ st, mes = Nagios::Prefix.new(:s => '2').check
128
+ st.should == Nagios::OK
129
+ mes.should == 'some 2'
130
+ end
131
+
132
+ describe "tresholds" do
133
+ it "ok" do
134
+ st, mes = Nagios::Tresh.new(:s => '2', 'c' => '10').check
135
+ st.should == Nagios::OK
136
+ mes.should == 'msg 2'
137
+ end
138
+
139
+ it "warn" do
140
+ st, mes = Nagios::Tresh.new(:s => '7', 'c' => '10').check
141
+ st.should == Nagios::WARN
142
+ mes.should == 'msg 7'
143
+ end
144
+
145
+ it "crit" do
146
+ st, mes = Nagios::Tresh.new(:s => '15', 'c' => '10').check
147
+ st.should == Nagios::CRIT
148
+ mes.should == 'msg 15'
149
+ end
150
+
151
+ it "crit undefined should be warn" do
152
+ st, mes = Nagios::Tresh.new(:s => '15').check
153
+ st.should == Nagios::WARN
154
+ mes.should == 'msg 15'
155
+ end
156
+ end
157
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  class Nagios::Bla < Nagios::Check
4
4
  params :s
5
-
5
+
6
6
  def execute
7
7
  crit 'a1' if s == 'crit' || s == 'crit_warn'
8
8
  warn 'a2' if s == 'warn' || s == 'crit_warn'
@@ -10,7 +10,7 @@ class Nagios::Bla < Nagios::Check
10
10
  other 'a4' if s == 'other'
11
11
  ok 'a5'
12
12
  end
13
-
13
+
14
14
  end
15
15
 
16
16
  class Nagios::BlaEm < Nagios::CheckEM
@@ -27,4 +27,43 @@ class Nagios::BlaEm < Nagios::CheckEM
27
27
  send_result
28
28
  end
29
29
 
30
- end
30
+ end
31
+
32
+
33
+ # block parameter
34
+ class Nagios::BlockObj < Nagios::Check
35
+ params :s
36
+
37
+ def execute
38
+ crit("2") { s == '2' }
39
+ ok "1"
40
+ end
41
+ end
42
+
43
+ class Nagios::Prefix < Nagios::Check
44
+ params :s
45
+
46
+ def message_prefix
47
+ 'some '
48
+ end
49
+
50
+ def execute
51
+ s == '1' ? crit('1') : ok('2')
52
+ end
53
+ end
54
+
55
+ class Nagios::Tresh < Nagios::Check
56
+ params :s, :c
57
+
58
+ def some_m
59
+ s && s.to_i
60
+ end
61
+
62
+ def criti
63
+ c && c.to_i
64
+ end
65
+
66
+ def execute
67
+ tresholds(:some_m, 5, criti) { |x| "msg #{x}" }
68
+ end
69
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,7 @@ Bundler.setup
4
4
  ENV['RAILS_ENV'] ||= 'test'
5
5
 
6
6
  $:.unshift(File.dirname(__FILE__) + '/../lib')
7
- require 'nagios_helper'
7
+ require 'nagios'
8
8
 
9
9
  require 'nagios_support'
10
10
 
metadata CHANGED
@@ -1,85 +1,76 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: '0.2'
6
5
  platform: ruby
7
6
  authors:
8
7
  - Makarchev Konstantin
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-23 00:00:00.000000000 Z
11
+ date: 2013-10-26 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: eventmachine
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: Gem for writing, testing, executing nagios checks inside Rails application.
79
- Checks running throught http or binary(nrpe).
70
+ Checks running throught http or script.
80
71
  email: kostya27@gmail.com
81
72
  executables:
82
- - nagios_check
73
+ - nagios
83
74
  extensions: []
84
75
  extra_rdoc_files: []
85
76
  files:
@@ -88,7 +79,7 @@ files:
88
79
  - LICENSE
89
80
  - README.md
90
81
  - Rakefile
91
- - bin/nagios_check
82
+ - bin/nagios
92
83
  - lib/generators/nagios/check_em_generator.rb
93
84
  - lib/generators/nagios/check_generator.rb
94
85
  - lib/generators/nagios/nagios_em_generator.rb
@@ -96,46 +87,40 @@ files:
96
87
  - lib/generators/nagios/templates/script_class.rb
97
88
  - lib/generators/nagios/templates/script_class_em.rb
98
89
  - lib/generators/nagios/templates/spec.rb
99
- - lib/nagios_helper.rb
100
- - lib/nagios_helper/boot.rb
101
- - lib/nagios_helper/check.rb
102
- - lib/nagios_helper/check_em.rb
103
- - lib/nagios_helper/runner.rb
104
- - lib/nagios_helper/runner_async.rb
105
- - lib/nagios_helper/spec_helper.rb
90
+ - lib/nagios.rb
91
+ - lib/nagios/boot.rb
92
+ - lib/nagios/check.rb
93
+ - lib/nagios/check_em.rb
94
+ - lib/nagios/runner.rb
95
+ - lib/nagios/runner_async.rb
96
+ - lib/nagios/spec_helper.rb
106
97
  - nagios_helper.gemspec
107
98
  - spec/nagios_spec.rb
108
99
  - spec/nagios_support.rb
109
100
  - spec/spec_helper.rb
110
101
  homepage: http://github.com/kostya/nagios_helper
111
- licenses: []
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
112
105
  post_install_message:
113
106
  rdoc_options: []
114
107
  require_paths:
115
108
  - lib
116
109
  required_ruby_version: !ruby/object:Gem::Requirement
117
- none: false
118
110
  requirements:
119
- - - ! '>='
111
+ - - '>='
120
112
  - !ruby/object:Gem::Version
121
113
  version: '0'
122
- segments:
123
- - 0
124
- hash: -299628239
125
114
  required_rubygems_version: !ruby/object:Gem::Requirement
126
- none: false
127
115
  requirements:
128
- - - ! '>='
116
+ - - '>='
129
117
  - !ruby/object:Gem::Version
130
118
  version: '0'
131
- segments:
132
- - 0
133
- hash: -299628239
134
119
  requirements: []
135
120
  rubyforge_project:
136
- rubygems_version: 1.8.24
121
+ rubygems_version: 2.1.4
137
122
  signing_key:
138
- specification_version: 3
123
+ specification_version: 4
139
124
  summary: Gem for writing, testing, executing nagios checks inside Rails application.
140
- Checks running throught http or binary(nrpe).
125
+ Checks running throught http or script.
141
126
  test_files: []
@@ -1,9 +0,0 @@
1
- if defined?(RAILS_ENV) && RAILS_ENV['test']
2
-
3
- module EM
4
- def self.defer
5
- yield
6
- end
7
- end
8
-
9
- end
data/lib/nagios_helper.rb DELETED
@@ -1,61 +0,0 @@
1
- require 'active_support'
2
- require 'active_support/inflector'
3
- begin
4
- require 'active_support/core_ext/object/blank'
5
- rescue LoadError
6
- end
7
- require 'active_support/core_ext/hash/indifferent_access'
8
- require 'logger'
9
- require 'timeout'
10
-
11
- module Nagios
12
- OK = 0
13
- WARN = 1
14
- CRIT = 2
15
- OTHER = 3
16
-
17
- CONCURRENCY_LEVEL = 100 # for server
18
-
19
- STATUS_NAMES = {
20
- OK => 'ok',
21
- WARN => 'warn',
22
- CRIT => 'crit',
23
- OTHER => 'other'
24
- }
25
-
26
- class << self
27
- def logger
28
- @logger ||= Logger.new(File.join(rails_root, "/log/nagios.log")).tap do |logger|
29
- logger.formatter = lambda { |s, d, p, m| "#{d.strftime("%d.%m.%Y %H:%M:%S")} #{m}\n" }
30
- end
31
- end
32
-
33
- attr_accessor :project_initializer_loaded
34
-
35
- def concurrency_level
36
- @concurrency_level
37
- end
38
-
39
- def mutex
40
- @mutex ||= Mutex.new
41
- end
42
-
43
- def concurrency_level=(cl)
44
- @concurrency_level = cl
45
- EM.threadpool_size = cl
46
- end
47
-
48
- def status_name(status)
49
- ":#{ STATUS_NAMES[status] || 'unknown' }"
50
- end
51
-
52
- def rails_root
53
- defined?(RAILS_ROOT) ? RAILS_ROOT : (defined?(Rails) ? Rails.root : nil)
54
- end
55
- end
56
-
57
- autoload :Check, 'nagios_helper/check'
58
- autoload :CheckEM, 'nagios_helper/check_em'
59
- autoload :Runner, 'nagios_helper/runner'
60
- autoload :RunnerAsync, 'nagios_helper/runner_async'
61
- end