icinga2 0.9.2.1 → 0.9.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -22
  3. data/doc/Array.html +4 -6
  4. data/doc/Boolean.html +4 -6
  5. data/doc/FalseClass.html +4 -6
  6. data/doc/Hash.html +126 -8
  7. data/doc/Icinga2.html +7 -9
  8. data/doc/Logging.html +5 -7
  9. data/doc/Object.html +8 -10
  10. data/doc/Time.html +4 -6
  11. data/doc/TrueClass.html +4 -6
  12. data/doc/_index.html +20 -7
  13. data/doc/class_list.html +1 -1
  14. data/doc/file.README.html +32 -30
  15. data/doc/frames.html +1 -1
  16. data/doc/index.html +32 -30
  17. data/doc/method_list.html +34 -2
  18. data/doc/services.md +45 -62
  19. data/doc/top-level-namespace.html +4 -6
  20. data/examples/_blank.rb +2 -2
  21. data/examples/config.rb +23 -0
  22. data/examples/downtimes.rb +4 -33
  23. data/examples/hostgroups.rb +4 -33
  24. data/examples/hosts.rb +18 -33
  25. data/examples/informations.rb +4 -33
  26. data/examples/notifications.rb +4 -33
  27. data/examples/servicegroups.rb +4 -25
  28. data/examples/services.rb +46 -67
  29. data/examples/statistics.rb +4 -33
  30. data/examples/test.rb +7 -28
  31. data/examples/usergroups.rb +4 -33
  32. data/examples/users.rb +4 -33
  33. data/lib/icinga2/client.rb +16 -42
  34. data/lib/icinga2/converts.rb +16 -54
  35. data/lib/icinga2/downtimes.rb +46 -44
  36. data/lib/icinga2/hostgroups.rb +35 -35
  37. data/lib/icinga2/hosts.rb +235 -228
  38. data/lib/icinga2/network.rb +53 -125
  39. data/lib/icinga2/notifications.rb +37 -46
  40. data/lib/icinga2/servicegroups.rb +31 -41
  41. data/lib/icinga2/services.rb +211 -236
  42. data/lib/icinga2/tools.rb +10 -9
  43. data/lib/icinga2/usergroups.rb +22 -32
  44. data/lib/icinga2/users.rb +64 -59
  45. data/lib/icinga2/validator.rb +59 -0
  46. data/lib/icinga2/version.rb +1 -1
  47. metadata +78 -7
  48. data/lib/icinga2/network.rb-SAVE +0 -1004
@@ -8,11 +8,11 @@ module Icinga2
8
8
  # static function for GET Requests
9
9
  #
10
10
  # @param [Hash] params
11
- # @option params [String] :host
12
- # @option params [String] :url
13
- # @option params [String] :headers
14
- # @option params [String] :options
15
- # @option params [Hash] :payload
11
+ # @option params [String] host
12
+ # @option params [String] url
13
+ # @option params [String] headers
14
+ # @option params [String] options
15
+ # @option params [Hash] payload
16
16
  #
17
17
  #
18
18
  # @return [Hash]
@@ -22,25 +22,16 @@ module Icinga2
22
22
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
23
23
  raise ArgumentError.new('missing params') if( params.size.zero? )
24
24
 
25
- url = params.dig(:url)
26
- headers = params.dig(:headers)
27
- options = params.dig(:options)
28
- payload = params.dig(:payload)
29
-
30
- raise ArgumentError.new('Missing url') if( url.nil? )
31
- raise ArgumentError.new('Missing headers') if( headers.nil? )
32
- raise ArgumentError.new('Missing options') if( options.nil? )
25
+ url = validate( params, required: true, var: 'url', type: String )
26
+ headers = validate( params, required: true, var: 'headers', type: Hash )
27
+ options = validate( params, required: true, var: 'options', type: Hash ).deep_symbolize_keys
28
+ payload = validate( params, required: false, var: 'payload', type: Hash )
33
29
 
34
30
  rest_client = RestClient::Resource.new( URI.encode( url ), options )
35
31
 
36
- if( payload )
37
- raise ArgumentError.new('only Hash for payload are allowed') unless( payload.is_a?(Hash) )
38
- headers['X-HTTP-Method-Override'] = 'GET'
39
- method = 'POST'
40
- else
41
- headers['X-HTTP-Method-Override'] = 'GET'
42
- method = 'GET'
43
- end
32
+ headers['X-HTTP-Method-Override'] = 'GET'
33
+ method = 'GET'
34
+ method = 'POST' if( payload )
44
35
 
45
36
  begin
46
37
  data = request( rest_client, method, headers, payload )
@@ -50,9 +41,7 @@ module Icinga2
50
41
  data = data.dig('results') if( data.is_a?(Hash) )
51
42
 
52
43
  return data
53
-
54
44
  rescue => e
55
-
56
45
  logger.error(e)
57
46
  logger.error(e.backtrace.join("\n"))
58
47
 
@@ -63,11 +52,11 @@ module Icinga2
63
52
  # static function for GET Requests without filters
64
53
  #
65
54
  # @param [Hash] params
66
- # @option params [String] :host
67
- # @option params [String] :url
68
- # @option params [String] :headers
69
- # @option params [String] :options
70
- # @option params [Hash] :payload
55
+ # @option params [String] host
56
+ # @option params [String] url
57
+ # @option params [String] headers
58
+ # @option params [String] options
59
+ # @option params [Hash] payload
71
60
  #
72
61
  #
73
62
  # @return [Hash]
@@ -77,25 +66,16 @@ module Icinga2
77
66
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
78
67
  raise ArgumentError.new('missing params') if( params.size.zero? )
79
68
 
80
- url = params.dig(:url)
81
- headers = params.dig(:headers)
82
- options = params.dig(:options)
83
-
84
- raise ArgumentError.new('Missing url') if( url.nil? )
85
- raise ArgumentError.new('Missing headers') if( headers.nil? )
86
- raise ArgumentError.new('Missing options') if( options.nil? )
69
+ url = validate( params, required: true, var: 'url', type: String )
70
+ headers = validate( params, required: true, var: 'headers', type: Hash )
71
+ options = validate( params, required: true, var: 'options', type: Hash ).deep_symbolize_keys
87
72
 
88
73
  begin
89
-
90
74
  data = api_data( url: url, headers: headers, options: options )
91
75
  data = data.first if( data.is_a?(Array) )
92
76
 
93
- data
94
-
95
77
  return data.dig('status') unless( data.nil? )
96
-
97
78
  rescue => e
98
-
99
79
  logger.error(e)
100
80
  logger.error(e.backtrace.join("\n"))
101
81
 
@@ -107,10 +87,10 @@ module Icinga2
107
87
  # static function for POST Requests
108
88
  #
109
89
  # @param [Hash] params
110
- # @option params [String] :url
111
- # @option params [String] :headers
112
- # @option params [String] :options
113
- # @option params [Hash] :payload
90
+ # @option params [String] url
91
+ # @option params [String] headers
92
+ # @option params [String] options
93
+ # @option params [Hash] payload
114
94
  #
115
95
  #
116
96
  # @return [Hash]
@@ -120,17 +100,13 @@ module Icinga2
120
100
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
121
101
  raise ArgumentError.new('missing params') if( params.size.zero? )
122
102
 
123
- url = params.dig(:url)
124
- headers = params.dig(:headers)
125
- options = params.dig(:options)
126
- payload = params.dig(:payload)
127
-
128
- raise ArgumentError.new('Missing url') if( url.nil? )
129
- raise ArgumentError.new('Missing headers') if( headers.nil? )
130
- raise ArgumentError.new('Missing options') if( options.nil? )
131
- raise ArgumentError.new('only Hash for payload are allowed') unless( payload.is_a?(Hash) )
103
+ url = validate( params, required: true, var: 'url', type: String )
104
+ headers = validate( params, required: true, var: 'headers', type: Hash )
105
+ options = validate( params, required: true, var: 'options', type: Hash ).deep_symbolize_keys
106
+ payload = validate( params, required: false, var: 'payload', type: Hash )
132
107
 
133
108
  rest_client = RestClient::Resource.new( URI.encode( url ), options )
109
+
134
110
  headers['X-HTTP-Method-Override'] = 'POST'
135
111
 
136
112
  begin
@@ -141,9 +117,7 @@ module Icinga2
141
117
  data = data.dig('results').first if( data.is_a?(Hash) )
142
118
 
143
119
  return { 'code' => data.dig('code').to_i, 'name' => data.dig('name'), 'status' => data.dig('status') } unless( data.nil? )
144
-
145
120
  rescue => e
146
-
147
121
  logger.error(e)
148
122
  logger.error(e.backtrace.join("\n"))
149
123
 
@@ -154,11 +128,11 @@ module Icinga2
154
128
  # static function for PUT Requests
155
129
  #
156
130
  # @param [Hash] params
157
- # @option params [String] :host
158
- # @option params [String] :url
159
- # @option params [String] :headers
160
- # @option params [String] :options
161
- # @option params [Hash] :payload
131
+ # @option params [String] host
132
+ # @option params [String] url
133
+ # @option params [String] headers
134
+ # @option params [String] options
135
+ # @option params [Hash] payload
162
136
  #
163
137
  #
164
138
  # @return [Hash]
@@ -168,21 +142,16 @@ module Icinga2
168
142
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
169
143
  raise ArgumentError.new('missing params') if( params.size.zero? )
170
144
 
171
- url = params.dig(:url)
172
- headers = params.dig(:headers)
173
- options = params.dig(:options)
174
- payload = params.dig(:payload)
175
-
176
- raise ArgumentError.new('Missing url') if( url.nil? )
177
- raise ArgumentError.new('Missing headers') if( headers.nil? )
178
- raise ArgumentError.new('Missing options') if( options.nil? )
179
- raise ArgumentError.new('only Hash for payload are allowed') unless( payload.is_a?(Hash) )
145
+ url = validate( params, required: true, var: 'url', type: String )
146
+ headers = validate( params, required: true, var: 'headers', type: Hash )
147
+ options = validate( params, required: true, var: 'options', type: Hash ).deep_symbolize_keys
148
+ payload = validate( params, required: false, var: 'payload', type: Hash )
180
149
 
181
150
  rest_client = RestClient::Resource.new( URI.encode( url ), options )
151
+
182
152
  headers['X-HTTP-Method-Override'] = 'PUT'
183
153
 
184
154
  begin
185
-
186
155
  data = request( rest_client, 'PUT', headers, payload )
187
156
  data = JSON.parse( data ) if( data.is_a?(String) )
188
157
  data = data.deep_string_keys
@@ -195,9 +164,7 @@ module Icinga2
195
164
  end
196
165
 
197
166
  return { 'code' => results.dig('code').to_i, 'name' => results.dig('name'), 'status' => results.dig('status') } unless( results.nil? )
198
-
199
167
  rescue => e
200
-
201
168
  logger.error(e)
202
169
  logger.error(e.backtrace.join("\n"))
203
170
 
@@ -208,9 +175,9 @@ module Icinga2
208
175
  # static function for DELETE Requests
209
176
  #
210
177
  # @param [Hash] params
211
- # @option params [String] :url
212
- # @option params [String] :headers
213
- # @option params [String] :options
178
+ # @option params [String] url
179
+ # @option params [String] headers
180
+ # @option params [String] options
214
181
  #
215
182
  #
216
183
  # @return [Hash]
@@ -220,15 +187,12 @@ module Icinga2
220
187
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
221
188
  raise ArgumentError.new('missing params') if( params.size.zero? )
222
189
 
223
- url = params.dig(:url)
224
- headers = params.dig(:headers)
225
- options = params.dig(:options)
226
-
227
- raise ArgumentError.new('Missing url') if( url.nil? )
228
- raise ArgumentError.new('Missing headers') if( headers.nil? )
229
- raise ArgumentError.new('Missing options') if( options.nil? )
190
+ url = validate( params, required: true, var: 'url', type: String )
191
+ headers = validate( params, required: true, var: 'headers', type: Hash )
192
+ options = validate( params, required: true, var: 'options', type: Hash ).deep_symbolize_keys
230
193
 
231
194
  rest_client = RestClient::Resource.new( URI.encode( url ), options )
195
+
232
196
  headers['X-HTTP-Method-Override'] = 'DELETE'
233
197
 
234
198
  begin
@@ -245,7 +209,6 @@ module Icinga2
245
209
  end
246
210
 
247
211
  return { 'code' => results.dig('code').to_i, 'name' => results.dig('name'), 'status' => results.dig('status') } unless( results.nil? )
248
-
249
212
  rescue => e
250
213
  logger.error(e)
251
214
  logger.error(e.backtrace.join("\n"))
@@ -264,10 +227,7 @@ module Icinga2
264
227
 
265
228
  raise ArgumentError.new('client must be an RestClient::Resource') unless( client.is_a?(RestClient::Resource) )
266
229
  raise ArgumentError.new('method must be an \'GET\', \'POST\', \'PUT\' or \'DELETE\'') unless( %w[GET POST PUT DELETE].include?(method) )
267
-
268
- unless( data.nil? )
269
- raise ArgumentError.new(format('data must be an Hash (%s)', data.class.to_s)) unless( data.is_a?(Hash) )
270
- end
230
+ raise ArgumentError.new(format('data must be an Hash (%s)', data.class.to_s)) unless( data.nil? || data.is_a?(Hash) )
271
231
 
272
232
  max_retries = 3
273
233
  retried = 0
@@ -286,16 +246,9 @@ module Icinga2
286
246
  client.put( data.to_json, headers ) do |response, req, _result|
287
247
 
288
248
  @req = req
289
- @response_raw = response
290
249
  @response_body = response.body
291
250
  @response_code = response.code.to_i
292
251
 
293
- # logger.debug('----------------------------')
294
- # logger.debug(@response_raw)
295
- # logger.debug(@response_body)
296
- # logger.debug(@response_code)
297
- # logger.debug('----------------------------')
298
-
299
252
  case response.code
300
253
  when 200
301
254
  return @response_body
@@ -313,7 +266,6 @@ module Icinga2
313
266
  when 'DELETE'
314
267
  response = client.delete( @headers )
315
268
  else
316
- @logger.error( "Error: #{__method__} is not a valid request method." )
317
269
  return false
318
270
  end
319
271
 
@@ -327,28 +279,15 @@ module Icinga2
327
279
 
328
280
  response_body = JSON.parse(response_body) if response_body.is_a?(String)
329
281
 
330
- return {
331
- 'results' => [{
332
- 'code' => 400,
333
- 'status' => response_body.nil? ? 'Bad Request' : response_body
334
- }]
335
- }
282
+ return { 'results' => [{ 'code' => 400, 'status' => response_body.nil? ? 'Bad Request' : response_body }] }
336
283
 
337
284
  rescue RestClient::Unauthorized
338
285
 
339
- return {
340
- 'code' => 401,
341
- 'status' => format('Not authorized to connect \'%s\' - wrong username or password?', @icinga_api_url_base)
342
- }
286
+ return { 'results' => [{ 'code' => 401, 'status' => format('Not authorized to connect \'%s\' - wrong username or password?', @icinga_api_url_base) }] }
343
287
 
344
288
  rescue RestClient::NotFound
345
289
 
346
- return {
347
- 'results' => [{
348
- 'code' => 404,
349
- 'status' => 'Object not Found'
350
- }]
351
- }
290
+ return { 'results' => [{ 'code' => 404, 'status' => 'Object not Found' }] }
352
291
 
353
292
  rescue RestClient::InternalServerError
354
293
 
@@ -361,12 +300,7 @@ module Icinga2
361
300
  errors = errors.first if( errors.is_a?(Array) )
362
301
  errors = errors.sub(/ \'.*\'/,'')
363
302
 
364
- return {
365
- 'results' => [{
366
- 'code' => 500,
367
- 'status' => format('%s (%s)', status, errors).delete('.')
368
- }]
369
- }
303
+ return { 'results' => [{ 'code' => 500, 'status' => format('%s (%s)', status, errors).delete('.') }] }
370
304
 
371
305
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
372
306
 
@@ -375,7 +309,7 @@ module Icinga2
375
309
  raise format( "Maximum retries (%d) against '%s' reached. Giving up ...", max_retries, @icinga_api_url_base ) if( retried >= max_retries )
376
310
 
377
311
  retried += 1
378
- $stderr.puts(format("Cannot execute request against '%s': '%s' (retry %d / %d)", @icinga_api_url_base, e, retried, max_retries))
312
+ warn(format("Cannot execute request against '%s': '%s' (retry %d / %d)", @icinga_api_url_base, e, retried, max_retries))
379
313
  sleep(3)
380
314
  retry
381
315
 
@@ -386,13 +320,7 @@ module Icinga2
386
320
  @logger.error( @headers )
387
321
  @logger.error( JSON.pretty_generate( response_headers ) )
388
322
 
389
-
390
- return {
391
- 'results' => [{
392
- 'code' => 500,
393
- 'status' => e
394
- }]
395
- }
323
+ return { 'results' => [{ 'code' => 500, 'status' => e }] }
396
324
  end
397
325
 
398
326
  end
@@ -84,50 +84,40 @@ module Icinga2
84
84
 
85
85
  # enable hostgroup notifications
86
86
  #
87
- # @param [Hash] params
88
- # @option params [String] host
89
- # @option params [String] host_group
87
+ # @param [String] host_group
90
88
  #
91
89
  # @example
92
- # @icinga.enable_hostgroup_notification(host: 'icinga2', host_group: 'linux-servers')
90
+ # @icinga.enable_hostgroup_notification('linux-servers')
93
91
  #
94
92
  # @return [Hash]
95
93
  #
96
- def enable_hostgroup_notification( params )
94
+ def enable_hostgroup_notification( host_group )
97
95
 
98
- raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
99
- raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
96
+ raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) unless( host_group.is_a?(String) )
97
+ raise ArgumentError.new('missing \'host_group\'') if( host_group.size.zero? )
100
98
 
101
- host_group = params.dig(:host_group)
102
- raise ArgumentError.new('Missing host_group') if( host_group.nil? )
103
-
104
- return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( host_group ) == false )
99
+ return { 'code' => 404, 'status' => 'Object not Found' } unless( exists_hostgroup?( host_group ) )
105
100
 
106
101
  hostgroup_notification( host_group: host_group, enable_notifications: true )
107
102
  end
108
103
 
109
104
  # disable hostgroup notifications
110
105
  #
111
- # @param [Hash] params
112
- # @option params [String] host
113
- # @option params [String] host_group
106
+ # @param [String] host_group
114
107
  #
115
108
  # @example
116
- # @icinga.disable_hostgroup_notification(host: 'icinga2', host_group: 'linux-servers')
109
+ # @icinga.disable_hostgroup_notification('linux-servers')
117
110
  #
118
111
  # @return [Hash]
119
112
  #
120
- def disable_hostgroup_notification( params )
113
+ def disable_hostgroup_notification( host_group )
121
114
 
122
- raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
123
- raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
124
-
125
- host_group = params.dig(:host_group)
126
- raise ArgumentError.new('Missing host_group') if( host_group.nil? )
115
+ raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) unless( host_group.is_a?(String) )
116
+ raise ArgumentError.new('missing \'host_group\'') if( host_group.size.zero? )
127
117
 
128
118
  return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( host_group ) == false )
129
119
 
130
- hostgroup_notification( host_group: host_group, enable_notifications: true )
120
+ hostgroup_notification( host_group: host_group, enable_notifications: false )
131
121
  end
132
122
 
133
123
  # return all notifications
@@ -154,16 +144,15 @@ module Icinga2
154
144
  #
155
145
  # @return [Hash]
156
146
  #
157
- def host_notification( params = {} )
147
+ def host_notification( params )
158
148
 
159
- name = params.dig(:name)
160
- notifications = params.dig(:enable_notifications) || false
149
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
150
+ raise ArgumentError.new('missing params') if( params.size.zero? )
161
151
 
162
- payload = {
163
- 'attrs' => {
164
- 'enable_notifications' => notifications
165
- }
166
- }
152
+ name = validate( params, required: true, var: 'name', type: String )
153
+ notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
154
+
155
+ payload = { attrs: { enable_notifications: notifications } }
167
156
 
168
157
  post(
169
158
  url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
@@ -177,21 +166,22 @@ module Icinga2
177
166
  # @api protected
178
167
  #
179
168
  # @param [Hash] params
180
- # @option params [String] host_group
181
- # @option params [Bool] enable_notifications (false)
169
+ # @option params [String] :host_group
170
+ # @option params [Bool] :enable_notifications (false)
182
171
  #
183
172
  # @return [Hash]
184
173
  #
185
- def hostgroup_notification( params = {} )
174
+ def hostgroup_notification( params )
186
175
 
187
- group = params.dig(:host_group)
188
- notifications = params.dig(:enable_notifications) || false
176
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
177
+ raise ArgumentError.new('missing params') if( params.size.zero? )
178
+
179
+ group = validate( params, required: true, var: 'host_group', type: String )
180
+ notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
189
181
 
190
182
  payload = {
191
- 'filter' => format( '"%s" in host.groups', group ),
192
- 'attrs' => {
193
- 'enable_notifications' => notifications
194
- }
183
+ filter: format( '"%s" in host.groups', group ),
184
+ attrs: { enable_notifications: notifications }
195
185
  }
196
186
 
197
187
  post(
@@ -211,16 +201,17 @@ module Icinga2
211
201
  #
212
202
  # @return [Hash]
213
203
  #
214
- def service_notification( params = {} )
204
+ def service_notification( params )
205
+
206
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
207
+ raise ArgumentError.new('missing params') if( params.size.zero? )
215
208
 
216
- name = params.dig(:name)
217
- notifications = params.dig(:enable_notifications) || false
209
+ name = validate( params, required: true, var: 'name', type: String )
210
+ notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
218
211
 
219
212
  payload = {
220
- 'filter' => format( 'host.name=="%s"', name ),
221
- 'attrs' => {
222
- 'enable_notifications' => notifications
223
- }
213
+ filter: format( 'host.name=="%s"', name ),
214
+ attrs: { enable_notifications: notifications }
224
215
  }
225
216
 
226
217
  post(