nagios_mklivestatus 0.0.3 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,221 @@
1
+ = Description
2
+
3
+ This gem provides support to \Nagios MkLiveStatus through an API.
4
+ The library have the following functionality :
5
+
6
+ * check query validity
7
+ * parse query from string
8
+ * create query
9
+ * request mklivestatus with query
10
+
11
+ See this {link}[http://mathias-kettner.de/checkmk_livestatus.html] for more detail about the MkLiveStatus.
12
+
13
+ ---
14
+
15
+ = Installation
16
+
17
+ In order to use the gem, you have to install it into your rubygem environment.
18
+
19
+ gem install nagios_maklivestatus
20
+
21
+ To integrate it in your applications, it can be done just by adding the two following lines :
22
+
23
+ require 'rubygems'
24
+ require 'nagios_mklivestatus'
25
+
26
+ Your reading to use the library
27
+
28
+ ---
29
+
30
+ = User Guide
31
+
32
+ == Configuration
33
+
34
+ The configuration is made through the following command:
35
+ nagmk-ruby-config
36
+
37
+ This command is defined like this:
38
+
39
+ nagmk-ruby-config [-h|--help] [-p|--path PATH] [-r|--remove] [-o|--options NAME=VALUE] [-u|--unset NAME][-s|--show]
40
+
41
+ arguments : -h|--help help
42
+ -p|--path PATH path to the nagios mklivestatus socket by default
43
+ -r|--remove remove the path from the config file
44
+ -o|--options NAME=VALUE add option to the configuration
45
+ -u|--unset NAME remove option from the configuration file.
46
+ -s|--show show configuration
47
+
48
+ == Client
49
+
50
+ The client is the executable file that use the gem in order to execute the query.
51
+ nagmk-ruby <query>
52
+
53
+ The client can take the query from 3 sources:
54
+
55
+ from argument :: simply puts your query as argument into the command line "<query>" and with \n for new line
56
+ from file :: with the -f option you can define a path to the file containing the query
57
+ from pipe :: echo -e "<query>" | nagmk-ruby
58
+
59
+ The client command is defined like this:
60
+
61
+ nagmk-ruby [-h|--help] -p|--path PATH [-o|--options NAME=VALUE] {[-f|--file FILE]|query}
62
+
63
+ arguments : -h|--help help
64
+ -p|--path PATH redefine path to the nagios mklivestatus socket
65
+ -o|--options NAME=VALUE add or override option to the request
66
+ -f|--file FILE file containing the query
67
+ query the nagios query to execute
68
+
69
+ ---
70
+
71
+ = Developper Guide
72
+
73
+ == Logging System
74
+
75
+ The logger use the default logger class of ruby : Logger.
76
+ The logger can be initiate and changed through the same way:
77
+
78
+ require 'nagios_mklivestatus'
79
+
80
+ ## changing nagios logging
81
+ # options
82
+ log_opts = { #logger options used to override default options
83
+ :name => STDOUT, # name of the logger like in Logger.new(name)
84
+ :level => Logger::ERROR, # logger level logger.level = Logger::ERROR
85
+
86
+ # used if defined or not nil
87
+ :shift_age => nil, # shift age of the logger like in Logger.new(name, shift_age)
88
+
89
+ # used if defined or not nil and if shift_age is defined
90
+ :shift_size => nil # shift size of the logger like in Logger.new(name, shift_age, shift_size)
91
+ }
92
+ # change log setting
93
+ Nagios::MkLiveStatus.init({:log=>log_opts})
94
+
95
+ == Creating Query
96
+
97
+ Creating a query can be made in multiples ways using :
98
+ * API
99
+ * Helper
100
+ * Parser
101
+
102
+ Next, we will show you how to use those methods to create query.
103
+
104
+ === API
105
+
106
+ ===== Query
107
+
108
+ Look at the following code :
109
+
110
+ require 'nagios_mklivestatus'
111
+
112
+ ##
113
+ # create the query
114
+ #
115
+ query = Nagios::MkLiveStatus::Query.new
116
+
117
+ # add GET hosts
118
+ query.get "hosts"
119
+
120
+ # add Columns: host_name groups
121
+ query.addColumn "host_name"
122
+ query.addColumn "groups"
123
+
124
+ # add Filter: (see below)
125
+ query.addFilter "<filter_expr>"
126
+
127
+ # add Stats: (see below)
128
+ query.addStats "<stat_expr>"
129
+
130
+ Only the two first are required in order to create a correct MkLiveStatus Query.
131
+
132
+ ===== Filter
133
+
134
+ The filter expression which can be added to query are defined like this :
135
+
136
+ require 'nagios_mklivestatus'
137
+
138
+ #both line are equals
139
+ filter1 = Nagios::MkLiveStatus::Filter::Attr.new("host_name", Nagios::MkLiveStatus::Filter::Attr::EQUAL, "<name>")
140
+ filter2 = Nagios::MkLiveStatus::Filter::Attr.new("host_name", "=", "<name>")
141
+
142
+ #filter and, or and negate
143
+ filter_and = Nagios::MkLiveStatus::Filter::And.new(filter1, filter2)
144
+ filter_or = Nagios::MkLiveStatus::Filter::Or.new(filter1, filter2)
145
+ filter_neg = Nagios::MkLiveStatus::Filter::Negate.new(filter2)
146
+
147
+ ===== Stats
148
+
149
+ The stats expression which can be added to the query are defined like this:
150
+
151
+ require 'nagios_mklivestatus'
152
+
153
+ #both line are equals: host_name = <name>
154
+ stats1 = Nagios::MkLiveStatus::Stats::Attr.new("host_name", Nagios::MkLiveStatus::Stats::Attr::EQUAL, "<name>")
155
+ stats2 = Nagios::MkLiveStatus::Stats::Attr.new("host_name", "=", "<name>")
156
+
157
+ #both line are equals Stats: sum host_name
158
+ stats1 = Nagios::MkLiveStatus::Stats::Attr.new("host_name", nil, nil, Nagios::MkLiveStatus::Stats::Attr::SUM)
159
+ stats2 = Nagios::MkLiveStatus::Stats::Attr.new("host_name", nil, nil,"sum")
160
+
161
+ #stats and, or
162
+ stats_and = Nagios::MkLiveStatus::Stats::And.new(stats1, stats2)
163
+ stats_or = Nagios::MkLiveStatus::Stats::Or.new(stats1, stats2)
164
+
165
+ Please refer to the corresponding class for more details on expressions.
166
+
167
+ === Helper
168
+
169
+ An helper exists containing the query, filter and stats creation. This helper also contains comparator operator and deviation in sub modules.
170
+
171
+ require 'nagios_mklivestatus'
172
+ include Nagios::MkLiveStatus::QueryHelper
173
+
174
+ # query helper : create a query
175
+ query = nagmk_query
176
+
177
+ query.get "host"
178
+ query.addColumn "host_name"
179
+
180
+ # query helper : create filter
181
+ filter = nagmk_filter("host_name", Comparator::EQUAL, "<name>")
182
+ query.addFilter filter
183
+
184
+ # query helper : create filter from string
185
+ filter = nagmk_filter_from_str("host_name = <name>")
186
+
187
+ See the Nagios::MkLiveStatus::QueryHelper for more methods.
188
+
189
+ === Parser
190
+
191
+ The Nagios::MkLiveStatus::Parser is a module that provides a string parser in order to create the query:
192
+
193
+ require 'nagios_mklivestatus'
194
+ include Nagios::MkLiveStatus::Parser
195
+
196
+ query_str = <nagios_query>
197
+ query = nagmk_parse(query_str)
198
+
199
+ == Making Request
200
+
201
+ Once you have the query you can send it to the Nagios MkLiveStatus Server using the Request Object
202
+
203
+ require 'nagios_mklivestatus'
204
+
205
+ ##
206
+ # creating request
207
+ # path can be tcp://<host>:<port> or <path>
208
+ mklive_req = Nagios::MkLiveStatus::Request.new(path)
209
+
210
+ ##
211
+ # common options:
212
+ # :limit : limit the number of results
213
+ # :output : output format
214
+ # :user : authenticated user
215
+ #
216
+ # see the class for more
217
+ results = mklive_req.query(query, options)
218
+
219
+ =TODO:
220
+ * advanced management for exceptions throws by the socket
221
+ * adding support to http
data/RELEASE.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = nagios_mklivestatus version 0.0.9
2
+
3
+ == 0.0.1
4
+
5
+ * Creation initial release of the gem
6
+
7
+ == 0.0.4
8
+
9
+ * include user authentification for query.
10
+ * include advanced query filtering
11
+ * include advanced count/group (stats)
12
+ * include exception management
13
+ * include query parser
14
+
15
+ == 0.0.9
16
+ * include query helper
17
+ * include logging management
18
+ * preparing release v1.0.0
19
+ * include wait query manager
20
+ * include binaries for library
@@ -0,0 +1,2 @@
1
+ ---
2
+ :options: {}
data/bin/nagmk-ruby ADDED
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env ruby
2
+ # Use the nagios_mklivestatus gem in order to create and execute the query in parameters.
3
+ #
4
+ # nagmk-ruby [-h|--help] -p|--path PATH [-o|--options NAME=VALUE] {[-f|--file FILE]|query}
5
+ #
6
+ # arguments : -h|--help help
7
+ # -p|--path PATH path to the nagios mklivestatus socket
8
+ # -o|--options NAME=VALUE add option to the request
9
+ # -f|--file FILE file containing the query
10
+ # query the nagios query to execute
11
+ require 'rubygems'
12
+ require 'optparse'
13
+ require 'nagios_mklivestatus'
14
+ require 'logger'
15
+ require 'yaml'
16
+
17
+ #default logging
18
+ log_opts = {
19
+ :name => '/var/logs/nagios_mklivestatus_ruby.log',
20
+ :level => Logger::INFO
21
+ }
22
+
23
+ # loading configuration
24
+ mk_conf_file = File.join(File.absolute_path(File.dirname(__FILE__)), "nagmk-conf.yml")
25
+ mk_conf_opts = YAML::load_file(mk_conf_file)
26
+
27
+
28
+ mk_file=nil
29
+ mk_opts=Hash.new
30
+ mk_path=nil
31
+ #loading conf path if sets
32
+ mk_path=mk_conf_opts[:path] if mk_conf_opts.has_key? :path
33
+ #loading conf options if sets
34
+ mk_opts=mk_conf_opts[:options] if mk_conf_opts.has_key? :options
35
+ optparser = OptionParser.new do |opts|
36
+
37
+ # set help banner
38
+ opts.banner = "nagmk-ruby [-h|--help] -p|--path PATH [-o|--options NAME=VALUE] {[-f|--file FILE]|query}\n\nExecute the query or the file to the request.\n\n"
39
+
40
+ # add help option
41
+ opts.on( '-h', '--help', 'Display this screen' ) do
42
+ # show help
43
+ puts opts
44
+ # exit command
45
+ exit
46
+ end
47
+
48
+ # add verbose option : default to false
49
+ opts.on( '-v', '--verbose', "Show logs. Multiple time (-vv or -v -v)makes it appears like this :\n\t-v => log on the screen\n\t-vv => log warn\n\t-vvv => log info\n\t-vvvv => log debug\n ") do
50
+ # if option found set verbose option
51
+ if log_opts[:name] != STDOUT
52
+ log_opts[:name] = STDOUT
53
+ elsif log_opts[:level] != Logger::DEBUG
54
+ log_opts[:level] = log_opts[:level]-1
55
+ end
56
+
57
+ end
58
+
59
+ opts.on( '-p', '--path PATH', "Path to the Nagios MkLiveStatus socket according to specification :\n\ttcp://<host>:<port> for TCP or <path> for unix socket\n ") do |path|
60
+ mk_path = path
61
+ end
62
+
63
+ opts.on( '-o', '--options NAME=VALUE', "Options to add to make the request.\n\tcolumn_headers : show column header\n\tlimit : limit output lines\n\toutput : \'json\' or \'python\' output formating\n\tlocal_time : local time settings\n\tuser : authenticated user\n ") do |opt|
64
+
65
+ if opt.match(/^([\w_]+)=(true|false)$/)
66
+ name,value = opt.split("=")
67
+ if value == "true"
68
+ mk_opts[name.to_sym] = true
69
+ elsif value == "false"
70
+ mk_opts[name.to_sym] = false
71
+ end
72
+ elsif opt.match(/^([\w_]+)=(\d)$/)
73
+ name,value = opt.split("=")
74
+ mk_opts[name.to_sym] = value.to_i
75
+ elsif opt.match(/^([\w_]+)=(\w)$/)
76
+ name,value = opt.split("=")
77
+ mk_opts[name.to_sym] = value
78
+ end
79
+ end
80
+
81
+ opts.on( '-f', '--file FILE', "The file containing the query.") do |file|
82
+ mk_file = file
83
+ end
84
+
85
+ end
86
+
87
+ optparser.parse!
88
+
89
+ #set logging options
90
+ Nagios::MkLiveStatus.init(:log=>log_opts)
91
+
92
+ begin
93
+
94
+ if not mk_path or mk_path.empty?
95
+ raise ArgumentError.new("Path must be specified in the command line.")
96
+ end
97
+
98
+ if not STDIN.tty?
99
+ tty_in = true
100
+ else
101
+ tty_in = false
102
+ end
103
+
104
+ # only file
105
+ file_ok = mk_file != nil and (not tty_in or ARGV.length == 0)
106
+ # only gets
107
+ gets_ok = tty_in and (not mk_file or ARGV.length == 0)
108
+ # only argv
109
+ argv_ok = ARGV.length == 1 and (not tty_in or mk_file == nil)
110
+
111
+ if (file_ok and gets_ok or gets_ok and argv_ok or argv_ok and file_ok) or (not argv_ok and not gets_ok and not file_ok)
112
+ raise ArgumentError.new("Wrong number of arguments. Please look at the help (-h).")
113
+ end
114
+
115
+ mk_query_str=""
116
+ if mk_file != nil and not File.exists? mk_file
117
+ raise ArgumentError.new("The file containing the query doesn't exists.")
118
+ elsif mk_file != nil
119
+ File.open(mk_file) do |file|
120
+ while (line = file.gets)
121
+ mk_query_str << line
122
+ end
123
+ end
124
+ elsif not STDIN.tty?
125
+ while (line = STDIN.gets)
126
+ mk_query_str << line
127
+ end
128
+ else
129
+ mk_query_str = ARGV[0].gsub(/\\n/,"\n")
130
+ end
131
+
132
+ mk_request = Nagios::MkLiveStatus::Request.new(mk_path)
133
+
134
+ include Nagios::MkLiveStatus::Parser
135
+ mk_query = nagmk_parse(mk_query_str)
136
+
137
+ mk_results = mk_request.query(mk_query, mk_opts)
138
+
139
+ if log_opts[:name] != STDOUT or log_opts[:level] > Logger::INFO
140
+ puts mk_results
141
+ end
142
+ rescue Exception => ex
143
+ if log_opts[:name] != STDOUT
144
+ puts ex.message
145
+ end
146
+ end
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ # Set default configuration for the nagmk-ruby
3
+ #
4
+ # nagmk-ruby-config [-h|--help] [-p|--path PATH] [-r|--remove] [-o|--options NAME=VALUE] [-u|--unset NAME][-s|--show]
5
+ #
6
+ # arguments : -h|--help help
7
+ # -p|--path PATH path to the nagios mklivestatus socket by default
8
+ # -r|--remove remove the path from the config file
9
+ # -o|--options NAME=VALUE add option to the configuration
10
+ # -u|--unset NAME remove option from the configuration file.
11
+ # -s|--show show configuration
12
+ require 'rubygems'
13
+ require 'optparse'
14
+ require 'yaml'
15
+
16
+ mk_show = false
17
+
18
+ #loading options
19
+ mk_conf_file = File.join(File.absolute_path(File.dirname(__FILE__)), "nagmk-conf.yml")
20
+ mk_opts = YAML::load_file(mk_conf_file)
21
+
22
+ optparser = OptionParser.new do |opts|
23
+
24
+ # set help banner
25
+ opts.banner = "nagmk-ruby [-h|--help] -p|--path PATH [-o|--options NAME=VALUE] {[-f|--file FILE]|query}\n\nExecute the query or the file to the request.\n\n"
26
+
27
+ # add help option
28
+ opts.on( '-h', '--help', 'Display this screen' ) do
29
+ # show help
30
+ puts opts
31
+ # exit command
32
+ exit
33
+ end
34
+
35
+ # add show list
36
+ opts.on( '-s', '--show', 'show options' ) do
37
+ #set to show
38
+ mk_show=true
39
+
40
+ end
41
+
42
+ opts.on( '-p', '--path PATH', "Path to the Nagios MkLiveStatus socket according to specification :\n\ttcp://<host>:<port> for TCP or <path> for unix socket\n ") do |path|
43
+ mk_opts[:path] = path
44
+ end
45
+
46
+ opts.on( '-r', '--remove', "Remove the path from the configuration file") do
47
+ mk_opts.delete(:path)
48
+ end
49
+
50
+ opts.on( '-o', '--options NAME=VALUE', "Options to add to make the request.\n\tcolumn_headers : show column header\n\tlimit : limit output lines\n\toutput : \'json\' or \'python\' output formating\n\tlocal_time : local time settings\n\tuser : authenticated user\n ") do |opt|
51
+ if opt.match(/^([\w_]+)=(true|false)$/)
52
+ name,value = opt.split("=")
53
+ if value == "true"
54
+ mk_opts[:options][name.to_sym] = true
55
+ elsif value == "false"
56
+ mk_opts[:options][name.to_sym] = false
57
+ end
58
+ elsif opt.match(/^([\w_]+)=(\d)$/)
59
+ name,value = opt.split("=")
60
+ mk_opts[:options][name.to_sym] = value.to_i
61
+ elsif opt.match(/^([\w_]+)=(\w)$/)
62
+ name,value = opt.split("=")
63
+ mk_opts[:options][name.to_sym] = value
64
+ end
65
+ end
66
+
67
+ opts.on( '-u', '--unset NAME', "Remove option from the configuration file.") do |opt|
68
+ if mk_opts[:options].has_key? opt.to_sym
69
+ mk_opts[:options].delete(opt.to_sym)
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ optparser.parse!
76
+
77
+ begin
78
+
79
+ if mk_show
80
+ #re loading options
81
+ mk_opts = YAML::load_file(mk_conf_file)
82
+ puts mk_opts.to_yaml
83
+ else
84
+ # write options
85
+ File.open(mk_conf_file, "w") do |io|
86
+ io.write(mk_opts.to_yaml)
87
+ io.close
88
+ end
89
+ end
90
+
91
+ rescue Exception => ex
92
+ puts ex.message
93
+ end
@@ -0,0 +1,9 @@
1
+ ##
2
+ # Exception for nagios query manipulation : creation of predicat Filter, Stats, Wait
3
+ # or converting query to string
4
+ #
5
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
6
+ # Copyright:: Copyright (c) 2012 GIP RECIA
7
+ # License:: General Public Licence
8
+ class Nagios::MkLiveStatus::QueryException < Nagios::MkLiveStatus::Exception
9
+ end
@@ -0,0 +1,8 @@
1
+ ##
2
+ # Exception for nagios request error
3
+ #
4
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
5
+ # Copyright:: Copyright (c) 2012 GIP RECIA
6
+ # License:: General Public Licence
7
+ class Nagios::MkLiveStatus::RequestException < Nagios::MkLiveStatus::Exception
8
+ end
@@ -0,0 +1,19 @@
1
+ ##
2
+ # General Exception for nagios.
3
+ # Add logging error when raised
4
+ #
5
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
6
+ # Copyright:: Copyright (c) 2012 GIP RECIA
7
+ # License:: General Public Licence
8
+ class Nagios::MkLiveStatus::Exception < Exception
9
+
10
+ include Nagios::MkLiveStatus
11
+
12
+ def initialize(msg=nil)
13
+ super(msg)
14
+ logger.error(msg)
15
+ end
16
+
17
+ require File.join(File.dirname(__FILE__), File.basename(__FILE__, ".rb"), "query_exception")
18
+ require File.join(File.dirname(__FILE__), File.basename(__FILE__, ".rb"), "request_exception")
19
+ end
@@ -0,0 +1,64 @@
1
+ # This class is used to make a logical "AND" operator between two filter expressions.
2
+ #
3
+ # If one of the filter expression is also an "AND",
4
+ # it takes all the expressions of the operator as its own.
5
+ #
6
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
7
+ # Copyright:: Copyright (c) 2012 GIP RECIA
8
+ # License:: General Public Licence
9
+ class Nagios::MkLiveStatus::Filter::And < Nagios::MkLiveStatus::Filter
10
+
11
+ include Nagios::MkLiveStatus
12
+
13
+ #
14
+ # Create a new "AND" operator between left and right expressions.
15
+ #
16
+ # Those expressions must be of type Nagios::MkLiveStatus::Filter
17
+ #
18
+ def initialize(left_expr, right_expr)
19
+ if not left_expr.is_a? Nagios::MkLiveStatus::Filter or not right_expr.is_a? Nagios::MkLiveStatus::Filter
20
+ raise QueryException.new("The left and the right operand for an AND expression must be filter expressions.")
21
+ end
22
+
23
+ @expressions = Array.new
24
+ if left_expr.is_a? Nagios::MkLiveStatus::Filter::And
25
+ left_expr.get_expressions.each do |expr|
26
+ @expressions.push expr
27
+ end
28
+ else
29
+ @expressions.push left_expr
30
+ end
31
+
32
+ if right_expr.is_a? Nagios::MkLiveStatus::Filter::And
33
+ right_expr.get_expressions.each do |expr|
34
+ @expressions.push expr
35
+ end
36
+ else
37
+ @expressions.push right_expr
38
+ end
39
+
40
+ end
41
+
42
+ #
43
+ # Return all the expressions under the "AND". It's used by
44
+ # the new method in order to get all "AND" expressions into the same object.
45
+ #
46
+ def get_expressions
47
+ return @expressions
48
+ end
49
+
50
+ #
51
+ # Convert the current "AND" expression into a nagios query string
52
+ # Filter: ...
53
+ # Filter: ...
54
+ # And: 2
55
+ #
56
+ def to_s
57
+ and_arr = []
58
+ @expressions.each do |expr|
59
+ and_arr.push expr.to_s
60
+ end
61
+ and_arr.push "And: #{@expressions.length}"
62
+ return and_arr.join("\n")
63
+ end
64
+ end
@@ -0,0 +1,57 @@
1
+ # This class symbolise a filter unit. This unit represents a small filter predicate like, in the nagios query, this :
2
+ # Filter: host_name = name
3
+ #
4
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
5
+ # Copyright:: Copyright (c) 2012 GIP RECIA
6
+ # License:: General Public Licence
7
+ class Nagios::MkLiveStatus::Filter::Attr < Nagios::MkLiveStatus::Filter
8
+
9
+ include Nagios::MkLiveStatus
10
+ include Nagios::MkLiveStatus::QueryHelper::Comparator
11
+
12
+ #
13
+ # Create the unit predicate with the column name, the operator use to compare, and the value
14
+ # [attr_name] name of the column
15
+ # [attr_comp] operator, one of the constant of the class
16
+ # [attr_value] the value to compare to
17
+ #
18
+ def initialize (attr_name, attr_comp, attr_value)
19
+
20
+ list_comparator = get_all_comparators
21
+
22
+ if attr_name == nil or attr_name.empty?
23
+ raise QueryException.new("The name of the attribute must be set in order to create the filter")
24
+ else
25
+ @attr_name = attr_name
26
+ end
27
+
28
+ if list_comparator.index(attr_comp) == nil
29
+ raise QueryException.new("The comparator \"#{attr_comp}\" is not recognized.\n Please use one of : #{list_comparator.join(", ")}")
30
+ else
31
+ @attr_comp = attr_comp
32
+ end
33
+
34
+ @attr_value = attr_value
35
+ end
36
+
37
+ #
38
+ # Convert the class into the nagios query string:
39
+ # Filter: name comp value
40
+ #
41
+ def to_s
42
+ if @attr_name == nil or @attr_name.empty?
43
+ raise QueryException.new("The filter cannot be converted into string because the name of the attribute is not set.")
44
+ end
45
+
46
+ if @attr_comp == nil or @attr_comp.empty?
47
+ raise QueryException.new("The filter cannot be converted into string because the comparator of the attribute is not set.")
48
+ end
49
+
50
+ if @attr_value == nil or @attr_value.empty?
51
+ return "Filter: "+@attr_name+" "+@attr_comp
52
+ end
53
+
54
+ return "Filter: "+@attr_name+" "+@attr_comp+" "+@attr_value;
55
+ end
56
+
57
+ end
@@ -0,0 +1,49 @@
1
+ # This class is used to make a logical "NOT" operator for the filter expression.
2
+ #
3
+ # Author:: Esco-lan Team (mailto:team@esco-lan.org)
4
+ # Copyright:: Copyright (c) 2012 GIP RECIA
5
+ # License:: General Public Licence
6
+ class Nagios::MkLiveStatus::Filter::Negate < Nagios::MkLiveStatus::Filter
7
+
8
+ include Nagios::MkLiveStatus
9
+
10
+ #
11
+ # Create a new "Not" operator for the expression.
12
+ #
13
+ # Those expressions must be of type Nagios::MkLiveStatus::Filter
14
+ #
15
+ def initialize(expr)
16
+ if not expr.is_a? Nagios::MkLiveStatus::Filter
17
+ raise QueryException.new("The operand for a NEGATE expression must be a filter expression.")
18
+ end
19
+
20
+ @expression = expr
21
+
22
+ end
23
+
24
+ #
25
+ # Return the expression under the "Negate". It's used by
26
+ # the to_s method in order to remove the overflow of Negate expression.
27
+ #
28
+ def get_expression
29
+ return @expression
30
+ end
31
+
32
+ #
33
+ # Convert the current "Negate" expression into a nagios query string.
34
+ # Filter: ...
35
+ # Negate:
36
+ #
37
+ # If the sub expression is also a Negate, it returns the sub-sub expression without negating it.
38
+ #
39
+ def to_s
40
+ if @expression.is_a? Nagios::MkLiveStatus::Filter::Negate
41
+ return @expression.get_expression.to_s
42
+ else
43
+ negate_arr = []
44
+ negate_arr.push @expression.to_s
45
+ negate_arr.push "Negate: "
46
+ return negate_arr.join("\n")
47
+ end
48
+ end
49
+ end