nagios_parser 1.1.0 → 1.2.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/CHANGES.md +9 -1
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/lib/nagios_parser/config/parser.rb +152 -0
- data/lib/nagios_parser/config/parser.y +59 -0
- data/lib/nagios_parser/object/parser.rb +2 -1
- data/lib/nagios_parser/object/parser.y +2 -1
- data/lib/nagios_parser/resource/parser.rb +150 -0
- data/lib/nagios_parser/resource/parser.y +57 -0
- data/lib/nagios_parser/version.rb +1 -1
- data/lib/nagios_parser.rb +2 -0
- data/spec/nagios_parser/config/parser_spec.rb +80 -0
- data/spec/nagios_parser/object/parser_spec.rb +1 -1
- data/spec/nagios_parser/resource/parser_spec.rb +69 -0
- metadata +11 -7
data/CHANGES.md
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
nagios_parser - ChangeLog
|
2
2
|
=========================
|
3
3
|
|
4
|
-
#
|
4
|
+
# 1.2.0
|
5
|
+
* Add NagiosParser::Resource parser to parse Nagios resource
|
6
|
+
macro files.
|
7
|
+
* Add NagiosParser::Config parser to parse the Nagios main
|
8
|
+
configuration file.
|
9
|
+
* Ignore inline comments starting with ';' in the object
|
10
|
+
configuration files.
|
11
|
+
|
12
|
+
# 1.1.0
|
5
13
|
* Remove hardcoded status types to make the parser less strict.
|
6
14
|
* More Nagios 2.x/3.x status types. (mariussturm)
|
7
15
|
|
data/README.md
CHANGED
@@ -8,6 +8,8 @@ files and a parser for the Nagios object definition files.
|
|
8
8
|
|
9
9
|
* NagiosParser::Status::Parser
|
10
10
|
* NagiosParser::Object::Parser
|
11
|
+
* NagiosParser::Config::Parser
|
12
|
+
* NagiosParser::Resource::Parser
|
11
13
|
|
12
14
|
Both parsers return plain hashes and arrays. There are no special
|
13
15
|
Nagios objects or something like that. That means you can create
|
@@ -100,6 +102,42 @@ This will print a data structure that looks like this.
|
|
100
102
|
"host_name"=>"server1",
|
101
103
|
"alias"=>"server1.example.com"}]}
|
102
104
|
|
105
|
+
## Config Parser
|
106
|
+
|
107
|
+
require 'nagios_parser/config/parser'
|
108
|
+
require 'pp'
|
109
|
+
|
110
|
+
config = <<-CONFIG
|
111
|
+
log_file=/var/log/nagios3/nagios.log
|
112
|
+
status_update_interval=10
|
113
|
+
CONFIG
|
114
|
+
|
115
|
+
data = NagiosParser::Config::Parser.parse(config)
|
116
|
+
pp data
|
117
|
+
|
118
|
+
This will print a data structure that looks like this.
|
119
|
+
|
120
|
+
{"log_file"=>"/var/log/nagios3/nagios.log",
|
121
|
+
"status_update_interval"=>10}
|
122
|
+
|
123
|
+
## Resource macro parser
|
124
|
+
|
125
|
+
require 'nagios_parser/resource/parser'
|
126
|
+
require 'pp'
|
127
|
+
|
128
|
+
resource = <<-RESOURCE
|
129
|
+
$USER1$=/usr/lib/nagios/plugins
|
130
|
+
$USER10$=mysqluser
|
131
|
+
RESOURCE
|
132
|
+
|
133
|
+
data = NagiosParser::Resource::Parser.parse(resource)
|
134
|
+
pp data
|
135
|
+
|
136
|
+
This will print a data structure that looks like this.
|
137
|
+
|
138
|
+
{"$USER10$"=>"mysqluser",
|
139
|
+
"$USER1$"=>"/usr/lib/nagios/plugins"}
|
140
|
+
|
103
141
|
# Development
|
104
142
|
|
105
143
|
The parsers are based on the racc parser generator. racc is needed
|
@@ -111,6 +149,8 @@ the Ruby files afterwards. There are two rake tasks to help with that.
|
|
111
149
|
|
112
150
|
* `rake parser::status`
|
113
151
|
* `rake parser::object`
|
152
|
+
* `rake parser::config`
|
153
|
+
* `rake parser::resource`
|
114
154
|
|
115
155
|
The bundler gem is used to handle the development dependencies.
|
116
156
|
Run `bundle install` to install them.
|
data/Rakefile
CHANGED
@@ -14,6 +14,12 @@ namespace :parser do
|
|
14
14
|
desc 'Generate the object parser with racc'
|
15
15
|
task :object => [ 'lib/nagios_parser/object/parser.rb' ]
|
16
16
|
|
17
|
+
desc 'Generate the resource parser with racc'
|
18
|
+
task :resource => [ 'lib/nagios_parser/resource/parser.rb' ]
|
19
|
+
|
20
|
+
desc 'Generate the main config parser with racc'
|
21
|
+
task :config => [ 'lib/nagios_parser/config/parser.rb' ]
|
22
|
+
|
17
23
|
rule '.rb' => '.y' do |target|
|
18
24
|
sh "racc -o #{target.name} #{target.source}"
|
19
25
|
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.6
|
4
|
+
# from Racc grammer file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
require 'strscan'
|
10
|
+
|
11
|
+
module NagiosParser
|
12
|
+
module Config
|
13
|
+
class Parser < Racc::Parser
|
14
|
+
|
15
|
+
module_eval(<<'...end parser.y/module_eval...', 'parser.y', 19)
|
16
|
+
|
17
|
+
def create_token(string)
|
18
|
+
result = []
|
19
|
+
scanner = StringScanner.new(string)
|
20
|
+
|
21
|
+
until scanner.empty?
|
22
|
+
case
|
23
|
+
when scanner.scan(/\s+/)
|
24
|
+
# ignore whitespace
|
25
|
+
when scanner.scan(/#[^\n]*/)
|
26
|
+
# ignore comments
|
27
|
+
when match = scanner.scan(/\w+\=/)
|
28
|
+
result << [:KEY, match.chop.gsub(/\s+$/, '')]
|
29
|
+
when match = scanner.scan(/\-?\d+$/)
|
30
|
+
result << [:VALUE, match.to_i]
|
31
|
+
when match = scanner.scan(/[^\n]+$/)
|
32
|
+
result << [:VALUE, match.gsub(/\s+$/, '')]
|
33
|
+
else
|
34
|
+
raise "Can't tokenize <#{scanner.peek(10)}>"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
result << [false, false]
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_accessor :token
|
42
|
+
|
43
|
+
def self.parse(string)
|
44
|
+
new.parse(string)
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse(string)
|
48
|
+
@result = {}
|
49
|
+
@token = create_token(string)
|
50
|
+
do_parse
|
51
|
+
@result
|
52
|
+
end
|
53
|
+
|
54
|
+
def next_token
|
55
|
+
@token.shift
|
56
|
+
end
|
57
|
+
...end parser.y/module_eval...
|
58
|
+
##### State transition tables begin ###
|
59
|
+
|
60
|
+
racc_action_table = [
|
61
|
+
5, 1, 1, 4, 7 ]
|
62
|
+
|
63
|
+
racc_action_check = [
|
64
|
+
2, 0, 2, 1, 5 ]
|
65
|
+
|
66
|
+
racc_action_pointer = [
|
67
|
+
-1, 0, 0, nil, nil, 4, nil, nil ]
|
68
|
+
|
69
|
+
racc_action_default = [
|
70
|
+
-4, -4, -4, -1, -3, -4, -2, 8 ]
|
71
|
+
|
72
|
+
racc_goto_table = [
|
73
|
+
3, 2, 6 ]
|
74
|
+
|
75
|
+
racc_goto_check = [
|
76
|
+
2, 1, 2 ]
|
77
|
+
|
78
|
+
racc_goto_pointer = [
|
79
|
+
nil, 1, 0 ]
|
80
|
+
|
81
|
+
racc_goto_default = [
|
82
|
+
nil, nil, nil ]
|
83
|
+
|
84
|
+
racc_reduce_table = [
|
85
|
+
0, 0, :racc_error,
|
86
|
+
1, 5, :_reduce_none,
|
87
|
+
2, 5, :_reduce_none,
|
88
|
+
2, 6, :_reduce_3 ]
|
89
|
+
|
90
|
+
racc_reduce_n = 4
|
91
|
+
|
92
|
+
racc_shift_n = 8
|
93
|
+
|
94
|
+
racc_token_table = {
|
95
|
+
false => 0,
|
96
|
+
:error => 1,
|
97
|
+
:KEY => 2,
|
98
|
+
:VALUE => 3 }
|
99
|
+
|
100
|
+
racc_nt_base = 4
|
101
|
+
|
102
|
+
racc_use_result_var = true
|
103
|
+
|
104
|
+
Racc_arg = [
|
105
|
+
racc_action_table,
|
106
|
+
racc_action_check,
|
107
|
+
racc_action_default,
|
108
|
+
racc_action_pointer,
|
109
|
+
racc_goto_table,
|
110
|
+
racc_goto_check,
|
111
|
+
racc_goto_default,
|
112
|
+
racc_goto_pointer,
|
113
|
+
racc_nt_base,
|
114
|
+
racc_reduce_table,
|
115
|
+
racc_token_table,
|
116
|
+
racc_shift_n,
|
117
|
+
racc_reduce_n,
|
118
|
+
racc_use_result_var ]
|
119
|
+
|
120
|
+
Racc_token_to_s_table = [
|
121
|
+
"$end",
|
122
|
+
"error",
|
123
|
+
"KEY",
|
124
|
+
"VALUE",
|
125
|
+
"$start",
|
126
|
+
"assignments",
|
127
|
+
"assignment" ]
|
128
|
+
|
129
|
+
Racc_debug_parser = false
|
130
|
+
|
131
|
+
##### State transition tables end #####
|
132
|
+
|
133
|
+
# reduce 0 omitted
|
134
|
+
|
135
|
+
# reduce 1 omitted
|
136
|
+
|
137
|
+
# reduce 2 omitted
|
138
|
+
|
139
|
+
module_eval(<<'.,.,', 'parser.y', 10)
|
140
|
+
def _reduce_3(val, _values, result)
|
141
|
+
@result[val[0]] = val[1]
|
142
|
+
result
|
143
|
+
end
|
144
|
+
.,.,
|
145
|
+
|
146
|
+
def _reduce_none(val, _values, result)
|
147
|
+
val[0]
|
148
|
+
end
|
149
|
+
|
150
|
+
end # class Parser
|
151
|
+
end # module Config
|
152
|
+
end # module NagiosParser
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class NagiosParser::Config::Parser
|
2
|
+
token
|
3
|
+
KEY VALUE
|
4
|
+
|
5
|
+
rule
|
6
|
+
assignments
|
7
|
+
: assignment
|
8
|
+
| assignments assignment
|
9
|
+
;
|
10
|
+
assignment
|
11
|
+
: KEY VALUE { @result[val[0]] = val[1] }
|
12
|
+
;
|
13
|
+
end
|
14
|
+
|
15
|
+
---- header
|
16
|
+
require 'strscan'
|
17
|
+
|
18
|
+
---- inner
|
19
|
+
|
20
|
+
def create_token(string)
|
21
|
+
result = []
|
22
|
+
scanner = StringScanner.new(string)
|
23
|
+
|
24
|
+
until scanner.empty?
|
25
|
+
case
|
26
|
+
when scanner.scan(/\s+/)
|
27
|
+
# ignore whitespace
|
28
|
+
when scanner.scan(/#[^\n]*/)
|
29
|
+
# ignore comments
|
30
|
+
when match = scanner.scan(/\w+\=/)
|
31
|
+
result << [:KEY, match.chop.gsub(/\s+$/, '')]
|
32
|
+
when match = scanner.scan(/\-?\d+$/)
|
33
|
+
result << [:VALUE, match.to_i]
|
34
|
+
when match = scanner.scan(/[^\n]+$/)
|
35
|
+
result << [:VALUE, match.gsub(/\s+$/, '')]
|
36
|
+
else
|
37
|
+
raise "Can't tokenize <#{scanner.peek(10)}>"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
result << [false, false]
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_accessor :token
|
45
|
+
|
46
|
+
def self.parse(string)
|
47
|
+
new.parse(string)
|
48
|
+
end
|
49
|
+
|
50
|
+
def parse(string)
|
51
|
+
@result = {}
|
52
|
+
@token = create_token(string)
|
53
|
+
do_parse
|
54
|
+
@result
|
55
|
+
end
|
56
|
+
|
57
|
+
def next_token
|
58
|
+
@token.shift
|
59
|
+
end
|
@@ -46,7 +46,8 @@ def create_token(string)
|
|
46
46
|
when (inside and match = scanner.scan(/\d+$/))
|
47
47
|
result << [:VALUE, match.to_i]
|
48
48
|
when (inside and match = scanner.scan(/[^\n\}]+/))
|
49
|
-
|
49
|
+
# Make sure to ignore inline comments starting with ';'.
|
50
|
+
result << [:VALUE, match.split(';').first.gsub(/\s+$/, '')]
|
50
51
|
else
|
51
52
|
raise "Can't tokenize <#{scanner.peek(10)}>"
|
52
53
|
end
|
@@ -64,7 +64,8 @@ def create_token(string)
|
|
64
64
|
when (inside and match = scanner.scan(/\d+$/))
|
65
65
|
result << [:VALUE, match.to_i]
|
66
66
|
when (inside and match = scanner.scan(/[^\n\}]+/))
|
67
|
-
|
67
|
+
# Make sure to ignore inline comments starting with ';'.
|
68
|
+
result << [:VALUE, match.split(';').first.gsub(/\s+$/, '')]
|
68
69
|
else
|
69
70
|
raise "Can't tokenize <#{scanner.peek(10)}>"
|
70
71
|
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.6
|
4
|
+
# from Racc grammer file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
require 'strscan'
|
10
|
+
|
11
|
+
module NagiosParser
|
12
|
+
module Resource
|
13
|
+
class Parser < Racc::Parser
|
14
|
+
|
15
|
+
module_eval(<<'...end parser.y/module_eval...', 'parser.y', 19)
|
16
|
+
|
17
|
+
def create_token(string)
|
18
|
+
result = []
|
19
|
+
scanner = StringScanner.new(string)
|
20
|
+
|
21
|
+
until scanner.empty?
|
22
|
+
case
|
23
|
+
when scanner.scan(/\s+/)
|
24
|
+
# ignore whitespace
|
25
|
+
when scanner.scan(/#[^\n]*/)
|
26
|
+
# ignore comments
|
27
|
+
when match = scanner.scan(/\$\w+\$\s*\=/)
|
28
|
+
result << [:KEY, match.chop.gsub(/\s+$/, '')]
|
29
|
+
when match = scanner.scan(/[^\n]+/)
|
30
|
+
result << [:VALUE, match.gsub(/\s+$/, '')]
|
31
|
+
else
|
32
|
+
raise "Can't tokenize <#{scanner.peek(10)}>"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
result << [false, false]
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_accessor :token
|
40
|
+
|
41
|
+
def self.parse(string)
|
42
|
+
new.parse(string)
|
43
|
+
end
|
44
|
+
|
45
|
+
def parse(string)
|
46
|
+
@result = {}
|
47
|
+
@token = create_token(string)
|
48
|
+
do_parse
|
49
|
+
@result
|
50
|
+
end
|
51
|
+
|
52
|
+
def next_token
|
53
|
+
@token.shift
|
54
|
+
end
|
55
|
+
...end parser.y/module_eval...
|
56
|
+
##### State transition tables begin ###
|
57
|
+
|
58
|
+
racc_action_table = [
|
59
|
+
5, 1, 1, 4, 7 ]
|
60
|
+
|
61
|
+
racc_action_check = [
|
62
|
+
2, 0, 2, 1, 5 ]
|
63
|
+
|
64
|
+
racc_action_pointer = [
|
65
|
+
-1, 0, 0, nil, nil, 4, nil, nil ]
|
66
|
+
|
67
|
+
racc_action_default = [
|
68
|
+
-4, -4, -4, -1, -3, -4, -2, 8 ]
|
69
|
+
|
70
|
+
racc_goto_table = [
|
71
|
+
3, 2, 6 ]
|
72
|
+
|
73
|
+
racc_goto_check = [
|
74
|
+
2, 1, 2 ]
|
75
|
+
|
76
|
+
racc_goto_pointer = [
|
77
|
+
nil, 1, 0 ]
|
78
|
+
|
79
|
+
racc_goto_default = [
|
80
|
+
nil, nil, nil ]
|
81
|
+
|
82
|
+
racc_reduce_table = [
|
83
|
+
0, 0, :racc_error,
|
84
|
+
1, 5, :_reduce_none,
|
85
|
+
2, 5, :_reduce_none,
|
86
|
+
2, 6, :_reduce_3 ]
|
87
|
+
|
88
|
+
racc_reduce_n = 4
|
89
|
+
|
90
|
+
racc_shift_n = 8
|
91
|
+
|
92
|
+
racc_token_table = {
|
93
|
+
false => 0,
|
94
|
+
:error => 1,
|
95
|
+
:KEY => 2,
|
96
|
+
:VALUE => 3 }
|
97
|
+
|
98
|
+
racc_nt_base = 4
|
99
|
+
|
100
|
+
racc_use_result_var = true
|
101
|
+
|
102
|
+
Racc_arg = [
|
103
|
+
racc_action_table,
|
104
|
+
racc_action_check,
|
105
|
+
racc_action_default,
|
106
|
+
racc_action_pointer,
|
107
|
+
racc_goto_table,
|
108
|
+
racc_goto_check,
|
109
|
+
racc_goto_default,
|
110
|
+
racc_goto_pointer,
|
111
|
+
racc_nt_base,
|
112
|
+
racc_reduce_table,
|
113
|
+
racc_token_table,
|
114
|
+
racc_shift_n,
|
115
|
+
racc_reduce_n,
|
116
|
+
racc_use_result_var ]
|
117
|
+
|
118
|
+
Racc_token_to_s_table = [
|
119
|
+
"$end",
|
120
|
+
"error",
|
121
|
+
"KEY",
|
122
|
+
"VALUE",
|
123
|
+
"$start",
|
124
|
+
"assignments",
|
125
|
+
"assignment" ]
|
126
|
+
|
127
|
+
Racc_debug_parser = false
|
128
|
+
|
129
|
+
##### State transition tables end #####
|
130
|
+
|
131
|
+
# reduce 0 omitted
|
132
|
+
|
133
|
+
# reduce 1 omitted
|
134
|
+
|
135
|
+
# reduce 2 omitted
|
136
|
+
|
137
|
+
module_eval(<<'.,.,', 'parser.y', 10)
|
138
|
+
def _reduce_3(val, _values, result)
|
139
|
+
@result[val[0]] = val[1]
|
140
|
+
result
|
141
|
+
end
|
142
|
+
.,.,
|
143
|
+
|
144
|
+
def _reduce_none(val, _values, result)
|
145
|
+
val[0]
|
146
|
+
end
|
147
|
+
|
148
|
+
end # class Parser
|
149
|
+
end # module Resource
|
150
|
+
end # module NagiosParser
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class NagiosParser::Resource::Parser
|
2
|
+
token
|
3
|
+
KEY VALUE
|
4
|
+
|
5
|
+
rule
|
6
|
+
assignments
|
7
|
+
: assignment
|
8
|
+
| assignments assignment
|
9
|
+
;
|
10
|
+
assignment
|
11
|
+
: KEY VALUE { @result[val[0]] = val[1] }
|
12
|
+
;
|
13
|
+
end
|
14
|
+
|
15
|
+
---- header
|
16
|
+
require 'strscan'
|
17
|
+
|
18
|
+
---- inner
|
19
|
+
|
20
|
+
def create_token(string)
|
21
|
+
result = []
|
22
|
+
scanner = StringScanner.new(string)
|
23
|
+
|
24
|
+
until scanner.empty?
|
25
|
+
case
|
26
|
+
when scanner.scan(/\s+/)
|
27
|
+
# ignore whitespace
|
28
|
+
when scanner.scan(/#[^\n]*/)
|
29
|
+
# ignore comments
|
30
|
+
when match = scanner.scan(/\$\w+\$\s*\=/)
|
31
|
+
result << [:KEY, match.chop.gsub(/\s+$/, '')]
|
32
|
+
when match = scanner.scan(/[^\n]+/)
|
33
|
+
result << [:VALUE, match.gsub(/\s+$/, '')]
|
34
|
+
else
|
35
|
+
raise "Can't tokenize <#{scanner.peek(10)}>"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
result << [false, false]
|
40
|
+
end
|
41
|
+
|
42
|
+
attr_accessor :token
|
43
|
+
|
44
|
+
def self.parse(string)
|
45
|
+
new.parse(string)
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse(string)
|
49
|
+
@result = {}
|
50
|
+
@token = create_token(string)
|
51
|
+
do_parse
|
52
|
+
@result
|
53
|
+
end
|
54
|
+
|
55
|
+
def next_token
|
56
|
+
@token.shift
|
57
|
+
end
|
data/lib/nagios_parser.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'nagios_parser/config/parser'
|
3
|
+
|
4
|
+
describe NagiosParser::Config::Parser do
|
5
|
+
let(:parser) { NagiosParser::Config::Parser.new }
|
6
|
+
|
7
|
+
describe ".parse" do
|
8
|
+
it "returns a hash of macro definitions" do
|
9
|
+
data = parser.parse('log_file=/var/log/nagios3/nagios.log')
|
10
|
+
data['log_file'].should == '/var/log/nagios3/nagios.log'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#create_token" do
|
15
|
+
context "given a valid string" do
|
16
|
+
it "creates a valid token list" do
|
17
|
+
string = <<-RUBY
|
18
|
+
# LOG FILE
|
19
|
+
# This is the main log file where service and host events are logged
|
20
|
+
# for historical purposes. This should be the first option specified
|
21
|
+
# in the config file!!!
|
22
|
+
|
23
|
+
log_file=/var/log/nagios3/nagios.log
|
24
|
+
object_cache_file=/var/cache/nagios3/objects.cache
|
25
|
+
|
26
|
+
nagios_group=nagios
|
27
|
+
|
28
|
+
status_update_interval=10
|
29
|
+
RUBY
|
30
|
+
|
31
|
+
parser.create_token(string).should == [
|
32
|
+
[:KEY, 'log_file'], [:VALUE, '/var/log/nagios3/nagios.log'],
|
33
|
+
[:KEY, 'object_cache_file'], [:VALUE, '/var/cache/nagios3/objects.cache'],
|
34
|
+
[:KEY, 'nagios_group'], [:VALUE, 'nagios'],
|
35
|
+
[:KEY, 'status_update_interval'], [:VALUE, 10],
|
36
|
+
[false, false]
|
37
|
+
]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#next_token" do
|
43
|
+
it "shifts the next element off the token list" do
|
44
|
+
parser.token = [:one, :two, :three]
|
45
|
+
parser.next_token
|
46
|
+
parser.next_token.should == :two
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#parse" do
|
51
|
+
context "with a valid string" do
|
52
|
+
it "returns a hash of config key value pairs" do
|
53
|
+
string = <<-RUBY
|
54
|
+
external_command_buffer_slots=4096
|
55
|
+
|
56
|
+
|
57
|
+
# LOCK FILE
|
58
|
+
# This is the lockfile that Nagios will use to store its PID number
|
59
|
+
# in when it is running in daemon mode.
|
60
|
+
|
61
|
+
lock_file=/var/run/nagios3/nagios3.pid
|
62
|
+
event_broker_options=-1
|
63
|
+
RUBY
|
64
|
+
|
65
|
+
data = parser.parse(string)
|
66
|
+
data['external_command_buffer_slots'].should == 4096
|
67
|
+
data['lock_file'].should == '/var/run/nagios3/nagios3.pid'
|
68
|
+
data['event_broker_options'].should == -1
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "with an invalid config" do
|
73
|
+
it "will raise an exception" do
|
74
|
+
expect {
|
75
|
+
parser.parse('$foo=bar')
|
76
|
+
}.to raise_error
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'nagios_parser/resource/parser'
|
3
|
+
|
4
|
+
describe NagiosParser::Resource::Parser do
|
5
|
+
let(:parser) { NagiosParser::Resource::Parser.new }
|
6
|
+
|
7
|
+
describe ".parse" do
|
8
|
+
it "returns a hash of macro definitions" do
|
9
|
+
data = parser.parse('$USER1$=/usr/lib/nagios/plugins')
|
10
|
+
data['$USER1$'].should == '/usr/lib/nagios/plugins'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#create_token" do
|
15
|
+
context "given a valid string" do
|
16
|
+
it "creates a valid token list" do
|
17
|
+
string = <<-RUBY
|
18
|
+
# This is the resource configuration
|
19
|
+
$USER1$=/usr/lib/nagios/plugins
|
20
|
+
$USER10$=mysqluser
|
21
|
+
|
22
|
+
$USER30$ = /bin
|
23
|
+
RUBY
|
24
|
+
|
25
|
+
parser.create_token(string).should == [
|
26
|
+
[:KEY, '$USER1$'], [:VALUE, '/usr/lib/nagios/plugins'],
|
27
|
+
[:KEY, '$USER10$'], [:VALUE, 'mysqluser'],
|
28
|
+
[:KEY, '$USER30$'], [:VALUE, '/bin'],
|
29
|
+
[false, false]
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#next_token" do
|
36
|
+
it "returns shifts the next element off the token list" do
|
37
|
+
parser.token = [:one, :two, :three]
|
38
|
+
parser.next_token
|
39
|
+
parser.next_token.should == :two
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#parse" do
|
44
|
+
context "with a valid string" do
|
45
|
+
it "returns a hash of resource macros" do
|
46
|
+
string = <<-RUBY
|
47
|
+
# This is the resource configuration
|
48
|
+
$USER1$=/usr/lib/nagios/plugins
|
49
|
+
$USER10$=mysqluser
|
50
|
+
|
51
|
+
$USER30$ = /bin
|
52
|
+
RUBY
|
53
|
+
|
54
|
+
data = parser.parse(string)
|
55
|
+
data['$USER1$'].should == '/usr/lib/nagios/plugins'
|
56
|
+
data['$USER10$'].should == 'mysqluser'
|
57
|
+
data['$USER30$'].should == '/bin'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "with invalid resource macro" do
|
62
|
+
it "will raise an exception" do
|
63
|
+
expect {
|
64
|
+
parser.parse('foo=bar')
|
65
|
+
}.to raise_error
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nagios_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Bernd Ahlers
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-06-20 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: rake
|
@@ -77,16 +76,21 @@ files:
|
|
77
76
|
- README.md
|
78
77
|
- Rakefile
|
79
78
|
- lib/nagios_parser.rb
|
79
|
+
- lib/nagios_parser/config/parser.rb
|
80
|
+
- lib/nagios_parser/config/parser.y
|
80
81
|
- lib/nagios_parser/object/parser.rb
|
81
82
|
- lib/nagios_parser/object/parser.y
|
83
|
+
- lib/nagios_parser/resource/parser.rb
|
84
|
+
- lib/nagios_parser/resource/parser.y
|
82
85
|
- lib/nagios_parser/status/parser.rb
|
83
86
|
- lib/nagios_parser/status/parser.y
|
84
87
|
- lib/nagios_parser/version.rb
|
85
88
|
- nagios_parser.gemspec
|
89
|
+
- spec/nagios_parser/config/parser_spec.rb
|
86
90
|
- spec/nagios_parser/object/parser_spec.rb
|
91
|
+
- spec/nagios_parser/resource/parser_spec.rb
|
87
92
|
- spec/nagios_parser/status/parser_spec.rb
|
88
93
|
- spec/spec_helper.rb
|
89
|
-
has_rdoc: true
|
90
94
|
homepage: http://rubygems.org/gems/nagios_parser
|
91
95
|
licenses: []
|
92
96
|
|
@@ -116,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
120
|
requirements: []
|
117
121
|
|
118
122
|
rubyforge_project:
|
119
|
-
rubygems_version: 1.
|
123
|
+
rubygems_version: 1.8.5
|
120
124
|
signing_key:
|
121
125
|
specification_version: 3
|
122
126
|
summary: parser lib for parsing Nagios status and config files
|