icinga2 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +504 -0
- data/README.md +118 -0
- data/doc/downtimes.md +7 -0
- data/doc/examples +122 -0
- data/doc/hostgroups.md +19 -0
- data/doc/hosts.md +22 -0
- data/doc/notifications.md +15 -0
- data/doc/servicegroups.md +19 -0
- data/doc/services.md +26 -0
- data/doc/usergroups.md +12 -0
- data/doc/users.md +41 -0
- data/examples/test.rb +150 -0
- data/lib/icinga2.rb +140 -0
- data/lib/icinga2/converts.rb +77 -0
- data/lib/icinga2/downtimes.rb +173 -0
- data/lib/icinga2/hostgroups.rb +98 -0
- data/lib/icinga2/hosts.rb +307 -0
- data/lib/icinga2/network.rb +397 -0
- data/lib/icinga2/notifications.rb +158 -0
- data/lib/icinga2/servicegroups.rb +101 -0
- data/lib/icinga2/services.rb +301 -0
- data/lib/icinga2/status.rb +52 -0
- data/lib/icinga2/tools.rb +13 -0
- data/lib/icinga2/usergroups.rb +101 -0
- data/lib/icinga2/users.rb +143 -0
- data/lib/icinga2/version.rb +15 -0
- data/lib/logging.rb +36 -0
- metadata +210 -0
@@ -0,0 +1,158 @@
|
|
1
|
+
|
2
|
+
module Icinga2
|
3
|
+
|
4
|
+
module Notifications
|
5
|
+
|
6
|
+
|
7
|
+
def enableHostNotification( host )
|
8
|
+
|
9
|
+
return hostNotification( { :name => host, :enable_notifications => true } )
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def disableHostNotification( host )
|
14
|
+
|
15
|
+
return self.hostNotification( { :name => host, :enable_notifications => false } )
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def enableServiceNotification( params = {} )
|
20
|
+
|
21
|
+
host = params.get(:host)
|
22
|
+
service = params.get(:service)
|
23
|
+
|
24
|
+
if( host == nil )
|
25
|
+
|
26
|
+
return {
|
27
|
+
:status => 404,
|
28
|
+
:message => 'missing host name'
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
return self.serviceNotification( { :name => host, :service => service, :enable_notifications => true } )
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def disableServiceNotification( host )
|
37
|
+
|
38
|
+
return self.serviceNotification( { :name => host, :enable_notifications => false } )
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def enableHostgroupNotification( group )
|
43
|
+
|
44
|
+
return self.hostgroupNotification( { :host_group => group, :enable_notifications => true } )
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def disableHostgroupNotification( group )
|
49
|
+
|
50
|
+
return self.hostgroupNotification( { :host_group => group, :enable_notifications => false } )
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def listNotifications( params = {} )
|
55
|
+
|
56
|
+
name = params.dig(:name)
|
57
|
+
|
58
|
+
result = Network.get( {
|
59
|
+
:host => name,
|
60
|
+
:url => sprintf( '%s/v1/objects/notifications/%s', @icingaApiUrlBase, name ),
|
61
|
+
:headers => @headers,
|
62
|
+
:options => @options
|
63
|
+
} )
|
64
|
+
|
65
|
+
return JSON.pretty_generate( result )
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
# PRIVATE SECTION
|
71
|
+
#
|
72
|
+
def hostNotification( params = {} )
|
73
|
+
|
74
|
+
name = params.dig(:name)
|
75
|
+
notifications = params.dig(:enable_notifications) || false
|
76
|
+
|
77
|
+
payload = {
|
78
|
+
"attrs" => {
|
79
|
+
"enable_notifications" => notifications
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
result = Network.post( {
|
84
|
+
:host => name,
|
85
|
+
:url => sprintf( '%s/v1/objects/hosts/%s', @icingaApiUrlBase, name ),
|
86
|
+
:headers => @headers,
|
87
|
+
:options => @options,
|
88
|
+
:payload => payload
|
89
|
+
} )
|
90
|
+
|
91
|
+
logger.debug( result.class.to_s )
|
92
|
+
|
93
|
+
return JSON.pretty_generate( result )
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
def hostgroupNotification( params = {} )
|
99
|
+
|
100
|
+
group = params.dig(:host_group)
|
101
|
+
notifications = params.dig(:enable_notifications) || false
|
102
|
+
|
103
|
+
payload = {
|
104
|
+
"filter" => sprintf( '"%s" in host.groups', group ),
|
105
|
+
"attrs" => {
|
106
|
+
"enable_notifications" => notifications
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
result = Network.post( {
|
111
|
+
:host => name,
|
112
|
+
:url => sprintf( '%s/v1/objects/services', @icingaApiUrlBase ),
|
113
|
+
:headers => @headers,
|
114
|
+
:options => @options,
|
115
|
+
:payload => payload
|
116
|
+
} )
|
117
|
+
|
118
|
+
logger.debug( result.class.to_s )
|
119
|
+
|
120
|
+
return JSON.pretty_generate( result )
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
def serviceNotification( params = {} )
|
126
|
+
|
127
|
+
name = params.dig(:name)
|
128
|
+
service = params.dig(:service)
|
129
|
+
notifications = params.dig(:enable_notifications) || false
|
130
|
+
|
131
|
+
payload = {
|
132
|
+
"filter" => sprintf( 'host.name=="%s"', name ),
|
133
|
+
"attrs" => {
|
134
|
+
"enable_notifications" => notifications
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
logger.debug( payload )
|
139
|
+
logger.debug( sprintf( '%s/v1/objects/services', @icingaApiUrlBase ) )
|
140
|
+
|
141
|
+
result = Network.post( {
|
142
|
+
:host => name,
|
143
|
+
:url => sprintf( '%s/v1/objects/services', @icingaApiUrlBase ),
|
144
|
+
:headers => @headers,
|
145
|
+
:options => @options,
|
146
|
+
:payload => payload
|
147
|
+
} )
|
148
|
+
|
149
|
+
logger.debug( result.class.to_s )
|
150
|
+
|
151
|
+
return JSON.pretty_generate( result )
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
|
2
|
+
module Icinga2
|
3
|
+
|
4
|
+
module Servicegroups
|
5
|
+
|
6
|
+
|
7
|
+
def addServicegroup( params = {} )
|
8
|
+
|
9
|
+
name = params.dig(:name)
|
10
|
+
displayName = params.dig(:display_name)
|
11
|
+
|
12
|
+
if( name == nil )
|
13
|
+
|
14
|
+
return {
|
15
|
+
:status => 404,
|
16
|
+
:message => 'missing servicegroup name'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
payload = {
|
21
|
+
"attrs" => {
|
22
|
+
"display_name" => displayName
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
result = Network.put( {
|
27
|
+
:host => name,
|
28
|
+
:url => sprintf( '%s/v1/objects/servicegroups/%s', @icingaApiUrlBase, name ),
|
29
|
+
:headers => @headers,
|
30
|
+
:options => @options,
|
31
|
+
:payload => payload
|
32
|
+
} )
|
33
|
+
|
34
|
+
return JSON.pretty_generate( result )
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def deleteServicegroup( params = {} )
|
40
|
+
|
41
|
+
name = params.dig(:name)
|
42
|
+
|
43
|
+
if( name == nil )
|
44
|
+
|
45
|
+
return {
|
46
|
+
:status => 404,
|
47
|
+
:message => 'missing servicegroup name'
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
result = Network.delete( {
|
52
|
+
:host => name,
|
53
|
+
:url => sprintf( '%s/v1/objects/servicegroups/%s?cascade=1', @icingaApiUrlBase, name ),
|
54
|
+
:headers => @headers,
|
55
|
+
:options => @options
|
56
|
+
} )
|
57
|
+
|
58
|
+
return JSON.pretty_generate( result )
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def listServicegroups( params = {} )
|
64
|
+
|
65
|
+
name = params.dig(:name)
|
66
|
+
|
67
|
+
result = Network.get( {
|
68
|
+
:host => name,
|
69
|
+
:url => sprintf( '%s/v1/objects/servicegroups/%s', @icingaApiUrlBase, name ),
|
70
|
+
:headers => @headers,
|
71
|
+
:options => @options
|
72
|
+
} )
|
73
|
+
|
74
|
+
return JSON.pretty_generate( result )
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
def existsServicegroup?( name )
|
80
|
+
|
81
|
+
|
82
|
+
result = self.listServicegroups( { :name => name } )
|
83
|
+
|
84
|
+
if( result.is_a?( String ) )
|
85
|
+
result = JSON.parse( result )
|
86
|
+
end
|
87
|
+
|
88
|
+
status = result.dig('status')
|
89
|
+
|
90
|
+
if( status != nil && status == 200 )
|
91
|
+
return true
|
92
|
+
end
|
93
|
+
|
94
|
+
return false
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,301 @@
|
|
1
|
+
|
2
|
+
module Icinga2
|
3
|
+
|
4
|
+
module Services
|
5
|
+
|
6
|
+
def addServices( host, services = {} )
|
7
|
+
|
8
|
+
def updateHost( hash, host )
|
9
|
+
|
10
|
+
hash.each do |k, v|
|
11
|
+
|
12
|
+
if( k == "host" && v.is_a?( String ) )
|
13
|
+
v.replace( host )
|
14
|
+
|
15
|
+
elsif( v.is_a?( Hash ) )
|
16
|
+
self.updateHost( v, host )
|
17
|
+
|
18
|
+
elsif( v.is_a?(Array) )
|
19
|
+
|
20
|
+
v.flatten.each { |x| self.updateHost( x, host ) if x.is_a?( Hash ) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
services.each do |s,v|
|
28
|
+
|
29
|
+
payload = {
|
30
|
+
"templates" => [ "generic-service" ],
|
31
|
+
"attrs" => updateHost( v, host )
|
32
|
+
}
|
33
|
+
|
34
|
+
logger.debug( s )
|
35
|
+
logger.debug( v.to_json )
|
36
|
+
|
37
|
+
logger.debug( JSON.pretty_generate( payload ) )
|
38
|
+
|
39
|
+
result = Network.put( {
|
40
|
+
:host => host,
|
41
|
+
:url => sprintf( '%s/v1/objects/services/%s!%s', @icingaApiUrlBase, host, s ),
|
42
|
+
:headers => @headers,
|
43
|
+
:options => @options,
|
44
|
+
:payload => payload
|
45
|
+
})
|
46
|
+
|
47
|
+
logger.debug( result )
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def unhandledServices( params = {} )
|
55
|
+
|
56
|
+
# taken from https://blog.netways.de/2016/11/18/icinga-2-api-cheat-sheet/
|
57
|
+
# 5) Anzeige aller Services die unhandled sind und weder in Downtime, noch acknowledged sind
|
58
|
+
# /usr/bin/curl -k -s -u 'root:icinga' -H 'X-HTTP-Method-Override: GET' -X POST 'https://127.0.0.1:5665/v1/objects/services' -d '{ "attrs": [ "__name", "state", "downtime_depth", "acknowledgement" ], "filter": "service.state != ServiceOK && service.downtime_depth == 0.0 && service.acknowledgement == 0.0" }''' | jq
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def listServices( params = {} )
|
64
|
+
|
65
|
+
name = params.dig(:host)
|
66
|
+
service = params.dig(:service)
|
67
|
+
|
68
|
+
if( service == nil )
|
69
|
+
url = sprintf( '%s/v1/objects/services/%s', @icingaApiUrlBase, name )
|
70
|
+
else
|
71
|
+
url = sprintf( '%s/v1/objects/services/%s!%s', @icingaApiUrlBase, name, service )
|
72
|
+
end
|
73
|
+
|
74
|
+
result = Network.get( {
|
75
|
+
:host => name,
|
76
|
+
:url => url,
|
77
|
+
:headers => @headers,
|
78
|
+
:options => @options
|
79
|
+
} )
|
80
|
+
|
81
|
+
return JSON.pretty_generate( result )
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def existsService?( params = {} )
|
87
|
+
|
88
|
+
host = params.dig(:host)
|
89
|
+
service = params.dig(:service)
|
90
|
+
|
91
|
+
if( host == nil )
|
92
|
+
|
93
|
+
return {
|
94
|
+
:status => 404,
|
95
|
+
:message => 'missing host name'
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
result = self.listServices( { :host => host, :service => service } )
|
100
|
+
|
101
|
+
if( result.is_a?( String ) )
|
102
|
+
result = JSON.parse( result )
|
103
|
+
end
|
104
|
+
|
105
|
+
status = result.dig('status')
|
106
|
+
|
107
|
+
if( status != nil && status == 200 )
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
|
111
|
+
return false
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
def serviceObjects( params = {} )
|
117
|
+
|
118
|
+
attrs = params.dig(:attrs)
|
119
|
+
filter = params.dig(:filter)
|
120
|
+
joins = params.dig(:joins)
|
121
|
+
payload = {}
|
122
|
+
|
123
|
+
if( attrs == nil )
|
124
|
+
attrs = ['name','state','acknowledgement','downtime_depth','last_check']
|
125
|
+
end
|
126
|
+
|
127
|
+
if( joins == nil )
|
128
|
+
joins = ['host.name','host.state','host.acknowledgement','host.downtime_depth','host.last_check']
|
129
|
+
end
|
130
|
+
|
131
|
+
if( attrs != nil )
|
132
|
+
payload['attrs'] = attrs
|
133
|
+
end
|
134
|
+
|
135
|
+
if( filter != nil )
|
136
|
+
payload['filter'] = filter
|
137
|
+
end
|
138
|
+
|
139
|
+
if( joins != nil )
|
140
|
+
payload['joins'] = joins
|
141
|
+
end
|
142
|
+
|
143
|
+
result = Network.get( {
|
144
|
+
:host => nil,
|
145
|
+
:url => sprintf( '%s/v1/objects/services', @icingaApiUrlBase ),
|
146
|
+
:headers => @headers,
|
147
|
+
:options => @options,
|
148
|
+
:payload => payload
|
149
|
+
} )
|
150
|
+
|
151
|
+
return JSON.pretty_generate( result )
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
def serviceProblems()
|
157
|
+
|
158
|
+
data = self.serviceObjects()
|
159
|
+
problems = 0
|
160
|
+
|
161
|
+
if( data.is_a?(String) )
|
162
|
+
data = JSON.parse(data)
|
163
|
+
end
|
164
|
+
|
165
|
+
nodes = data.dig('nodes')
|
166
|
+
|
167
|
+
nodes.each do |n|
|
168
|
+
|
169
|
+
attrs = n.last.dig('attrs')
|
170
|
+
|
171
|
+
state = attrs.dig('state')
|
172
|
+
downtimeDepth = attrs.dig('downtime_depth')
|
173
|
+
acknowledgement = attrs.dig('acknowledgement')
|
174
|
+
|
175
|
+
# puts state
|
176
|
+
|
177
|
+
if( state != 0 && downtimeDepth == 0 && acknowledgement == 0 )
|
178
|
+
problems += 1 #= problems +1
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
return problems
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
def problemServices( max_items = 5 )
|
189
|
+
|
190
|
+
count = 0
|
191
|
+
@serviceProblems = {}
|
192
|
+
@serviceProblemsSeverity = {}
|
193
|
+
|
194
|
+
# only fetch the minimal attribute set required for severity calculation
|
195
|
+
servicesData = self.serviceObjects()
|
196
|
+
|
197
|
+
if( servicesData.is_a?(String) )
|
198
|
+
|
199
|
+
servicesData = JSON.parse( servicesData )
|
200
|
+
end
|
201
|
+
|
202
|
+
servicesData = servicesData.dig('nodes')
|
203
|
+
|
204
|
+
servicesData.each do |service,v|
|
205
|
+
|
206
|
+
name = v.dig('name')
|
207
|
+
state = v.dig('attrs','state')
|
208
|
+
# logger.debug( "Severity for #{name}" )
|
209
|
+
if( state == 0 )
|
210
|
+
next
|
211
|
+
end
|
212
|
+
|
213
|
+
@serviceProblems[name] = self.serviceSeverity(v)
|
214
|
+
end
|
215
|
+
|
216
|
+
@serviceProblems.sort_by {|v| v}.reverse!
|
217
|
+
|
218
|
+
@serviceProblems.keys[1..max_items].each { |k,v| @serviceProblemsSeverity[k] = @serviceProblems[k] }
|
219
|
+
|
220
|
+
# @serviceProblems.each do |k,v|
|
221
|
+
#
|
222
|
+
# if( count >= max_items )
|
223
|
+
# break
|
224
|
+
# end
|
225
|
+
#
|
226
|
+
# @serviceProblemsSeverity[k] = v
|
227
|
+
#
|
228
|
+
# count += 1
|
229
|
+
# end
|
230
|
+
|
231
|
+
return @serviceProblemsSeverity
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
# private
|
236
|
+
# stolen from Icinga Web 2
|
237
|
+
# ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
|
238
|
+
#
|
239
|
+
def serviceSeverity( service )
|
240
|
+
|
241
|
+
attrs = service.dig('attrs')
|
242
|
+
state = attrs.dig('state')
|
243
|
+
acknowledgement = attrs.dig('acknowledgement')
|
244
|
+
downtimeDepth = attrs.dig('downtime_depth')
|
245
|
+
|
246
|
+
# logger.debug( attrs )
|
247
|
+
|
248
|
+
severity = 0
|
249
|
+
|
250
|
+
if( state == 0 )
|
251
|
+
|
252
|
+
if( self.getObjectHasBeenChecked( service ) )
|
253
|
+
severity += 16
|
254
|
+
end
|
255
|
+
|
256
|
+
if( acknowledgement != 0 )
|
257
|
+
severity += 2
|
258
|
+
elsif( downtimeDepth > 0 )
|
259
|
+
severity += 1
|
260
|
+
else
|
261
|
+
severity += 4
|
262
|
+
end
|
263
|
+
else
|
264
|
+
if( self.getObjectHasBeenChecked( service ) )
|
265
|
+
severity += 16
|
266
|
+
elsif( state == 1 )
|
267
|
+
severity += 32
|
268
|
+
elsif( state == 2 )
|
269
|
+
severity += 128
|
270
|
+
elsif( state == 3 )
|
271
|
+
severity += 64
|
272
|
+
else
|
273
|
+
severity += 256
|
274
|
+
end
|
275
|
+
|
276
|
+
# requires joins
|
277
|
+
host_attrs = service.dig('joins','host')
|
278
|
+
|
279
|
+
host_state = host_attrs.dig('state')
|
280
|
+
host_acknowledgement = host_attrs.dig('acknowledgement')
|
281
|
+
host_downtimeDepth = host_attrs.dig('downtime_depth')
|
282
|
+
|
283
|
+
if( host_state > 0 )
|
284
|
+
severity += 1024
|
285
|
+
elsif( host_acknowledgement )
|
286
|
+
severity += 512
|
287
|
+
elsif( host_downtimeDepth > 0 )
|
288
|
+
severity += 256
|
289
|
+
else
|
290
|
+
severity += 2048
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
return severity
|
295
|
+
end
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|