icinga2 0.7.0.1 → 0.8.1.2

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.
@@ -17,6 +17,9 @@ module Icinga2
17
17
  #
18
18
  def enable_host_notification( host )
19
19
 
20
+ raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
21
+ raise ArgumentError.new('missing host') if( host.size.zero? )
22
+
20
23
  host_notification( name: host, enable_notifications: true )
21
24
  end
22
25
 
@@ -31,6 +34,9 @@ module Icinga2
31
34
  #
32
35
  def disable_host_notification( host )
33
36
 
37
+ raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
38
+ raise ArgumentError.new('missing host') if( host.size.zero? )
39
+
34
40
  host_notification( name: host, enable_notifications: false )
35
41
  end
36
42
 
@@ -45,13 +51,8 @@ module Icinga2
45
51
  #
46
52
  def enable_service_notification( host )
47
53
 
48
- if( host.nil? )
49
-
50
- return {
51
- status: 404,
52
- message: 'missing host name'
53
- }
54
- end
54
+ raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
55
+ raise ArgumentError.new('missing host') if( host.size.zero? )
55
56
 
56
57
  service_notification( name: host, enable_notifications: true )
57
58
  end
@@ -67,12 +68,8 @@ module Icinga2
67
68
  #
68
69
  def disable_service_notification( host )
69
70
 
70
- if( host.nil? )
71
- return {
72
- status: 404,
73
- message: 'missing host name'
74
- }
75
- end
71
+ raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
72
+ raise ArgumentError.new('missing host') if( host.size.zero? )
76
73
 
77
74
  service_notification( name: host, enable_notifications: false )
78
75
  end
@@ -88,24 +85,16 @@ module Icinga2
88
85
  #
89
86
  # @return [Hash]
90
87
  #
91
- def enable_hostgroup_notification( params = {} )
88
+ def enable_hostgroup_notification( params )
92
89
 
93
- host = params.dig(:host)
94
- host_group = params.dig(:host_group)
90
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
91
+ raise ArgumentError.new('missing params') if( params.size.zero? )
95
92
 
96
- if( host.nil? )
97
- return {
98
- status: 404,
99
- message: 'missing host name'
100
- }
101
- end
93
+ host = params.dig(:host)
94
+ host_group = params.dig(:host_group)
102
95
 
103
- if( host_group.nil? )
104
- return {
105
- status: 404,
106
- message: 'missing host_group name'
107
- }
108
- end
96
+ raise ArgumentError.new('Missing host') if( host.nil? )
97
+ raise ArgumentError.new('Missing host_group') if( host_group.nil? )
109
98
 
110
99
  hostgroup_notification( host: host, host_group: host_group, enable_notifications: true )
111
100
  end
@@ -121,49 +110,41 @@ module Icinga2
121
110
  #
122
111
  # @return [Hash]
123
112
  #
124
- def disable_hostgroup_notification( params = {} )
113
+ def disable_hostgroup_notification( params )
125
114
 
126
- host = params.dig(:host)
127
- host_group = params.dig(:host_group)
115
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
116
+ raise ArgumentError.new('missing params') if( params.size.zero? )
128
117
 
129
- if( host.nil? )
130
- return {
131
- status: 404,
132
- message: 'missing host name'
133
- }
134
- end
118
+ host = params.dig(:host)
119
+ host_group = params.dig(:host_group)
135
120
 
136
- if( host_group.nil? )
137
- return {
138
- status: 404,
139
- message: 'missing host_group name'
140
- }
141
- end
121
+ raise ArgumentError.new('Missing host') if( host.nil? )
122
+ raise ArgumentError.new('Missing host_group') if( host_group.nil? )
142
123
 
143
124
  hostgroup_notification( host: host, host_group: host_group, enable_notifications: false )
144
125
  end
145
126
 
146
127
  # return all notifications
147
128
  #
148
- # @param [Hash] params
149
- # @option params [String] name
150
129
  #
151
- # @return [Hash]
130
+ # @return [Array]
152
131
  #
153
- def notifications( params = {} )
154
-
155
- name = params.dig(:name)
132
+ def notifications
156
133
 
157
- Network.get( host: name,
158
- url: format( '%s/v1/objects/notifications/%s', @icinga_api_url_base, name ),
134
+ data = Network.api_data(
135
+ url: format( '%s/objects/notifications', @icinga_api_url_base ),
159
136
  headers: @headers,
160
- options: @options )
137
+ options: @options
138
+ )
161
139
 
162
- end
140
+ return data.dig('results') if( data.dig(:status).nil? )
163
141
 
142
+ nil
143
+ end
164
144
 
145
+ protected
165
146
  # function for host notifications
166
- # @private
147
+ # @api protected
167
148
  #
168
149
  # @param [Hash] params
169
150
  # @option params [String] name
@@ -171,7 +152,6 @@ module Icinga2
171
152
  #
172
153
  # @return [Hash]
173
154
  #
174
- private
175
155
  def host_notification( params = {} )
176
156
 
177
157
  name = params.dig(:name)
@@ -183,16 +163,16 @@ module Icinga2
183
163
  }
184
164
  }
185
165
 
186
- Network.post( host: name,
187
- url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, name ),
166
+ Network.post(
167
+ url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
188
168
  headers: @headers,
189
169
  options: @options,
190
- payload: payload )
191
-
170
+ payload: payload
171
+ )
192
172
  end
193
173
 
194
174
  # function for hostgroup notifications
195
- # @private
175
+ # @api protected
196
176
  #
197
177
  # @param [Hash] params
198
178
  # @option params [String] host_group
@@ -200,10 +180,8 @@ module Icinga2
200
180
  #
201
181
  # @return [Hash]
202
182
  #
203
- private
204
183
  def hostgroup_notification( params = {} )
205
184
 
206
- host = params.dig(:host)
207
185
  group = params.dig(:host_group)
208
186
  notifications = params.dig(:enable_notifications) || false
209
187
 
@@ -214,16 +192,16 @@ module Icinga2
214
192
  }
215
193
  }
216
194
 
217
- Network.post( host: host,
218
- url: format( '%s/v1/objects/services', @icinga_api_url_base ),
195
+ Network.post(
196
+ url: format( '%s/objects/services', @icinga_api_url_base ),
219
197
  headers: @headers,
220
198
  options: @options,
221
- payload: payload )
222
-
199
+ payload: payload
200
+ )
223
201
  end
224
202
 
225
203
  # function for service notifications
226
- # @private
204
+ # @api protected
227
205
  #
228
206
  # @param [Hash] params
229
207
  # @option params [String] name
@@ -231,7 +209,6 @@ module Icinga2
231
209
  #
232
210
  # @return [Hash]
233
211
  #
234
- private
235
212
  def service_notification( params = {} )
236
213
 
237
214
  name = params.dig(:name)
@@ -244,12 +221,12 @@ module Icinga2
244
221
  }
245
222
  }
246
223
 
247
- Network.post( host: name,
248
- url: format( '%s/v1/objects/services', @icinga_api_url_base ),
224
+ Network.post(
225
+ url: format( '%s/objects/services', @icinga_api_url_base ),
249
226
  headers: @headers,
250
227
  options: @options,
251
- payload: payload )
252
-
228
+ payload: payload
229
+ )
253
230
  end
254
231
 
255
232
  end
@@ -9,104 +9,116 @@ module Icinga2
9
9
  # add a servicegroup
10
10
  #
11
11
  # @param [Hash] params
12
- # @option params [String] :name servicegroup to create
12
+ # @option params [String] :service_group servicegroup to create
13
13
  # @option params [String] :display_name the displayed name
14
14
  #
15
15
  # @example
16
- # @icinga.add_servicegroup(name: 'foo', display_name: 'FOO')
16
+ # @icinga.add_servicegroup(service_group: 'foo', display_name: 'FOO')
17
17
  #
18
18
  # @return [Hash] result
19
19
  #
20
- def add_servicegroup( params = {} )
20
+ def add_servicegroup( params )
21
21
 
22
- name = params.dig(:name)
22
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
23
+ raise ArgumentError.new('missing params') if( params.size.zero? )
24
+
25
+ service_group = params.dig(:service_group)
23
26
  display_name = params.dig(:display_name)
24
27
 
25
- if( name.nil? )
26
- return {
27
- status: 404,
28
- message: 'missing servicegroup name'
29
- }
30
- end
28
+ raise ArgumentError.new('Missing service_group') if( service_group.nil? )
29
+ raise ArgumentError.new('Missing display_name') if( display_name.nil? )
31
30
 
32
31
  payload = { 'attrs' => { 'display_name' => display_name } }
33
32
 
34
- Network.put( host: name,
35
- url: format( '%s/v1/objects/servicegroups/%s', @icinga_api_url_base, name ),
33
+ Network.put(
34
+ url: format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ),
36
35
  headers: @headers,
37
36
  options: @options,
38
- payload: payload )
39
-
37
+ payload: payload
38
+ )
40
39
  end
41
40
 
42
41
  # delete a servicegroup
43
42
  #
44
43
  # @param [Hash] params
45
- # @option params [String] :name servicegroup to delete
44
+ # @option params [String] :service_group servicegroup to delete
46
45
  #
47
46
  # @example
48
- # @icinga.delete_servicegroup(name: 'foo')
47
+ # @icinga.delete_servicegroup(service_group: 'foo')
49
48
  #
50
- # @return [Hash] result
49
+ # @return [Array] result
51
50
  #
52
- def delete_servicegroup( params = {} )
51
+ def delete_servicegroup( params )
53
52
 
54
- name = params.dig(:name)
53
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
54
+ raise ArgumentError.new('missing params') if( params.size.zero? )
55
55
 
56
- if( name.nil? )
57
- return {
58
- status: 404,
59
- message: 'missing servicegroup name'
60
- }
61
- end
56
+ service_group = params.dig(:service_group)
62
57
 
63
- Network.delete( host: name,
64
- url: format( '%s/v1/objects/servicegroups/%s?cascade=1', @icinga_api_url_base, name ),
65
- headers: @headers,
66
- options: @options )
58
+ raise ArgumentError.new('Missing service_group') if( service_group.nil? )
67
59
 
60
+ Network.delete(
61
+ url: format( '%s/objects/servicegroups/%s?cascade=1', @icinga_api_url_base, service_group ),
62
+ headers: @headers,
63
+ options: @options
64
+ )
68
65
  end
69
66
 
70
67
  # returns all servicegroups
71
68
  #
72
69
  # @param [Hash] params
73
- # @option params [String] :name ('') optional for a single servicegroup
70
+ # @option params [String] :service_group ('') optional for a single servicegroup
74
71
  #
75
72
  # @example to get all users
76
73
  # @icinga.servicegroups
77
74
  #
78
75
  # @example to get one user
79
- # @icinga.servicegroups(name: 'disk')
76
+ # @icinga.servicegroups(service_group: 'disk')
80
77
  #
81
- # @return [Hash] returns a hash with all servicegroups
78
+ # @return [Array] returns a hash with all servicegroups
82
79
  #
83
80
  def servicegroups( params = {} )
84
81
 
85
- name = params.dig(:name)
82
+ service_group = params.dig(:service_group)
86
83
 
87
- Network.get( host: name,
88
- url: format( '%s/v1/objects/servicegroups/%s', @icinga_api_url_base, name ),
84
+ url =
85
+ if( service_group.nil? )
86
+ format( '%s/objects/servicegroups' , @icinga_api_url_base )
87
+ else
88
+ format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group )
89
+ end
90
+
91
+ data = Network.api_data(
92
+ url: url,
89
93
  headers: @headers,
90
- options: @options )
94
+ options: @options
95
+ )
96
+
97
+ return data.dig('results') if( data.dig(:status).nil? )
91
98
 
99
+ nil
92
100
  end
93
101
 
94
- # returns true if the servicegroup exists
102
+ # checks if the servicegroup exists
95
103
  #
96
- # @param [String] name the name of the servicegroups
104
+ # @param [String] service_group the name of the servicegroup
97
105
  #
98
106
  # @example
99
107
  # @icinga.exists_servicegroup?('disk')
100
108
  #
101
109
  # @return [Bool] returns true if the servicegroup exists
102
110
  #
103
- def exists_servicegroup?( name )
104
- result = servicegroups( name: name )
111
+ def exists_servicegroup?( service_group )
112
+
113
+ raise ArgumentError.new('only String are allowed') unless( service_group.is_a?(String) )
114
+ raise ArgumentError.new('Missing service_group') if( service_group.size.zero? )
115
+
116
+
117
+ result = servicegroups( service_group: service_group )
105
118
  result = JSON.parse( result ) if result.is_a?( String )
106
119
 
107
- status = result.dig(:status)
120
+ return true if !result.nil? && result.is_a?(Array)
108
121
 
109
- return true if !status.nil? && status == 200
110
122
  false
111
123
  end
112
124
 
@@ -8,19 +8,31 @@ module Icinga2
8
8
 
9
9
  # add services
10
10
  #
11
- # @param [String] host
12
- # @param [Hash] services
11
+ # @param [Hash] params
12
+ # @option params [String] :host
13
+ # @option params [String] :services
13
14
  #
15
+ # @todo
16
+ # this function is not operable
17
+ # need help, time or beer
14
18
  #
15
19
  # @return [Hash]
16
20
  #
17
- def add_services( host, services = {} )
21
+ def add_services( params = {} )
22
+
23
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
24
+
25
+ # TODO
26
+ puts 'add_services() ToDo'
27
+
28
+ host_name = params.dig(:host)
29
+ services = params.dig(:services)
18
30
 
19
31
  services.each do |s,v|
20
32
 
21
33
  payload = {
22
34
  'templates' => [ 'generic-service' ],
23
- 'attrs' => update_host( v, host )
35
+ 'attrs' => update_host( v, host_name )
24
36
  }
25
37
 
26
38
  logger.debug( s )
@@ -28,14 +40,12 @@ module Icinga2
28
40
 
29
41
  logger.debug( JSON.pretty_generate( payload ) )
30
42
 
31
- Network.put( host: host,
32
- url: format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, host, s ),
43
+ Network.put(
44
+ url: format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, s ),
33
45
  headers: @headers,
34
46
  options: @options,
35
- payload: payload)
36
-
37
- logger.debug( result )
38
-
47
+ payload: payload
48
+ )
39
49
  end
40
50
 
41
51
  end
@@ -45,16 +55,23 @@ module Icinga2
45
55
  # @param [Hash] params
46
56
  #
47
57
  # @todo
48
- # this function are not operable
58
+ # this function is not operable
49
59
  # need help, time or beer
50
60
  #
51
61
  # @return [Nil]
52
62
  #
53
63
  def unhandled_services( params = {} )
54
64
 
65
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
66
+
67
+ # TODO
68
+ puts 'unhandled_services() ToDo'
69
+
55
70
  # taken from https://blog.netways.de/2016/11/18/icinga-2-api-cheat-sheet/
56
71
  # 5) Anzeige aller Services die unhandled sind und weder in Downtime, noch acknowledged sind
57
- # /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
72
+ # /usr/bin/curl -k -s -u 'root:icinga' -H 'X-HTTP-Method-Override: GET' -X POST
73
+ # 'https://127.0.0.1:5665/objects/services' #
74
+ # -d '{ "attrs": [ "__name", "state", "downtime_depth", "acknowledgement" ], "filter": "service.state != ServiceOK && service.downtime_depth == 0.0 && service.acknowledgement == 0.0" }''' | jq
58
75
 
59
76
  end
60
77
 
@@ -74,21 +91,27 @@ module Icinga2
74
91
  #
75
92
  def services( params = {} )
76
93
 
77
- name = params.dig(:host)
78
- service = params.dig(:service)
94
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
95
+
96
+ host_name = params.dig(:host)
97
+ service = params.dig(:service)
79
98
 
80
99
  url =
81
100
  if( service.nil? )
82
- format( '%s/v1/objects/services/%s', @icinga_api_url_base, name )
101
+ format( '%s/objects/services/%s', @icinga_api_url_base, host_name )
83
102
  else
84
- format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, name, service )
103
+ format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, service )
85
104
  end
86
105
 
87
- Network.get( host: name,
106
+ data = Network.api_data(
88
107
  url: url,
89
108
  headers: @headers,
90
- options: @options )
109
+ options: @options
110
+ )
111
+
112
+ return data.dig('results') if( data.dig(:status).nil? )
91
113
 
114
+ nil
92
115
  end
93
116
 
94
117
  # returns true if the service exists
@@ -102,23 +125,22 @@ module Icinga2
102
125
  #
103
126
  # @return [Bool]
104
127
  #
105
- def exists_service?( params = {} )
128
+ def exists_service?( params )
129
+
130
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
131
+ raise ArgumentError.new('missing params') if( params.size.zero? )
106
132
 
107
133
  host = params.dig(:host)
108
134
  service = params.dig(:service)
109
135
 
110
- if( host.nil? )
111
- return {
112
- status: 404,
113
- message: 'missing host name'
114
- }
115
- end
136
+ raise ArgumentError.new('Missing host') if( host.nil? )
137
+ raise ArgumentError.new('Missing service') if( service.nil? )
116
138
 
117
139
  result = services( host: host, service: service )
118
140
  result = JSON.parse( result ) if result.is_a?( String )
119
- status = result.dig(:status)
120
141
 
121
- return true if !status.nil? && status == 200
142
+ return true if !result.nil? && result.is_a?(Array)
143
+
122
144
  false
123
145
  end
124
146
 
@@ -135,7 +157,7 @@ module Icinga2
135
157
  # @example
136
158
  # @icinga.service_objects(attrs: ['name', 'state'], joins: ['host.name','host.state'])
137
159
  #
138
- # @return [Hash]
160
+ # @return [Array]
139
161
  #
140
162
  def service_objects( params = {} )
141
163
 
@@ -152,47 +174,92 @@ module Icinga2
152
174
  joins = ['host.name', 'host.state', 'host.acknowledgement', 'host.downtime_depth', 'host.last_check']
153
175
  end
154
176
 
155
- payload['attrs'] = attrs unless attrs.nil?
156
- payload['filter'] = filter unless filter.nil?
157
- payload['joins'] = joins unless joins.nil?
177
+ payload['attrs'] = attrs unless attrs.nil?
178
+ payload['filter'] = filter unless filter.nil?
179
+ payload['joins'] = joins unless joins.nil?
158
180
 
159
- Network.get( host: nil,
160
- url: format( '%s/v1/objects/services', @icinga_api_url_base ),
181
+ data = Network.api_data(
182
+ url: format( '%s/objects/services', @icinga_api_url_base ),
161
183
  headers: @headers,
162
184
  options: @options,
163
- payload: payload )
185
+ payload: payload
186
+ )
187
+
188
+ status = data.dig(:status)
189
+
190
+ if( status.nil? )
191
+
192
+ results = data.dig('results')
193
+
194
+ unless( results.nil? )
195
+
196
+ all_services = results.clone
197
+
198
+ unless( all_services.nil? )
164
199
 
200
+ @services_all = all_services.size
201
+ @services_problems = count_problems(results)
202
+ @services_handled_warning = count_problems(results, Icinga2::SERVICE_STATE_WARNING)
203
+ @services_handled_critical = count_problems(results, Icinga2::SERVICE_STATE_CRITICAL)
204
+ @services_handled_unknown = count_problems(results, Icinga2::SERVICE_STATE_UNKNOWN)
205
+ end
206
+ end
207
+ end
208
+
209
+ results
210
+ end
211
+
212
+ # returns adjusted service state
213
+ #
214
+ # @example
215
+ # @icinga.cib_data
216
+ # @icinga.service_objects
217
+ # warning, critical, unknown = @icinga.services_adjusted.values
218
+ #
219
+ # s = @icinga.services_adjusted
220
+ # unknown = s.dig(:unknown)
221
+ #
222
+ # @return [Hash]
223
+ # * warning
224
+ # * critical
225
+ # * unknown
226
+ #
227
+ def services_adjusted
228
+
229
+ service_warning = @services_warning.nil? ? 0 : @services_warning
230
+ service_critical = @services_critical.nil? ? 0 : @services_critical
231
+ service_unknown = @services_unknown.nil? ? 0 : @services_unknown
232
+ service_handled_warning = @services_handled_warning.nil? ? 0 : @services_handled_warning
233
+ service_handled_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
234
+ service_handled_unknown = @services_handled_unknown.nil? ? 0 : @services_handled_unknown
235
+
236
+ # calculate service problems adjusted by handled problems
237
+ service_adjusted_warning = service_warning - service_handled_warning
238
+ service_adjusted_critical = service_critical - service_handled_critical
239
+ service_adjusted_unknown = service_unknown - service_handled_unknown
240
+
241
+ {
242
+ warning: service_adjusted_warning.to_i,
243
+ critical: service_adjusted_critical.to_i,
244
+ unknown: service_adjusted_unknown.to_i
245
+ }
165
246
  end
166
247
 
167
248
  # return count of services with problems
168
249
  #
169
250
  # @example
170
- # @icinga.service_problems
251
+ # @icinga.count_services_with_problems
171
252
  #
172
253
  # @return [Integer]
173
254
  #
174
- def service_problems
175
-
176
- data = service_objects
177
- problems = 0
178
-
179
- data = JSON.parse(data) if data.is_a?(String)
180
- nodes = data.dig(:nodes)
255
+ def count_services_with_problems
181
256
 
182
- unless( nodes.nil? )
257
+ service_data = service_objects
258
+ service_data = JSON.parse(service_data) if service_data.is_a?(String)
183
259
 
184
- nodes.each do |n|
185
- attrs = n.last.dig('attrs')
186
- state = attrs.dig('state') || 0
187
- downtime_depth = attrs.dig('downtime_depth') || 0
188
- acknowledgement = attrs.dig('acknowledgement') || 0
260
+ f = service_data.select { |t| t.dig('attrs','state') != 0 && t.dig('attrs','downtime_depth').zero? && t.dig('attrs','acknowledgement').zero? }
189
261
 
190
- if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
191
- problems += 1
192
- end
193
- end
194
- end
195
- problems
262
+ f.size
196
263
  end
197
264
 
198
265
  # return a list of services with problems
@@ -200,42 +267,45 @@ module Icinga2
200
267
  # @param [Integer] max_items numbers of list entries
201
268
  #
202
269
  # @example
203
- # @icinga.problem_services
270
+ # problems, problems_and_severity = @icinga.list_services_with_problems.values
271
+ #
272
+ # l = @icinga.list_services_with_problems
273
+ # problems_and_severity = l.dig(:services_with_problems_and_severity)
204
274
  #
205
275
  # @return [Hash]
276
+ # * Array (services_with_problems)
277
+ # * Hash (services_with_problems_and_severity)
206
278
  #
207
- def problem_services( max_items = 5 )
279
+ def list_services_with_problems( max_items = 5 )
208
280
 
209
- service_problems = {}
210
- service_problems_severity = {}
281
+ services_with_problems = {}
282
+ services_with_problems_and_severity = {}
211
283
 
212
284
  # only fetch the minimal attribute set required for severity calculation
213
285
  services_data = service_objects
214
-
215
- if( services_data.is_a?(String) )
216
- services_data = JSON.parse( services_data )
217
- end
218
-
219
- services_data = services_data.dig(:nodes)
286
+ services_data = JSON.parse( services_data ) if services_data.is_a?(String)
220
287
 
221
288
  unless( services_data.nil? )
222
289
 
223
- services_data.each do |_service,v|
290
+ services_data.each do |s,_v|
224
291
 
225
- name = v.dig('name')
226
- state = v.dig('attrs','state')
292
+ name = s.dig('name')
293
+ state = s.dig('attrs','state')
227
294
  next if state.zero?
228
295
 
229
- service_problems[name] = service_severity(v)
296
+ services_with_problems[name] = service_severity(s)
230
297
  end
231
298
 
232
- if( service_problems.count != 0 )
233
- service_problems.sort.reverse!
234
- service_problems = service_problems.keys[1..max_items].each { |k,_v| service_problems_severity[k] = service_problems[k] }
299
+ if( services_with_problems.count != 0 )
300
+ services_with_problems.sort.reverse!
301
+ services_with_problems = services_with_problems.keys[1..max_items].each { |k,_v| services_with_problems_and_severity[k] = services_with_problems[k] }
235
302
  end
236
303
  end
237
304
 
238
- [service_problems, service_problems_severity]
305
+ {
306
+ services_with_problems: services_with_problems,
307
+ services_with_problems_and_severity: services_with_problems_and_severity
308
+ }
239
309
  end
240
310
 
241
311
  # update host
@@ -268,23 +338,83 @@ module Icinga2
268
338
  hash
269
339
  end
270
340
 
341
+ # returns a counter of all services
342
+ #
343
+ # @example
344
+ # @icinga.cib_data
345
+ # @icinga.service_objects
346
+ # @icinga.services_all
347
+ #
348
+ # @return [Integer]
349
+ #
350
+ def services_all
351
+ @services_all
352
+ end
353
+
354
+ # returns data with service problems they be handled (acknowledged or in downtime)
355
+ #
356
+ # @example
357
+ # @icinga.cib_data
358
+ # @icinga.service_objects
359
+ # all, critical, warning, unknown = @icinga.service_problems_handled.values
360
+ #
361
+ # p = @icinga.service_problems_handled
362
+ # warning = p.dig(:warning)
363
+ #
364
+ # @return [Hash]
365
+ # * all
366
+ # * critical
367
+ # * warning
368
+ # * unknown
369
+ #
370
+ def service_problems_handled
371
+
372
+ problems_all = @services_handled.nil? ? 0 : @services_handled
373
+ problems_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
374
+ problems_warning = @services_handled_warning.nil? ? 0 : @services_handled_warning
375
+ problems_unknown = @services_handled_unknown.nil? ? 0 : @services_handled_unknown
376
+
377
+ {
378
+ all: problems_all.to_i,
379
+ critical: problems_critical.to_i,
380
+ warning: problems_warning.to_i,
381
+ unknown: problems_unknown.to_i
382
+ }
383
+
384
+ # [problems_all,problems_critical,problems_warning,problems_unknown]
385
+ end
386
+
387
+ protected
271
388
  # calculate a service severity
272
389
  #
273
390
  # stolen from Icinga Web 2
274
391
  # ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
275
392
  #
276
- # @param [Hash] service
393
+ # @param [Hash] params
394
+ # @option params [hash] attrs ()
395
+ # * state [Float]
396
+ # * acknowledgement [Float] (default: 0)
397
+ # * downtime_depth [Float] (default: 0)
277
398
  #
278
- # @private
399
+ # @api protected
279
400
  #
280
- # @return [Hash]
401
+ # @example
402
+ # service_severity( {'attrs' => { 'state' => 0.0, 'acknowledgement' => 0.0, 'downtime_depth' => 0.0 } } )
281
403
  #
282
- def service_severity(service)
404
+ # @return [Integer]
405
+ #
406
+ def service_severity( params )
407
+
408
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
409
+ raise ArgumentError.new('missing params') if( params.size.zero? )
410
+
411
+ state = params.dig('attrs','state')
412
+ acknowledgement = params.dig('attrs','acknowledgement') || 0
413
+ downtime_depth = params.dig('attrs','downtime_depth') || 0
283
414
 
284
- attrs = service.dig('attrs')
285
- state = attrs.dig('state')
286
- acknowledgement = attrs.dig('acknowledgement') || 0
287
- downtime_depth = attrs.dig('downtime_depth') || 0
415
+ raise ArgumentError.new('only Float for state are allowed') unless( state.is_a?(Float) )
416
+ raise ArgumentError.new('only Float for acknowledgement are allowed') unless( acknowledgement.is_a?(Float) )
417
+ raise ArgumentError.new('only Float for downtime_depth are allowed') unless( downtime_depth.is_a?(Float) )
288
418
 
289
419
  severity = 0
290
420
 
@@ -297,7 +427,7 @@ module Icinga2
297
427
  4
298
428
  end
299
429
 
300
- severity += 16 if object_has_been_checked?(service)
430
+ severity += 16 if object_has_been_checked?(params)
301
431
 
302
432
  unless state.zero?
303
433
 
@@ -311,7 +441,7 @@ module Icinga2
311
441
  end
312
442
 
313
443
  # requires joins
314
- host_attrs = service.dig('joins','host')
444
+ host_attrs = params.dig('joins','host')
315
445
  host_state = host_attrs.dig('state')
316
446
  host_acknowledgement = host_attrs.dig('acknowledgement')
317
447
  host_downtime_depth = host_attrs.dig('downtime_depth')