nagios_parser 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,6 @@ pkg/*
3
3
  *.gem
4
4
  .bundle
5
5
  .rspec
6
+ scripts
7
+ .ruby-version
8
+ .rvmrc
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.8.7
3
4
  - 1.9.2
4
5
  - 1.9.3
5
- - rbx-2.0
6
- - jruby
6
+ - rbx-18mode
7
+ - rbx-19mode
data/CHANGES.md CHANGED
@@ -1,6 +1,10 @@
1
1
  nagios\_parser - ChangeLog
2
2
  ==========================
3
3
 
4
+ # 1.3.0 (2013-03-23)
5
+ * Support multiple values for `cfg_file` and `cfg_dir` options.
6
+ * Loose grammar to support more nagios configuration files. (alisonrosewarne)
7
+
4
8
  # 1.2.2 (2011-10-22)
5
9
  * Unbreak on Ruby 1.9.x.
6
10
 
@@ -1,18 +1,19 @@
1
1
  #
2
2
  # DO NOT MODIFY!!!!
3
- # This file is automatically generated by Racc 1.4.6
3
+ # This file is automatically generated by Racc 1.4.7
4
4
  # from Racc grammer file "".
5
5
  #
6
6
 
7
7
  require 'racc/parser.rb'
8
8
 
9
9
  require 'strscan'
10
+ require 'nagios_parser/result'
10
11
 
11
12
  module NagiosParser
12
13
  module Config
13
14
  class Parser < Racc::Parser
14
15
 
15
- module_eval(<<'...end parser.y/module_eval...', 'parser.y', 19)
16
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 20)
16
17
 
17
18
  def create_token(string)
18
19
  result = []
@@ -45,10 +46,10 @@ def self.parse(string)
45
46
  end
46
47
 
47
48
  def parse(string)
48
- @result = {}
49
+ @result = NagiosParser::Result.new(:multi_value => %w{cfg_file cfg_dir})
49
50
  @token = create_token(string)
50
51
  do_parse
51
- @result
52
+ @result.to_hash
52
53
  end
53
54
 
54
55
  def next_token
@@ -58,25 +59,25 @@ end
58
59
  ##### State transition tables begin ###
59
60
 
60
61
  racc_action_table = [
61
- 5, 1, 1, 4, 7 ]
62
+ 4, 6, 3, 3, 7 ]
62
63
 
63
64
  racc_action_check = [
64
- 2, 0, 2, 1, 5 ]
65
+ 1, 3, 1, 0, 4 ]
65
66
 
66
67
  racc_action_pointer = [
67
- -1, 0, 0, nil, nil, 4, nil, nil ]
68
+ 1, 0, nil, -2, 4, nil, nil, nil ]
68
69
 
69
70
  racc_action_default = [
70
- -4, -4, -4, -1, -3, -4, -2, 8 ]
71
+ -4, -4, -1, -4, -4, -2, -3, 8 ]
71
72
 
72
73
  racc_goto_table = [
73
- 3, 2, 6 ]
74
+ 2, 5, 1 ]
74
75
 
75
76
  racc_goto_check = [
76
- 2, 1, 2 ]
77
+ 2, 2, 1 ]
77
78
 
78
79
  racc_goto_pointer = [
79
- nil, 1, 0 ]
80
+ nil, 2, 0 ]
80
81
 
81
82
  racc_goto_default = [
82
83
  nil, nil, nil ]
@@ -14,6 +14,7 @@ end
14
14
 
15
15
  ---- header
16
16
  require 'strscan'
17
+ require 'nagios_parser/result'
17
18
 
18
19
  ---- inner
19
20
 
@@ -48,10 +49,10 @@ def self.parse(string)
48
49
  end
49
50
 
50
51
  def parse(string)
51
- @result = {}
52
+ @result = NagiosParser::Result.new(:multi_value => %w{cfg_file cfg_dir})
52
53
  @token = create_token(string)
53
54
  do_parse
54
- @result
55
+ @result.to_hash
55
56
  end
56
57
 
57
58
  def next_token
@@ -1,6 +1,6 @@
1
1
  #
2
2
  # DO NOT MODIFY!!!!
3
- # This file is automatically generated by Racc 1.4.6
3
+ # This file is automatically generated by Racc 1.4.9
4
4
  # from Racc grammer file "".
5
5
  #
6
6
 
@@ -12,7 +12,7 @@ module NagiosParser
12
12
  module Object
13
13
  class Parser < Racc::Parser
14
14
 
15
- module_eval(<<'...end parser.y/module_eval...', 'parser.y', 34)
15
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 35)
16
16
 
17
17
  private
18
18
  def last_is_key?(list)
@@ -23,6 +23,7 @@ public
23
23
  def create_token(string)
24
24
  result = []
25
25
  inside = false
26
+ inside_squigglies = false
26
27
  scanner = StringScanner.new(string)
27
28
 
28
29
  until scanner.empty?
@@ -40,16 +41,18 @@ def create_token(string)
40
41
  result << [:DEFINE, nil]
41
42
  when (!inside and match = scanner.scan(/\w+/))
42
43
  result << [:TYPE, match]
43
- when match = scanner.scan(/\{/)
44
+ when (!inside and match = scanner.scan(/\{/))
44
45
  inside = true
45
46
  result << [:OPEN, nil]
46
- when match = scanner.scan(/\}/)
47
+ when (match = scanner.scan(/\}/))
47
48
  inside = false
48
49
  result << [:CLOSE, nil]
49
50
  when (!last_is_key?(result) and match = scanner.scan(/\w+/))
50
51
  result << [:KEY, match.chomp.gsub(/\s+$/, '')]
51
52
  when (inside and match = scanner.scan(/\d+$/))
52
53
  result << [:VALUE, match.to_i]
54
+ when (inside and match = scanner.scan(/.*\{.+\}.*/))
55
+ result << [:VALUE, match.gsub(/\s+$/, '')]
53
56
  when (inside and match = scanner.scan(/[^\n\}]+/))
54
57
  # Make sure to ignore inline comments starting with ';'.
55
58
  result << [:VALUE, match.gsub(/\s+$/, '')]
@@ -81,27 +84,27 @@ end
81
84
  ##### State transition tables begin ###
82
85
 
83
86
  racc_action_table = [
84
- 5, 8, 1, 13, 11, 7, 1, 11, 4, 14 ]
87
+ 4, 8, 3, 12, 11, 7, 6, 11, 3, 14 ]
85
88
 
86
89
  racc_action_check = [
87
- 2, 5, 2, 9, 9, 4, 0, 7, 1, 11 ]
90
+ 1, 6, 1, 9, 9, 4, 3, 8, 0, 11 ]
88
91
 
89
92
  racc_action_pointer = [
90
- 4, 5, 0, nil, 1, 1, nil, 1, nil, -2,
93
+ 6, 0, nil, 3, 5, nil, -3, nil, 1, -2,
91
94
  nil, 2, nil, nil, nil ]
92
95
 
93
96
  racc_action_default = [
94
- -7, -7, -7, -1, -7, -7, -2, -7, 15, -7,
95
- -4, -7, -5, -3, -6 ]
97
+ -8, -8, -1, -8, -8, -2, -8, 15, -8, -8,
98
+ -4, -7, -3, -5, -6 ]
96
99
 
97
100
  racc_goto_table = [
98
- 10, 3, 12, 6, 9, 2 ]
101
+ 10, 13, 2, 5, 9, 1 ]
99
102
 
100
103
  racc_goto_check = [
101
- 4, 2, 4, 2, 3, 1 ]
104
+ 4, 4, 2, 2, 3, 1 ]
102
105
 
103
106
  racc_goto_pointer = [
104
- nil, 5, 1, -3, -7 ]
107
+ nil, 5, 2, -4, -8 ]
105
108
 
106
109
  racc_goto_default = [
107
110
  nil, nil, nil, nil, nil ]
@@ -113,9 +116,10 @@ racc_reduce_table = [
113
116
  5, 10, :_reduce_3,
114
117
  1, 11, :_reduce_none,
115
118
  2, 11, :_reduce_5,
116
- 2, 12, :_reduce_6 ]
119
+ 2, 12, :_reduce_6,
120
+ 1, 12, :_reduce_7 ]
117
121
 
118
- racc_reduce_n = 7
122
+ racc_reduce_n = 8
119
123
 
120
124
  racc_shift_n = 15
121
125
 
@@ -203,6 +207,13 @@ module_eval(<<'.,.,', 'parser.y', 25)
203
207
  end
204
208
  .,.,
205
209
 
210
+ module_eval(<<'.,.,', 'parser.y', 26)
211
+ def _reduce_7(val, _values, result)
212
+ result = {val[0] => nil }
213
+ result
214
+ end
215
+ .,.,
216
+
206
217
  def _reduce_none(val, _values, result)
207
218
  val[0]
208
219
  end
@@ -24,6 +24,7 @@ class NagiosParser::Object::Parser
24
24
  ;
25
25
  assignment
26
26
  : KEY VALUE { result = {val[0] => val[1]} }
27
+ | KEY { result = {val[0] => nil } }
27
28
  ;
28
29
  end
29
30
 
@@ -41,6 +42,7 @@ public
41
42
  def create_token(string)
42
43
  result = []
43
44
  inside = false
45
+ inside_squigglies = false
44
46
  scanner = StringScanner.new(string)
45
47
 
46
48
  until scanner.empty?
@@ -58,16 +60,18 @@ def create_token(string)
58
60
  result << [:DEFINE, nil]
59
61
  when (!inside and match = scanner.scan(/\w+/))
60
62
  result << [:TYPE, match]
61
- when match = scanner.scan(/\{/)
63
+ when (!inside and match = scanner.scan(/\{/))
62
64
  inside = true
63
65
  result << [:OPEN, nil]
64
- when match = scanner.scan(/\}/)
66
+ when (match = scanner.scan(/\}/))
65
67
  inside = false
66
68
  result << [:CLOSE, nil]
67
69
  when (!last_is_key?(result) and match = scanner.scan(/\w+/))
68
70
  result << [:KEY, match.chomp.gsub(/\s+$/, '')]
69
71
  when (inside and match = scanner.scan(/\d+$/))
70
72
  result << [:VALUE, match.to_i]
73
+ when (inside and match = scanner.scan(/.*\{.+\}.*/))
74
+ result << [:VALUE, match.gsub(/\s+$/, '')]
71
75
  when (inside and match = scanner.scan(/[^\n\}]+/))
72
76
  # Make sure to ignore inline comments starting with ';'.
73
77
  result << [:VALUE, match.gsub(/\s+$/, '')]
@@ -0,0 +1,30 @@
1
+ module NagiosParser
2
+ class Result
3
+ def initialize(options = {})
4
+ @options = options
5
+ @result = {}
6
+ end
7
+
8
+ def []=(key, value)
9
+ if multi_value?(key)
10
+ (@result[key] ||= []) << value
11
+ else
12
+ @result[key] = value
13
+ end
14
+ end
15
+
16
+ def [](key)
17
+ @result[key]
18
+ end
19
+
20
+ def to_hash
21
+ @result.dup
22
+ end
23
+
24
+ private
25
+
26
+ def multi_value?(key)
27
+ Array(@options[:multi_value]).include?(key)
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module NagiosParser
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -60,12 +60,28 @@ describe NagiosParser::Config::Parser do
60
60
 
61
61
  lock_file=/var/run/nagios3/nagios3.pid
62
62
  event_broker_options=-1
63
+
64
+ cfg_dir=/etc/nagios/config1
65
+ cfg_dir=/etc/nagios/config2
66
+ cfg_dir=/etc/nagios/config3
67
+
68
+ cfg_file=/etc/nagios/file1
69
+ cfg_file=/etc/nagios/file2
63
70
  RUBY
64
71
 
65
72
  data = parser.parse(string)
66
73
  data['external_command_buffer_slots'].should == 4096
67
74
  data['lock_file'].should == '/var/run/nagios3/nagios3.pid'
68
75
  data['event_broker_options'].should == -1
76
+ data['cfg_dir'].should == [
77
+ '/etc/nagios/config1',
78
+ '/etc/nagios/config2',
79
+ '/etc/nagios/config3'
80
+ ]
81
+ data['cfg_file'].should == [
82
+ '/etc/nagios/file1',
83
+ '/etc/nagios/file2'
84
+ ]
69
85
  end
70
86
  end
71
87
 
@@ -73,6 +73,33 @@ describe NagiosParser::Object::Parser do
73
73
  end
74
74
  end
75
75
 
76
+ context "with a valid string containing squiggly braces" do
77
+ it "returns a hash of object definitions" do
78
+ string = <<-RUBY
79
+ define command {
80
+ command_line check_http -u /api -P '{"key":"value"}' -k "content-type: application/json"
81
+ }
82
+ RUBY
83
+
84
+ data = parser.parse(string)
85
+ data['command'].first['command_line'] == "check_http -u /api -P '{\"key\":\"value\"}' -k \"content-type: application/json\""
86
+ end
87
+ end
88
+
89
+ context "with a valid service definition containing empty notes" do
90
+ it "parses optional value" do
91
+ string = <<-RUBY
92
+ define service {
93
+ host_name foo.example.com
94
+ notes
95
+ }
96
+ RUBY
97
+
98
+ data = parser.parse(string)
99
+ data['service'].first['notes'].should be_nil
100
+ end
101
+ end
102
+
76
103
  context "with an invalid object definition" do
77
104
  describe "example: FooBar { info test }" do
78
105
  it "will raise an exception" do
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'nagios_parser/result'
3
+
4
+ describe NagiosParser::Result do
5
+ let(:result) { described_class.new }
6
+
7
+ it "sets a key value pair" do
8
+ result['foo'] = 'bar'
9
+
10
+ result['foo'].should == 'bar'
11
+ end
12
+
13
+ context "with multi_value option set" do
14
+ context "and a single value for a multi value key" do
15
+ it "returns a list with one value" do
16
+ result = described_class.new(:multi_value => %w{foo})
17
+ result['foo'] = 'bar'
18
+
19
+ result['foo'].should == ['bar']
20
+ end
21
+ end
22
+
23
+ context "and multiple values for a multi value key" do
24
+ it "returns a list with all values" do
25
+ result = described_class.new(:multi_value => %w{eek})
26
+ result['eek'] = 'foo'
27
+ result['eek'] = 'bar'
28
+
29
+ result['eek'].should == ['foo', 'bar']
30
+ end
31
+ end
32
+ end
33
+
34
+ context "without multi_value options set" do
35
+ context "and multiple values for the same key" do
36
+ it "returns the last value" do
37
+ result['baz'] = 'foo'
38
+ result['baz'] = 'bar'
39
+
40
+ result['baz'].should == 'bar'
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "#to_hash" do
46
+ it "returns a hash representation of the result" do
47
+ result['foo'] = ['bar', 'baz']
48
+ result['one'] = 1
49
+
50
+ result.to_hash.should == {'foo' => ['bar', 'baz'], 'one' => 1}
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,75 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: nagios_parser
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 2
10
- version: 1.2.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Bernd Ahlers
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-22 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-03-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: racc
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
39
25
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: racc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
47
38
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
39
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
53
41
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
61
54
  type: :development
62
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
63
62
  description: The nagios_parser gem provides parsers for Nagios config and status files.
64
- email:
63
+ email:
65
64
  - bernd@tuneafish.de
66
65
  executables: []
67
-
68
66
  extensions: []
69
-
70
67
  extra_rdoc_files: []
71
-
72
- files:
68
+ files:
73
69
  - .gitignore
74
70
  - .travis.yml
75
71
  - CHANGES.md
@@ -84,6 +80,7 @@ files:
84
80
  - lib/nagios_parser/object/parser.y
85
81
  - lib/nagios_parser/resource/parser.rb
86
82
  - lib/nagios_parser/resource/parser.y
83
+ - lib/nagios_parser/result.rb
87
84
  - lib/nagios_parser/status/parser.rb
88
85
  - lib/nagios_parser/status/parser.y
89
86
  - lib/nagios_parser/version.rb
@@ -92,41 +89,37 @@ files:
92
89
  - spec/nagios_parser/config/parser_spec.rb
93
90
  - spec/nagios_parser/object/parser_spec.rb
94
91
  - spec/nagios_parser/resource/parser_spec.rb
92
+ - spec/nagios_parser/result_spec.rb
95
93
  - spec/nagios_parser/status/parser_spec.rb
96
94
  - spec/spec_helper.rb
97
- has_rdoc: true
98
95
  homepage: http://rubygems.org/gems/nagios_parser
99
96
  licenses: []
100
-
101
97
  post_install_message:
102
98
  rdoc_options: []
103
-
104
- require_paths:
99
+ require_paths:
105
100
  - lib
106
- required_ruby_version: !ruby/object:Gem::Requirement
101
+ required_ruby_version: !ruby/object:Gem::Requirement
107
102
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- hash: 3
112
- segments:
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
113
108
  - 0
114
- version: "0"
115
- required_rubygems_version: !ruby/object:Gem::Requirement
109
+ hash: 3262026437497709506
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
111
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- hash: 3
121
- segments:
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
122
117
  - 0
123
- version: "0"
118
+ hash: 3262026437497709506
124
119
  requirements: []
125
-
126
120
  rubyforge_project:
127
- rubygems_version: 1.5.2
121
+ rubygems_version: 1.8.25
128
122
  signing_key:
129
123
  specification_version: 3
130
124
  summary: parser lib for parsing Nagios status and config files
131
125
  test_files: []
132
-