icinga2 0.9.0.1 → 0.9.2.1
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 +51 -45
- data/doc/hosts.md +62 -21
- data/doc/services.md +215 -54
- data/doc/statistics.md +28 -10
- data/doc/usergroups.md +49 -11
- data/doc/users.md +64 -13
- data/examples/_blank.rb +72 -0
- data/examples/downtimes.rb +79 -0
- data/examples/hostgroups.rb +91 -0
- data/examples/hosts.rb +180 -0
- data/examples/informations.rb +95 -0
- data/examples/notifications.rb +109 -0
- data/examples/servicegroups.rb +102 -0
- data/examples/services.rb +202 -0
- data/examples/statistics.rb +137 -0
- data/examples/test.rb +32 -377
- data/examples/usergroups.rb +95 -0
- data/examples/users.rb +98 -0
- data/lib/icinga2.rb +4 -394
- data/lib/icinga2/client.rb +431 -0
- data/lib/icinga2/downtimes.rb +40 -20
- data/lib/icinga2/hostgroups.rb +38 -22
- data/lib/icinga2/hosts.rb +308 -92
- data/lib/icinga2/network.rb +211 -213
- data/lib/icinga2/notifications.rb +30 -28
- data/lib/icinga2/servicegroups.rb +43 -24
- data/lib/icinga2/services.rb +218 -130
- data/lib/icinga2/statistics.rb +14 -10
- data/lib/icinga2/tools.rb +1 -1
- data/lib/icinga2/usergroups.rb +12 -15
- data/lib/icinga2/users.rb +16 -17
- data/lib/icinga2/version.rb +1 -15
- data/lib/monkey_patches.rb +89 -0
- metadata +21 -8
data/lib/icinga2/hostgroups.rb
CHANGED
@@ -11,6 +11,9 @@ module Icinga2
|
|
11
11
|
# @param [Hash] params
|
12
12
|
# @option params [String] :host_group hostgroup to create
|
13
13
|
# @option params [String] :display_name the displayed name
|
14
|
+
# @option params [String] :notes
|
15
|
+
# @option params [String] :notes_url
|
16
|
+
# @option params [String] :action_url
|
14
17
|
#
|
15
18
|
# @example
|
16
19
|
# @icinga.add_hostgroup(host_group: 'foo', display_name: 'FOO')
|
@@ -19,20 +22,36 @@ module Icinga2
|
|
19
22
|
#
|
20
23
|
def add_hostgroup( params )
|
21
24
|
|
22
|
-
raise ArgumentError.new('
|
23
|
-
raise ArgumentError.new('missing params') if( params.size.zero? )
|
25
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
26
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
24
27
|
|
25
28
|
host_group = params.dig(:host_group)
|
26
29
|
display_name = params.dig(:display_name)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
notes = params.dig(:notes)
|
31
|
+
notes_url = params.dig(:notes_url)
|
32
|
+
action_url = params.dig(:action_url)
|
33
|
+
|
34
|
+
raise ArgumentError.new('Missing \'host_group\'') if( host_group.nil? )
|
35
|
+
raise ArgumentError.new('Missing \'display_name\'') if( display_name.nil? )
|
36
|
+
|
37
|
+
payload = {
|
38
|
+
'attrs' => {
|
39
|
+
'display_name' => display_name,
|
40
|
+
'notes' => notes,
|
41
|
+
'notes_url' => notes_url,
|
42
|
+
'action_url' => action_url
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
# remove all empty attrs
|
47
|
+
payload.reject!{ |_k, v| v.nil? }
|
48
|
+
payload['attrs'].reject!{ |_k, v| v.nil? }
|
49
|
+
|
50
|
+
put(
|
32
51
|
url: format('%s/objects/hostgroups/%s', @icinga_api_url_base, host_group),
|
33
52
|
headers: @headers,
|
34
53
|
options: @options,
|
35
|
-
payload:
|
54
|
+
payload: payload
|
36
55
|
)
|
37
56
|
end
|
38
57
|
|
@@ -48,14 +67,14 @@ module Icinga2
|
|
48
67
|
#
|
49
68
|
def delete_hostgroup( params )
|
50
69
|
|
51
|
-
raise ArgumentError.new('
|
52
|
-
raise ArgumentError.new('missing params') if( params.size.zero? )
|
70
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
71
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
53
72
|
|
54
73
|
host_group = params.dig(:host_group)
|
55
74
|
|
56
|
-
raise ArgumentError.new('Missing host_group') if( host_group.nil? )
|
75
|
+
raise ArgumentError.new('Missing \'host_group\'') if( host_group.nil? )
|
57
76
|
|
58
|
-
|
77
|
+
delete(
|
59
78
|
url: format('%s/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, host_group),
|
60
79
|
headers: @headers,
|
61
80
|
options: @options
|
@@ -86,15 +105,11 @@ module Icinga2
|
|
86
105
|
format( '%s/objects/hostgroups/%s', @icinga_api_url_base, host_group )
|
87
106
|
end
|
88
107
|
|
89
|
-
|
108
|
+
api_data(
|
90
109
|
url: url,
|
91
110
|
headers: @headers,
|
92
111
|
options: @options
|
93
112
|
)
|
94
|
-
|
95
|
-
return data.dig('results') if( data.dig(:status).nil? )
|
96
|
-
|
97
|
-
nil
|
98
113
|
end
|
99
114
|
|
100
115
|
# returns true if the hostgroup exists
|
@@ -108,15 +123,16 @@ module Icinga2
|
|
108
123
|
#
|
109
124
|
def exists_hostgroup?( host_group )
|
110
125
|
|
111
|
-
raise ArgumentError.new('
|
112
|
-
raise ArgumentError.new('Missing host_group') if( host_group.size.zero? )
|
126
|
+
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) )
|
127
|
+
raise ArgumentError.new('Missing \'host_group\'') if( host_group.size.zero? )
|
113
128
|
|
114
129
|
result = hostgroups(host_group: host_group)
|
115
|
-
result = JSON.parse( result ) if
|
130
|
+
result = JSON.parse( result ) if( result.is_a?(String) )
|
131
|
+
result = result.first if( result.is_a?(Array) )
|
116
132
|
|
117
|
-
return
|
133
|
+
return false if( result.is_a?(Hash) && result.dig('code') == 404 )
|
118
134
|
|
119
|
-
|
135
|
+
true
|
120
136
|
end
|
121
137
|
|
122
138
|
end
|
data/lib/icinga2/hosts.rb
CHANGED
@@ -8,8 +8,8 @@ module Icinga2
|
|
8
8
|
# add host
|
9
9
|
#
|
10
10
|
# @param [Hash] params
|
11
|
-
# @option params [String] :
|
12
|
-
# @option params [String] :
|
11
|
+
# @option params [String] :name
|
12
|
+
# @option params [String] :address
|
13
13
|
# @option params [String] :display_name
|
14
14
|
# @option params [Bool] :enable_notifications (false)
|
15
15
|
# @option params [Integer] :max_check_attempts (3)
|
@@ -22,23 +22,244 @@ module Icinga2
|
|
22
22
|
#
|
23
23
|
# @example
|
24
24
|
# param = {
|
25
|
-
#
|
26
|
-
#
|
25
|
+
# name: 'foo',
|
26
|
+
# address: 'foo.bar.com',
|
27
27
|
# display_name: 'test node',
|
28
28
|
# max_check_attempts: 5,
|
29
|
-
# notes: 'test node'
|
29
|
+
# notes: 'test node',
|
30
|
+
# vars: {
|
31
|
+
# description: 'host foo',
|
32
|
+
# os: 'Linux',
|
33
|
+
# partitions: {
|
34
|
+
# '/' => {
|
35
|
+
# crit: '95%',
|
36
|
+
# warn: '90%'
|
37
|
+
# }
|
38
|
+
# }
|
39
|
+
# }
|
30
40
|
# }
|
41
|
+
#
|
31
42
|
# @icinga.add_host(param)
|
32
43
|
#
|
33
44
|
# @return [Hash]
|
34
45
|
#
|
35
46
|
def add_host( params )
|
36
47
|
|
48
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
49
|
+
raise ArgumentError.new('missing params') if( params.size.zero? )
|
50
|
+
|
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) )
|
126
|
+
|
127
|
+
address = Socket.gethostbyname( name ).first if( address.nil? )
|
128
|
+
|
129
|
+
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
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
payload['attrs']['vars'] = vars unless vars.empty?
|
162
|
+
|
163
|
+
# remove all empty attrs
|
164
|
+
payload.reject!{ |_k, v| v.nil? }
|
165
|
+
payload['attrs'].reject!{ |_k, v| v.nil? }
|
166
|
+
|
167
|
+
# puts JSON.pretty_generate payload
|
168
|
+
|
169
|
+
put(
|
170
|
+
url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
|
171
|
+
headers: @headers,
|
172
|
+
options: @options,
|
173
|
+
payload: payload
|
174
|
+
)
|
175
|
+
end
|
176
|
+
|
177
|
+
# delete a host
|
178
|
+
#
|
179
|
+
# @param [Hash] params
|
180
|
+
# @option params [String] :name host to delete
|
181
|
+
#
|
182
|
+
# @example
|
183
|
+
# @icinga.delete_host(name: 'foo')
|
184
|
+
#
|
185
|
+
# @return [Hash] result
|
186
|
+
#
|
187
|
+
def delete_host( params )
|
188
|
+
|
37
189
|
raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
|
38
190
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
39
191
|
|
40
|
-
host
|
41
|
-
|
192
|
+
host = params.dig(:name)
|
193
|
+
|
194
|
+
raise ArgumentError.new('Missing name') if( host.nil? )
|
195
|
+
|
196
|
+
delete(
|
197
|
+
url: format( '%s/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
|
198
|
+
headers: @headers,
|
199
|
+
options: @options
|
200
|
+
)
|
201
|
+
end
|
202
|
+
|
203
|
+
# modify a host
|
204
|
+
#
|
205
|
+
# @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)
|
218
|
+
#
|
219
|
+
# @example
|
220
|
+
# param = {
|
221
|
+
# name: 'foo',
|
222
|
+
# address: 'foo.bar.com',
|
223
|
+
# display_name: 'Host for an example Problem',
|
224
|
+
# max_check_attempts: 10,
|
225
|
+
# }
|
226
|
+
#
|
227
|
+
# param = {
|
228
|
+
# name: 'foo',
|
229
|
+
# address: 'foo.bar.com',
|
230
|
+
# notes: 'an demonstration object',
|
231
|
+
# vars: {
|
232
|
+
# description: 'schould be delete ASAP',
|
233
|
+
# os: 'Linux',
|
234
|
+
# partitions: {
|
235
|
+
# '/' => {
|
236
|
+
# crit: '98%',
|
237
|
+
# warn: '95%'
|
238
|
+
# }
|
239
|
+
# }
|
240
|
+
# },
|
241
|
+
# merge_vars: true
|
242
|
+
# }
|
243
|
+
#
|
244
|
+
# param = {
|
245
|
+
# name: 'foo',
|
246
|
+
# address: 'foo.bar.com',
|
247
|
+
# vars: {
|
248
|
+
# description: 'removed all other custom vars',
|
249
|
+
# }
|
250
|
+
# }
|
251
|
+
#
|
252
|
+
# @icinga.add_host(param)
|
253
|
+
#
|
254
|
+
# @return [Hash]
|
255
|
+
#
|
256
|
+
def modify_host( params )
|
257
|
+
|
258
|
+
raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
|
259
|
+
raise ArgumentError.new('missing params') if( params.size.zero? )
|
260
|
+
|
261
|
+
name = params.dig(:name)
|
262
|
+
address = params.dig(:address)
|
42
263
|
display_name = params.dig(:display_name) || host
|
43
264
|
notifications = params.dig(:enable_notifications) || false
|
44
265
|
max_check_attempts = params.dig(:max_check_attempts) || 3
|
@@ -48,8 +269,9 @@ module Icinga2
|
|
48
269
|
notes_url = params.dig(:notes_url)
|
49
270
|
action_url = params.dig(:action_url)
|
50
271
|
vars = params.dig(:vars) || {}
|
272
|
+
merge_vars = params.dig(:merge_vars) || false
|
51
273
|
|
52
|
-
raise ArgumentError.new('Missing host') if(
|
274
|
+
raise ArgumentError.new('Missing host name') if( name.nil? )
|
53
275
|
raise ArgumentError.new('only true or false for notifications are allowed') unless( notifications.is_a?(TrueClass) || notifications.is_a?(FalseClass) )
|
54
276
|
raise ArgumentError.new('only Integer for max_check_attempts are allowed') unless( max_check_attempts.is_a?(Integer) )
|
55
277
|
raise ArgumentError.new('only Integer for check_interval are allowed') unless( check_interval.is_a?(Integer) )
|
@@ -57,16 +279,27 @@ module Icinga2
|
|
57
279
|
raise ArgumentError.new('only String for notes are allowed') unless( notes.is_a?(String) || notes.nil? )
|
58
280
|
raise ArgumentError.new('only String for notes_url are allowed') unless( notes_url.is_a?(String) || notes_url.nil? )
|
59
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) )
|
283
|
+
|
284
|
+
# check if host exists
|
285
|
+
exists = exists_host?( name )
|
286
|
+
raise ArgumentError.new( format( 'host %s do not exists', name ) ) if( exists == false )
|
60
287
|
|
61
|
-
|
62
|
-
|
63
|
-
|
288
|
+
# merge the new with the old vars
|
289
|
+
if( merge_vars == true )
|
290
|
+
current_host = hosts( name: name )
|
291
|
+
current_host_vars = current_host.first
|
292
|
+
current_host_vars = current_host_vars.dig('attrs','vars')
|
293
|
+
current_host_vars = current_host_vars.deep_string_keys
|
294
|
+
|
295
|
+
vars = vars.deep_string_keys unless( vars.empty? )
|
296
|
+
vars = current_host_vars.merge( vars )
|
64
297
|
end
|
65
298
|
|
299
|
+
# POST request
|
66
300
|
payload = {
|
67
|
-
'templates' => [ 'generic-host' ],
|
68
301
|
'attrs' => {
|
69
|
-
'address' =>
|
302
|
+
'address' => address,
|
70
303
|
'display_name' => display_name,
|
71
304
|
'max_check_attempts' => max_check_attempts.to_i,
|
72
305
|
'check_interval' => check_interval.to_i,
|
@@ -80,50 +313,19 @@ module Icinga2
|
|
80
313
|
|
81
314
|
payload['attrs']['vars'] = vars unless vars.empty?
|
82
315
|
|
83
|
-
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
# logger.debug( JSON.pretty_generate( payload ) )
|
88
|
-
|
89
|
-
Network.put(
|
90
|
-
url: format( '%s/objects/hosts/%s', @icinga_api_url_base, host ),
|
316
|
+
post(
|
317
|
+
url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
|
91
318
|
headers: @headers,
|
92
319
|
options: @options,
|
93
320
|
payload: payload
|
94
321
|
)
|
95
322
|
end
|
96
323
|
|
97
|
-
# delete a host
|
98
|
-
#
|
99
|
-
# @param [Hash] params
|
100
|
-
# @option params [String] :host host to delete
|
101
|
-
#
|
102
|
-
# @example
|
103
|
-
# @icinga.delete_host(host: 'foo')
|
104
|
-
#
|
105
|
-
# @return [Hash] result
|
106
|
-
#
|
107
|
-
def delete_host( params )
|
108
|
-
|
109
|
-
raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
|
110
|
-
raise ArgumentError.new('missing params') if( params.size.zero? )
|
111
|
-
|
112
|
-
host = params.dig(:host)
|
113
|
-
|
114
|
-
raise ArgumentError.new('Missing host') if( host.nil? )
|
115
|
-
|
116
|
-
Network.delete(
|
117
|
-
url: format( '%s/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
|
118
|
-
headers: @headers,
|
119
|
-
options: @options
|
120
|
-
)
|
121
|
-
end
|
122
324
|
|
123
325
|
# return hosts
|
124
326
|
#
|
125
327
|
# @param [Hash] params
|
126
|
-
# @option params [String] :
|
328
|
+
# @option params [String] :name
|
127
329
|
# @option params [String] :attrs
|
128
330
|
# @option params [String] :filter
|
129
331
|
# @option params [String] :joins
|
@@ -132,30 +334,28 @@ module Icinga2
|
|
132
334
|
# @icinga.hosts
|
133
335
|
#
|
134
336
|
# @example to get one host
|
135
|
-
# @icinga.hosts(
|
337
|
+
# @icinga.hosts( name: 'icinga2')
|
136
338
|
#
|
137
339
|
# @return [Array]
|
138
340
|
#
|
139
341
|
def hosts( params = {} )
|
140
342
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
343
|
+
name = params.dig(:name)
|
344
|
+
# attrs = params.dig(:attrs)
|
345
|
+
# filter = params.dig(:filter)
|
346
|
+
# joins = params.dig(:joins)
|
145
347
|
|
146
|
-
|
147
|
-
|
148
|
-
|
348
|
+
# payload = {}
|
349
|
+
#
|
350
|
+
# payload['attrs'] = attrs unless attrs.nil?
|
351
|
+
# payload['filter'] = filter unless filter.nil?
|
352
|
+
# payload['joins'] = joins unless joins.nil?
|
149
353
|
|
150
|
-
|
151
|
-
url: format( '%s/objects/hosts/%s', @icinga_api_url_base,
|
354
|
+
api_data(
|
355
|
+
url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
|
152
356
|
headers: @headers,
|
153
357
|
options: @options
|
154
358
|
)
|
155
|
-
|
156
|
-
return data.dig('results') if( data.dig(:status).nil? )
|
157
|
-
|
158
|
-
nil
|
159
359
|
end
|
160
360
|
|
161
361
|
# returns true if the host exists
|
@@ -172,12 +372,13 @@ module Icinga2
|
|
172
372
|
raise ArgumentError.new('only String are allowed') unless( host_name.is_a?(String) )
|
173
373
|
raise ArgumentError.new('Missing host_name') if( host_name.size.zero? )
|
174
374
|
|
175
|
-
result = hosts(
|
176
|
-
result = JSON.parse( result ) if
|
375
|
+
result = hosts( name: host_name )
|
376
|
+
result = JSON.parse( result ) if( result.is_a?(String) )
|
377
|
+
result = result.first if( result.is_a?(Array) )
|
177
378
|
|
178
|
-
return
|
379
|
+
return false if( result.is_a?(Hash) && result.dig('code') == 404 )
|
179
380
|
|
180
|
-
|
381
|
+
true
|
181
382
|
end
|
182
383
|
|
183
384
|
# returns host objects
|
@@ -206,7 +407,6 @@ module Icinga2
|
|
206
407
|
# raise ArgumentError.new('only Array for joins are allowed') unless( joins.is_a?(Hash) )
|
207
408
|
|
208
409
|
payload = {}
|
209
|
-
results = nil
|
210
410
|
|
211
411
|
if( attrs.nil? )
|
212
412
|
attrs = %w[name state acknowledgement downtime_depth last_check]
|
@@ -216,46 +416,39 @@ module Icinga2
|
|
216
416
|
payload['filter'] = filter unless filter.nil?
|
217
417
|
payload['joins'] = joins unless joins.nil?
|
218
418
|
|
219
|
-
data =
|
419
|
+
data = api_data(
|
220
420
|
url: format( '%s/objects/hosts', @icinga_api_url_base ),
|
221
421
|
headers: @headers,
|
222
422
|
options: @options,
|
223
423
|
payload: payload
|
224
424
|
)
|
225
425
|
|
226
|
-
|
227
|
-
|
228
|
-
if( status.nil? )
|
229
|
-
|
230
|
-
results = data.dig('results')
|
231
|
-
|
232
|
-
unless( results.nil? )
|
426
|
+
@last_host_objects_called = Time.now.to_i
|
233
427
|
|
234
|
-
|
428
|
+
if( !data.nil? && data.is_a?(Array) )
|
235
429
|
|
236
|
-
|
430
|
+
all_hosts = data.clone
|
237
431
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
432
|
+
unless( all_hosts.nil? )
|
433
|
+
# global var for count of all hosts
|
434
|
+
@hosts_all = all_hosts.size
|
435
|
+
# global var for count of all host with a problem
|
436
|
+
@hosts_problems = count_problems(all_hosts)
|
437
|
+
# global var for count of all gost with state HOSTS_DOWN
|
438
|
+
@hosts_problems_down = count_problems(all_hosts, Icinga2::HOSTS_DOWN)
|
439
|
+
@hosts_problems_critical = count_problems(all_hosts, Icinga2::HOSTS_CRITICAL)
|
440
|
+
@hosts_problems_unknown = count_problems(all_hosts, Icinga2::HOSTS_UNKNOWN)
|
246
441
|
|
247
|
-
end
|
248
442
|
end
|
249
443
|
end
|
250
444
|
|
251
|
-
|
445
|
+
data
|
252
446
|
end
|
253
447
|
|
254
448
|
# returns adjusted hosts state
|
449
|
+
# OBSOLETE
|
255
450
|
#
|
256
451
|
# @example
|
257
|
-
# @icinga.cib_data
|
258
|
-
# @icinga.host_objects
|
259
452
|
# handled, down = @icinga.hosts_adjusted.values
|
260
453
|
#
|
261
454
|
# h = @icinga.hosts_adjusted
|
@@ -267,6 +460,12 @@ module Icinga2
|
|
267
460
|
#
|
268
461
|
def hosts_adjusted
|
269
462
|
|
463
|
+
puts 'function hosts_adjusted() is obsolete'
|
464
|
+
puts 'Please use host_problems()'
|
465
|
+
|
466
|
+
cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
|
467
|
+
host_objects if((Time.now.to_i - @last_host_objects_called).to_i > @last_call_timeout)
|
468
|
+
|
270
469
|
raise ArgumentError.new('Integer for @hosts_problems_down needed') unless( @hosts_problems_down.is_a?(Integer) )
|
271
470
|
raise ArgumentError.new('Integer for @hosts_problems_critical needed') unless( @hosts_problems_critical.is_a?(Integer) )
|
272
471
|
raise ArgumentError.new('Integer for @hosts_problems_unknown needed') unless( @hosts_problems_unknown.is_a?(Integer) )
|
@@ -319,6 +518,8 @@ module Icinga2
|
|
319
518
|
host_data = host_objects
|
320
519
|
host_data = JSON.parse( host_data ) if host_data.is_a?(String)
|
321
520
|
|
521
|
+
# logger.debug(host_data)
|
522
|
+
|
322
523
|
unless( host_data.nil? )
|
323
524
|
|
324
525
|
host_data.each do |h,_v|
|
@@ -334,7 +535,7 @@ module Icinga2
|
|
334
535
|
# get the count of problems
|
335
536
|
#
|
336
537
|
if( host_problems.count != 0 )
|
337
|
-
host_problems.keys[1..max_items].each { |k
|
538
|
+
host_problems.keys[1..max_items].each { |k| host_problems_severity[k] = host_problems[k] }
|
338
539
|
end
|
339
540
|
|
340
541
|
host_problems_severity
|
@@ -343,12 +544,12 @@ module Icinga2
|
|
343
544
|
# returns a counter of all hosts
|
344
545
|
#
|
345
546
|
# @example
|
346
|
-
# @icinga.host_objects
|
347
547
|
# @icinga.hosts_all
|
348
548
|
#
|
349
549
|
# @return [Integer]
|
350
550
|
#
|
351
551
|
def hosts_all
|
552
|
+
host_objects if( @hosts_all.nil? || @hosts_all.zero? )
|
352
553
|
@hosts_all
|
353
554
|
end
|
354
555
|
|
@@ -356,7 +557,7 @@ module Icinga2
|
|
356
557
|
#
|
357
558
|
# @example
|
358
559
|
# @icinga.host_objects
|
359
|
-
# all, down, critical, unknown = @icinga.host_problems.values
|
560
|
+
# all, down, critical, unknown, handled, adjusted = @icinga.host_problems.values
|
360
561
|
#
|
361
562
|
# p = @icinga.host_problems
|
362
563
|
# down = h.dig(:down)
|
@@ -369,16 +570,31 @@ module Icinga2
|
|
369
570
|
#
|
370
571
|
def host_problems
|
371
572
|
|
573
|
+
cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
|
574
|
+
host_objects if((Time.now.to_i - @last_host_objects_called).to_i > @last_call_timeout)
|
575
|
+
|
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) )
|
580
|
+
|
372
581
|
problems_all = @hosts_problems.nil? ? 0 : @hosts_problems
|
373
582
|
problems_down = @hosts_problems_down.nil? ? 0 : @hosts_problems_down
|
374
583
|
problems_critical = @hosts_problems_critical.nil? ? 0 : @hosts_problems_critical
|
375
584
|
problems_unknown = @hosts_problems_unknown.nil? ? 0 : @hosts_problems_unknown
|
376
585
|
|
586
|
+
# calculate host problems adjusted by handled problems
|
587
|
+
# count togther handled host problems
|
588
|
+
problems_handled = @hosts_problems_down + @hosts_problems_critical + @hosts_problems_unknown
|
589
|
+
problems_adjusted = @hosts_down - problems_handled
|
590
|
+
|
377
591
|
{
|
378
592
|
all: problems_all.to_i,
|
379
593
|
down: problems_down.to_i,
|
380
594
|
critical: problems_critical.to_i,
|
381
|
-
unknown: problems_unknown.to_i
|
595
|
+
unknown: problems_unknown.to_i,
|
596
|
+
handled: problems_handled.to_i,
|
597
|
+
adjusted: problems_adjusted.to_i
|
382
598
|
}
|
383
599
|
end
|
384
600
|
|