icinga2 0.6.1 → 0.6.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.
@@ -3,14 +3,17 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespace for service handling
9
7
  module Services
10
8
 
9
+ # add services
11
10
  #
11
+ # @param [String] host
12
+ # @param [Hash] services
12
13
  #
13
14
  #
15
+ # @return [Hash]
16
+ #
14
17
  def add_services( host, services = {} )
15
18
 
16
19
  services.each do |s,v|
@@ -25,7 +28,7 @@ module Icinga2
25
28
 
26
29
  logger.debug( JSON.pretty_generate( payload ) )
27
30
 
28
- result = Network.put( host: host,
31
+ Network.put( host: host,
29
32
  url: format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, host, s ),
30
33
  headers: @headers,
31
34
  options: @options,
@@ -37,8 +40,15 @@ module Icinga2
37
40
 
38
41
  end
39
42
 
43
+ # return all unhandled services
44
+ #
45
+ # @param [Hash] params
40
46
  #
47
+ # @todo
48
+ # this function are not operable
49
+ # need help, time or beer
41
50
  #
51
+ # @return [Nil]
42
52
  #
43
53
  def unhandled_services( params = {} )
44
54
 
@@ -48,31 +58,49 @@ module Icinga2
48
58
 
49
59
  end
50
60
 
61
+ # return services
62
+ #
63
+ # @param [Hash] params
64
+ # @option params [String] :host
65
+ # @option params [String] :service
51
66
  #
67
+ # @example to get all services
68
+ # @icinga.services
52
69
  #
70
+ # @example to get one service for host
71
+ # @icinga.services( host: 'icinga2', service: 'ping4' )
72
+ #
73
+ # @return [Hash]
53
74
  #
54
75
  def services( params = {} )
55
76
 
56
77
  name = params.dig(:host)
57
78
  service = params.dig(:service)
58
79
 
59
- url = if( service.nil? )
80
+ url =
81
+ if( service.nil? )
60
82
  format( '%s/v1/objects/services/%s', @icinga_api_url_base, name )
61
83
  else
62
84
  format( '%s/v1/objects/services/%s!%s', @icinga_api_url_base, name, service )
63
- end
85
+ end
64
86
 
65
- result = Network.get( host: name,
87
+ Network.get( host: name,
66
88
  url: url,
67
89
  headers: @headers,
68
90
  options: @options )
69
91
 
70
- JSON.pretty_generate( result )
71
-
72
92
  end
73
93
 
94
+ # returns true if the service exists
95
+ #
96
+ # @param [Hash] params
97
+ # @option params [String] :host
98
+ # @option params [String] :service
74
99
  #
100
+ # @example
101
+ # @icinga.exists_service?(host: 'icinga2', service: 'users' )
75
102
  #
103
+ # @return [Bool]
76
104
  #
77
105
  def exists_service?( params = {} )
78
106
 
@@ -80,7 +108,6 @@ module Icinga2
80
108
  service = params.dig(:service)
81
109
 
82
110
  if( host.nil? )
83
-
84
111
  return {
85
112
  status: 404,
86
113
  message: 'missing host name'
@@ -88,19 +115,27 @@ module Icinga2
88
115
  end
89
116
 
90
117
  result = services( host: host, service: service )
91
-
92
118
  result = JSON.parse( result ) if result.is_a?( String )
93
-
94
- status = result.dig('status')
119
+ status = result.dig(:status)
95
120
 
96
121
  return true if !status.nil? && status == 200
97
-
98
122
  false
99
-
100
123
  end
101
124
 
125
+ # returns service objects
126
+ #
127
+ # @param [Hash] params
128
+ # @option params [Array] :attrs (['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check'])
129
+ # @option params [Array] :filter ([])
130
+ # @option params [Array] :joins (['host.name','host.state','host.acknowledgement','host.downtime_depth','host.last_check'])
131
+ #
132
+ # @example with default attrs and joins
133
+ # @icinga.service_objects
102
134
  #
135
+ # @example
136
+ # @icinga.service_objects(attrs: ['name', 'state'], joins: ['host.name','host.state'])
103
137
  #
138
+ # @return [Hash]
104
139
  #
105
140
  def service_objects( params = {} )
106
141
 
@@ -110,31 +145,31 @@ module Icinga2
110
145
  payload = {}
111
146
 
112
147
  if( attrs.nil? )
113
- attrs = %w[name state acknowledgement downtime_depth last_check]
148
+ attrs = ['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check']
114
149
  end
115
150
 
116
151
  if( joins.nil? )
117
- joins = ['host.name','host.state','host.acknowledgement','host.downtime_depth','host.last_check']
152
+ joins = ['host.name', 'host.state', 'host.acknowledgement', 'host.downtime_depth', 'host.last_check']
118
153
  end
119
154
 
120
155
  payload['attrs'] = attrs unless attrs.nil?
121
-
122
156
  payload['filter'] = filter unless filter.nil?
123
-
124
157
  payload['joins'] = joins unless joins.nil?
125
158
 
126
- result = Network.get( host: nil,
159
+ Network.get( host: nil,
127
160
  url: format( '%s/v1/objects/services', @icinga_api_url_base ),
128
161
  headers: @headers,
129
162
  options: @options,
130
163
  payload: payload )
131
164
 
132
- JSON.pretty_generate( result )
133
-
134
165
  end
135
166
 
167
+ # return count of services with problems
136
168
  #
169
+ # @example
170
+ # @icinga.service_problems
137
171
  #
172
+ # @return [Integer]
138
173
  #
139
174
  def service_problems
140
175
 
@@ -142,31 +177,31 @@ module Icinga2
142
177
  problems = 0
143
178
 
144
179
  data = JSON.parse(data) if data.is_a?(String)
180
+ nodes = data.dig(:nodes)
145
181
 
146
- nodes = data.dig('nodes')
147
-
148
- nodes.each do |n|
182
+ unless !nodes
183
+ nodes.each do |n|
184
+ attrs = n.last.dig('attrs')
185
+ state = attrs.dig('state') || 0
186
+ downtime_depth = attrs.dig('downtime_depth') || 0
187
+ acknowledgement = attrs.dig('acknowledgement') || 0
149
188
 
150
- attrs = n.last.dig('attrs')
151
-
152
- state = attrs.dig('state')
153
- downtime_depth = attrs.dig('downtime_depth')
154
- acknowledgement = attrs.dig('acknowledgement')
155
-
156
- # puts state
157
-
158
- if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
159
- problems += 1 #= problems +1
189
+ if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
190
+ problems += 1
191
+ end
160
192
  end
161
-
162
193
  end
163
-
164
194
  problems
165
-
166
195
  end
167
196
 
197
+ # return a list of services with problems
168
198
  #
199
+ # @param [Integer] max_items numbers of list entries
169
200
  #
201
+ # @example
202
+ # @icinga.problem_services
203
+ #
204
+ # @return [Hash]
170
205
  #
171
206
  def problem_services( max_items = 5 )
172
207
 
@@ -177,31 +212,38 @@ module Icinga2
177
212
  services_data = service_objects
178
213
 
179
214
  if( services_data.is_a?(String) )
180
-
181
215
  services_data = JSON.parse( services_data )
182
216
  end
183
217
 
184
- services_data = services_data.dig('nodes')
218
+ services_data = services_data.dig(:nodes)
185
219
 
186
- services_data.each do |_service,v|
220
+ unless !services_data
187
221
 
188
- name = v.dig('name')
189
- state = v.dig('attrs','state')
190
- # logger.debug( "Severity for #{name}" )
191
- next if state.zero?
222
+ services_data.each do |_service,v|
192
223
 
193
- @service_problems[name] = service_severity(v)
194
- end
195
-
196
- @service_problems.sort.reverse!
224
+ name = v.dig('name')
225
+ state = v.dig('attrs','state')
226
+ next if state.zero?
197
227
 
198
- @service_problems.keys[1..max_items].each { |k,_v| @service_problems_severity[k] = @service_problems[k] }
228
+ @service_problems[name] = service_severity(v)
229
+ end
199
230
 
231
+ @service_problems.sort.reverse!
232
+ @service_problems.keys[1..max_items].each { |k,_v| @service_problems_severity[k] = @service_problems[k] }
233
+ end
200
234
  @service_problems_severity
201
235
  end
202
236
 
237
+ # update host
238
+ #
239
+ # @param [Hash] hash
240
+ # @param [String] host
203
241
  #
242
+ # @todo
243
+ # this function are not operable
244
+ # need help, time or beer
204
245
  #
246
+ # @return [Hash]
205
247
  #
206
248
  def update_host( hash, host )
207
249
 
@@ -222,10 +264,17 @@ module Icinga2
222
264
  hash
223
265
  end
224
266
 
225
- # private
267
+ # calculate a service severity
268
+ #
226
269
  # stolen from Icinga Web 2
227
270
  # ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
228
271
  #
272
+ # @param [Hash] service
273
+ #
274
+ # @private
275
+ #
276
+ # @return [Hash]
277
+ #
229
278
  def service_severity(service)
230
279
 
231
280
  attrs = service.dig('attrs')
@@ -3,49 +3,55 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespace for status handling
9
7
  module Status
10
8
 
9
+ # return Icinga2 Application data
11
10
  #
11
+ # @example
12
+ # @icinga.application_data
12
13
  #
14
+ # @return [Hash]
13
15
  #
14
16
  def application_data
15
17
 
16
- result = Network.get(host: nil,
18
+ Network.get(host: nil,
17
19
  url: format( '%s/v1/status/IcingaApplication', @icinga_api_url_base ),
18
20
  headers: @headers,
19
21
  options: @options)
20
22
 
21
- JSON.pretty_generate( result )
22
-
23
23
  end
24
24
 
25
+ # return Icinga2 CIB
25
26
  #
27
+ # @example
28
+ # @icinga.cib_data
26
29
  #
30
+ # @return [Hash]
27
31
  #
28
32
  def cib_data
29
33
 
30
- result = Network.get(host: nil,
34
+ Network.get(host: nil,
31
35
  url: format( '%s/v1/status/CIB', @icinga_api_url_base ),
32
36
  headers: @headers,
33
37
  options: @options)
34
38
 
35
- JSON.pretty_generate( result )
36
39
  end
37
40
 
41
+ # return Icinga2 API Listener
38
42
  #
43
+ # @example
44
+ # @icinga.api_listener
39
45
  #
46
+ # @return [Hash]
40
47
  #
41
48
  def api_listener
42
49
 
43
- result = Network.get(host: nil,
50
+ Network.get(host: nil,
44
51
  url: format( '%s/v1/status/ApiListener', @icinga_api_url_base ),
45
52
  headers: @headers,
46
53
  options: @options)
47
54
 
48
- JSON.pretty_generate( result )
49
55
  end
50
56
 
51
57
  end
@@ -3,13 +3,15 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespache for tools
9
7
  module Tools
10
8
 
9
+ # returns true for the last check
10
+ # @private
11
11
  #
12
+ # @param [Hash] object
12
13
  #
14
+ # @return [Bool]
13
15
  #
14
16
  def object_has_been_checked?(object)
15
17
  object.dig('attrs', 'last_check').positive?
@@ -3,17 +3,25 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespace for usergroup handling
9
7
  module Usergroups
10
8
 
9
+ # add a usergroup
11
10
  #
11
+ # @param [Hash] params
12
+ # @option params [String] :name usergroup to create
13
+ # @option params [String] :display_name the displayed name
12
14
  #
15
+ # @example
16
+ # @icinga.add_usergroup(name: 'foo', display_name: 'FOO')
17
+ #
18
+ # @return [Hash] result
13
19
  #
14
20
  def add_usergroup( params = {} )
15
21
 
16
22
  name = params.dig(:name)
23
+ display_name = params.dig(:display_name)
24
+
17
25
  if( name.nil? )
18
26
  return {
19
27
  status: 404,
@@ -23,58 +31,85 @@ module Icinga2
23
31
 
24
32
  payload = {
25
33
  'attrs' => {
26
- 'display_name' => name
34
+ 'display_name' => display_name
27
35
  }
28
36
  }
29
37
 
30
- result = Network.put( host: name,
38
+ Network.put( host: name,
31
39
  url: format( '%s/v1/objects/usergroups/%s', @icinga_api_url_base, name ),
32
40
  headers: @headers,
33
41
  options: @options,
34
42
  payload: payload )
35
43
 
36
- JSON.pretty_generate( result )
37
-
38
44
  end
39
45
 
46
+ # delete a usergroup
47
+ #
48
+ # @param [Hash] params
49
+ # @option params [String] :name usergroup to delete
40
50
  #
51
+ # @example
52
+ # @icinga.delete_usergroup(name: 'foo')
41
53
  #
54
+ # @return [Hash] result
42
55
  #
43
56
  def delete_usergroup( params = {} )
57
+
44
58
  name = params.dig(:name)
59
+
45
60
  if( name.nil? )
46
61
  return {
47
62
  status: 404,
48
63
  message: 'missing usergroup name'
49
64
  }
50
65
  end
51
- result = Network.delete( host: name,
66
+
67
+ Network.delete( host: name,
52
68
  url: format( '%s/v1/objects/usergroups/%s?cascade=1', @icinga_api_url_base, name ),
53
69
  headers: @headers,
54
70
  options: @options )
55
- JSON.pretty_generate( result )
71
+
56
72
  end
57
73
 
74
+ # returns all usersgroups
58
75
  #
76
+ # @param [Hash] params
77
+ # @option params [String] :name ('') optional for a single usergroup
59
78
  #
79
+ # @example to get all users
80
+ # @icinga.usergroups
81
+ #
82
+ # @example to get one user
83
+ # @icinga.usergroups(name: 'icingaadmins')
84
+ #
85
+ # @return [Hash] returns a hash with all usergroups
60
86
  #
61
87
  def usergroups( params = {} )
88
+
62
89
  name = params.dig(:name)
63
- result = Network.get( host: name,
90
+
91
+ Network.get( host: name,
64
92
  url: format( '%s/v1/objects/usergroups/%s', @icinga_api_url_base, name ),
65
93
  headers: @headers,
66
94
  options: @options )
67
- JSON.pretty_generate( result )
95
+
68
96
  end
69
97
 
98
+ # returns true if the usergroup exists
99
+ #
100
+ # @param [String] name the name of the usergroups
70
101
  #
102
+ # @example
103
+ # @icinga.exists_usergroup?('icingaadmins')
71
104
  #
105
+ # @return [Bool] returns true if the usergroup exists
72
106
  #
73
107
  def exists_usergroup?( name )
74
108
 
75
109
  result = usergroups( name: name )
76
110
  result = JSON.parse( result ) if result.is_a?( String )
77
- status = result.dig('status')
111
+ status = result.dig(:status)
112
+
78
113
  return true if !status.nil? && status == 200
79
114
  false
80
115
  end