nagios_analyzer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  .bundle
3
3
  .rvmrc
4
+ Gemfile.lock
4
5
  pkg/*
5
6
  tmp/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jean-Baptiste Barth <jeanbaptiste.barth@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile CHANGED
@@ -41,6 +41,9 @@ pp status.host_problems
41
41
  # and can be accessed as a Hash
42
42
  pp status.items.first[:current_state]
43
43
 
44
+ # or directly
45
+ pp status.items.first.current_state
46
+
44
47
  # get all sections, even those where status is OK
45
48
  status = NagiosAnalyzer::Status.new("/path/to/status.dat", :include_ok => true)
46
49
 
@@ -55,3 +58,7 @@ status.scopes << lambda{|s| s.start_with?("hoststatus") } #only host statuses
55
58
  # reset cached results (if you changed scopes!)
56
59
  status.reset_cache!
57
60
  </pre>
61
+
62
+ h2. License
63
+
64
+ This gem is distributed under the MIT license, see LICENSE file for more details.
@@ -5,7 +5,12 @@ module NagiosAnalyzer
5
5
  end
6
6
 
7
7
  def method_missing(method, *args)
8
- hash.send(method, *args)
8
+ begin
9
+ hash.send(method, *args)
10
+ rescue NoMethodError => e
11
+ raise e if args.size > 0
12
+ hash[method]
13
+ end
9
14
  end
10
15
 
11
16
  def hash
@@ -13,10 +18,16 @@ module NagiosAnalyzer
13
18
  @hash = {}
14
19
  @section.each_line do |line|
15
20
  line.strip!
16
- if line.match(/(\S+) \{/)
21
+ if line.match(/^\s*([a-zA-Z0-9]*)\s*\{/)
17
22
  @hash[:type] = $1
18
23
  elsif line.match(/(\S+)=(.*)/) #more efficient than include?+split+join..
19
- @hash[$1.to_sym] = ($2 == "#{$2.to_i}" ? $2.to_i : $2)
24
+ property, value = ["#{$1}", "#{$2}"]
25
+ @hash[property.to_sym] =
26
+ case
27
+ when value.strip =~ /^[0-9]+$/ then value.to_i
28
+ when value.strip =~ /^[0-9.]+$/ then value.to_f
29
+ else value
30
+ end
20
31
  end
21
32
  end
22
33
  if @hash[:type] == "servicestatus"
@@ -17,7 +17,7 @@ module NagiosAnalyzer
17
17
  4 => 3,
18
18
  0 => 4
19
19
  }
20
-
20
+
21
21
  def initialize(statusfile, options = {})
22
22
  @file = statusfile
23
23
  sections #loads section at this point so we raise immediatly if file has a item
@@ -27,39 +27,67 @@ module NagiosAnalyzer
27
27
  @scopes << lambda { |section| !section.include?("current_state=#{STATE_OK}") } unless options[:include_ok]
28
28
  @scopes << options[:scope] if options[:scope].is_a?(Proc)
29
29
  end
30
-
30
+
31
31
  def sections
32
32
  # don't try to instanciate each section ! on my conf (85hosts/700services),
33
33
  # it makes the script more 10 times slower (0.25s => >3s)
34
- @sections ||= File.read(@file).split("\n\n")
34
+ @sections ||= File.read(@file).split(/\}\s*\n/).map(&:strip)
35
35
  end
36
36
 
37
37
  def host_items
38
- @host_items ||= sections.map do |s|
39
- Section.new(s) if s =~ /^hoststatus/ && in_scope?(s)
40
- end.compact
38
+ section_items(:hoststatus)
41
39
  end
42
40
 
43
41
  def service_items
44
- @service_items ||= sections.map do |s|
45
- Section.new(s) if s =~ /^servicestatus/ && in_scope?(s)
46
- end.compact
42
+ section_items(:servicestatus)
47
43
  end
48
44
 
49
45
  def items
50
46
  @items ||= (host_items + service_items)
51
47
  end
52
48
 
49
+ def contactstatus_items
50
+ section_items(:contactstatus)
51
+ end
52
+
53
+ def hostcomment_items
54
+ section_items(:hostcomment)
55
+ end
56
+
57
+ def hostdowntime_items
58
+ section_items(:hostdowntime)
59
+ end
60
+
61
+ def hoststatus_items
62
+ section_items(:hoststatus)
63
+ end
64
+
65
+ def info_items
66
+ section_items(:info)
67
+ end
68
+
69
+ def programstatus_items
70
+ section_items(:programstatus)
71
+ end
72
+
73
+ def servicecomment_items
74
+ section_items(:servicecomment)
75
+ end
76
+
77
+ def servicedowntime_items
78
+ section_items(:servicedowntime)
79
+ end
80
+
81
+ def servicestatus_items
82
+ section_items(:servicestatus)
83
+ end
84
+
53
85
  def host_problems
54
- @host_problems ||= sections.map do |s|
55
- Section.new(s) if s =~ /^hoststatus/ && in_scope?(s) && problem?(s)
56
- end.compact
86
+ section_items(:hoststatus, true)
57
87
  end
58
88
 
59
89
  def service_problems
60
- @service_problems ||= sections.map do |s|
61
- Section.new(s) if s =~ /^servicestatus/ && in_scope?(s) && problem?(s)
62
- end.compact
90
+ section_items(:servicestatus, true)
63
91
  end
64
92
 
65
93
  def in_scope?(section)
@@ -73,7 +101,42 @@ module NagiosAnalyzer
73
101
  end
74
102
 
75
103
  def reset_cache!
76
- @items = @service_items = @host_items = nil
104
+ @items = nil
105
+ [
106
+ :contactstatus,
107
+ :hostcomment,
108
+ :hostdowntime,
109
+ :hoststatus,
110
+ :info,
111
+ :programstatus,
112
+ :servicecomment,
113
+ :servicedowntime,
114
+ :servicestatus,
115
+ ].each do |name|
116
+ self.instance_variable_set(section_var_sym(name, true), nil)
117
+ self.instance_variable_set(section_var_sym(name, false), nil)
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def section_items(name, filter_problems = false)
124
+ var_sym = section_var_sym(name, filter_problems)
125
+ if !self.instance_variable_get(var_sym)
126
+ map = sections.map do |s|
127
+ Section.new(s) if s =~ regexp_for_section(name) && in_scope?(s) && (!filter_problems || problem?(s))
128
+ end.compact
129
+ self.instance_variable_set(var_sym, map)
130
+ end
131
+ self.instance_variable_get(var_sym)
132
+ end
133
+
134
+ def regexp_for_section(name)
135
+ Regexp.new("^\\s*#{name}\\s*{")
136
+ end
137
+
138
+ def section_var_sym(name, filter_problems)
139
+ "@#{name}_#{filter_problems}".to_sym
77
140
  end
78
141
  end
79
142
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosAnalyzer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,35 @@
1
+ contactstatus {
2
+ id_for_testing=1
3
+ }
4
+
5
+ hostcomment {
6
+ id_for_testing=2
7
+ }
8
+
9
+ hostdowntime {
10
+ id_for_testing=3
11
+ }
12
+
13
+ hoststatus {
14
+ id_for_testing=4
15
+ }
16
+
17
+ info {
18
+ id_for_testing=5
19
+ }
20
+
21
+ programstatus {
22
+ id_for_testing=6
23
+ }
24
+
25
+ servicecomment {
26
+ id_for_testing=7
27
+ }
28
+
29
+ servicedowntime {
30
+ id_for_testing=8
31
+ }
32
+
33
+ servicestatus {
34
+ id_for_testing=9
35
+ }
data/spec/section_spec.rb CHANGED
@@ -34,6 +34,32 @@ describe NA::Section do
34
34
  NA::Section.new("hoststatus {\ncurrent_state=42\n}")[:status].should == "CRITICAL"
35
35
  end
36
36
 
37
+ it "properly parses sections with free text" do
38
+ section = NA::Section.new("somethinghere {\n\tcomment_data=Free Text here. Possibly even with characters like } or = or even {.\n\nhello_prop=789321\n}")
39
+ section.type.should == "somethinghere"
40
+ section.comment_data.should == "Free Text here. Possibly even with characters like } or = or even {."
41
+ section.hello_prop.should == 789321
42
+ end
43
+
44
+ it "properly parses sections with decimal values" do
45
+ section = NA::Section.new("somethinghere {\n\nnumerical_value=3.141529\n}")
46
+ section.type.should == "somethinghere"
47
+ section.numerical_value.should == 3.141529
48
+ end
49
+
50
+ context "direct access" do
51
+ it "allows direct access to properties" do
52
+ section = NA::Section.new("servicestatus {\ncurrent_state=2\n}")
53
+ section.current_state.should == 2
54
+ section.something_else.should be_nil
55
+ end
56
+
57
+ it "properly bubbles a NoMethodError when using inexistant methods" do
58
+ section = NA::Section.new("servicestatus {\ncurrent_state=2\n}")
59
+ lambda { section.weird_inexistent_method(1) }.should raise_error NoMethodError
60
+ end
61
+ end
62
+
37
63
  context "#sort" do
38
64
  it "places servicestatus'es after hoststatus'es" do
39
65
  a = NA::Section.new("servicestatus {\ncurrent_state=0\n}")
data/spec/status_spec.rb CHANGED
@@ -3,84 +3,150 @@ require File.expand_path('../spec_helper',__FILE__)
3
3
  describe NagiosAnalyzer::Status do
4
4
  before(:each) do
5
5
  @file = File.expand_path('../data/status.dat',__FILE__)
6
- @status = NagiosAnalyzer::Status.new(@file, :include_ok => true)
7
6
  end
8
7
 
8
+ let(:status) { NagiosAnalyzer::Status.new(@file, :include_ok => true) }
9
+
9
10
  it "creates a NagiosAnalyzer::Status object" do
10
- @status.should be
11
- @status.sections.should be_a(Array)
12
- @status.sections.first.should include("created=")
13
- @status.should have(6).sections
11
+ status.should be
12
+ status.sections.should be_a(Array)
13
+ status.sections.first.should include("created=")
14
+ status.should have(6).sections
14
15
  end
15
16
 
16
17
  it "provides a last_updated attribute" do
17
- @status.last_updated.should be_a(Time)
18
+ status.last_updated.should be_a(Time)
18
19
  end
19
20
 
20
21
  context "#scopes" do
21
22
  it "provides scopes to filter sections" do
22
- @status.should have(6).sections
23
- @status.should have(0).scopes
23
+ status.should have(6).sections
24
+ status.should have(0).scopes
24
25
  end
25
26
 
26
27
  it "tells if a section is in the scopes" do
27
- @status.scopes << lambda{|s|s.include?("host_name=server-web")}
28
- @status.in_scope?(@status.sections[2]).should be_true
29
- @status.in_scope?(@status.sections[3]).should be_false
28
+ status.scopes << lambda{|s|s.include?("host_name=server-web")}
29
+ status.in_scope?(status.sections[2]).should be_true
30
+ status.in_scope?(status.sections[3]).should be_false
30
31
  end
31
-
32
+
32
33
  it "defines scope in the initialization" do
33
- @status = NagiosAnalyzer::Status.new(@file, :include_ok => true,
34
- :scope => lambda{|s|s.include?("host_name=server-web")})
35
- @status.in_scope?(@status.sections[2]).should be_true
36
- @status.in_scope?(@status.sections[3]).should be_false
34
+ status = NagiosAnalyzer::Status.new(@file, :include_ok => true,
35
+ :scope => lambda{|s|s.include?("host_name=server-web")})
36
+ status.in_scope?(status.sections[2]).should be_true
37
+ status.in_scope?(status.sections[3]).should be_false
37
38
  end
38
39
  end
39
40
 
40
- context "#items, #service_items, #host_items" do
41
+ context "#items, #service_items, #host_items, #hostcomment_items" do
41
42
  it "returns all items" do
42
- @status.should have(6).sections
43
- @status.should have(4).items #don't return info{} and programstatus{} sections
44
- @status.items.first.should be_a(NagiosAnalyzer::Section)
43
+ status.should have(6).sections
44
+ status.should have(4).items #don't return info{} and programstatus{} sections
45
+ status.items.first.should be_a(NagiosAnalyzer::Section)
45
46
  end
46
47
 
47
48
  it "returns host items" do
48
- @status.should have(2).host_items #4 = 2 host_items
49
- @status.host_items.first[:type].should == "hoststatus"
49
+ status.should have(2).host_items #4 = 2 host_items
50
+ status.host_items.first[:type].should == "hoststatus"
50
51
  end
51
52
 
52
53
  it "returns service items" do
53
- @status.should have(2).service_items # ... + 2 service_items
54
- @status.service_items.first[:type].should == "servicestatus"
54
+ status.should have(2).service_items # ... + 2 service_items
55
+ status.service_items.first[:type].should == "servicestatus"
55
56
  end
56
57
 
57
58
  it "returns only service problems, keeping scopes on every items" do
58
- @status.should have(1).service_problems
59
- @status.should have(2).service_items
59
+ status.should have(1).service_problems
60
+ status.should have(2).service_items
60
61
  end
61
62
 
62
63
  it "returns host problems, currently none" do
63
- @status.should have(0).host_problems
64
+ status.should have(0).host_problems
64
65
  end
65
66
 
66
67
  it "resets cached attributes" do
67
- @status.should have(4).items
68
- @status.scopes << lambda{|s| s.start_with?("servicestatus")}
69
- @status.should have(4).items
70
- @status.reset_cache!
71
- @status.should have(2).items
72
- @status.should have(0).host_items
73
- @status.should have(2).service_items
68
+ status.should have(4).items
69
+ status.scopes << lambda{|s| s.start_with?("servicestatus")}
70
+ status.should have(4).items
71
+ status.reset_cache!
72
+ status.should have(2).items
73
+ status.should have(0).host_items
74
+ status.should have(2).service_items
74
75
  end
75
76
  end
76
77
 
77
78
  context "without :include_ok option" do
78
79
  it "should filter items" do
79
- @status = NagiosAnalyzer::Status.new(@file)
80
- @status.should have(1).items #don't return info{} and programstatus{} sections
81
- @status.should have(0).host_items #4 = 2 host_items
82
- @status.should have(1).service_items # ... + 2 service_items
83
- @status.service_items.first.should_not include("current_state=0")
80
+ status = NagiosAnalyzer::Status.new(@file)
81
+ status.should have(1).items #don't return info{} and programstatus{} sections
82
+ status.should have(0).host_items #4 = 2 host_items
83
+ status.should have(1).service_items # ... + 2 service_items
84
+ status.service_items.first.should_not include("current_state=0")
85
+ end
86
+ end
87
+
88
+ context "more status" do
89
+ before(:each) do
90
+ @file = File.expand_path('../data/morestatus.dat',__FILE__)
91
+ end
92
+
93
+ it "sould have lists for all kinds of nagios sections" do
94
+ status.contactstatus_items.should have(1).items
95
+ status.hostcomment_items.should have(1).items
96
+ status.hostdowntime_items.should have(1).items
97
+ status.hoststatus_items.should have(1).items
98
+ status.info_items.should have(1).items
99
+ status.programstatus_items.should have(1).items
100
+ status.servicecomment_items.should have(1).items
101
+ status.servicedowntime_items.should have(1).items
102
+ status.servicestatus_items.should have(1).items
103
+ end
104
+
105
+ it "sould properly classify each kind of nagios sections" do
106
+ status.contactstatus_items.first.id_for_testing.should == 1
107
+ status.hostcomment_items.first.id_for_testing.should == 2
108
+ status.hostdowntime_items.first.id_for_testing.should == 3
109
+ status.hoststatus_items.first.id_for_testing.should == 4
110
+ status.info_items.first.id_for_testing.should == 5
111
+ status.programstatus_items.first.id_for_testing.should == 6
112
+ status.servicecomment_items.first.id_for_testing.should == 7
113
+ status.servicedowntime_items.first.id_for_testing.should == 8
114
+ status.servicestatus_items.first.id_for_testing.should == 9
115
+ end
116
+ end
117
+
118
+ context "parsing sections" do
119
+ it "parses sections when they are appart" do
120
+ status = status_for_data <<-DAT
121
+ hoststatus {
122
+ host_name=abc
123
+ }
124
+
125
+
126
+ hoststatus {
127
+ host_name=abc
128
+ }
129
+ DAT
130
+ status.hoststatus_items.size.should == 2
131
+ end
132
+
133
+ it "parses when sections are close by" do
134
+ status = status_for_data <<-DAT
135
+ hoststatus {
136
+ host_name=abc
137
+ }
138
+ hoststatus {
139
+ host_name=abc
140
+ }
141
+ DAT
142
+ status.hoststatus_items.size.should == 2
143
+ end
144
+
145
+ def status_for_data(data)
146
+ filename = "fixture.dat"
147
+ File.stub(:mtime).with(filename).and_return(Time.now)
148
+ File.stub(:read).with(filename).and_return(data)
149
+ NagiosAnalyzer::Status.new(filename, :include_ok => true)
84
150
  end
85
151
  end
86
152
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000Z
12
+ date: 2013-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &75650760 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *75650760
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: Helps you parse a status.dat file produced by nagios or shinken. It aims
26
31
  at being performant for big status.dat files. Take a look at nagios_parser too if
27
32
  you want, looks very cool too !
@@ -34,7 +39,7 @@ files:
34
39
  - .gitignore
35
40
  - .travis.yml
36
41
  - Gemfile
37
- - Gemfile.lock
42
+ - LICENSE
38
43
  - README.textile
39
44
  - Rakefile
40
45
  - lib/nagios_analyzer.rb
@@ -42,6 +47,7 @@ files:
42
47
  - lib/nagios_analyzer/status.rb
43
48
  - lib/nagios_analyzer/version.rb
44
49
  - nagios_analyzer.gemspec
50
+ - spec/data/morestatus.dat
45
51
  - spec/data/status.dat
46
52
  - spec/section_spec.rb
47
53
  - spec/spec_helper.rb
@@ -60,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
66
  version: '0'
61
67
  segments:
62
68
  - 0
63
- hash: 91061875
69
+ hash: 458145368960384668
64
70
  required_rubygems_version: !ruby/object:Gem::Requirement
65
71
  none: false
66
72
  requirements:
@@ -69,10 +75,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
75
  version: '0'
70
76
  segments:
71
77
  - 0
72
- hash: 91061875
78
+ hash: 458145368960384668
73
79
  requirements: []
74
80
  rubyforge_project: nagios_analyzer
75
- rubygems_version: 1.8.10
81
+ rubygems_version: 1.8.25
76
82
  signing_key:
77
83
  specification_version: 3
78
84
  summary: Parses a nagios/shinken status.dat file
data/Gemfile.lock DELETED
@@ -1,26 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- nagios_analyzer (0.0.3)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- diff-lcs (1.1.2)
10
- rake (0.8.7)
11
- rspec (2.4.0)
12
- rspec-core (~> 2.4.0)
13
- rspec-expectations (~> 2.4.0)
14
- rspec-mocks (~> 2.4.0)
15
- rspec-core (2.4.0)
16
- rspec-expectations (2.4.0)
17
- diff-lcs (~> 1.1.2)
18
- rspec-mocks (2.4.0)
19
-
20
- PLATFORMS
21
- ruby
22
-
23
- DEPENDENCIES
24
- nagios_analyzer!
25
- rake
26
- rspec