icinga2 0.7.0.1 → 0.8.1.2

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.
@@ -17,23 +17,23 @@ module Icinga2
17
17
  #
18
18
  # @return [Hash] result
19
19
  #
20
- def add_hostgroup(params = {})
20
+ def add_hostgroup( params )
21
21
 
22
- host_group = params.dig(:host_group)
22
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
23
+ raise ArgumentError.new('missing params') if( params.size.zero? )
24
+
25
+ host_group = params.dig(:host_group)
23
26
  display_name = params.dig(:display_name)
24
27
 
25
- if host_group.nil?
26
- return {
27
- status: 404,
28
- message: 'no name for the hostgroup'
29
- }
30
- end
28
+ raise ArgumentError.new('Missing host_group') if( host_group.nil? )
29
+ raise ArgumentError.new('Missing display_name') if( display_name.nil? )
31
30
 
32
- Network.put(url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, host_group),
31
+ Network.put(
32
+ url: format('%s/objects/hostgroups/%s', @icinga_api_url_base, host_group),
33
33
  headers: @headers,
34
34
  options: @options,
35
- payload: { 'attrs' => { 'display_name' => display_name } })
36
-
35
+ payload: { 'attrs' => { 'display_name' => display_name } }
36
+ )
37
37
  end
38
38
 
39
39
  # delete a hostgroup
@@ -46,21 +46,20 @@ module Icinga2
46
46
  #
47
47
  # @return [Hash] result
48
48
  #
49
- def delete_hostgroup(params = {})
49
+ def delete_hostgroup( params )
50
+
51
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
52
+ raise ArgumentError.new('missing params') if( params.size.zero? )
50
53
 
51
54
  host_group = params.dig(:host_group)
52
55
 
53
- if host_group.nil?
54
- return {
55
- status: 404,
56
- message: 'no name for the hostgroup'
57
- }
58
- end
56
+ raise ArgumentError.new('Missing host_group') if( host_group.nil? )
59
57
 
60
- Network.delete(host: host_group,
61
- url: format('%s/v1/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, host_group),
58
+ Network.delete(
59
+ url: format('%s/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, host_group),
62
60
  headers: @headers,
63
- options: @options)
61
+ options: @options
62
+ )
64
63
  end
65
64
 
66
65
  # returns all usersgroups
@@ -72,36 +71,51 @@ module Icinga2
72
71
  # @icinga.hostgroups
73
72
  #
74
73
  # @example to get one user
75
- # @icinga.hostgroups(name: 'linux-servers')
74
+ # @icinga.hostgroups(host_group: 'linux-servers')
76
75
  #
77
76
  # @return [Hash] returns a hash with all hostgroups
78
77
  #
79
- def hostgroups(params = {})
78
+ def hostgroups( params = {} )
80
79
 
81
80
  host_group = params.dig(:host_group)
82
81
 
83
- Network.get(host: host_group,
84
- url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, host_group),
82
+ url =
83
+ if( host_group.nil? )
84
+ format( '%s/objects/hostgroups' , @icinga_api_url_base )
85
+ else
86
+ format( '%s/objects/hostgroups/%s', @icinga_api_url_base, host_group )
87
+ end
88
+
89
+ data = Network.api_data(
90
+ url: url,
85
91
  headers: @headers,
86
- options: @options)
92
+ options: @options
93
+ )
94
+
95
+ return data.dig('results') if( data.dig(:status).nil? )
87
96
 
97
+ nil
88
98
  end
89
99
 
90
100
  # returns true if the hostgroup exists
91
101
  #
92
- # @param [String] name the name of the hostgroup
102
+ # @param [String] host_group the name of the hostgroup
93
103
  #
94
104
  # @example
95
105
  # @icinga.exists_hostgroup?('linux-servers')
96
106
  #
97
107
  # @return [Bool] returns true if the hostgroup exists
98
108
  #
99
- def exists_hostgroup?(name)
100
- result = hostgroups(host_group: name)
101
- result = JSON.parse(result) if result.is_a?(String)
102
- status = result.dig(:status)
109
+ def exists_hostgroup?( host_group )
110
+
111
+ raise ArgumentError.new('only String are allowed') unless( host_group.is_a?(String) )
112
+ raise ArgumentError.new('Missing host_group') if( host_group.size.zero? )
113
+
114
+ result = hostgroups(host_group: host_group)
115
+ result = JSON.parse( result ) if result.is_a?( String )
116
+
117
+ return true if !result.nil? && result.is_a?(Array)
103
118
 
104
- return true if !status.nil? && status == 200
105
119
  false
106
120
  end
107
121
 
@@ -32,7 +32,10 @@ module Icinga2
32
32
  #
33
33
  # @return [Hash]
34
34
  #
35
- def add_host( params = {} )
35
+ def add_host( params )
36
+
37
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
38
+ raise ArgumentError.new('missing params') if( params.size.zero? )
36
39
 
37
40
  host = params.dig(:host)
38
41
  fqdn = params.dig(:fqdn)
@@ -46,12 +49,14 @@ module Icinga2
46
49
  action_url = params.dig(:action_url)
47
50
  vars = params.dig(:vars) || {}
48
51
 
49
- if( host.nil? )
50
- return {
51
- status: 404,
52
- message: 'missing host name'
53
- }
54
- end
52
+ raise ArgumentError.new('Missing host') if( host.nil? )
53
+ raise ArgumentError.new('only true or false for notifications are allowed') unless( notifications.is_a?(TrueClass) || notifications.is_a?(FalseClass) )
54
+ raise ArgumentError.new('only Integer for max_check_attempts are allowed') unless( max_check_attempts.is_a?(Integer) )
55
+ raise ArgumentError.new('only Integer for check_interval are allowed') unless( check_interval.is_a?(Integer) )
56
+ raise ArgumentError.new('only Integer for retry_interval are allowed') unless( retry_interval.is_a?(Integer) )
57
+ raise ArgumentError.new('only String for notes are allowed') unless( notes.is_a?(String) || notes.nil? )
58
+ raise ArgumentError.new('only String for notes_url are allowed') unless( notes_url.is_a?(String) || notes_url.nil? )
59
+ raise ArgumentError.new('only Hash for vars are allowed') unless( vars.is_a?(Hash) )
55
60
 
56
61
  if( fqdn.nil? )
57
62
  # build FQDN
@@ -79,14 +84,14 @@ module Icinga2
79
84
  payload['attrs']['zone'] = @icinga_satellite
80
85
  end
81
86
 
82
- logger.debug( JSON.pretty_generate( payload ) )
87
+ # logger.debug( JSON.pretty_generate( payload ) )
83
88
 
84
- Network.put( host: host,
85
- url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
89
+ Network.put(
90
+ url: format( '%s/objects/hosts/%s', @icinga_api_url_base, host ),
86
91
  headers: @headers,
87
92
  options: @options,
88
- payload: payload )
89
-
93
+ payload: payload
94
+ )
90
95
  end
91
96
 
92
97
  # delete a host
@@ -99,23 +104,20 @@ module Icinga2
99
104
  #
100
105
  # @return [Hash] result
101
106
  #
102
- def delete_host( params = {} )
107
+ def delete_host( params )
103
108
 
104
- host = params.dig(:host)
109
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
110
+ raise ArgumentError.new('missing params') if( params.size.zero? )
105
111
 
106
- if( host.nil? )
112
+ host = params.dig(:host)
107
113
 
108
- return {
109
- status: 404,
110
- message: 'missing host name'
111
- }
112
- end
114
+ raise ArgumentError.new('Missing host') if( host.nil? )
113
115
 
114
- Network.delete( host: host,
115
- url: format( '%s/v1/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
116
+ Network.delete(
117
+ url: format( '%s/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
116
118
  headers: @headers,
117
- options: @options )
118
-
119
+ options: @options
120
+ )
119
121
  end
120
122
 
121
123
  # return hosts
@@ -130,9 +132,9 @@ module Icinga2
130
132
  # @icinga.hosts
131
133
  #
132
134
  # @example to get one host
133
- # @icinga.host(host: 'icinga2')
135
+ # @icinga.hosts(host: 'icinga2')
134
136
  #
135
- # @return [Hash]
137
+ # @return [Array]
136
138
  #
137
139
  def hosts( params = {} )
138
140
 
@@ -141,33 +143,40 @@ module Icinga2
141
143
  filter = params.dig(:filter)
142
144
  joins = params.dig(:joins)
143
145
 
144
- payload['attrs'] = attrs unless attrs.nil?
145
- payload['filter'] = filter unless filter.nil?
146
- payload['joins'] = joins unless joins.nil?
146
+ payload['attrs'] = attrs unless attrs.nil?
147
+ payload['filter'] = filter unless filter.nil?
148
+ payload['joins'] = joins unless joins.nil?
147
149
 
148
- Network.get( host: host,
149
- url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
150
+ data = Network.api_data(
151
+ url: format( '%s/objects/hosts/%s', @icinga_api_url_base, host ),
150
152
  headers: @headers,
151
- options: @options )
153
+ options: @options
154
+ )
152
155
 
156
+ return data.dig('results') if( data.dig(:status).nil? )
157
+
158
+ nil
153
159
  end
154
160
 
155
161
  # returns true if the host exists
156
162
  #
157
- # @param [String] name
163
+ # @param [String] host_name
158
164
  #
159
165
  # @example
160
166
  # @icinga.exists_host?('icinga2')
161
167
  #
162
168
  # @return [Bool]
163
169
  #
164
- def exists_host?( name )
170
+ def exists_host?( host_name )
171
+
172
+ raise ArgumentError.new('only String are allowed') unless( host_name.is_a?(String) )
173
+ raise ArgumentError.new('Missing host_name') if( host_name.size.zero? )
165
174
 
166
- result = hosts( name: name )
175
+ result = hosts( host: host_name )
167
176
  result = JSON.parse( result ) if result.is_a?( String )
168
- status = result.dig(:status)
169
177
 
170
- return true if !status.nil? && status == 200
178
+ return true if !result.nil? && result.is_a?(Array)
179
+
171
180
  false
172
181
  end
173
182
 
@@ -191,115 +200,219 @@ module Icinga2
191
200
  attrs = params.dig(:attrs)
192
201
  filter = params.dig(:filter)
193
202
  joins = params.dig(:joins)
203
+
204
+ # raise ArgumentError.new('only Array for attrs are allowed') unless( attrs.is_a?(Hash) )
205
+ # raise ArgumentError.new('only Array for filter are allowed') unless( filter.is_a?(Hash) )
206
+ # raise ArgumentError.new('only Array for joins are allowed') unless( joins.is_a?(Hash) )
207
+
194
208
  payload = {}
209
+ results = nil
195
210
 
196
211
  if( attrs.nil? )
197
212
  attrs = %w[name state acknowledgement downtime_depth last_check]
198
213
  end
199
214
 
200
- payload['attrs'] = attrs unless attrs.nil?
201
- payload['filter'] = filter unless filter.nil?
202
- payload['joins'] = joins unless joins.nil?
215
+ payload['attrs'] = attrs unless attrs.nil?
216
+ payload['filter'] = filter unless filter.nil?
217
+ payload['joins'] = joins unless joins.nil?
203
218
 
204
- Network.get( host: nil,
205
- url: format( '%s/v1/objects/hosts', @icinga_api_url_base ),
219
+ data = Network.api_data(
220
+ url: format( '%s/objects/hosts', @icinga_api_url_base ),
206
221
  headers: @headers,
207
222
  options: @options,
208
- payload: payload )
223
+ payload: payload
224
+ )
225
+
226
+ status = data.dig(:status)
227
+
228
+ if( status.nil? )
229
+
230
+ results = data.dig('results')
231
+
232
+ unless( results.nil? )
233
+
234
+ all_hosts = results.clone
209
235
 
236
+ unless( all_hosts.nil? )
237
+
238
+ # global var for count of all hosts
239
+ @hosts_all = all_hosts.size
240
+ # global var for count of all host with a problem
241
+ @hosts_problems = count_problems(results)
242
+ # global var for count of all gost with state HOSTS_DOWN
243
+ @hosts_problems_down = count_problems(results, Icinga2::HOSTS_DOWN)
244
+ @hosts_problems_critical = count_problems(results, Icinga2::HOSTS_CRITICAL)
245
+ @hosts_problems_unknown = count_problems(results, Icinga2::HOSTS_UNKNOWN)
246
+
247
+ end
248
+ end
249
+ end
250
+
251
+ results
210
252
  end
211
253
 
212
- # return count of hosts with problems
254
+ # returns adjusted hosts state
213
255
  #
214
256
  # @example
215
- # @icinga.host_problems
257
+ # @icinga.cib_data
258
+ # @icinga.host_objects
259
+ # handled, down = @icinga.hosts_adjusted.values
216
260
  #
217
- # @return [Integer]
261
+ # h = @icinga.hosts_adjusted
262
+ # down = h.dig(:down_adjusted)
218
263
  #
219
- def host_problems
264
+ # @return [Hash]
265
+ # * handled_problems
266
+ # * down_adjusted
267
+ #
268
+ def hosts_adjusted
220
269
 
221
- data = host_objects
222
- problems = 0
270
+ raise ArgumentError.new('Integer for @hosts_problems_down needed') unless( @hosts_problems_down.is_a?(Integer) )
271
+ raise ArgumentError.new('Integer for @hosts_problems_critical needed') unless( @hosts_problems_critical.is_a?(Integer) )
272
+ raise ArgumentError.new('Integer for @hosts_problems_unknown needed') unless( @hosts_problems_unknown.is_a?(Integer) )
273
+ raise ArgumentError.new('Integer for @hosts_down needed') unless( @hosts_down.is_a?(Integer) )
223
274
 
224
- data = JSON.parse(data) if data.is_a?(String)
225
- nodes = data.dig(:nodes)
275
+ # calculate host problems adjusted by handled problems
276
+ # count togther handled host problems
277
+ handled_problems = @hosts_problems_down + @hosts_problems_critical + @hosts_problems_unknown
278
+ down_adjusted = @hosts_down - handled_problems
226
279
 
227
- unless( nodes.nil? )
280
+ {
281
+ handled_problems: handled_problems.to_i,
282
+ down_adjusted: down_adjusted.to_i
283
+ }
284
+ end
228
285
 
229
- nodes.each do |n|
286
+ # return count of hosts with problems
287
+ #
288
+ # @example
289
+ # @icinga.count_hosts_with_problems
290
+ #
291
+ # @return [Integer]
292
+ #
293
+ def count_hosts_with_problems
230
294
 
231
- attrs = n.last.dig('attrs')
232
- state = attrs.dig('state') || 0
233
- downtime_depth = attrs.dig('downtime_depth') || 0
234
- acknowledgement = attrs.dig('acknowledgement') || 0
295
+ host_data = host_objects
296
+ host_data = JSON.parse(host_data) if host_data.is_a?(String)
235
297
 
236
- if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
237
- problems += 1
238
- end
298
+ f = host_data.select { |t| t.dig('attrs','state') != 0 && t.dig('attrs','downtime_depth').zero? && t.dig('attrs','acknowledgement').zero? }
239
299
 
240
- end
241
- end
242
- problems
300
+ f.size
243
301
  end
244
302
 
245
- # return a list of host with problems
303
+ # return a list of hosts with problems
246
304
  #
247
305
  # @param [Integer] max_items numbers of list entries
248
306
  #
249
307
  # @example
250
- # @icinga.problem_hosts
308
+ # @icinga.list_hosts_with_problems
251
309
  #
252
310
  # @return [Hash]
253
311
  #
254
- def problem_hosts( max_items = 5 )
312
+ def list_hosts_with_problems( max_items = 5 )
255
313
 
256
- @host_problems = {}
257
- @host_problems_severity = {}
314
+ raise ArgumentError.new('only Integer for max_items are allowed') unless( max_items.is_a?(Integer) )
258
315
 
259
- host_data = host_objects
316
+ host_problems = {}
317
+ host_problems_severity = {}
260
318
 
261
- host_data = JSON.parse( host_data ) if host_data.is_a?(String)
262
- host_data = host_data.dig(:nodes)
319
+ host_data = host_objects
320
+ host_data = JSON.parse( host_data ) if host_data.is_a?(String)
263
321
 
264
322
  unless( host_data.nil? )
265
323
 
266
- host_data.each do |_host,v|
267
-
268
- name = v.dig('name')
269
- state = v.dig('attrs','state')
324
+ host_data.each do |h,_v|
325
+ name = h.dig('name')
326
+ state = h.dig('attrs','state')
270
327
 
271
- next if state.zero?
328
+ next if state.to_i.zero?
272
329
 
273
- @host_problems[name] = host_severity(v)
330
+ host_problems[name] = host_severity(h)
274
331
  end
332
+ end
275
333
 
276
- # get the count of problems
277
- #
278
- if( @host_problems.count != 0 )
279
- @host_problems.keys[1..max_items].each { |k,_v| @host_problems_severity[k] = @host_problems[k] }
280
- end
334
+ # get the count of problems
335
+ #
336
+ if( host_problems.count != 0 )
337
+ host_problems.keys[1..max_items].each { |k,_v| host_problems_severity[k] = host_problems[k] }
281
338
  end
282
- @host_problems_severity
283
339
 
340
+ host_problems_severity
341
+ end
342
+
343
+ # returns a counter of all hosts
344
+ #
345
+ # @example
346
+ # @icinga.host_objects
347
+ # @icinga.hosts_all
348
+ #
349
+ # @return [Integer]
350
+ #
351
+ def hosts_all
352
+ @hosts_all
353
+ end
354
+
355
+ # returns data with host problems
356
+ #
357
+ # @example
358
+ # @icinga.host_objects
359
+ # all, down, critical, unknown = @icinga.host_problems.values
360
+ #
361
+ # p = @icinga.host_problems
362
+ # down = h.dig(:down)
363
+ #
364
+ # @return [Hash]
365
+ # * all
366
+ # * down
367
+ # * critical
368
+ # * unknown
369
+ #
370
+ def host_problems
371
+
372
+ problems_all = @hosts_problems.nil? ? 0 : @hosts_problems
373
+ problems_down = @hosts_problems_down.nil? ? 0 : @hosts_problems_down
374
+ problems_critical = @hosts_problems_critical.nil? ? 0 : @hosts_problems_critical
375
+ problems_unknown = @hosts_problems_unknown.nil? ? 0 : @hosts_problems_unknown
376
+
377
+ {
378
+ all: problems_all.to_i,
379
+ down: problems_down.to_i,
380
+ critical: problems_critical.to_i,
381
+ unknown: problems_unknown.to_i
382
+ }
284
383
  end
285
384
 
385
+ protected
286
386
  # calculate a host severity
287
387
  #
288
388
  # stolen from Icinga Web 2
289
389
  # ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
290
390
  #
291
- # @param [Hash] host
391
+ # @param [Hash] params
392
+ # @option params [hash] attrs ()
393
+ # * state [Float]
394
+ # * acknowledgement [Float] (default: 0)
395
+ # * downtime_depth [Float] (default: 0)
292
396
  #
293
- # @private
397
+ # @api protected
294
398
  #
295
- # @return [Hash]
399
+ # @example
400
+ # host_severity( {'attrs' => { 'state' => 0.0, 'acknowledgement' => 0.0, 'downtime_depth' => 0.0 } } )
296
401
  #
297
- def host_severity( host )
402
+ # @return [Integer]
403
+ #
404
+ def host_severity( params )
405
+
406
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
407
+ raise ArgumentError.new('missing params') if( params.size.zero? )
408
+
409
+ state = params.dig('attrs','state')
410
+ acknowledgement = params.dig('attrs','acknowledgement') || 0
411
+ downtime_depth = params.dig('attrs','downtime_depth') || 0
298
412
 
299
- attrs = host.dig('attrs')
300
- state = attrs.dig('state')
301
- acknowledgement = attrs.dig('acknowledgement') || 0
302
- downtime_depth = attrs.dig('downtime_depth') || 0
413
+ raise ArgumentError.new('only Float for state are allowed') unless( state.is_a?(Float) )
414
+ raise ArgumentError.new('only Float for acknowledgement are allowed') unless( acknowledgement.is_a?(Float) )
415
+ raise ArgumentError.new('only Float for downtime_depth are allowed') unless( downtime_depth.is_a?(Float) )
303
416
 
304
417
  severity = 0
305
418
 
@@ -312,7 +425,7 @@ module Icinga2
312
425
  4
313
426
  end
314
427
 
315
- severity += 16 if object_has_been_checked?(host)
428
+ severity += 16 if object_has_been_checked?(params)
316
429
 
317
430
  unless state.zero?
318
431