nagios_analyzer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,5 @@
1
1
  *.gem
2
2
  .bundle
3
- Gemfile.lock
3
+ .rvmrc
4
4
  pkg/*
5
5
  tmp/*
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nagios_analyzer (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.4.0)
11
+ rspec-core (~> 2.4.0)
12
+ rspec-expectations (~> 2.4.0)
13
+ rspec-mocks (~> 2.4.0)
14
+ rspec-core (2.4.0)
15
+ rspec-expectations (2.4.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.4.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ nagios_analyzer!
24
+ rspec
@@ -31,8 +31,13 @@ pp status.host_items
31
31
  # all items ?
32
32
  pp status.items
33
33
 
34
- # in fact, each items is a hash
35
- pp status.items.first
34
+ # only problems, services or hosts
35
+ pp status.service_problems
36
+ pp status.host_problems
37
+
38
+ # in fact, each item contains the section
39
+ # and can be accessed as a Hash
40
+ pp status.items.first[:current_state]
36
41
 
37
42
  # get all sections, even those where status is OK
38
43
  status = NagiosAnalyzer::Status.new("/path/to/status.dat", :include_ok => true)
@@ -1,20 +1,30 @@
1
1
  module NagiosAnalyzer
2
- class Section < ::Hash
2
+ class Section
3
3
  def initialize(section)
4
- section.strip!
5
- section.each_line do |line|
4
+ @section = section.strip
5
+ end
6
+
7
+ def method_missing(method, *args)
8
+ hash.send(method, *args)
9
+ end
10
+
11
+ def hash
12
+ return @hash if @hash
13
+ @hash = {}
14
+ @section.each_line do |line|
6
15
  line.strip!
7
16
  if line.match(/(\S+) \{/)
8
- self[:type] = $1
17
+ @hash[:type] = $1
9
18
  elsif line.match(/(\S+)=(.*)/) #more efficient than include?+split+join..
10
- self[$1.to_sym] = ($2 == "#{$2.to_i}" ? $2.to_i : $2)
19
+ @hash[$1.to_sym] = ($2 == "#{$2.to_i}" ? $2.to_i : $2)
11
20
  end
12
21
  end
13
- if self[:type] == "servicestatus"
14
- self[:status] = Status::STATES[self[:current_state]]
22
+ if @hash[:type] == "servicestatus"
23
+ @hash[:status] = Status::STATES[@hash[:current_state]]
15
24
  else
16
- self[:status] = (self[:current_state] == Status::STATE_OK ? "OK" : "CRITICAL")
25
+ @hash[:status] = (@hash[:current_state] == Status::STATE_OK ? "OK" : "CRITICAL")
17
26
  end
27
+ @hash
18
28
  end
19
29
 
20
30
  def <=>(other)
@@ -37,17 +37,29 @@ module NagiosAnalyzer
37
37
  def host_items
38
38
  @host_items ||= sections.map do |s|
39
39
  Section.new(s) if s.start_with?("hoststatus") && in_scope?(s)
40
- end.compact.sort
40
+ end.compact
41
41
  end
42
42
 
43
43
  def service_items
44
44
  @service_items ||= sections.map do |s|
45
45
  Section.new(s) if s.start_with?("servicestatus") && in_scope?(s)
46
- end.compact.sort
46
+ end.compact
47
47
  end
48
48
 
49
49
  def items
50
- @items ||= (host_items + service_items).sort
50
+ @items ||= (host_items + service_items)
51
+ end
52
+
53
+ def host_problems
54
+ @host_problems ||= sections.map do |s|
55
+ Section.new(s) if s.start_with?("hoststatus") && in_scope?(s) && problem?(s)
56
+ end.compact
57
+ end
58
+
59
+ def service_problems
60
+ @service_problems ||= sections.map do |s|
61
+ Section.new(s) if s.start_with?("servicestatus") && in_scope?(s) && problem?(s)
62
+ end.compact
51
63
  end
52
64
 
53
65
  def in_scope?(section)
@@ -56,6 +68,10 @@ module NagiosAnalyzer
56
68
  end
57
69
  end
58
70
 
71
+ def problem?(section)
72
+ section.match(/current_state=(\d+)/) && $1.to_i != STATE_OK
73
+ end
74
+
59
75
  def reset_cache!
60
76
  @items = @service_items = @host_items = nil
61
77
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosAnalyzer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ["jeanbaptiste.barth@gmail.com"]
11
11
  s.homepage = "http://github.com/jbbarth/nagios_analyzer"
12
12
  s.summary = %q{Parses a nagios/shinken status.dat file}
13
- s.description = File.read(File.expand_path('../README.textile',__FILE__))
13
+ s.description = %q{Helps you parse a status.dat file produced by nagios or shinken. It aims at being performant for big status.dat files. Take a look at nagios_parser too if you want, looks very cool too !}
14
14
 
15
15
  s.rubyforge_project = "nagios_analyzer"
16
16
 
@@ -11,7 +11,6 @@ describe NagiosAnalyzer::Section do
11
11
 
12
12
  it "returns a hash with keys only" do
13
13
  @section.should be_a(Section)
14
- @section.should be_a(Hash)
15
14
  @section.keys.map(&:class).uniq.should == [Symbol]
16
15
  end
17
16
 
@@ -54,6 +54,15 @@ describe NagiosAnalyzer::Status do
54
54
  @status.service_items.first[:type].should == "servicestatus"
55
55
  end
56
56
 
57
+ 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
60
+ end
61
+
62
+ it "returns host problems, currently none" do
63
+ @status.should have(0).host_problems
64
+ end
65
+
57
66
  it "resets cached attributes" do
58
67
  @status.should have(4).items
59
68
  @status.scopes << lambda{|s| s.start_with?("servicestatus")}
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jean-Baptiste Barth
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-28 00:00:00 +01:00
17
+ date: 2011-05-07 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,58 +30,7 @@ dependencies:
30
30
  version: "0"
31
31
  type: :development
32
32
  version_requirements: *id001
33
- description: |
34
- h1. nagios_analyzer
35
-
36
- h2. Description
37
-
38
- nagios_analyzer gem allows you to parse a status.dat file produced by nagios or shinken.
39
-
40
- It's similar to nagios_parser in some way, but has different goals:
41
- * the parser doesn't rely on 3rd party library nor standard parser like 'racc', I want to keep the code very simple to read and maintain ;
42
- * the parser supports defining scopes, which are processed on the raw file for performance concern, ruby objects being instanciated lately when necessary : on my conf (85hosts/700services), spawning a ruby object for each section makes the script at least 10 times slower (0.25s => >3s). Most of the time, you'll only want to access a subset of your services or hosts, so it's ok.
43
-
44
- Since nagios_parser looks very cool too, you should try both and keep the best one for you.
45
-
46
- h2. Installation
47
-
48
- <pre>gem install nagios_analyzer</pre>
49
-
50
- h2. Usage
51
-
52
- <pre>
53
- require 'nagios_analyzer'
54
- require 'pp'
55
-
56
- status = NagiosAnalyzer::Status.new("/path/to/status.dat")
57
-
58
- # get services items
59
- pp status.service_items
60
-
61
- # get host items
62
- pp status.host_items
63
-
64
- # all items ?
65
- pp status.items
66
-
67
- # in fact, each items is a hash
68
- pp status.items.first
69
-
70
- # get all sections, even those where status is OK
71
- status = NagiosAnalyzer::Status.new("/path/to/status.dat", :include_ok => true)
72
-
73
- # define a personal scope (applied to section string, so look at your status.dat!)
74
- not_acknowleged = lambda{|section| section.include?("problem_has_been_acknowledged=0") }
75
- status = NagiosAnalyzer::Status.new("/path/to/status.dat", :scope => not_acknowledged)
76
-
77
- # add more scopes
78
- status.scopes << lambda{|s| s.include?("notifications_enabled=1") } #no notifications
79
- status.scopes << lambda{|s| s.start_with?("hoststatus") } #only host statuses
80
-
81
- # reset cached results (if you changed scopes!)
82
- status.reset_cache!
83
- </pre>
84
-
33
+ description: Helps you parse a status.dat file produced by nagios or shinken. It aims at being performant for big status.dat files. Take a look at nagios_parser too if you want, looks very cool too !
85
34
  email:
86
35
  - jeanbaptiste.barth@gmail.com
87
36
  executables: []
@@ -93,6 +42,7 @@ extra_rdoc_files: []
93
42
  files:
94
43
  - .gitignore
95
44
  - Gemfile
45
+ - Gemfile.lock
96
46
  - README.textile
97
47
  - Rakefile
98
48
  - lib/nagios_analyzer.rb