icinga2 0.9.2.1 → 0.9.2.8

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.
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,17 +8,36 @@ module Icinga2
8
8
  # add host
9
9
  #
10
10
  # @param [Hash] params
11
- # @option params [String] :name
12
- # @option params [String] :address
13
- # @option params [String] :display_name
14
- # @option params [Bool] :enable_notifications (false)
15
- # @option params [Integer] :max_check_attempts (3)
16
- # @option params [Integer] :check_interval (60)
17
- # @option params [Integer] :retry_interval (45)
18
- # @option params [String] :notes
19
- # @option params [String] :notes_url
20
- # @option params [String] :action_url
21
- # @option params [Hash] :vars ({})
11
+ # @option params [String] name
12
+ # @option params [String] address
13
+ # @option params [String] address6
14
+ # @option params [String] display_name
15
+ # @option params [Bool] enable_notifications (false)
16
+ # @option params [Integer] max_check_attempts (3)
17
+ # @option params [Integer] check_interval (60)
18
+ # @option params [Integer] retry_interval (45)
19
+ # @option params [String] notes
20
+ # @option params [String] notes_url
21
+ # @option params [String] action_url
22
+ # @option params [String] check_command
23
+ # @option params [Integer] check_interval
24
+ # @option params [String] check_period
25
+ # @option params [Integer] check_timeout
26
+ # @option params [String] command_endpoint
27
+ # @option params [Bool] enable_active_checks
28
+ # @option params [Bool] enable_event_handler
29
+ # @option params [Bool] enable_flapping
30
+ # @option params [Bool] enable_passive_checks
31
+ # @option params [Bool] enable_perfdata
32
+ # @option params [String] event_command
33
+ # @option params [Integer] flapping_threshold
34
+ # @option params [Integer] flapping_threshold_high
35
+ # @option params [Integer] flapping_threshold_low
36
+ # @option params [String] icon_image
37
+ # @option params [String] icon_image_alt
38
+ # @option params [Integer] retry_interval
39
+ # @option params [Bool] volatile
40
+ # @option params [Hash] vars ({})
22
41
  #
23
42
  # @example
24
43
  # param = {
@@ -38,7 +57,6 @@ module Icinga2
38
57
  # }
39
58
  # }
40
59
  # }
41
- #
42
60
  # @icinga.add_host(param)
43
61
  #
44
62
  # @return [Hash]
@@ -48,121 +66,74 @@ module Icinga2
48
66
  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
49
67
  raise ArgumentError.new('missing params') if( params.size.zero? )
50
68
 
51
- action_url = params.dig(:action_url)
52
- address = params.dig(:address)
53
- address6 = params.dig(:address6)
54
- check_command = params.dig(:check_command)
55
- check_interval = params.dig(:check_interval)
56
- check_period = params.dig(:check_period)
57
- check_timeout = params.dig(:check_timeout)
58
- command_endpoint = params.dig(:command_endpoint)
59
- display_name = params.dig(:display_name)
60
- enable_active_checks = params.dig(:enable_active_checks)
61
- enable_event_handler = params.dig(:enable_event_handler)
62
- enable_flapping = params.dig(:enable_flapping) || false
63
- enable_notifications = params.dig(:enable_notifications)
64
- enable_passive_checks = params.dig(:enable_passive_checks)
65
- enable_perfdata = params.dig(:enable_perfdata)
66
- event_command = params.dig(:event_command)
67
- flapping_threshold = params.dig(:flapping_threshold)
68
- groups = params.dig(:groups)
69
- icon_image = params.dig(:icon_image)
70
- icon_image_alt = params.dig(:icon_image_alt)
71
- max_check_attempts = params.dig(:max_check_attempts)
72
- name = params.dig(:name)
73
- notes = params.dig(:notes)
74
- notes_url = params.dig(:notes_url)
75
- retry_interval = params.dig(:retry_interval)
76
- templates = params.dig(:templates) || [ 'generic-host' ]
77
- vars = params.dig(:vars) || {}
78
- volatile = params.dig(:volatile)
79
- zone = params.dig(:zone)
80
-
81
- raise ArgumentError.new('missing name') if( name.nil? )
82
-
83
- %w[action_url
84
- address
85
- address6
86
- check_command
87
- check_period
88
- command_endpoint
89
- display_name
90
- event_command
91
- icon_image
92
- icon_image_alt
93
- name
94
- notes
95
- notes_url
96
- zone].each do |attr|
97
- v = eval(attr)
98
- raise ArgumentError.new(format('wrong type. \'%s\' must be an String, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(String) || v.nil? )
99
- end
100
-
101
- %w[check_interval
102
- flapping_threshold
103
- max_check_attempts
104
- retry_interval].each do |attr|
105
- v = eval(attr)
106
- raise ArgumentError.new(format('wrong type. \'%s\' must be an Integer, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(Integer) || v.nil? )
107
- end
108
-
109
- %w[enable_active_checks
110
- enable_event_handler
111
- enable_flapping
112
- enable_notifications
113
- enable_passive_checks
114
- enable_perfdata
115
- volatile].each do |attr|
116
- v = eval(attr)
117
- raise ArgumentError.new(format('wrong type. \'%s\' must be True or False, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(TrueClass) || v.is_a?(FalseClass) || v.nil? )
118
- end
119
-
120
- %w[groups templates].each do |attr|
121
- v = eval(attr)
122
- raise ArgumentError.new(format('wrong type. \'%s\' must be an Array, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(Array) || v.nil? )
123
- end
124
-
125
- raise ArgumentError.new(format('wrong type. \'vars\' must be an Hash, given \'%s\'', v.class.to_s)) unless( vars.is_a?(Hash) )
69
+ name = validate( params, required: true, var: 'name', type: String )
70
+ action_url = validate( params, required: false, var: 'action_url', type: String )
71
+ address = validate( params, required: false, var: 'address', type: String )
72
+ address6 = validate( params, required: false, var: 'address6', type: String )
73
+ check_command = validate( params, required: false, var: 'check_command', type: String )
74
+ check_interval = validate( params, required: false, var: 'check_interval', type: Integer ) || 60
75
+ check_period = validate( params, required: false, var: 'check_period', type: Integer )
76
+ check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
77
+ command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
78
+ display_name = validate( params, required: false, var: 'display_name', type: String )
79
+ enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
80
+ enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
81
+ enable_flapping = validate( params, required: false, var: 'enable_flapping', type: Boolean )
82
+ enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
83
+ enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
84
+ volatile = validate( params, required: false, var: 'volatile', type: Boolean )
85
+ enable_perfdata = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
86
+ event_command = validate( params, required: false, var: 'event_command', type: String )
87
+ flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
88
+ groups = validate( params, required: false, var: 'groups', type: Array )
89
+ icon_image = validate( params, required: false, var: 'icon_image', type: String )
90
+ icon_image_alt = validate( params, required: false, var: 'icon_image_alt', type: String )
91
+ notes = validate( params, required: false, var: 'notes', type: String )
92
+ notes_url = validate( params, required: false, var: 'notes_url', type: String )
93
+ max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer ) || 3
94
+ retry_interval = validate( params, required: false, var: 'retry_interval', type: Integer ) || 45
95
+ templates = validate( params, required: false, var: 'templates', type: Array ) || [ 'generic-host' ]
96
+ vars = validate( params, required: false, var: 'vars', type: Hash ) || {}
97
+ zone = validate( params, required: false, var: 'zone', type: String )
126
98
 
127
99
  address = Socket.gethostbyname( name ).first if( address.nil? )
128
100
 
129
101
  payload = {
130
- 'templates' => templates,
131
- 'attrs' => {
132
- 'action_url' => action_url,
133
- 'address' => address,
134
- 'address6' => address6,
135
- 'check_period' => check_period,
136
- 'check_command' => check_command,
137
- 'check_interval' => check_interval,
138
- 'check_timeout' => check_timeout,
139
- 'command_endpoint' => command_endpoint,
140
- 'display_name' => display_name,
141
- 'enable_active_checks' => enable_active_checks,
142
- 'enable_event_handler' => enable_event_handler,
143
- 'enable_flapping' => enable_flapping,
144
- 'enable_notifications' => enable_notifications,
145
- 'enable_passive_checks' => enable_passive_checks,
146
- 'enable_perfdata' => enable_perfdata,
147
- 'event_command' => event_command,
148
- 'flapping_threshold' => flapping_threshold,
149
- 'groups' => groups,
150
- 'icon_image' => icon_image,
151
- 'icon_image_alt' => icon_image_alt,
152
- 'max_check_attempts' => max_check_attempts,
153
- 'notes' => notes,
154
- 'notes_url' => notes_url,
155
- 'retry_interval' => retry_interval,
156
- 'volatile' => volatile,
157
- 'zone' => zone
102
+ templates: templates,
103
+ attrs: {
104
+ action_url: action_url,
105
+ address: address,
106
+ address6: address6,
107
+ check_period: check_period,
108
+ check_command: check_command,
109
+ check_interval: check_interval,
110
+ check_timeout: check_timeout,
111
+ command_endpoint: command_endpoint,
112
+ display_name: display_name,
113
+ enable_active_checks: enable_active_checks,
114
+ enable_event_handler: enable_event_handler,
115
+ enable_flapping: enable_flapping,
116
+ enable_notifications: enable_notifications,
117
+ enable_passive_checks: enable_passive_checks,
118
+ enable_perfdata: enable_perfdata,
119
+ event_command: event_command,
120
+ flapping_threshold: flapping_threshold,
121
+ groups: groups,
122
+ icon_image: icon_image,
123
+ icon_image_alt: icon_image_alt,
124
+ max_check_attempts: max_check_attempts,
125
+ notes: notes,
126
+ notes_url: notes_url,
127
+ retry_interval: retry_interval,
128
+ volatile: volatile,
129
+ zone: zone,
130
+ vars: vars
158
131
  }
159
132
  }
160
133
 
161
- payload['attrs']['vars'] = vars unless vars.empty?
162
-
163
134
  # remove all empty attrs
164
135
  payload.reject!{ |_k, v| v.nil? }
165
- payload['attrs'].reject!{ |_k, v| v.nil? }
136
+ payload[:attrs].reject!{ |_k, v| v.nil? }
166
137
 
167
138
  # puts JSON.pretty_generate payload
168
139
 
@@ -177,10 +148,13 @@ module Icinga2
177
148
  # delete a host
178
149
  #
179
150
  # @param [Hash] params
180
- # @option params [String] :name host to delete
151
+ # @option params [String] name host to delete
152
+ # @option params [Bool] cascade (false) delete host also when other objects depend on it
181
153
  #
182
154
  # @example
183
155
  # @icinga.delete_host(name: 'foo')
156
+ # @icinga.delete_host(name: 'foo', cascade: true)
157
+ #
184
158
  #
185
159
  # @return [Hash] result
186
160
  #
@@ -189,12 +163,13 @@ module Icinga2
189
163
  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
190
164
  raise ArgumentError.new('missing params') if( params.size.zero? )
191
165
 
192
- host = params.dig(:name)
166
+ name = validate( params, required: true, var: 'name', type: String )
167
+ cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false
193
168
 
194
- raise ArgumentError.new('Missing name') if( host.nil? )
169
+ url = format( '%s/objects/hosts/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
195
170
 
196
171
  delete(
197
- url: format( '%s/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
172
+ url: url,
198
173
  headers: @headers,
199
174
  options: @options
200
175
  )
@@ -203,18 +178,38 @@ module Icinga2
203
178
  # modify a host
204
179
  #
205
180
  # @param [Hash] params
206
- # @option params [String] :name
207
- # @option params [String] :address
208
- # @option params [String] :display_name
209
- # @option params [Bool] :enable_notifications (false)
210
- # @option params [Integer] :max_check_attempts (3)
211
- # @option params [Integer] :check_interval (60)
212
- # @option params [Integer] :retry_interval (45)
213
- # @option params [String] :notes
214
- # @option params [String] :notes_url
215
- # @option params [String] :action_url
216
- # @option params [Hash] :vars ({})
217
- # @option params [Bool] :merge_vars (false)
181
+ # @option params [String] name
182
+ # @option params [String] name
183
+ # @option params [String] address
184
+ # @option params [String] address6
185
+ # @option params [String] display_name
186
+ # @option params [Bool] enable_notifications
187
+ # @option params [Integer] max_check_attempts
188
+ # @option params [Integer] check_interval
189
+ # @option params [Integer] retry_interval
190
+ # @option params [String] notes
191
+ # @option params [String] notes_url
192
+ # @option params [String] action_url
193
+ # @option params [String] check_command
194
+ # @option params [Integer] check_interval
195
+ # @option params [String] check_period
196
+ # @option params [Integer] check_timeout
197
+ # @option params [String] command_endpoint
198
+ # @option params [Bool] enable_active_checks
199
+ # @option params [Bool] enable_event_handler
200
+ # @option params [Bool] enable_flapping
201
+ # @option params [Bool] enable_passive_checks
202
+ # @option params [Bool] enable_perfdata
203
+ # @option params [String] event_command
204
+ # @option params [Integer] flapping_threshold
205
+ # @option params [Integer] flapping_threshold_high
206
+ # @option params [Integer] flapping_threshold_low
207
+ # @option params [String] icon_image
208
+ # @option params [String] icon_image_alt
209
+ # @option params [Integer] retry_interval
210
+ # @option params [Bool] volatile
211
+ # @option params [Hash] vars ({})
212
+ # @option params [Bool] merge_vars (false)
218
213
  #
219
214
  # @example
220
215
  # param = {
@@ -255,35 +250,42 @@ module Icinga2
255
250
  #
256
251
  def modify_host( params )
257
252
 
258
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
253
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
259
254
  raise ArgumentError.new('missing params') if( params.size.zero? )
260
255
 
261
- name = params.dig(:name)
262
- address = params.dig(:address)
263
- display_name = params.dig(:display_name) || host
264
- notifications = params.dig(:enable_notifications) || false
265
- max_check_attempts = params.dig(:max_check_attempts) || 3
266
- check_interval = params.dig(:check_interval) || 60
267
- retry_interval = params.dig(:retry_interval) || 45
268
- notes = params.dig(:notes)
269
- notes_url = params.dig(:notes_url)
270
- action_url = params.dig(:action_url)
271
- vars = params.dig(:vars) || {}
272
- merge_vars = params.dig(:merge_vars) || false
273
-
274
- raise ArgumentError.new('Missing host name') if( name.nil? )
275
- raise ArgumentError.new('only true or false for notifications are allowed') unless( notifications.is_a?(TrueClass) || notifications.is_a?(FalseClass) )
276
- raise ArgumentError.new('only Integer for max_check_attempts are allowed') unless( max_check_attempts.is_a?(Integer) )
277
- raise ArgumentError.new('only Integer for check_interval are allowed') unless( check_interval.is_a?(Integer) )
278
- raise ArgumentError.new('only Integer for retry_interval are allowed') unless( retry_interval.is_a?(Integer) )
279
- raise ArgumentError.new('only String for notes are allowed') unless( notes.is_a?(String) || notes.nil? )
280
- raise ArgumentError.new('only String for notes_url are allowed') unless( notes_url.is_a?(String) || notes_url.nil? )
281
- raise ArgumentError.new('only Hash for vars are allowed') unless( vars.is_a?(Hash) )
282
- raise ArgumentError.new('wrong type. merge_vars must be an Boolean') unless( merge_vars.is_a?(TrueClass) || merge_vars.is_a?(FalseClass) )
256
+ name = validate( params, required: true, var: 'name', type: String )
257
+ action_url = validate( params, required: false, var: 'action_url', type: String )
258
+ address = validate( params, required: false, var: 'address', type: String )
259
+ address6 = validate( params, required: false, var: 'address6', type: String )
260
+ check_command = validate( params, required: false, var: 'check_command', type: String )
261
+ check_interval = validate( params, required: false, var: 'check_interval', type: Integer )
262
+ check_period = validate( params, required: false, var: 'check_period', type: Integer )
263
+ check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
264
+ command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
265
+ display_name = validate( params, required: false, var: 'display_name', type: String )
266
+ enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
267
+ enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
268
+ enable_flapping = validate( params, required: false, var: 'enable_flapping', type: Boolean )
269
+ enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean )
270
+ enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
271
+ volatile = validate( params, required: false, var: 'volatile', type: Boolean )
272
+ enable_perfdata = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
273
+ event_command = validate( params, required: false, var: 'event_command', type: String )
274
+ flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
275
+ groups = validate( params, required: false, var: 'groups', type: Array )
276
+ icon_image = validate( params, required: false, var: 'icon_image', type: String )
277
+ icon_image_alt = validate( params, required: false, var: 'icon_image_alt', type: String )
278
+ notes = validate( params, required: false, var: 'notes', type: String )
279
+ notes_url = validate( params, required: false, var: 'notes_url', type: String )
280
+ max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer )
281
+ retry_interval = validate( params, required: false, var: 'retry_interval', type: Integer )
282
+ templates = validate( params, required: false, var: 'templates', type: Array ) || [ 'generic-host' ]
283
+ vars = validate( params, required: false, var: 'vars', type: Hash ) || {}
284
+ zone = validate( params, required: false, var: 'zone', type: String )
285
+ merge_vars = validate( params, required: false, var: 'merge_vars', type: Boolean ) || false
283
286
 
284
287
  # check if host exists
285
- exists = exists_host?( name )
286
- raise ArgumentError.new( format( 'host %s do not exists', name ) ) if( exists == false )
288
+ return { 'code' => 404, 'name' => name, 'status' => 'Object not Found' } unless( exists_host?( name ) )
287
289
 
288
290
  # merge the new with the old vars
289
291
  if( merge_vars == true )
@@ -291,27 +293,46 @@ module Icinga2
291
293
  current_host_vars = current_host.first
292
294
  current_host_vars = current_host_vars.dig('attrs','vars')
293
295
  current_host_vars = current_host_vars.deep_string_keys
294
-
295
296
  vars = vars.deep_string_keys unless( vars.empty? )
296
297
  vars = current_host_vars.merge( vars )
297
298
  end
298
299
 
299
- # POST request
300
300
  payload = {
301
- 'attrs' => {
302
- 'address' => address,
303
- 'display_name' => display_name,
304
- 'max_check_attempts' => max_check_attempts.to_i,
305
- 'check_interval' => check_interval.to_i,
306
- 'retry_interval' => retry_interval.to_i,
307
- 'enable_notifications' => notifications,
308
- 'action_url' => action_url,
309
- 'notes' => notes,
310
- 'notes_url' => notes_url
301
+ templates: templates,
302
+ attrs: {
303
+ action_url: action_url,
304
+ address: address,
305
+ address6: address6,
306
+ check_period: check_period,
307
+ check_command: check_command,
308
+ check_interval: check_interval,
309
+ check_timeout: check_timeout,
310
+ command_endpoint: command_endpoint,
311
+ display_name: display_name,
312
+ enable_active_checks: enable_active_checks,
313
+ enable_event_handler: enable_event_handler,
314
+ enable_flapping: enable_flapping,
315
+ enable_notifications: enable_notifications,
316
+ enable_passive_checks: enable_passive_checks,
317
+ enable_perfdata: enable_perfdata,
318
+ event_command: event_command,
319
+ flapping_threshold: flapping_threshold,
320
+ groups: groups,
321
+ icon_image: icon_image,
322
+ icon_image_alt: icon_image_alt,
323
+ max_check_attempts: max_check_attempts,
324
+ notes: notes,
325
+ notes_url: notes_url,
326
+ retry_interval: retry_interval,
327
+ volatile: volatile,
328
+ zone: zone,
329
+ vars: vars
311
330
  }
312
331
  }
313
332
 
314
- payload['attrs']['vars'] = vars unless vars.empty?
333
+ # remove all empty attrs
334
+ payload.reject!{ |_k, v| v.nil? }
335
+ payload[:attrs].reject!{ |_k, v| v.nil? }
315
336
 
316
337
  post(
317
338
  url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
@@ -325,10 +346,10 @@ module Icinga2
325
346
  # return hosts
326
347
  #
327
348
  # @param [Hash] params
328
- # @option params [String] :name
329
- # @option params [String] :attrs
330
- # @option params [String] :filter
331
- # @option params [String] :joins
349
+ # @option params [String] name
350
+ # @option params [Array] attrs
351
+ # @option params [String] filter
352
+ # @option params [Array] joins
332
353
  #
333
354
  # @example to get all hosts
334
355
  # @icinga.hosts
@@ -340,21 +361,25 @@ module Icinga2
340
361
  #
341
362
  def hosts( params = {} )
342
363
 
343
- name = params.dig(:name)
344
- # attrs = params.dig(:attrs)
345
- # filter = params.dig(:filter)
346
- # joins = params.dig(:joins)
364
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
347
365
 
348
- # payload = {}
349
- #
350
- # payload['attrs'] = attrs unless attrs.nil?
351
- # payload['filter'] = filter unless filter.nil?
352
- # payload['joins'] = joins unless joins.nil?
366
+ name = validate( params, required: false, var: 'name', type: String )
367
+ attrs = validate( params, required: false, var: 'attrs', type: Array )
368
+ filter = validate( params, required: false, var: 'filter', type: String )
369
+ joins = validate( params, required: false, var: 'joins', type: Array )
370
+
371
+ payload = {
372
+ attrs: attrs,
373
+ filter: filter,
374
+ joins: joins
375
+ }
376
+ payload.reject!{ |_k, v| v.nil? }
353
377
 
354
378
  api_data(
355
379
  url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
356
380
  headers: @headers,
357
- options: @options
381
+ options: @options,
382
+ payload: payload
358
383
  )
359
384
  end
360
385
 
@@ -369,7 +394,7 @@ module Icinga2
369
394
  #
370
395
  def exists_host?( host_name )
371
396
 
372
- raise ArgumentError.new('only String are allowed') unless( host_name.is_a?(String) )
397
+ raise ArgumentError.new(format('wrong type. \'host_name\' must be an String, given \'%s\'',host_name.class.to_s)) unless( host_name.is_a?(String) )
373
398
  raise ArgumentError.new('Missing host_name') if( host_name.size.zero? )
374
399
 
375
400
  result = hosts( name: host_name )
@@ -384,9 +409,9 @@ module Icinga2
384
409
  # returns host objects
385
410
  #
386
411
  # @param [Hash] params
387
- # @option params [Array] :attrs (['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check'])
388
- # @option params [Array] :filter ([])
389
- # @option params [Array] :joins ([])
412
+ # @option params [Array] attrs (['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check'])
413
+ # @option params [String] filter ([])
414
+ # @option params [Array] joins ([])
390
415
  #
391
416
  # @example with default attrs and joins
392
417
  # @icinga.host_objects
@@ -398,23 +423,16 @@ module Icinga2
398
423
  #
399
424
  def host_objects( params = {} )
400
425
 
401
- attrs = params.dig(:attrs)
402
- filter = params.dig(:filter)
403
- joins = params.dig(:joins)
404
-
405
- # raise ArgumentError.new('only Array for attrs are allowed') unless( attrs.is_a?(Hash) )
406
- # raise ArgumentError.new('only Array for filter are allowed') unless( filter.is_a?(Hash) )
407
- # raise ArgumentError.new('only Array for joins are allowed') unless( joins.is_a?(Hash) )
408
-
409
- payload = {}
410
-
411
- if( attrs.nil? )
412
- attrs = %w[name state acknowledgement downtime_depth last_check]
413
- end
426
+ attrs = validate( params, required: false, var: 'attrs', type: Array ) || %w[name state acknowledgement downtime_depth last_check]
427
+ filter = validate( params, required: false, var: 'filter', type: String )
428
+ joins = validate( params, required: false, var: 'joins', type: Array )
414
429
 
415
- payload['attrs'] = attrs unless attrs.nil?
416
- payload['filter'] = filter unless filter.nil?
417
- payload['joins'] = joins unless joins.nil?
430
+ payload = {
431
+ attrs: attrs,
432
+ filter: filter,
433
+ joins: joins
434
+ }
435
+ payload.reject!{ |_k, v| v.nil? }
418
436
 
419
437
  data = api_data(
420
438
  url: format( '%s/objects/hosts', @icinga_api_url_base ),
@@ -426,9 +444,7 @@ module Icinga2
426
444
  @last_host_objects_called = Time.now.to_i
427
445
 
428
446
  if( !data.nil? && data.is_a?(Array) )
429
-
430
447
  all_hosts = data.clone
431
-
432
448
  unless( all_hosts.nil? )
433
449
  # global var for count of all hosts
434
450
  @hosts_all = all_hosts.size
@@ -438,7 +454,6 @@ module Icinga2
438
454
  @hosts_problems_down = count_problems(all_hosts, Icinga2::HOSTS_DOWN)
439
455
  @hosts_problems_critical = count_problems(all_hosts, Icinga2::HOSTS_CRITICAL)
440
456
  @hosts_problems_unknown = count_problems(all_hosts, Icinga2::HOSTS_UNKNOWN)
441
-
442
457
  end
443
458
  end
444
459
 
@@ -510,7 +525,7 @@ module Icinga2
510
525
  #
511
526
  def list_hosts_with_problems( max_items = 5 )
512
527
 
513
- raise ArgumentError.new('only Integer for max_items are allowed') unless( max_items.is_a?(Integer) )
528
+ raise ArgumentError.new(format('wrong type. \'max_items\' must be an Integer, given \'%s\'', max_items.class.to_s)) unless( max_items.is_a?(Integer) )
514
529
 
515
530
  host_problems = {}
516
531
  host_problems_severity = {}
@@ -518,10 +533,7 @@ module Icinga2
518
533
  host_data = host_objects
519
534
  host_data = JSON.parse( host_data ) if host_data.is_a?(String)
520
535
 
521
- # logger.debug(host_data)
522
-
523
536
  unless( host_data.nil? )
524
-
525
537
  host_data.each do |h,_v|
526
538
  name = h.dig('name')
527
539
  state = h.dig('attrs','state')
@@ -534,9 +546,7 @@ module Icinga2
534
546
 
535
547
  # get the count of problems
536
548
  #
537
- if( host_problems.count != 0 )
538
- host_problems.keys[1..max_items].each { |k| host_problems_severity[k] = host_problems[k] }
539
- end
549
+ host_problems.keys[1..max_items].each { |k| host_problems_severity[k] = host_problems[k] } if( host_problems.count != 0 )
540
550
 
541
551
  host_problems_severity
542
552
  end
@@ -573,10 +583,10 @@ module Icinga2
573
583
  cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
574
584
  host_objects if((Time.now.to_i - @last_host_objects_called).to_i > @last_call_timeout)
575
585
 
576
- raise ArgumentError.new('Integer for @hosts_problems_down needed') unless( @hosts_problems_down.is_a?(Integer) )
577
- raise ArgumentError.new('Integer for @hosts_problems_critical needed') unless( @hosts_problems_critical.is_a?(Integer) )
578
- raise ArgumentError.new('Integer for @hosts_problems_unknown needed') unless( @hosts_problems_unknown.is_a?(Integer) )
579
- raise ArgumentError.new('Integer for @hosts_down needed') unless( @hosts_down.is_a?(Integer) )
586
+ raise ArgumentError.new(format('wrong type. \'@hosts_problems_down\' must be an Integer, given \'%s\'', @hosts_problems_down.class.to_s)) unless( @hosts_problems_down.is_a?(Integer) )
587
+ raise ArgumentError.new(format('wrong type. \'@hosts_problems_critical\' must be an Integer, given \'%s\'', @hosts_problems_critical.class.to_s)) unless( @hosts_problems_critical.is_a?(Integer) )
588
+ raise ArgumentError.new(format('wrong type. \'@hosts_problems_critical\' must be an Integer, given \'%s\'', @hosts_problems_critical.class.to_s)) unless( @hosts_problems_critical.is_a?(Integer) )
589
+ raise ArgumentError.new(format('wrong type. \'@hosts_down\' must be an Integer, given \'%s\'', @hosts_down.class.to_s)) unless( @hosts_down.is_a?(Integer) )
580
590
 
581
591
  problems_all = @hosts_problems.nil? ? 0 : @hosts_problems
582
592
  problems_down = @hosts_problems_down.nil? ? 0 : @hosts_problems_down
@@ -619,16 +629,14 @@ module Icinga2
619
629
  #
620
630
  def host_severity( params )
621
631
 
622
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
632
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
623
633
  raise ArgumentError.new('missing params') if( params.size.zero? )
624
634
 
625
- state = params.dig('attrs','state')
626
- acknowledgement = params.dig('attrs','acknowledgement') || 0
627
- downtime_depth = params.dig('attrs','downtime_depth') || 0
635
+ attrs = params.dig('attrs')
628
636
 
629
- raise ArgumentError.new('only Float for state are allowed') unless( state.is_a?(Float) )
630
- raise ArgumentError.new('only Float for acknowledgement are allowed') unless( acknowledgement.is_a?(Float) )
631
- raise ArgumentError.new('only Float for downtime_depth are allowed') unless( downtime_depth.is_a?(Float) )
637
+ state = validate( attrs, required: true, var: 'state', type: Float )
638
+ acknowledgement = validate( attrs, required: false, var: 'acknowledgement', type: Float ) || 0
639
+ downtime_depth = validate( attrs, required: false, var: 'downtime_depth', type: Float ) || 0
632
640
 
633
641
  severity = 0
634
642
 
@@ -644,7 +652,6 @@ module Icinga2
644
652
  severity += 16 if object_has_been_checked?(params)
645
653
 
646
654
  unless state.zero?
647
-
648
655
  severity +=
649
656
  if state == 1
650
657
  32