icinga2 0.9.0.1 → 0.9.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,8 +17,10 @@ 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? )
20
+ raise ArgumentError.new(format('wrong type. \'host\' must be an String, given \'%s\'', host.class.to_s)) unless( host.is_a?(String) )
21
+ raise ArgumentError.new('missing \'host\'') if( host.size.zero? )
22
+
23
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_host?( host ) == false )
22
24
 
23
25
  host_notification( name: host, enable_notifications: true )
24
26
  end
@@ -34,8 +36,10 @@ module Icinga2
34
36
  #
35
37
  def disable_host_notification( host )
36
38
 
37
- raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
38
- raise ArgumentError.new('missing host') if( host.size.zero? )
39
+ raise ArgumentError.new(format('wrong type. \'host\' must be an String, given \'%s\'', host.class.to_s)) unless( host.is_a?(String) )
40
+ raise ArgumentError.new('missing \'host\'') if( host.size.zero? )
41
+
42
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_host?( host ) == false )
39
43
 
40
44
  host_notification( name: host, enable_notifications: false )
41
45
  end
@@ -51,8 +55,10 @@ module Icinga2
51
55
  #
52
56
  def enable_service_notification( host )
53
57
 
54
- raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
55
- raise ArgumentError.new('missing host') if( host.size.zero? )
58
+ raise ArgumentError.new(format('wrong type. \'host\' must be an String, given \'%s\'', host.class.to_s)) unless( host.is_a?(String) )
59
+ raise ArgumentError.new('missing \'host\'') if( host.size.zero? )
60
+
61
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_host?( host ) == false )
56
62
 
57
63
  service_notification( name: host, enable_notifications: true )
58
64
  end
@@ -68,8 +74,10 @@ module Icinga2
68
74
  #
69
75
  def disable_service_notification( host )
70
76
 
71
- raise ArgumentError.new('only String are allowed') unless( host.is_a?(String) )
72
- raise ArgumentError.new('missing host') if( host.size.zero? )
77
+ raise ArgumentError.new(format('wrong type. \'host\' must be an String, given \'%s\'', host.class.to_s)) unless( host.is_a?(String) )
78
+ raise ArgumentError.new('missing \'host\'') if( host.size.zero? )
79
+
80
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_host?( host ) == false )
73
81
 
74
82
  service_notification( name: host, enable_notifications: false )
75
83
  end
@@ -87,16 +95,15 @@ module Icinga2
87
95
  #
88
96
  def enable_hostgroup_notification( params )
89
97
 
90
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
91
- raise ArgumentError.new('missing params') if( params.size.zero? )
98
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
99
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
92
100
 
93
- host = params.dig(:host)
94
101
  host_group = params.dig(:host_group)
95
-
96
- raise ArgumentError.new('Missing host') if( host.nil? )
97
102
  raise ArgumentError.new('Missing host_group') if( host_group.nil? )
98
103
 
99
- hostgroup_notification( host: host, host_group: host_group, enable_notifications: true )
104
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( host_group ) == false )
105
+
106
+ hostgroup_notification( host_group: host_group, enable_notifications: true )
100
107
  end
101
108
 
102
109
  # disable hostgroup notifications
@@ -112,16 +119,15 @@ module Icinga2
112
119
  #
113
120
  def disable_hostgroup_notification( params )
114
121
 
115
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
116
- raise ArgumentError.new('missing params') if( params.size.zero? )
122
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
123
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
117
124
 
118
- host = params.dig(:host)
119
125
  host_group = params.dig(:host_group)
120
-
121
- raise ArgumentError.new('Missing host') if( host.nil? )
122
126
  raise ArgumentError.new('Missing host_group') if( host_group.nil? )
123
127
 
124
- hostgroup_notification( host: host, host_group: host_group, enable_notifications: false )
128
+ return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( host_group ) == false )
129
+
130
+ hostgroup_notification( host_group: host_group, enable_notifications: true )
125
131
  end
126
132
 
127
133
  # return all notifications
@@ -131,15 +137,11 @@ module Icinga2
131
137
  #
132
138
  def notifications
133
139
 
134
- data = Network.api_data(
140
+ api_data(
135
141
  url: format( '%s/objects/notifications', @icinga_api_url_base ),
136
142
  headers: @headers,
137
143
  options: @options
138
144
  )
139
-
140
- return data.dig('results') if( data.dig(:status).nil? )
141
-
142
- nil
143
145
  end
144
146
 
145
147
  protected
@@ -163,7 +165,7 @@ module Icinga2
163
165
  }
164
166
  }
165
167
 
166
- Network.post(
168
+ post(
167
169
  url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
168
170
  headers: @headers,
169
171
  options: @options,
@@ -192,7 +194,7 @@ module Icinga2
192
194
  }
193
195
  }
194
196
 
195
- Network.post(
197
+ post(
196
198
  url: format( '%s/objects/services', @icinga_api_url_base ),
197
199
  headers: @headers,
198
200
  options: @options,
@@ -221,7 +223,7 @@ module Icinga2
221
223
  }
222
224
  }
223
225
 
224
- Network.post(
226
+ post(
225
227
  url: format( '%s/objects/services', @icinga_api_url_base ),
226
228
  headers: @headers,
227
229
  options: @options,
@@ -11,6 +11,9 @@ module Icinga2
11
11
  # @param [Hash] params
12
12
  # @option params [String] :service_group servicegroup 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_servicegroup(service_group: 'foo', display_name: 'FOO')
@@ -19,18 +22,38 @@ module Icinga2
19
22
  #
20
23
  def add_servicegroup( params )
21
24
 
22
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
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
  service_group = params.dig(:service_group)
26
- display_name = params.dig(:display_name)
27
-
28
- raise ArgumentError.new('Missing service_group') if( service_group.nil? )
29
- raise ArgumentError.new('Missing display_name') if( display_name.nil? )
30
-
31
- payload = { 'attrs' => { 'display_name' => display_name } }
32
-
33
- Network.put(
29
+ display_name = params.dig(:display_name)
30
+ notes = params.dig(:notes)
31
+ notes_url = params.dig(:notes_url)
32
+ action_url = params.dig(:action_url)
33
+
34
+ # ignore = params.dig(:ignore)
35
+ # assgin = params.dig(:assign)
36
+
37
+ raise ArgumentError.new('Missing \'service_group\'') if( service_group.nil? )
38
+ raise ArgumentError.new('Missing \'display_name\'') if( display_name.nil? )
39
+
40
+ payload = {
41
+ 'attrs' => {
42
+ 'display_name' => display_name,
43
+ 'notes' => notes,
44
+ 'notes_url' => notes_url,
45
+ 'action_url' => action_url
46
+ }
47
+ }
48
+
49
+ # payload['attrs']['assign'] ||= format('assgin where %s', assign) unless(assign.nil?)
50
+ # payload['attrs']['ignore'] ||= format('ignore where %s', assign) unless(assign.nil?)
51
+
52
+ # remove all empty attrs
53
+ payload.reject!{ |_k, v| v.nil? }
54
+ payload['attrs'].reject!{ |_k, v| v.nil? }
55
+
56
+ put(
34
57
  url: format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ),
35
58
  headers: @headers,
36
59
  options: @options,
@@ -50,14 +73,14 @@ module Icinga2
50
73
  #
51
74
  def delete_servicegroup( params )
52
75
 
53
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
54
- raise ArgumentError.new('missing params') if( params.size.zero? )
76
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
77
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
55
78
 
56
79
  service_group = params.dig(:service_group)
57
80
 
58
- raise ArgumentError.new('Missing service_group') if( service_group.nil? )
81
+ raise ArgumentError.new('Missing \'service_group\'') if( service_group.nil? )
59
82
 
60
- Network.delete(
83
+ delete(
61
84
  url: format( '%s/objects/servicegroups/%s?cascade=1', @icinga_api_url_base, service_group ),
62
85
  headers: @headers,
63
86
  options: @options
@@ -88,15 +111,11 @@ module Icinga2
88
111
  format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group )
89
112
  end
90
113
 
91
- data = Network.api_data(
114
+ api_data(
92
115
  url: url,
93
116
  headers: @headers,
94
117
  options: @options
95
118
  )
96
-
97
- return data.dig('results') if( data.dig(:status).nil? )
98
-
99
- nil
100
119
  end
101
120
 
102
121
  # checks if the servicegroup exists
@@ -110,16 +129,16 @@ module Icinga2
110
129
  #
111
130
  def exists_servicegroup?( service_group )
112
131
 
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
-
132
+ raise ArgumentError.new(format('wrong type. \'service_group\' must be an String, given \'%s\'', service_group.class.to_s)) unless( service_group.is_a?(String) )
133
+ raise ArgumentError.new('Missing \'service_group\'') if( service_group.size.zero? )
116
134
 
117
135
  result = servicegroups( service_group: service_group )
118
136
  result = JSON.parse( result ) if result.is_a?( String )
137
+ result = result.first if( result.is_a?(Array) )
119
138
 
120
- return true if !result.nil? && result.is_a?(Array)
139
+ return false if( result.is_a?(Hash) && result.dig('code') == 404 )
121
140
 
122
- false
141
+ true
123
142
  end
124
143
 
125
144
  end
@@ -1,4 +1,3 @@
1
-
2
1
  # frozen_string_literal: true
3
2
 
4
3
  module Icinga2
@@ -6,29 +5,25 @@ module Icinga2
6
5
  # namespace for service handling
7
6
  module Services
8
7
 
9
- # add services
8
+ # add service
10
9
  #
11
10
  # @param [Hash] params
12
- # @option params [String] :host
13
- # @option params [String] :service_name
11
+ # @option params [String] :name
12
+ # @option params [String] :host_name
14
13
  # @option params [Array] :templates
15
14
  # @option params [Hash] :vars
16
15
  #
17
16
  # @example
18
- # @icinga.add_services(
19
- # host: 'icinga2',
20
- # service_name: 'http2',
17
+ # @icinga.add_service(
18
+ # name: 'http2',
19
+ # host_name: 'icinga2',
20
+ # check_command: 'http',
21
+ # check_interval: 10,
22
+ # retry_interval: 30,
21
23
  # vars: {
22
- # attrs: {
23
- # check_command: 'http',
24
- # check_interval: 10,
25
- # retry_interval: 30,
26
- # vars: {
27
- # http_address: '127.0.0.1',
28
- # http_url: '/access/index',
29
- # http_port: 80
30
- # }
31
- # }
24
+ # http_address: '127.0.0.1',
25
+ # http_url: '/access/index',
26
+ # http_port: 80
32
27
  # }
33
28
  # )
34
29
  #
@@ -36,63 +31,141 @@ module Icinga2
36
31
  # * status
37
32
  # * message
38
33
  #
39
- def add_services( params = {} )
40
-
41
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
34
+ def add_service( params = {} )
42
35
 
43
- host_name = params.dig(:host)
44
- service_name = params.dig(:service_name)
45
- templates = params.dig(:templates) || ['generic-service']
46
- vars = params.dig(:vars)
47
-
48
- raise ArgumentError.new('Missing host') if( host_name.nil? )
49
- raise ArgumentError.new('Missing service_name') if( service_name.nil? )
50
- raise ArgumentError.new('only Array for templates are allowed') unless( templates.is_a?(Array) )
51
- raise ArgumentError.new('Missing vars') if( vars.nil? )
52
- raise ArgumentError.new('only Hash for vars are allowed') unless( vars.is_a?(Hash) )
36
+ raise ArgumentError.new(format('wrong type. params must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
37
+ raise ArgumentError.new('missing params') if( params.size.zero? )
53
38
 
54
- # validate
55
- # needed_values = %w[check_command check_interval retry_interval]
39
+ name = params.dig(:name)
40
+ host_name = params.dig(:host_name)
41
+ display_name = params.dig(:display_name)
42
+ groups = params.dig(:groups)
43
+ check_command = params.dig(:check_command)
44
+ max_check_attempts = params.dig(:max_check_attempts)
45
+ check_period = params.dig(:check_period)
46
+ check_timeout = params.dig(:check_timeout)
47
+ check_interval = params.dig(:check_interval)
48
+ retry_interval = params.dig(:retry_interval)
49
+ enable_notifications = params.dig(:enable_notifications)
50
+ enable_active_checks = params.dig(:enable_active_checks)
51
+ enable_passive_checks = params.dig(:enable_passive_checks)
52
+ enable_event_handler = params.dig(:enable_event_handler)
53
+ enable_flapping = params.dig(:enable_flapping)
54
+ flapping_threshold = params.dig(:flapping_threshold)
55
+ enable_perfdata = params.dig(:enable_perfdata)
56
+ event_command = params.dig(:event_command)
57
+ volatile = params.dig(:volatile)
58
+ zone = params.dig(:zone)
59
+ command_endpoint = params.dig(:command_endpoint)
60
+ notes = params.dig(:notes)
61
+ notes_url = params.dig(:notes_url)
62
+ action_url = params.dig(:action_url)
63
+ icon_image = params.dig(:icon_image)
64
+ icon_image_alt = params.dig(:icon_image_alt)
65
+ templates = params.dig(:templates) || ['generic-service']
66
+ vars = params.dig(:vars) || {}
67
+
68
+ raise ArgumentError.new('missing name') if( name.nil? )
69
+
70
+ %w[display_name
71
+ host_name
72
+ check_command
73
+ check_period
74
+ event_command
75
+ zone
76
+ name
77
+ command_endpoint
78
+ notes
79
+ notes_url
80
+ action_url
81
+ icon_image
82
+ icon_image_alt
83
+ ].each do |attr|
84
+ v = eval(attr)
85
+ 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? )
86
+ end
56
87
 
57
- attrs = vars.dig(:attrs)
88
+ %w[max_check_attempts
89
+ flapping_threshold
90
+ check_timeout
91
+ check_interval
92
+ retry_interval].each do |attr|
93
+ v = eval(attr)
94
+ 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? )
95
+ end
58
96
 
59
- validate_check_command = attrs.select { |k,_v| k == 'check_command'.to_sym }.size
60
- validate_check_interval = attrs.select { |k,_v| k == 'check_interval'.to_sym }.size
61
- validate_retry_interval = attrs.select { |k,_v| k == 'retry_interval'.to_sym }.size
97
+ %w[enable_notifications
98
+ enable_active_checks
99
+ enable_passive_checks
100
+ enable_event_handler
101
+ enable_flapping
102
+ enable_perfdata
103
+ volatile].each do |attr|
104
+ v = eval(attr)
105
+ 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? )
106
+ end
62
107
 
63
- raise 'Error in attrs, expected \'check_command\' but was not found' if( validate_check_command.zero? )
64
- raise 'Error in attrs, expected \'check_interval\' but was not found' if( validate_check_interval.zero? )
65
- raise 'Error in attrs, expected \'retry_interval\' but was not found' if( validate_retry_interval.zero? )
108
+ %w[groups templates].each do |attr|
109
+ v = eval(attr)
110
+ 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? )
111
+ end
66
112
 
67
- payload = ''
113
+ raise ArgumentError.new(format('wrong type. \'vars\' must be an Hash, given \'%s\'', v.class.to_s)) unless( vars.is_a?(Hash) )
114
+
115
+ payload = {
116
+ 'templates' => templates,
117
+ 'attrs' => {
118
+ 'display_name' => display_name,
119
+ 'groups' => groups,
120
+ 'check_command' => check_command,
121
+ 'max_check_attempts' => max_check_attempts,
122
+ 'check_period' => check_period,
123
+ 'check_timeout' => check_timeout,
124
+ 'check_interval' => check_interval,
125
+ 'retry_interval' => retry_interval,
126
+ 'enable_notifications' => enable_notifications,
127
+ 'enable_active_checks' => enable_active_checks,
128
+ 'enable_passive_checks' => enable_passive_checks,
129
+ 'enable_event_handler' => enable_event_handler,
130
+ 'enable_flapping' => enable_flapping,
131
+ 'flapping_threshold' => flapping_threshold,
132
+ 'zone' => zone,
133
+ 'enable_perfdata' => enable_perfdata,
134
+ 'event_command' => event_command,
135
+ 'volatile' => volatile,
136
+ 'command_endpoint' => command_endpoint,
137
+ 'notes' => notes,
138
+ 'notes_url' => notes_url,
139
+ 'action_url' => action_url,
140
+ 'icon_image' => icon_image,
141
+ 'icon_image_alt' => icon_image_alt,
142
+ }
143
+ }
68
144
 
69
- vars.each do |_s,v|
145
+ payload['attrs']['vars'] = vars unless vars.empty?
70
146
 
71
- payload = {
72
- 'templates' => templates,
73
- 'attrs' => update_host( v, host_name )
74
- }
75
- end
147
+ # clear undefined settings
148
+ payload.reject!{ |_k, v| v.nil? }
149
+ payload['attrs'].reject!{ |_k, v| v.nil? }
76
150
 
77
- Network.put(
78
- url: format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, service_name ),
151
+ put(
152
+ url: format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, name ),
79
153
  headers: @headers,
80
154
  options: @options,
81
155
  payload: payload
82
156
  )
83
-
84
157
  end
85
158
 
86
159
  # delete a service
87
160
  #
88
161
  # @param [Hash] params
89
- # @option params [String] :host host name for the service
90
- # @option params [String] :service_name
162
+ # @option params [String] :host_name host name for the service
163
+ # @option params [String] :name
91
164
  # @option params [Bool] :cascade (false) delete service also when other objects depend on it
92
165
  #
93
166
  # @example
94
- # @icinga.delete_service(host: 'foo', service_name: 'http2')
95
- # @icinga.delete_service(host: 'foo', service_name: 'http2', cascade: true)
167
+ # @icinga.delete_service(host_name: 'foo', name: 'http2')
168
+ # @icinga.delete_service(host_name: 'foo', name: 'http2', cascade: true)
96
169
  #
97
170
  # @return [Hash] result
98
171
  #
@@ -101,20 +174,20 @@ module Icinga2
101
174
  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
102
175
  raise ArgumentError.new('missing params') if( params.size.zero? )
103
176
 
104
- host_name = params.dig(:host)
105
- service_name = params.dig(:service_name)
177
+ host_name = params.dig(:host_name)
178
+ name = params.dig(:name)
106
179
  cascade = params.dig(:cascade)
107
180
 
108
181
  raise ArgumentError.new('Missing host') if( host_name.nil? )
109
- raise ArgumentError.new('Missing service_name') if( service_name.nil? )
182
+ raise ArgumentError.new('Missing service name') if( name.nil? )
110
183
 
111
184
  if( ! cascade.nil? && ( ! cascade.is_a?(TrueClass) && ! cascade.is_a?(FalseClass) ) )
112
185
  raise ArgumentError.new('cascade can only be true or false')
113
186
  end
114
187
 
115
- url = format( '%s/objects/services/%s!%s%s', @icinga_api_url_base, host_name, service_name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
188
+ url = format( '%s/objects/services/%s!%s%s', @icinga_api_url_base, host_name, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
116
189
 
117
- Network.delete(
190
+ delete(
118
191
  url: url,
119
192
  headers: @headers,
120
193
  options: @options
@@ -125,12 +198,13 @@ module Icinga2
125
198
  # modify an service
126
199
  #
127
200
  # @param [Hash] params
128
- # @option params [String] :service_name
201
+ # @option params [String] :name
202
+ # @option params [Array] :templates
129
203
  # @option params [Hash] :vars
130
204
  #
131
205
  # @example
132
206
  # @icinga.modify_service(
133
- # service_name: 'http2',
207
+ # name: 'http2',
134
208
  # vars: {
135
209
  # attrs: {
136
210
  # check_interval: 60,
@@ -154,16 +228,16 @@ module Icinga2
154
228
 
155
229
  templates = params.dig(:templates) || ['generic-service']
156
230
  vars = params.dig(:vars)
157
- service_name = params.dig(:service_name)
231
+ name = params.dig(:name)
158
232
 
159
- raise ArgumentError.new('Missing service_name') if( service_name.nil? )
233
+ raise ArgumentError.new('Missing service name') if( name.nil? )
160
234
  raise ArgumentError.new('only Array for templates are allowed') unless( templates.is_a?(Array) )
161
235
  raise ArgumentError.new('Missing vars') if( vars.nil? )
162
236
  raise ArgumentError.new('only Hash for vars are allowed') unless( vars.is_a?(Hash) )
163
237
 
164
238
  payload = {}
165
239
 
166
- vars.each do |_s,v|
240
+ vars.each_value do |v|
167
241
 
168
242
  payload = {
169
243
  'templates' => templates,
@@ -171,9 +245,9 @@ module Icinga2
171
245
  }
172
246
  end
173
247
 
174
- payload['filter'] = format( 'service.name=="%s"', service_name )
248
+ payload['filter'] = format( 'service.name=="%s"', name )
175
249
 
176
- Network.post(
250
+ post(
177
251
  url: format( '%s/objects/services', @icinga_api_url_base ),
178
252
  headers: @headers,
179
253
  options: @options,
@@ -198,29 +272,25 @@ module Icinga2
198
272
  payload['attrs'] = attrs unless attrs.nil?
199
273
  payload['filter'] = filter unless filter.nil?
200
274
 
201
- data = Network.api_data(
275
+ api_data(
202
276
  url: format( '%s/objects/services', @icinga_api_url_base ),
203
277
  headers: @headers,
204
278
  options: @options,
205
279
  payload: payload
206
280
  )
207
-
208
- status = data.dig(:status)
209
-
210
- data.dig('results') if( status.nil? )
211
281
  end
212
282
 
213
283
  # return services
214
284
  #
215
285
  # @param [Hash] params
216
- # @option params [String] :host
217
- # @option params [String] :service
286
+ # @option params [String] :host_name
287
+ # @option params [String] :name
218
288
  #
219
289
  # @example to get all services
220
290
  # @icinga.services
221
291
  #
222
292
  # @example to get one service for host
223
- # @icinga.services( host: 'icinga2', service: 'ping4' )
293
+ # @icinga.services( host_name: 'icinga2', name: 'ping4' )
224
294
  #
225
295
  # @return [Hash]
226
296
  #
@@ -228,35 +298,31 @@ module Icinga2
228
298
 
229
299
  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
230
300
 
231
- host_name = params.dig(:host)
232
- service = params.dig(:service)
301
+ host_name = params.dig(:host_name)
302
+ name = params.dig(:name)
233
303
 
234
304
  url =
235
- if( service.nil? )
305
+ if( name.nil? )
236
306
  format( '%s/objects/services/%s', @icinga_api_url_base, host_name )
237
307
  else
238
- format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, service )
308
+ format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, name )
239
309
  end
240
310
 
241
- data = Network.api_data(
311
+ api_data(
242
312
  url: url,
243
313
  headers: @headers,
244
314
  options: @options
245
315
  )
246
-
247
- return data.dig('results') if( data.dig(:status).nil? )
248
-
249
- nil
250
316
  end
251
317
 
252
318
  # returns true if the service exists
253
319
  #
254
320
  # @param [Hash] params
255
- # @option params [String] :host
256
- # @option params [String] :service
321
+ # @option params [String] :host_name
322
+ # @option params [String] :name
257
323
  #
258
324
  # @example
259
- # @icinga.exists_service?(host: 'icinga2', service: 'users')
325
+ # @icinga.exists_service?(host_name: 'icinga2', name: 'users')
260
326
  #
261
327
  # @return [Bool]
262
328
  #
@@ -265,18 +331,19 @@ module Icinga2
265
331
  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
266
332
  raise ArgumentError.new('missing params') if( params.size.zero? )
267
333
 
268
- host = params.dig(:host)
269
- service = params.dig(:service)
334
+ host = params.dig(:host_name)
335
+ service = params.dig(:name)
270
336
 
271
337
  raise ArgumentError.new('Missing host') if( host.nil? )
272
338
  raise ArgumentError.new('Missing service') if( service.nil? )
273
339
 
274
- result = services( host: host, service: service )
340
+ result = services( host_name: host, name: service )
275
341
  result = JSON.parse( result ) if result.is_a?( String )
342
+ result = result.first if( result.is_a?(Array) )
276
343
 
277
- return true if !result.nil? && result.is_a?(Array)
344
+ return false if( result.is_a?(Hash) && result.dig('code') == 404 )
278
345
 
279
- false
346
+ true
280
347
  end
281
348
 
282
349
  # returns service objects
@@ -313,42 +380,32 @@ module Icinga2
313
380
  payload['filter'] = filter unless filter.nil?
314
381
  payload['joins'] = joins unless joins.nil?
315
382
 
316
- data = Network.api_data(
383
+ data = api_data(
317
384
  url: format( '%s/objects/services', @icinga_api_url_base ),
318
385
  headers: @headers,
319
386
  options: @options,
320
387
  payload: payload
321
388
  )
322
389
 
323
- status = data.dig(:status)
324
-
325
- if( status.nil? )
326
-
327
- results = data.dig('results')
390
+ @last_service_objects_called = Time.now.to_i
328
391
 
329
- unless( results.nil? )
330
-
331
- all_services = results.clone
332
-
333
- unless( all_services.nil? )
334
-
335
- @services_all = all_services.size
336
- @services_problems = count_problems(results)
337
- @services_handled_warning = count_problems(results, Icinga2::SERVICE_STATE_WARNING)
338
- @services_handled_critical = count_problems(results, Icinga2::SERVICE_STATE_CRITICAL)
339
- @services_handled_unknown = count_problems(results, Icinga2::SERVICE_STATE_UNKNOWN)
340
- end
392
+ if( !data.nil? && data.is_a?(Array) )
393
+ all_services = data.clone
394
+ unless( all_services.nil? )
395
+ @services_all = all_services.size
396
+ @services_problems = count_problems(all_services)
397
+ @services_handled_warning = count_problems(all_services, Icinga2::SERVICE_STATE_WARNING)
398
+ @services_handled_critical = count_problems(all_services, Icinga2::SERVICE_STATE_CRITICAL)
399
+ @services_handled_unknown = count_problems(all_services, Icinga2::SERVICE_STATE_UNKNOWN)
341
400
  end
342
401
  end
343
402
 
344
- results
403
+ data
345
404
  end
346
405
 
347
406
  # returns adjusted service state
348
407
  #
349
408
  # @example
350
- # @icinga.cib_data
351
- # @icinga.service_objects
352
409
  # warning, critical, unknown = @icinga.services_adjusted.values
353
410
  #
354
411
  # s = @icinga.services_adjusted
@@ -361,6 +418,12 @@ module Icinga2
361
418
  #
362
419
  def services_adjusted
363
420
 
421
+ puts 'function services_adjusted is obsolete'
422
+ puts 'Please use service_problems()'
423
+
424
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
425
+ service_objects if((Time.now.to_i - @last_service_objects_called).to_i > @last_call_timeout)
426
+
364
427
  service_warning = @services_warning.nil? ? 0 : @services_warning
365
428
  service_critical = @services_critical.nil? ? 0 : @services_critical
366
429
  service_unknown = @services_unknown.nil? ? 0 : @services_unknown
@@ -392,6 +455,8 @@ module Icinga2
392
455
  service_data = service_objects
393
456
  service_data = JSON.parse(service_data) if service_data.is_a?(String)
394
457
 
458
+ return 0 if( service_data.nil? )
459
+
395
460
  f = service_data.select { |t| t.dig('attrs','state') != 0 && t.dig('attrs','downtime_depth').zero? && t.dig('attrs','acknowledgement').zero? }
396
461
 
397
462
  f.size
@@ -422,7 +487,7 @@ module Icinga2
422
487
 
423
488
  unless( services_data.nil? )
424
489
 
425
- services_data.each do |s,_v|
490
+ services_data.each do |s|
426
491
 
427
492
  name = s.dig('name')
428
493
  state = s.dig('attrs','state')
@@ -433,7 +498,7 @@ module Icinga2
433
498
 
434
499
  if( services_with_problems.count != 0 )
435
500
  services_with_problems.sort.reverse!
436
- services_with_problems = services_with_problems.keys[1..max_items].each { |k,_v| services_with_problems_and_severity[k] = services_with_problems[k] }
501
+ services_with_problems = services_with_problems.keys[1..max_items].each { |k| services_with_problems_and_severity[k] = services_with_problems[k] }
437
502
  end
438
503
  end
439
504
 
@@ -446,21 +511,22 @@ module Icinga2
446
511
  # returns a counter of all services
447
512
  #
448
513
  # @example
449
- # @icinga.cib_data
450
- # @icinga.service_objects
451
514
  # @icinga.services_all
452
515
  #
453
516
  # @return [Integer]
454
517
  #
455
518
  def services_all
519
+
520
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
521
+ service_objects if((Time.now.to_i - @last_service_objects_called).to_i > @last_call_timeout)
522
+
456
523
  @services_all
457
524
  end
458
525
 
459
526
  # returns data with service problems
460
527
  #
461
528
  # @example
462
- # @icinga.cib_data
463
- # all, warning, critical, unknown, pending, in_downtime, acknowledged = @icinga.service_problems.values
529
+ # all, warning, critical, unknown, pending, in_downtime, acknowledged, adjusted_warning, adjusted_critical, adjusted_unknown = @icinga.service_problems.values
464
530
  #
465
531
  # p = @icinga.service_problems
466
532
  # warning = p.dig(:warning)
@@ -476,13 +542,25 @@ module Icinga2
476
542
  #
477
543
  def service_problems
478
544
 
479
- services_ok = @services_ok.nil? ? 0 : @services_ok
480
- services_warning = @services_warning.nil? ? 0 : @services_warning
481
- services_critical = @services_critical.nil? ? 0 : @services_critical
482
- services_unknown = @services_unknown.nil? ? 0 : @services_unknown
483
- services_pending = @services_pending.nil? ? 0 : @services_pending
484
- services_in_downtime = @services_in_downtime.nil? ? 0 : @services_in_downtime
485
- services_acknowledged = @services_acknowledged.nil? ? 0 : @services_acknowledged
545
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
546
+ service_objects if((Time.now.to_i - @last_service_objects_called).to_i > @last_call_timeout)
547
+
548
+ services_ok = @services_ok.nil? ? 0 : @services_ok
549
+ services_warning = @services_warning.nil? ? 0 : @services_warning
550
+ services_critical = @services_critical.nil? ? 0 : @services_critical
551
+ services_unknown = @services_unknown.nil? ? 0 : @services_unknown
552
+ services_pending = @services_pending.nil? ? 0 : @services_pending
553
+ services_in_downtime = @services_in_downtime.nil? ? 0 : @services_in_downtime
554
+ services_acknowledged = @services_acknowledged.nil? ? 0 : @services_acknowledged
555
+ services_handled_all = @services_handled.nil? ? 0 : @services_handled
556
+ services_handled_warning = @services_handled_warning.nil? ? 0 : @services_handled_warning
557
+ services_handled_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
558
+ services_handled_unknown = @services_handled_unknown.nil? ? 0 : @services_handled_unknown
559
+
560
+ # calculate service problems adjusted by handled problems
561
+ services_adjusted_warning = services_warning - services_handled_warning
562
+ services_adjusted_critical = services_critical - services_handled_critical
563
+ services_adjusted_unknown = services_unknown - services_handled_unknown
486
564
 
487
565
  {
488
566
  ok: services_ok.to_i,
@@ -491,14 +569,20 @@ module Icinga2
491
569
  unknown: services_unknown.to_i,
492
570
  pending: services_pending.to_i,
493
571
  in_downtime: services_in_downtime.to_i,
494
- acknowledged: services_acknowledged.to_i
572
+ acknowledged: services_acknowledged.to_i,
573
+ adjusted_warning: services_adjusted_warning.to_i,
574
+ adjusted_critical: services_adjusted_critical.to_i,
575
+ adjusted_unknown: services_adjusted_unknown.to_i,
576
+ handled_all: services_handled_all.to_i,
577
+ handled_warning: services_handled_warning.to_i,
578
+ handled_critical: services_handled_critical.to_i,
579
+ handled_unknown: services_handled_unknown.to_i
495
580
  }
496
581
  end
497
582
 
498
583
  # returns data with service problems they be handled (acknowledged or in downtime)
499
584
  #
500
585
  # @example
501
- # @icinga.cib_data
502
586
  # @icinga.service_objects
503
587
  # all, warning, critical, unknown = @icinga.service_problems_handled.values
504
588
  #
@@ -513,6 +597,11 @@ module Icinga2
513
597
  #
514
598
  def service_problems_handled
515
599
 
600
+ puts 'function service_problems_handled is obsolete'
601
+ puts 'Please use service_problems()'
602
+
603
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
604
+
516
605
  problems_all = @services_handled.nil? ? 0 : @services_handled
517
606
  problems_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
518
607
  problems_warning = @services_handled_warning.nil? ? 0 : @services_handled_warning
@@ -586,6 +675,9 @@ module Icinga2
586
675
 
587
676
  # requires joins
588
677
  host_attrs = params.dig('joins','host')
678
+
679
+ return 0 if( host_attrs.nil? )
680
+
589
681
  host_state = host_attrs.dig('state')
590
682
  host_acknowledgement = host_attrs.dig('acknowledgement')
591
683
  host_downtime_depth = host_attrs.dig('downtime_depth')
@@ -612,10 +704,6 @@ module Icinga2
612
704
  # @param [Hash] hash
613
705
  # @param [String] host
614
706
  #
615
- # @todo
616
- # this function are not operable
617
- # need help, time or beer
618
- #
619
707
  # @api protected
620
708
  #
621
709
  # @return [Hash]