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,13 +3,34 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespace for downtimes handling
9
7
  module Downtimes
10
8
 
9
+ # add downtime
11
10
  #
11
+ # @param [Hash] params
12
+ # @option params [String] :name
13
+ # @option params [String] :host
14
+ # @option params [String] :host_group
15
+ # @option params [Integer] :start_time (Time.new.to_i)
16
+ # @option params [Integer] :end_time
17
+ # @option params [String] :author
18
+ # @option params [String] :comment
19
+ # @option params [String] :type 'host' or 'service' downtime
12
20
  #
21
+ # @example
22
+ # param = {
23
+ # name: 'test',
24
+ # type: 'service',
25
+ # host: 'icinga2',
26
+ # comment: 'test downtime',
27
+ # author: 'icingaadmin',
28
+ # start_time: Time.now.to_i,
29
+ # end_time: Time.now.to_i + 20
30
+ # }
31
+ # @icinga.add_downtime(param)
32
+ #
33
+ # @return [Hash]
13
34
  #
14
35
  def add_downtime( params = {} )
15
36
 
@@ -84,7 +105,6 @@ module Icinga2
84
105
  }
85
106
  end
86
107
 
87
-
88
108
  if( end_time.nil? )
89
109
  return {
90
110
  status: 404,
@@ -97,9 +117,6 @@ module Icinga2
97
117
  }
98
118
  end
99
119
 
100
- # logger.debug( Time.at( start_time ).strftime( '%Y-%m-%d %H:%M:%S' ) )
101
- # logger.debug( Time.at( end_time ).strftime( '%Y-%m-%d %H:%M:%S' ) )
102
-
103
120
  payload = {
104
121
  'type' => type,
105
122
  'start_time' => start_time,
@@ -111,8 +128,6 @@ module Icinga2
111
128
  'filter' => filter
112
129
  }
113
130
 
114
- # logger.debug( JSON.pretty_generate( payload ) )
115
-
116
131
  result = Network.post( host: name,
117
132
  url: format( '%s/v1/actions/schedule-downtime', @icinga_api_url_base ),
118
133
  headers: @headers,
@@ -131,19 +146,24 @@ module Icinga2
131
146
 
132
147
  # --data '{ "type": "Service", "filter": "\"api_dummy_hostgroup\" in host.groups)", ... }'
133
148
 
134
- JSON.pretty_generate( result )
135
-
136
149
  end
137
150
 
151
+ # return downtimes
152
+ #
153
+ # @param [Hash] params
154
+ # @option params [String] :host
138
155
  #
156
+ # @example
157
+ # @icinga.downtimes
139
158
  #
159
+ # @return [Hash]
140
160
  #
141
161
  def downtimes( params = {} )
142
162
 
143
- name = params.dig(:name)
163
+ host = params.dig(:host)
144
164
 
145
- result = Network.get( host: name,
146
- url: format( '%s/v1/objects/downtimes/%s', @icinga_api_url_base, name ),
165
+ result = Network.get( host: host,
166
+ url: format( '%s/v1/objects/downtimes/%s', @icinga_api_url_base, host ),
147
167
  headers: @headers,
148
168
  options: @options )
149
169
 
@@ -3,71 +3,105 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- #
7
- #
8
- #
6
+ # namespace for hostgroup handling
9
7
  module Hostgroups
10
8
 
9
+ # add a hostgroup
11
10
  #
11
+ # @param [Hash] params
12
+ # @option params [String] :host_group hostgroup to create
13
+ # @option params [String] :display_name the displayed name
12
14
  #
15
+ # @example
16
+ # @icinga.add_hostgroup(host_group: 'foo', display_name: 'FOO')
17
+ #
18
+ # @return [Hash] result
13
19
  #
14
20
  def add_hostgroup(params = {})
15
- name = params.dig(:name)
21
+
22
+ host_group = params.dig(:host_group)
16
23
  display_name = params.dig(:display_name)
17
- if name.nil?
24
+
25
+ if host_group.nil?
18
26
  return {
19
27
  status: 404,
20
28
  message: 'no name for the hostgroup'
21
29
  }
22
30
  end
23
- result = Network.put(url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, name),
31
+
32
+ Network.put(url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, host_group),
24
33
  headers: @headers,
25
34
  options: @options,
26
35
  payload: { 'attrs' => { 'display_name' => display_name } })
27
- JSON.pretty_generate(result)
36
+
28
37
  end
29
38
 
39
+ # delete a hostgroup
40
+ #
41
+ # @param [Hash] params
42
+ # @option params [String] :name hostgroup to delete
30
43
  #
44
+ # @example
45
+ # @icinga.delete_hostgroup(host_group: 'foo')
31
46
  #
47
+ # @return [Hash] result
32
48
  #
33
49
  def delete_hostgroup(params = {})
34
- name = params.dig(:name)
35
- if name.nil?
50
+
51
+ host_group = params.dig(:host_group)
52
+
53
+ if host_group.nil?
36
54
  return {
37
55
  status: 404,
38
56
  message: 'no name for the hostgroup'
39
57
  }
40
58
  end
41
- result = Network.delete(host: name,
42
- url: format('%s/v1/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, name),
59
+
60
+ Network.delete(host: host_group,
61
+ url: format('%s/v1/objects/hostgroups/%s?cascade=1', @icinga_api_url_base, host_group),
43
62
  headers: @headers,
44
63
  options: @options)
45
- JSON.pretty_generate(result)
46
64
  end
47
65
 
66
+ # returns all usersgroups
67
+ #
68
+ # @param [Hash] params
69
+ # @option params [String] :host_group ('') optional for a single hostgroup
48
70
  #
71
+ # @example to get all users
72
+ # @icinga.hostgroups
49
73
  #
74
+ # @example to get one user
75
+ # @icinga.hostgroups(name: 'linux-servers')
76
+ #
77
+ # @return [Hash] returns a hash with all hostgroups
50
78
  #
51
79
  def hostgroups(params = {})
52
- name = params.dig(:name) || ''
53
- result = Network.get(host: name,
54
- url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, name),
80
+
81
+ host_group = params.dig(:host_group)
82
+
83
+ Network.get(host: host_group,
84
+ url: format('%s/v1/objects/hostgroups/%s', @icinga_api_url_base, host_group),
55
85
  headers: @headers,
56
86
  options: @options)
57
- result = JSON.pretty_generate(result) unless result.nil?
58
- result
87
+
59
88
  end
60
89
 
90
+ # returns true if the hostgroup exists
61
91
  #
92
+ # @param [String] name the name of the hostgroup
62
93
  #
94
+ # @example
95
+ # @icinga.exists_hostgroup?('linux-servers')
96
+ #
97
+ # @return [Bool] returns true if the hostgroup exists
63
98
  #
64
99
  def exists_hostgroup?(name)
65
- result = hostgroups(name: name)
100
+ result = hostgroups(host_group: name)
66
101
  result = JSON.parse(result) if result.is_a?(String)
67
- status = result.dig('status')
102
+ status = result.dig(:status)
68
103
 
69
104
  return true if !status.nil? && status == 200
70
-
71
105
  false
72
106
  end
73
107
 
@@ -2,30 +2,51 @@
2
2
  # frozen_string_literal: true
3
3
  module Icinga2
4
4
 
5
- #
6
- #
7
- #
5
+ # namespace for host handling
8
6
  module Hosts
9
7
 
8
+ # add host
10
9
  #
10
+ # @param [Hash] params
11
+ # @option params [String] :host
12
+ # @option params [String] :fqdn
13
+ # @option params [String] :display_name
14
+ # @option params [Bool] :enable_notifications (false)
15
+ # @option params [Integer] :max_check_attempts (3)
16
+ # @option params [Integer] :check_interval (60)
17
+ # @option params [Integer] :retry_interval (45)
18
+ # @option params [String] :notes
19
+ # @option params [String] :notes_url
20
+ # @option params [String] :action_url
21
+ # @option params [Hash] :vars ({})
11
22
  #
23
+ # @example
24
+ # param = {
25
+ # host: 'foo',
26
+ # fqdn: 'foo.bar.com',
27
+ # display_name: 'test node',
28
+ # max_check_attempts: 5,
29
+ # notes: 'test node'
30
+ # }
31
+ # @icinga.add_host(param)
32
+ #
33
+ # @return [Hash]
12
34
  #
13
35
  def add_host( params = {} )
14
36
 
15
- name = params.dig(:name)
16
- fqdn = params.dig(:fqdn)
17
- display_name = params.dig(:display_name) || name
18
- notifications = params.dig(:enable_notifications) || false
37
+ host = params.dig(:host)
38
+ fqdn = params.dig(:fqdn)
39
+ display_name = params.dig(:display_name) || host
40
+ notifications = params.dig(:enable_notifications) || false
19
41
  max_check_attempts = params.dig(:max_check_attempts) || 3
20
- check_interval = params.dig(:check_interval) || 60
21
- retry_interval = params.dig(:retry_interval) || 45
22
- notes = params.dig(:notes)
23
- notes_url = params.dig(:notes_url)
24
- action_url = params.dig(:action_url)
25
- vars = params.dig(:vars) || {}
26
-
27
- if( name.nil? )
28
-
42
+ check_interval = params.dig(:check_interval) || 60
43
+ retry_interval = params.dig(:retry_interval) || 45
44
+ notes = params.dig(:notes)
45
+ notes_url = params.dig(:notes_url)
46
+ action_url = params.dig(:action_url)
47
+ vars = params.dig(:vars) || {}
48
+
49
+ if( host.nil? )
29
50
  return {
30
51
  status: 404,
31
52
  message: 'missing host name'
@@ -34,7 +55,7 @@ module Icinga2
34
55
 
35
56
  if( fqdn.nil? )
36
57
  # build FQDN
37
- fqdn = Socket.gethostbyname( name ).first
58
+ fqdn = Socket.gethostbyname( host ).first
38
59
  end
39
60
 
40
61
  payload = {
@@ -60,24 +81,29 @@ module Icinga2
60
81
 
61
82
  logger.debug( JSON.pretty_generate( payload ) )
62
83
 
63
- result = Network.put( host: name,
64
- url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, name ),
84
+ Network.put( host: host,
85
+ url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
65
86
  headers: @headers,
66
87
  options: @options,
67
88
  payload: payload )
68
89
 
69
- JSON.pretty_generate( result )
70
-
71
90
  end
72
91
 
92
+ # delete a host
73
93
  #
94
+ # @param [Hash] params
95
+ # @option params [String] :host host to delete
74
96
  #
97
+ # @example
98
+ # @icinga.delete_host(host: 'foo')
99
+ #
100
+ # @return [Hash] result
75
101
  #
76
102
  def delete_host( params = {} )
77
103
 
78
- name = params.dig(:name)
104
+ host = params.dig(:host)
79
105
 
80
- if( name.nil? )
106
+ if( host.nil? )
81
107
 
82
108
  return {
83
109
  status: 404,
@@ -85,21 +111,32 @@ module Icinga2
85
111
  }
86
112
  end
87
113
 
88
- result = Network.delete( host: name,
89
- url: format( '%s/v1/objects/hosts/%s?cascade=1', @icinga_api_url_base, name ),
114
+ Network.delete( host: host,
115
+ url: format( '%s/v1/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
90
116
  headers: @headers,
91
117
  options: @options )
92
118
 
93
- JSON.pretty_generate( result )
94
-
95
119
  end
96
120
 
121
+ # return hosts
122
+ #
123
+ # @param [Hash] params
124
+ # @option params [String] :host
125
+ # @option params [String] :attrs
126
+ # @option params [String] :filter
127
+ # @option params [String] :joins
128
+ #
129
+ # @example to get all hosts
130
+ # @icinga.hosts
97
131
  #
132
+ # @example to get one host
133
+ # @icinga.host(host: 'icinga2')
98
134
  #
135
+ # @return [Hash]
99
136
  #
100
137
  def hosts( params = {} )
101
138
 
102
- name = params.dig(:name)
139
+ host = params.dig(:host)
103
140
  attrs = params.dig(:attrs)
104
141
  filter = params.dig(:filter)
105
142
  joins = params.dig(:joins)
@@ -108,34 +145,46 @@ module Icinga2
108
145
  payload['filter'] = filter unless filter.nil?
109
146
  payload['joins'] = joins unless joins.nil?
110
147
 
111
- result = Network.get( host: name,
112
- url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, name ),
148
+ Network.get( host: host,
149
+ url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
113
150
  headers: @headers,
114
151
  options: @options )
115
152
 
116
- JSON.pretty_generate( result )
117
-
118
153
  end
119
154
 
155
+ # returns true if the host exists
156
+ #
157
+ # @param [String] name
120
158
  #
159
+ # @example
160
+ # @icinga.exists_host?('icinga2')
121
161
  #
162
+ # @return [Bool]
122
163
  #
123
164
  def exists_host?( name )
124
165
 
125
166
  result = hosts( name: name )
126
-
127
167
  result = JSON.parse( result ) if result.is_a?( String )
128
-
129
- status = result.dig('status')
168
+ status = result.dig(:status)
130
169
 
131
170
  return true if !status.nil? && status == 200
132
-
133
171
  false
134
-
135
172
  end
136
173
 
174
+ # returns host objects
175
+ #
176
+ # @param [Hash] params
177
+ # @option params [Array] :attrs (['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check'])
178
+ # @option params [Array] :filter ([])
179
+ # @option params [Array] :joins ([])
180
+ #
181
+ # @example with default attrs and joins
182
+ # @icinga.host_objects
137
183
  #
184
+ # @example
185
+ # @icinga.host_objects(attrs: ['name', 'state'])
138
186
  #
187
+ # @return [Hash]
139
188
  #
140
189
  def host_objects( params = {} )
141
190
 
@@ -149,23 +198,23 @@ module Icinga2
149
198
  end
150
199
 
151
200
  payload['attrs'] = attrs unless attrs.nil?
152
-
153
201
  payload['filter'] = filter unless filter.nil?
154
-
155
202
  payload['joins'] = joins unless joins.nil?
156
203
 
157
- result = Network.get( host: nil,
204
+ Network.get( host: nil,
158
205
  url: format( '%s/v1/objects/hosts', @icinga_api_url_base ),
159
206
  headers: @headers,
160
207
  options: @options,
161
208
  payload: payload )
162
209
 
163
- JSON.pretty_generate( result )
164
-
165
210
  end
166
211
 
212
+ # return count of hosts with problems
167
213
  #
214
+ # @example
215
+ # @icinga.host_problems
168
216
  #
217
+ # @return [Integer]
169
218
  #
170
219
  def host_problems
171
220
 
@@ -173,29 +222,34 @@ module Icinga2
173
222
  problems = 0
174
223
 
175
224
  data = JSON.parse(data) if data.is_a?(String)
225
+ nodes = data.dig(:nodes)
176
226
 
177
- nodes = data.dig('nodes')
227
+ unless !nodes
178
228
 
179
- nodes.each do |n|
229
+ nodes.each do |n|
180
230
 
181
- attrs = n.last.dig('attrs')
231
+ attrs = n.last.dig('attrs')
232
+ state = attrs.dig('state') || 0
233
+ downtime_depth = attrs.dig('downtime_depth') || 0
234
+ acknowledgement = attrs.dig('acknowledgement') || 0
182
235
 
183
- state = attrs.dig('state') || 0
184
- downtime_depth = attrs.dig('downtime_depth') || 0
185
- acknowledgement = attrs.dig('acknowledgement') || 0
236
+ if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
237
+ problems += 1
238
+ end
186
239
 
187
- if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
188
- problems += 1
189
240
  end
190
-
191
241
  end
192
-
193
242
  problems
194
-
195
243
  end
196
244
 
245
+ # return a list of host with problems
246
+ #
247
+ # @param [Integer] max_items numbers of list entries
197
248
  #
249
+ # @example
250
+ # @icinga.problem_hosts
198
251
  #
252
+ # @return [Hash]
199
253
  #
200
254
  def problem_hosts( max_items = 5 )
201
255
 
@@ -205,32 +259,39 @@ module Icinga2
205
259
  host_data = host_objects
206
260
 
207
261
  host_data = JSON.parse( host_data ) if host_data.is_a?(String)
262
+ host_data = host_data.dig(:nodes)
208
263
 
209
- # logger.debug( host_data )
264
+ unless !host_data
210
265
 
211
- host_data = host_data.dig('nodes')
266
+ host_data.each do |_host,v|
212
267
 
213
- host_data.each do |_host,v|
268
+ name = v.dig('name')
269
+ state = v.dig('attrs','state')
214
270
 
215
- name = v.dig('name')
216
- state = v.dig('attrs','state')
271
+ next if state.zero?
217
272
 
218
- next if state.zero?
273
+ @host_problems[name] = host_severity(v)
274
+ end
219
275
 
220
- @host_problems[name] = host_severity(v)
276
+ # get the count of problems
277
+ #
278
+ @host_problems.keys[1..max_items].each { |k,_v| @host_problems_severity[k] = @host_problems[k] }
221
279
  end
222
-
223
- # get the count of problems
224
- #
225
- @host_problems.keys[1..max_items].each { |k,_v| @host_problems_severity[k] = @host_problems[k] }
226
-
227
280
  @host_problems_severity
228
281
 
229
282
  end
230
283
 
284
+ # calculate a host severity
285
+ #
231
286
  # stolen from Icinga Web 2
232
287
  # ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
233
288
  #
289
+ # @param [Hash] host
290
+ #
291
+ # @private
292
+ #
293
+ # @return [Hash]
294
+ #
234
295
  def host_severity( host )
235
296
 
236
297
  attrs = host.dig('attrs')