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.
- checksums.yaml +4 -4
- data/README.md +25 -22
- data/doc/Array.html +4 -6
- data/doc/Boolean.html +4 -6
- data/doc/FalseClass.html +4 -6
- data/doc/Hash.html +126 -8
- data/doc/Icinga2.html +7 -9
- data/doc/Logging.html +5 -7
- data/doc/Object.html +8 -10
- data/doc/Time.html +4 -6
- data/doc/TrueClass.html +4 -6
- data/doc/_index.html +20 -7
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +32 -30
- data/doc/frames.html +1 -1
- data/doc/index.html +32 -30
- data/doc/method_list.html +34 -2
- data/doc/services.md +45 -62
- data/doc/top-level-namespace.html +4 -6
- data/examples/_blank.rb +2 -2
- data/examples/config.rb +23 -0
- data/examples/downtimes.rb +4 -33
- data/examples/hostgroups.rb +4 -33
- data/examples/hosts.rb +18 -33
- data/examples/informations.rb +4 -33
- data/examples/notifications.rb +4 -33
- data/examples/servicegroups.rb +4 -25
- data/examples/services.rb +46 -67
- data/examples/statistics.rb +4 -33
- data/examples/test.rb +7 -28
- data/examples/usergroups.rb +4 -33
- data/examples/users.rb +4 -33
- data/lib/icinga2/client.rb +16 -42
- data/lib/icinga2/converts.rb +16 -54
- data/lib/icinga2/downtimes.rb +46 -44
- data/lib/icinga2/hostgroups.rb +35 -35
- data/lib/icinga2/hosts.rb +235 -228
- data/lib/icinga2/network.rb +53 -125
- data/lib/icinga2/notifications.rb +37 -46
- data/lib/icinga2/servicegroups.rb +31 -41
- data/lib/icinga2/services.rb +211 -236
- data/lib/icinga2/tools.rb +10 -9
- data/lib/icinga2/usergroups.rb +22 -32
- data/lib/icinga2/users.rb +64 -59
- data/lib/icinga2/validator.rb +59 -0
- data/lib/icinga2/version.rb +1 -1
- metadata +78 -7
- data/lib/icinga2/network.rb-SAVE +0 -1004
data/lib/icinga2/hosts.rb
CHANGED
@@ -8,17 +8,36 @@ module Icinga2
|
|
8
8
|
# add host
|
9
9
|
#
|
10
10
|
# @param [Hash] params
|
11
|
-
# @option params [String]
|
12
|
-
# @option params [String]
|
13
|
-
# @option params [String]
|
14
|
-
# @option params [
|
15
|
-
# @option params [
|
16
|
-
# @option params [Integer]
|
17
|
-
# @option params [Integer]
|
18
|
-
# @option params [
|
19
|
-
# @option params [String]
|
20
|
-
# @option params [String]
|
21
|
-
# @option params [
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
notes
|
74
|
-
notes_url
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
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
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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[
|
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]
|
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
|
-
|
166
|
+
name = validate( params, required: true, var: 'name', type: String )
|
167
|
+
cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false
|
193
168
|
|
194
|
-
|
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:
|
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]
|
207
|
-
# @option params [String]
|
208
|
-
# @option params [String]
|
209
|
-
# @option params [
|
210
|
-
# @option params [
|
211
|
-
# @option params [
|
212
|
-
# @option params [Integer]
|
213
|
-
# @option params [
|
214
|
-
# @option params [
|
215
|
-
# @option params [String]
|
216
|
-
# @option params [
|
217
|
-
# @option params [
|
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('
|
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
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
check_interval
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
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
|
-
|
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
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
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
|
-
|
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]
|
329
|
-
# @option params [
|
330
|
-
# @option params [String]
|
331
|
-
# @option params [
|
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
|
-
|
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
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
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('
|
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]
|
388
|
-
# @option params [
|
389
|
-
# @option params [Array]
|
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
|
402
|
-
filter = params
|
403
|
-
joins = params
|
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
|
416
|
-
|
417
|
-
|
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('
|
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('
|
577
|
-
raise ArgumentError.new('
|
578
|
-
raise ArgumentError.new('
|
579
|
-
raise ArgumentError.new('
|
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('
|
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
|
-
|
626
|
-
acknowledgement = params.dig('attrs','acknowledgement') || 0
|
627
|
-
downtime_depth = params.dig('attrs','downtime_depth') || 0
|
635
|
+
attrs = params.dig('attrs')
|
628
636
|
|
629
|
-
|
630
|
-
|
631
|
-
|
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
|