icinga2 0.5.2 → 0.6.1

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