icinga2 0.9.2.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ module Icinga2
28
28
  # pager: '0000',
29
29
  # groups: ['icingaadmins']
30
30
  # }
31
- # @icinga.add_user(param)
31
+ # add_user(param)
32
32
  #
33
33
  # @return [Hash] result
34
34
  # * code [Integer]
@@ -96,7 +96,7 @@ module Icinga2
96
96
  # @param [String] user_name user to delete
97
97
  #
98
98
  # @example
99
- # @icinga.delete_user('foo')
99
+ # delete_user('foo')
100
100
  #
101
101
  # @return [Hash] result
102
102
  #
@@ -117,10 +117,10 @@ module Icinga2
117
117
  # @param [String] user_name ('') optional for a single user
118
118
  #
119
119
  # @example to get all users
120
- # @icinga.users
120
+ # users
121
121
  #
122
122
  # @example to get one user
123
- # @icinga.users('icingaadmin')
123
+ # users('icingaadmin')
124
124
  #
125
125
  # @return [Array]
126
126
  #
@@ -141,7 +141,7 @@ module Icinga2
141
141
  # @param [String] user_name the name of the user
142
142
  #
143
143
  # @example
144
- # @icinga.exists_user?('icingaadmin')
144
+ # exists_user?('icingaadmin')
145
145
  #
146
146
  # @return [Bool] returns true if the user exists
147
147
  #
@@ -1,20 +1,20 @@
1
1
 
2
- class Hash
3
- def assert_required_keys(*keys)
4
- keys.flatten.each do |key|
5
- raise ArgumentError.new("Required key: #{key.inspect}") unless key?(key)
6
- end
7
- end
8
-
9
- def assert_valid_keys(params, *valid_keys)
10
- valid_keys.flatten!
11
- params.each_key do |k|
12
- unless valid_keys.include?(k)
13
- raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
14
- end
15
- end
16
- end
17
- end
2
+ # class Hash
3
+ # def assert_required_keys(*keys)
4
+ # keys.flatten.each do |key|
5
+ # raise ArgumentError.new("Required key: #{key.inspect}") unless key?(key)
6
+ # end
7
+ # end
8
+ #
9
+ # def assert_valid_keys(params, *valid_keys)
10
+ # valid_keys.flatten!
11
+ # params.each_key do |k|
12
+ # unless valid_keys.include?(k)
13
+ # raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
14
+ # end
15
+ # end
16
+ # end
17
+ # end
18
18
 
19
19
 
20
20
  module Icinga2
@@ -22,34 +22,40 @@ module Icinga2
22
22
  # namespace for validate options
23
23
  module Validator
24
24
 
25
- def validate_options!(params, options)
26
- options[:optional] ||= []
27
- options[:required] ||= []
28
-
29
- params = params.deep_symbolize_keys
30
- params.assert_required_keys(options[:required])
31
- params.assert_valid_keys(params, options[:required] + options[:optional])
32
- end
33
-
34
- # validate( params, { required: true, var: name, type: String } )
25
+ # def validate_options!(params, options)
26
+ # options[:optional] ||= []
27
+ # options[:required] ||= []
28
+ #
29
+ # params = params.deep_symbolize_keys
30
+ # params.assert_required_keys(options[:required])
31
+ # params.assert_valid_keys(params, options[:required] + options[:optional])
32
+ # end
33
+
34
+ # function to validate function parameters
35
+ #
36
+ # @param [Hash] params
37
+ # @param [Hash] options
38
+ # @option options [Bool] requiered
39
+ # @option options [String] vat
40
+ # @option options [Object] type Ruby Object to check the valid type. e.g String, Ineger Hash, ...
41
+ #
42
+ # @example
43
+ # name = validate( params, required: true, var: 'name', type: String )
44
+ # vars = validate( params, required: false, var: 'vars', type: Hash )
45
+ #
46
+ # @return [Mixed] the variable from the parmas Hash or nil
47
+ #
35
48
  def validate( params, options )
36
49
  required = options.dig(:required) || false
37
50
  var = options.dig(:var)
38
51
  type = options.dig(:type)
39
52
 
40
53
  params = params.deep_symbolize_keys
41
-
42
54
  variable = params.dig(var.to_sym)
43
55
 
44
- # puts params
45
- # puts options
46
-
47
56
  clazz = Object.const_get(type.to_s)
48
57
 
49
- if(required == true )
50
- raise ArgumentError.new(format('\'%s\' is requiered and missing!', var)) if(variable.nil?)
51
- end
52
-
58
+ raise ArgumentError.new(format('\'%s\' is requiered and missing!', var)) if(variable.nil? && required == true )
53
59
  raise ArgumentError.new(format('wrong type. \'%s\' must be an %s, given \'%s\'', var, type, variable.class.to_s)) unless( variable.nil? || variable.is_a?(clazz) )
54
60
 
55
61
  variable
@@ -2,8 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Icinga2
5
-
6
5
  # Current version of gem.
7
- VERSION = '0.9.2.8'.freeze # [Version::MAJOR, Version::MINOR, Version::TINY, Version::PATCH].compact * '.'
8
-
6
+ VERSION = '1.0.0'.freeze
9
7
  end
@@ -2,19 +2,22 @@
2
2
  # -----------------------------------------------------------------------------
3
3
  # Monkey patches
4
4
 
5
- # Modify `Object` (https://gist.github.com/Integralist/9503099)
6
-
5
+ # Modify `Object`
6
+ #
7
+ # original from (https://gist.github.com/Integralist/9503099)
8
+ #
7
9
  # None of the above solutions work with a multi-level hash
8
10
  # They only work on the first level: {:foo=>"bar", :level1=>{"level2"=>"baz"}}
9
11
  # The following two variations solve the problem in the same way
10
12
  # transform hash keys to symbols
11
- # multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } }
12
- # multi_hash = multi_hash.deep_string_keys
13
-
13
+ #
14
+ # @example
15
+ # multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } }
16
+ # multi_hash = multi_hash.deep_string_keys
17
+ #
14
18
  class Object
15
19
 
16
20
  def deep_symbolize_keys
17
-
18
21
  if( is_a?( Hash ) )
19
22
  return inject({}) do |memo, (k, v)|
20
23
  memo.tap { |m| m[k.to_sym] = v.deep_string_keys }
@@ -27,7 +30,6 @@ class Object
27
30
  end
28
31
 
29
32
  def deep_string_keys
30
-
31
33
  if( is_a?( Hash ) )
32
34
  return inject({}) do |memo, (k, v)|
33
35
  memo.tap { |m| m[k.to_s] = v.deep_string_keys }
@@ -43,7 +45,19 @@ end
43
45
 
44
46
  # -----------------------------------------------------------------------------
45
47
 
48
+ # Monkey Patch for Array
49
+ #
46
50
  class Array
51
+
52
+ # add a compare function to Array class
53
+ #
54
+ # @example
55
+ # a = ['aaa','bbb']
56
+ # b = ['ccc','xxx']
57
+ # a.compare(b)
58
+ #
59
+ # @return [Bool]
60
+ #
47
61
  def compare( comparate )
48
62
  to_set == comparate.to_set
49
63
  end
@@ -51,12 +65,18 @@ end
51
65
 
52
66
  # -----------------------------------------------------------------------------
53
67
 
54
- # filter hash
55
- # example:
56
- # tags = [ 'foo', 'bar', 'fii' ]
57
- # useableTags = tags.filter( 'fii' )
58
-
68
+ # Monkey Patch for Hash
69
+ #
59
70
  class Hash
71
+
72
+ # add a filter function to Hash Class
73
+ #
74
+ # @example
75
+ # tags = [ 'foo', 'bar', 'fii' ]
76
+ # useableTags = tags.filter( 'fii' )
77
+ #
78
+ # @return [Hash]
79
+ #
60
80
  def filter( *args )
61
81
  if( args.size == 1 )
62
82
  args[0] = args[0].to_s if args[0].is_a?( Symbol )
@@ -69,7 +89,18 @@ end
69
89
 
70
90
  # -----------------------------------------------------------------------------
71
91
 
92
+ # Monkey Patch for Time
93
+ #
72
94
  class Time
95
+
96
+ # add function add_minutes to Time Class
97
+ #
98
+ # @example
99
+ # time = Time.now
100
+ # time = time.add_minutes(10)
101
+ #
102
+ # @return [Time]
103
+ #
73
104
  def add_minutes(m)
74
105
  self + (60 * m)
75
106
  end
@@ -77,8 +108,10 @@ end
77
108
 
78
109
  # -----------------------------------------------------------------------------
79
110
 
80
- # https://stackoverflow.com/questions/3028243/check-if-ruby-object-is-a-boolean/3028378#3028378
81
-
111
+ # Monkey Patch to implement an Boolean Check
112
+ # original from: https://stackoverflow.com/questions/3028243/check-if-ruby-object-is-a-boolean/3028378#3028378
113
+ #
114
+ #
82
115
  module Boolean; end
83
116
  class TrueClass; include Boolean; end
84
117
  class FalseClass; include Boolean; end
@@ -87,3 +120,23 @@ true.is_a?(Boolean) #=> true
87
120
  false.is_a?(Boolean) #=> true
88
121
 
89
122
  # -----------------------------------------------------------------------------
123
+
124
+ # Monkey Patch for String Class
125
+ #
126
+ class String
127
+
128
+ # get the first character in a string
129
+ #
130
+ # original from https://stackoverflow.com/a/2730984
131
+ #
132
+ # @example
133
+ # 'foo'.initial
134
+ #
135
+ # @return [String]
136
+ #
137
+ def initial
138
+ self[0,1]
139
+ end
140
+ end
141
+
142
+ # -----------------------------------------------------------------------------
@@ -0,0 +1,1013 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'rspec'
5
+ require 'icinga2'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rspec
9
+ end
10
+
11
+ describe Icinga2 do
12
+
13
+ before do
14
+ config = {
15
+ icinga: {
16
+ host: ENV.fetch( 'ICINGA_HOST', 'localhost' ),
17
+ api: {
18
+ username: 'root',
19
+ password: 'icinga'
20
+ }
21
+ }
22
+ }
23
+ @icinga2 = Icinga2::Client.new( config )
24
+ end
25
+
26
+ describe 'Available' do
27
+ it 'available' do
28
+ expect(@icinga2.available?).to be_truthy
29
+ end
30
+ end
31
+
32
+ describe 'Converts' do
33
+
34
+ it 'host state to string' do
35
+ expect(@icinga2.state_to_string(0, true)).to be == 'Up'
36
+ expect(@icinga2.state_to_string(1, true)).to be == 'Down'
37
+ expect(@icinga2.state_to_string(5, true)).to be == 'Undefined'
38
+ end
39
+ it 'service state to string' do
40
+ expect(@icinga2.state_to_string(0, false)).to be == 'OK'
41
+ expect(@icinga2.state_to_string(1, false)).to be == 'Warning'
42
+ expect(@icinga2.state_to_string(2, false)).to be == 'Critical'
43
+ expect(@icinga2.state_to_string(3, false)).to be == 'Unknown'
44
+ expect(@icinga2.state_to_string(5, false)).to be == 'Undefined'
45
+ end
46
+
47
+ it 'host state to color' do
48
+ expect(@icinga2.state_to_color(0, true)).to be == 'green'
49
+ expect(@icinga2.state_to_color(1, true)).to be == 'red'
50
+ expect(@icinga2.state_to_color(5, true)).to be == 'blue'
51
+ end
52
+
53
+ it 'service state to color' do
54
+ expect(@icinga2.state_to_color(0, false)).to be == 'green'
55
+ expect(@icinga2.state_to_color(1, false)).to be == 'yellow'
56
+ expect(@icinga2.state_to_color(2, false)).to be == 'red'
57
+ expect(@icinga2.state_to_color(3, false)).to be == 'purple'
58
+ expect(@icinga2.state_to_color(5, false)).to be == 'blue'
59
+ end
60
+ end
61
+
62
+ describe 'Information' do
63
+
64
+ it 'status_data' do
65
+ expect(@icinga2.status_data).to be_a(Hash)
66
+ end
67
+
68
+ it 'application_data' do
69
+ expect(@icinga2.application_data).to be_a(Hash)
70
+ end
71
+
72
+ it 'cib_data' do
73
+ expect(@icinga2.cib_data).to be_a(Hash)
74
+ end
75
+ it 'api_listener' do
76
+ expect(@icinga2.api_listener).to be_a(Hash)
77
+ end
78
+
79
+ it 'version' do
80
+ @icinga2.application_data
81
+ v = @icinga2.version
82
+ expect(v).to be_a(Hash)
83
+ expect(v.count).to be == 2
84
+ expect(v.dig(:version)).to be_a(String)
85
+ expect(v.dig(:revision)).to be_a(String)
86
+ end
87
+
88
+ it 'node name' do
89
+ @icinga2.application_data
90
+ n = @icinga2.node_name
91
+ expect(n).to be_a(String)
92
+ end
93
+
94
+ it 'start time' do
95
+ @icinga2.application_data
96
+ expect(@icinga2.start_time).to be_a(Time)
97
+ end
98
+
99
+ it 'uptime' do
100
+ @icinga2.cib_data
101
+ expect(@icinga2.uptime).to be_a(String)
102
+ end
103
+ end
104
+
105
+ describe 'Module Statistics' do
106
+
107
+ it 'average' do
108
+ @icinga2.cib_data
109
+ latency, execution_time = @icinga2.average_statistics.values
110
+ expect(latency).to be_a(Float)
111
+ expect(execution_time).to be_a(Float)
112
+ end
113
+
114
+ it 'interval' do
115
+ @icinga2.cib_data
116
+ i = @icinga2.interval_statistics
117
+ expect(i).to be_a(Hash)
118
+ expect(i.count).to be == 4
119
+ expect(i.dig(:hosts_active_checks)).to be_a(Float)
120
+ expect(i.dig(:hosts_passive_checks)).to be_a(Float)
121
+ expect(i.dig(:services_active_checks)).to be_a(Float)
122
+ expect(i.dig(:services_passive_checks)).to be_a(Float)
123
+ end
124
+
125
+ it 'service' do
126
+ @icinga2.cib_data
127
+ s = @icinga2.service_statistics
128
+ expect(s).to be_a(Hash)
129
+ expect(s.count).to be == 7
130
+ expect(s.dig(:ok)).to be_a(Integer)
131
+ expect(s.dig(:warning)).to be_a(Integer)
132
+ expect(s.dig(:critical)).to be_a(Integer)
133
+ expect(s.dig(:unknown)).to be_a(Integer)
134
+ expect(s.dig(:pending)).to be_a(Integer)
135
+ expect(s.dig(:in_downtime)).to be_a(Integer)
136
+ expect(s.dig(:acknowledged)).to be_a(Integer)
137
+ end
138
+
139
+ it 'host' do
140
+ @icinga2.cib_data
141
+ s = @icinga2.host_statistics
142
+ expect(s).to be_a(Hash)
143
+ expect(s.count).to be == 6
144
+ expect(s.dig(:up)).to be_a(Integer)
145
+ expect(s.dig(:down)).to be_a(Integer)
146
+ expect(s.dig(:pending)).to be_a(Integer)
147
+ expect(s.dig(:unreachable)).to be_a(Integer)
148
+ expect(s.dig(:in_downtime)).to be_a(Integer)
149
+ expect(s.dig(:acknowledged)).to be_a(Integer)
150
+ end
151
+
152
+ it 'work queue' do
153
+ r = @icinga2.work_queue_statistics
154
+ expect(r).to be_a(Hash)
155
+ expect(r.count).to be >= 1
156
+ r.each_value do |v|
157
+ expect(v).to be_a(Float)
158
+ end
159
+ end
160
+ end
161
+
162
+ describe 'Module Host' do
163
+
164
+ it 'list all hosts' do
165
+ h = @icinga2.hosts
166
+ expect(h).to be_a(Array)
167
+ expect(h.count).to be >= 1
168
+ end
169
+
170
+ it 'create host \'foo\'' do
171
+ options = {
172
+ name: 'foo',
173
+ address: '127.0.0.1',
174
+ display_name: 'test node',
175
+ max_check_attempts: 5,
176
+ notes: 'test node',
177
+ vars: {
178
+ description: 'spec test',
179
+ os: 'Docker',
180
+ partitions: {
181
+ '/' => {
182
+ crit: '95%',
183
+ warn: '90%'
184
+ }
185
+ }
186
+ }
187
+ }
188
+ h = @icinga2.add_host(options)
189
+ expect(h).to be_a(Hash)
190
+ status_code = h['code']
191
+ expect(status_code).to be_a(Integer)
192
+ expect(status_code).to be == 200
193
+ end
194
+
195
+ it 'list host \'foo\'' do
196
+ h = @icinga2.hosts( name: 'foo')
197
+ expect(h).to be_a(Array)
198
+ name = h.first['attrs']['name']
199
+ expect(name).to be_a(String)
200
+ expect(name).to be == 'foo'
201
+ expect(h.count).to be == 1
202
+ end
203
+
204
+ it 'list host \'foo\'' do
205
+ h = @icinga2.hosts(
206
+ name: 'foo',
207
+ attrs: %w[display_name name address]
208
+ )
209
+ expect(h).to be_a(Array)
210
+ name = h.first['attrs']['name']
211
+ expect(name).to be_a(String)
212
+ expect(name).to be == 'foo'
213
+ expect(h.count).to be == 1
214
+ end
215
+
216
+ it 'exists host \'foo\'' do
217
+ expect(@icinga2.exists_host?('foo')).to be_truthy
218
+ end
219
+
220
+ it 'modify host \'foo\' with merge (must be failed)' do
221
+ options = {
222
+ name: 'foo-2',
223
+ display_name: 'test node (changed)',
224
+ max_check_attempts: 10,
225
+ notes: 'spec test',
226
+ vars: {
227
+ description: 'changed at ...'
228
+ },
229
+ merge_vars: true
230
+ }
231
+ h = @icinga2.modify_host(options)
232
+ expect(h).to be_a(Hash)
233
+ status_code = h['code']
234
+ expect(status_code).to be_a(Integer)
235
+ expect(status_code).to be == 404
236
+ end
237
+
238
+ it 'modify host \'foo\' with merge' do
239
+ options = {
240
+ name: 'foo',
241
+ display_name: 'test node (changed)',
242
+ max_check_attempts: 10,
243
+ notes: 'spec test',
244
+ vars: {
245
+ description: 'changed at ...'
246
+ },
247
+ merge_vars: true
248
+ }
249
+ h = @icinga2.modify_host(options)
250
+ expect(h).to be_a(Hash)
251
+ status_code = h['code']
252
+ expect(status_code).to be_a(Integer)
253
+ expect(status_code).to be == 200
254
+
255
+ # # check values
256
+ # h = @icinga2.hosts(host: 'foo')
257
+ # h = h.first
258
+ # puts JSON.pretty_generate h
259
+ # display_name = h['attrs']['display_name']
260
+ # expect(display_name).to be_a(String)
261
+ # max_check_attempts = h['attrs']['max_check_attempts']
262
+ # expect(max_check_attempts).to be_a(Float)
263
+ end
264
+
265
+ it 'modify host \'foo\' without merge' do
266
+ options = {
267
+ name: 'foo',
268
+ display_name: 'test node (changed)',
269
+ max_check_attempts: 10,
270
+ notes: 'spec test',
271
+ vars: {
272
+ description: 'changed only the description'
273
+ }
274
+ }
275
+ h = @icinga2.modify_host(options)
276
+ expect(h).to be_a(Hash)
277
+ status_code = h['code']
278
+ expect(status_code).to be_a(Integer)
279
+ expect(status_code).to be == 200
280
+ end
281
+
282
+ it 'delete host \'foo\'' do
283
+ h = @icinga2.delete_host( name: 'foo', cascade: true )
284
+ expect(h).to be_a(Hash)
285
+ status_code = h['code']
286
+ expect(status_code).to be_a(Integer)
287
+ expect(status_code).to be == 200
288
+ end
289
+
290
+ it 'delete host \'foo\' (again)' do
291
+ h = @icinga2.delete_host( name: 'foo' )
292
+ expect(h).to be_a(Hash)
293
+ status_code = h['code']
294
+ expect(status_code).to be_a(Integer)
295
+ expect(status_code).to be == 404
296
+ end
297
+
298
+
299
+ it 'exists_host \'test\'' do
300
+ expect(@icinga2.exists_host?('test')).to be_falsey
301
+ end
302
+
303
+ it 'count of all hosts' do
304
+ @icinga2.host_objects
305
+ c = @icinga2.hosts_all
306
+ expect(c).to be_a(Integer)
307
+ expect(c).to be >= 1
308
+ end
309
+
310
+ it 'count of all hosts with filter' do
311
+ @icinga2.host_objects(
312
+ filter: '"icinga-satellites" in host.groups'
313
+ )
314
+ c = @icinga2.hosts_all
315
+ expect(c).to be_a(Integer)
316
+ expect(c).to be >= 1
317
+ end
318
+
319
+ it 'count_hosts_with_problems' do
320
+ expect(@icinga2.count_hosts_with_problems).to be_a(Integer)
321
+ end
322
+
323
+ it 'list_hosts_with_problems 5' do
324
+ h = @icinga2.list_hosts_with_problems
325
+ expect(h).to be_a(Hash)
326
+ expect(h.count).to be <= 5
327
+ end
328
+
329
+ it 'list_hosts_with_problems 15' do
330
+ h = @icinga2.list_hosts_with_problems(15)
331
+ expect(h).to be_a(Hash)
332
+ expect(h.count).to be <= 15
333
+ end
334
+
335
+
336
+ it 'data with host problems' do
337
+ @icinga2.host_objects
338
+ h = @icinga2.host_problems
339
+ expect(h).to be_a(Hash)
340
+ expect(h.count).to be == 6
341
+ expect(h.dig(:all)).to be_a(Integer)
342
+ expect(h.dig(:down)).to be_a(Integer)
343
+ expect(h.dig(:critical)).to be_a(Integer)
344
+ expect(h.dig(:unknown)).to be_a(Integer)
345
+ expect(h.dig(:handled)).to be_a(Integer)
346
+ expect(h.dig(:adjusted)).to be_a(Integer)
347
+ end
348
+ end
349
+
350
+ describe 'Module Hostgroups' do
351
+
352
+ it 'list hostgroup \'icinga-satellites\'' do
353
+ h = @icinga2.hostgroups('icinga-satellites')
354
+ name = h.first.dig('attrs','__name')
355
+ expect(h).to be_a(Array)
356
+ expect(h.count).to be == 1
357
+ expect(name).to be == 'icinga-satellites'
358
+ end
359
+
360
+ it 'list all hostgroups' do
361
+ h = @icinga2.hostgroups
362
+ expect(h).to be_a(Array)
363
+ expect(h.count).to be >= 1
364
+ end
365
+
366
+ it 'exists hostgroup \'icinga-satellites\'' do
367
+ expect(@icinga2.exists_hostgroup?('icinga-satellites')).to be_truthy
368
+ end
369
+
370
+ it 'exists hostgroup \'test\'' do
371
+ expect(@icinga2.exists_hostgroup?('test')).to be_falsey
372
+ end
373
+ end
374
+
375
+ describe 'Module Services' do
376
+
377
+ it 'list services \'ping4\' for host \'c1-mysql-1\'' do
378
+ h = @icinga2.services( host_name: 'c1-mysql-1', name: 'ping4' )
379
+ expect(h).to be_a(Array)
380
+ expect(h.count).to be == 1
381
+ end
382
+
383
+ it 'list all services' do
384
+ h = @icinga2.services
385
+ expect(h).to be_a(Array)
386
+ expect(h.count).to be >= 1
387
+ end
388
+
389
+ it 'exists service check \'bp-mysql-uptime\' for host \'c1-mysql-1\'' do
390
+ expect(@icinga2.exists_service?( host_name: 'c1-mysql-1', name: 'bp-mysql-uptime' )).to be_truthy
391
+ end
392
+
393
+ it 'exists service check \'hdb\' for host \'c1-mysql-1\'' do
394
+ expect(@icinga2.exists_service?( host_name: 'c1-mysql-1', name: 'hdb' )).to be_falsey
395
+ end
396
+
397
+ it 'count of all services with default parameters' do
398
+ c = @icinga2.service_objects
399
+ expect(c).to be_a(Array)
400
+ expect(c.count).to be >= 1
401
+ end
402
+
403
+ it 'count of all services with \'attr\' and \'joins\' as parameter' do
404
+ c = @icinga2.service_objects(
405
+ attrs: %w[name state],
406
+ joins: ['host.name','host.state']
407
+ )
408
+ expect(c).to be_a(Array)
409
+ expect(c.count).to be >= 1
410
+ end
411
+
412
+ it 'count of all services with \'attrs\', \'filter\' and \'joins\' as parameter' do
413
+ c = @icinga2.service_objects(
414
+ attrs: %w[display_name check_command enable_active_checks],
415
+ filter: 'service.state == 1' ,
416
+ joins: ['host.name', 'host.address']
417
+ )
418
+ expect(c).to be_a(Array)
419
+ end
420
+
421
+ it 'count of services with problems' do
422
+ expect(@icinga2.count_services_with_problems).to be_a(Integer)
423
+ end
424
+
425
+ it 'list_services_with_problems 5' do
426
+ h = @icinga2.list_services_with_problems
427
+ expect(h).to be_a(Hash)
428
+ expect(h.count).to be == 2
429
+ expect(h.dig(:services_with_problems)).to be_a(Array)
430
+ expect(h.dig(:services_with_problems).count).to be <= 5
431
+ expect(h.dig(:services_with_problems_and_severity)).to be_a(Hash)
432
+ expect(h.dig(:services_with_problems_and_severity).count).to be <= 5
433
+ end
434
+
435
+ it 'list_services_with_problems 15' do
436
+ h = @icinga2.list_services_with_problems(15)
437
+ expect(h).to be_a(Hash)
438
+ expect(h.count).to be == 2
439
+ expect(h.dig(:services_with_problems)).to be_a(Array)
440
+ expect(h.dig(:services_with_problems).count).to be <= 15
441
+ expect(h.dig(:services_with_problems_and_severity)).to be_a(Hash)
442
+ expect(h.dig(:services_with_problems_and_severity).count).to be <= 15
443
+ end
444
+
445
+ it 'count of all services' do
446
+ @icinga2.cib_data
447
+ @icinga2.service_objects
448
+ expect(@icinga2.services_all).to be_a(Integer)
449
+ end
450
+
451
+ it 'services with problems' do
452
+ a = @icinga2.service_problems
453
+ expect(a).to be_a(Hash)
454
+ expect(a.count).to be == 14
455
+ expect(a.dig(:ok)).to be_a(Integer)
456
+ expect(a.dig(:warning)).to be_a(Integer)
457
+ expect(a.dig(:critical)).to be_a(Integer)
458
+ expect(a.dig(:unknown)).to be_a(Integer)
459
+ expect(a.dig(:pending)).to be_a(Integer)
460
+ expect(a.dig(:in_downtime)).to be_a(Integer)
461
+ expect(a.dig(:acknowledged)).to be_a(Integer)
462
+ expect(a.dig(:adjusted_warning)).to be_a(Integer)
463
+ expect(a.dig(:adjusted_critical)).to be_a(Integer)
464
+ expect(a.dig(:adjusted_unknown)).to be_a(Integer)
465
+ expect(a.dig(:handled_all)).to be_a(Integer)
466
+ expect(a.dig(:handled_warning)).to be_a(Integer)
467
+ expect(a.dig(:handled_critical)).to be_a(Integer)
468
+ expect(a.dig(:handled_unknown)).to be_a(Integer)
469
+ end
470
+
471
+ it 'list all unhandled service' do
472
+ s = @icinga2.unhandled_services
473
+ expect(s).to be_a(Array)
474
+ expect(s.count).to be >= 0
475
+ end
476
+
477
+ it 'add service \'new_http\' to host \'c1-mysql-1\'' do
478
+ data = {
479
+ host_name: 'c1-mysql-1',
480
+ name: 'new_http',
481
+ check_command: 'http',
482
+ check_interval: 10,
483
+ retry_interval: 30,
484
+ max_check_attempts: 5,
485
+ vars: {
486
+ http_address: '127.0.0.1',
487
+ http_url: '/access/index',
488
+ http_port: 80
489
+ }
490
+ }
491
+ s = @icinga2.add_service( data )
492
+ status_code = s['code']
493
+ expect(s).to be_a(Hash)
494
+ expect(status_code).to be_a(Integer)
495
+
496
+ if(status_code != 200)
497
+ expect(s['status']).to be_a(String)
498
+ else
499
+ expect(status_code).to be == 200
500
+ end
501
+ end
502
+
503
+ it 'modify service \'new_http\'' do
504
+ data = {
505
+ name: 'new_http',
506
+ check_interval: 60,
507
+ retry_interval: 10,
508
+ notes: 'modifyed with spec test',
509
+ vars: {
510
+ http_url: '/access/login',
511
+ http_address: '10.41.80.63'
512
+ }
513
+ }
514
+ s = @icinga2.modify_service( data )
515
+
516
+ status_code = s['code']
517
+ expect(s).to be_a(Hash)
518
+ expect(status_code).to be_a(Integer)
519
+ expect(status_code).to be == 200
520
+ end
521
+
522
+ it 'delete service \'new_http\' from \'c1-mysql-1\'' do
523
+
524
+ s = @icinga2.delete_service(
525
+ host_name: 'c1-mysql-1',
526
+ name: 'new_http',
527
+ cascade: true
528
+ )
529
+ expect(s).to be_a(Hash)
530
+ status_code = s['code']
531
+ expect(status_code).to be_a(Integer)
532
+ expect(status_code).to be == 200
533
+ end
534
+
535
+ end
536
+
537
+ describe 'Module Servicegroups' do
538
+
539
+ it 'list all servicegroups' do
540
+ h = @icinga2.servicegroups
541
+ expect(h).to be_a(Array)
542
+ expect(h.count).to be >= 1
543
+ end
544
+
545
+ it 'list servicegroup \'icinga\'' do
546
+ h = @icinga2.servicegroups('icinga')
547
+ expect(h).to be_a(Array)
548
+ expect(h.count).to be == 1
549
+ end
550
+
551
+ it 'exists servicegroup \'icinga\'' do
552
+ expect(@icinga2.exists_servicegroup?('icinga')).to be_truthy
553
+ end
554
+
555
+ it 'exists servicegroup \'test\'' do
556
+ expect(@icinga2.exists_servicegroup?('test')).to be_falsey
557
+ end
558
+
559
+ it 'add Servicegroup \'foo\'' do
560
+ s = @icinga2.add_servicegroup( service_group: 'foo', display_name: 'FOO' )
561
+ expect(s).to be_a(Hash)
562
+
563
+ status_code = s['code']
564
+ expect(status_code).to be_a(Integer)
565
+ expect(status_code).to be == 200
566
+ end
567
+
568
+ it 'add Servicegroup \'foo\' (again)' do
569
+ s = @icinga2.add_servicegroup( service_group: 'foo', display_name: 'FOO' )
570
+ expect(s).to be_a(Hash)
571
+ status_code = s['code']
572
+ expect(status_code).to be_a(Integer)
573
+ expect(status_code).to be == 500
574
+ end
575
+
576
+ it 'delete Servicegroup \'foo\'' do
577
+ s = @icinga2.delete_servicegroup(name: 'foo', cascade: true)
578
+ expect(s).to be_a(Hash)
579
+
580
+ status_code = s['code']
581
+ expect(status_code).to be_a(Integer)
582
+ expect(status_code).to be == 200
583
+ end
584
+
585
+ it 'delete Servicegroup \'foo\' (again)' do
586
+ s = @icinga2.delete_servicegroup(name: 'foo')
587
+ expect(s).to be_a(Hash)
588
+
589
+ status_code = s['code']
590
+ expect(status_code).to be_a(Integer)
591
+ expect(status_code).to be == 404
592
+ end
593
+
594
+
595
+ end
596
+
597
+ describe 'Module Notification' do
598
+
599
+ it "enable notification for host 'c1-mysql-1'" do
600
+ h = @icinga2.enable_host_notification( 'c1-mysql-1' )
601
+
602
+ status_code = h['code']
603
+ expect(h).to be_a(Hash)
604
+ expect(status_code).to be == 200
605
+ end
606
+
607
+ it "enable notification for host 'c1-mysql-1' and all services" do
608
+ h = @icinga2.enable_service_notification( 'c1-mysql-1' )
609
+ expect(h).to be_a(Hash)
610
+ status_code = h['code']
611
+ expect(status_code).to be == 200
612
+ end
613
+
614
+ it "disable notification for host 'c1-web-1' and all services" do
615
+ h = @icinga2.disable_service_notification( 'c1-web-1' )
616
+ expect(h).to be_a(Hash)
617
+ status_code = h['code']
618
+ expect(status_code).to be == 200
619
+ end
620
+
621
+
622
+ it 'list notifications' do
623
+ h = @icinga2.notifications
624
+ expect(h).to be_a(Array)
625
+ expect(h.count).to be_a(Integer)
626
+ end
627
+
628
+ it 'enable Notifications for hostgroup' do
629
+ h = @icinga2.enable_hostgroup_notification('icinga-satellites')
630
+ expect(h).to be_a(Hash)
631
+ status_code = h['code']
632
+ expect(status_code).to be == 200
633
+ end
634
+
635
+ it 'disable Notifications for hostgroup' do
636
+ h = @icinga2.disable_hostgroup_notification('icinga-satellites')
637
+ expect(h).to be_a(Hash)
638
+ status_code = h['code']
639
+ expect(status_code).to be == 200
640
+ end
641
+
642
+ end
643
+
644
+ describe 'Module Users' do
645
+
646
+ it 'list all users' do
647
+ h = @icinga2.users
648
+ expect(h).to be_a(Array)
649
+ expect(h.count).to be >= 1
650
+ end
651
+
652
+ it 'list named users' do
653
+ h = @icinga2.users('icingaadmin')
654
+ expect(h).to be_a(Array)
655
+ expect(h.count).to be == 1
656
+ end
657
+
658
+ it 'exists user' do
659
+ expect(@icinga2.exists_user?('icingaadmin')).to be_truthy
660
+ end
661
+
662
+ it 'exists user' do
663
+ expect(@icinga2.exists_user?('icinga-admin')).to be_falsey
664
+ end
665
+
666
+ it 'add user with multiple and not existing groups' do
667
+ h = @icinga2.add_user(
668
+ user_name: 'foo',
669
+ display_name: 'FOO',
670
+ email: 'foo@bar.com',
671
+ pager: '0000',
672
+ groups: ['non-existing-group', 'icingaadmins', 'icinga-admins']
673
+ )
674
+ expect(h).to be_a(Hash)
675
+ status_code = h['code']
676
+ expect(status_code).to be == 404
677
+ end
678
+
679
+ it 'add user \'foo\'' do
680
+ h = @icinga2.add_user(
681
+ user_name: 'foo',
682
+ display_name: 'FOO',
683
+ email: 'foo@bar.com',
684
+ pager: '0000',
685
+ groups: ['icingaadmins']
686
+ )
687
+ expect(h).to be_a(Hash)
688
+ status_code = h['code']
689
+ expect(status_code).to be == 200
690
+ end
691
+
692
+ it 'add user \'foo\' (again)' do
693
+ h = @icinga2.add_user(
694
+ user_name: 'foo',
695
+ display_name: 'FOO',
696
+ email: 'foo@bar.com',
697
+ pager: '0000',
698
+ groups: ['icingaadmins']
699
+ )
700
+ expect(h).to be_a(Hash)
701
+ status_code = h['code']
702
+ expect(status_code).to be == 500
703
+ end
704
+
705
+ it 'list named users' do
706
+ h = @icinga2.users('foo')
707
+ expect(h).to be_a(Array)
708
+ expect(h.count).to be == 1
709
+ end
710
+
711
+ it 'delete user' do
712
+ h = @icinga2.delete_user('foo')
713
+
714
+ expect(h).to be_a(Hash)
715
+ status_code = h['code']
716
+ expect(status_code).to be == 200
717
+ end
718
+
719
+ it 'delete user (again)' do
720
+ h = @icinga2.delete_user('foo')
721
+
722
+ expect(h).to be_a(Hash)
723
+ status_code = h['code']
724
+ expect(status_code).to be == 404
725
+ end
726
+ end
727
+
728
+ describe 'Module Usergroups' do
729
+
730
+ it 'list all usergroups' do
731
+ h = @icinga2.usergroups
732
+ expect(h).to be_a(Array)
733
+ expect(h.count).to be >= 1
734
+ end
735
+
736
+ it 'list named usergroup' do
737
+ h = @icinga2.usergroups('icingaadmins')
738
+ expect(h).to be_a(Array)
739
+ expect(h.count).to be == 1
740
+ end
741
+
742
+ it 'exists usergroup' do
743
+ expect(@icinga2.exists_usergroup?('icingaadmins')).to be_truthy
744
+ end
745
+
746
+ it 'exists usergroup' do
747
+ expect(@icinga2.exists_usergroup?('linux-admin')).to be_falsey
748
+ end
749
+
750
+ it 'add usergroup' do
751
+ h = @icinga2.add_usergroup(user_group: 'foo', display_name: 'FOO' )
752
+ expect(h).to be_a(Hash)
753
+ status_code = h['code']
754
+ expect(status_code).to be == 200
755
+ end
756
+
757
+ it 'add usergroup (again)' do
758
+ h = @icinga2.add_usergroup(user_group: 'foo', display_name: 'FOO' )
759
+ expect(h).to be_a(Hash)
760
+ status_code = h['code']
761
+ expect(status_code).to be == 500
762
+ end
763
+
764
+ it 'list named usergroup' do
765
+ h = @icinga2.usergroups( 'foo' )
766
+ expect(h).to be_a(Array)
767
+ expect(h.count).to be >= 1
768
+ end
769
+
770
+ it 'delete usergroup' do
771
+ h = @icinga2.delete_usergroup( 'foo' )
772
+
773
+ expect(h).to be_a(Hash)
774
+ status_code = h['code']
775
+ expect(status_code).to be == 200
776
+ end
777
+
778
+ it 'delete usergroup (again)' do
779
+ h = @icinga2.delete_usergroup('foo')
780
+
781
+ expect(h).to be_a(Hash)
782
+ status_code = h['code']
783
+ expect(status_code).to be == 404
784
+ end
785
+ end
786
+
787
+ describe 'Module Downtimes' do
788
+
789
+ it "add downtime 'test'" do
790
+
791
+ h = @icinga2.add_downtime(
792
+ type: 'service',
793
+ host_name: 'c1-mysql-1',
794
+ comment: 'test downtime',
795
+ author: 'icingaadmin',
796
+ start_time: Time.now.to_i,
797
+ end_time: Time.now.to_i + 240
798
+ )
799
+ status_code = h['code']
800
+ expect(h).to be_a(Hash)
801
+ expect(status_code).to be == 200
802
+ end
803
+
804
+ it 'list downtimes' do
805
+ h = @icinga2.downtimes
806
+ expect(h).to be_a(Array)
807
+ expect(h.count).to be >= 1
808
+ end
809
+
810
+ it 'remove_downtime' do
811
+ h = @icinga2.remove_downtime(
812
+ filter: '"host.name == filterHost && !service && downtime.author == filterAuthor"',
813
+ filter_vars: { filterHost: 'c1-mysql-1', filterAuthor: 'icingaadmin' }
814
+ )
815
+ expect(h).to be_a(Hash)
816
+ status_code = h['code']
817
+ expect(status_code).to be == 200
818
+ end
819
+
820
+
821
+ end
822
+
823
+ describe 'Configuration Management' do
824
+
825
+ it 'create config package \'cfg_spec-test\' - (successful)' do
826
+ r = @icinga2.create_config_package('cfg_spec-test')
827
+ expect(r).to be_a(Hash)
828
+ status_code = r['code']
829
+ expect(status_code).to be_a(Integer)
830
+ expect(status_code).to be == 200
831
+ end
832
+
833
+ it 'create config package \'cfg_spec-test\' - (failed)' do
834
+ r = @icinga2.create_config_package('_cfg_spec-test')
835
+ expect(r).to be_a(Hash)
836
+ status_code = r['code']
837
+ expect(status_code).to be_a(Integer)
838
+ expect(status_code).to be == 404
839
+ end
840
+
841
+ it 'list config package' do
842
+ r = @icinga2.list_config_packages
843
+
844
+ expect(r).to be_a(Hash)
845
+ code = r['code']
846
+ results = r['results']
847
+ expect(code).to be_a(Integer)
848
+ expect(code).to be == 200
849
+ expect(results).to be_a(Array)
850
+ expect(results.count).to be >= 1
851
+ end
852
+
853
+ it 'upload local config package' do
854
+
855
+ params = {
856
+ package: 'cfg_spec-test',
857
+ name: 'host1.conf',
858
+ cluster: false,
859
+ vars: 'object Host "cmdb-host" { chec_command = "dummy" }',
860
+ reload: false
861
+ }
862
+
863
+ r = @icinga2.upload_config_package(params)
864
+ code = r['code']
865
+ status = r['status']
866
+ expect(code).to be_a(Integer)
867
+ expect(code).to be == 200
868
+ expect(status).to be_a(String)
869
+ end
870
+
871
+ it 'fetch local config stages' do
872
+
873
+ packages = @icinga2.list_config_packages
874
+ stages = packages['results']
875
+ data = stages.select { |k,_v| k['name'] == 'cfg_spec-test' }
876
+ stages = data.first['stages']
877
+
878
+ params = {
879
+ package: 'cfg_spec-test',
880
+ stage: stages.last,
881
+ name: 'host1',
882
+ cluster: false
883
+ }
884
+ r = @icinga2.fetch_config_stages(params)
885
+ expect(r).to be_a(String)
886
+ end
887
+
888
+ it 'upload cluser config package' do
889
+
890
+ params = {
891
+ package: 'cfg_spec-test',
892
+ name: 'host2',
893
+ cluster: true,
894
+ vars: 'object Host "satellite-host" { address = "192.168.1.100", check_command = "hostalive" }',
895
+ reload: false
896
+ }
897
+
898
+ r = @icinga2.upload_config_package(params)
899
+ code = r['code']
900
+ status = r['status']
901
+ expect(code).to be_a(Integer)
902
+ expect(code).to be == 200
903
+ expect(status).to be_a(String)
904
+ end
905
+
906
+ it 'list config stages' do
907
+
908
+ packages = @icinga2.list_config_packages
909
+ stages = packages['results']
910
+ data = stages.select { |k,_v| k['name'] == 'cfg_spec-test' }
911
+ stages = data.first['stages']
912
+
913
+ params = {
914
+ package: 'cfg_spec-test',
915
+ stage: stages.last
916
+ }
917
+
918
+ r = @icinga2.list_config_stages(params)
919
+
920
+ expect(r).to be_a(Hash)
921
+ code = r['code']
922
+ results = r['results']
923
+ expect(code).to be_a(Integer)
924
+ expect(code).to be == 200
925
+ expect(results).to be_a(Array)
926
+ end
927
+
928
+ it 'package stage errors' do
929
+ packages = @icinga2.list_config_packages
930
+ stages = packages['results']
931
+ data = stages.select { |k,_v| k['name'] == 'cfg_spec-test' }
932
+ stages = data.first['stages']
933
+
934
+ params = {
935
+ package: 'cfg_spec-test',
936
+ stage: stages.first,
937
+ cluster: true
938
+ }
939
+
940
+ r = @icinga2.package_stage_errors(params)
941
+ expect(r).to be_a(String)
942
+ end
943
+
944
+ it 'fetch cluster config stages' do
945
+
946
+ packages = @icinga2.list_config_packages
947
+ stages = packages['results']
948
+ data = stages.select { |k,_v| k['name'] == 'cfg_spec-test' }
949
+ stages = data.first['stages']
950
+
951
+ params = {
952
+ package: 'cfg_spec-test',
953
+ stage: stages.last,
954
+ name: 'host2',
955
+ cluster: true
956
+ }
957
+
958
+ r = @icinga2.fetch_config_stages(params)
959
+ expect(r).to be_a(String)
960
+ end
961
+
962
+ it 'remove config stage' do
963
+
964
+ packages = @icinga2.list_config_packages
965
+ stages = packages['results']
966
+ data = stages.select { |k,_v| k['name'] == 'cfg_spec-test' }
967
+ stages = data.first['stages']
968
+
969
+ params = {
970
+ package: 'cfg_spec-test',
971
+ stage: stages.last
972
+ }
973
+
974
+ r = @icinga2.remove_config_stage(params)
975
+ expect(r).to be_a(Hash)
976
+ code = r['code']
977
+ status = r['status']
978
+ expect(code).to be_a(Integer)
979
+ expect(code).to be == 200
980
+ expect(status).to be_a(String)
981
+ end
982
+
983
+ it 'remove config package' do
984
+ r = @icinga2.remove_config_package( 'cfg_spec-test' )
985
+ expect(r).to be_a(Hash)
986
+ code = r['code']
987
+ status = r['status']
988
+ expect(code).to be_a(Integer)
989
+ expect(code).to be == 200
990
+ expect(status).to be_a(String)
991
+ end
992
+ end
993
+
994
+
995
+ describe 'Actions' do
996
+
997
+ it 'restart process' do
998
+ r = @icinga2.restart_process
999
+ expect(r).to be_a(Hash)
1000
+ code = r['code']
1001
+ expect(code).to be == 200
1002
+ end
1003
+
1004
+ it 'shutdown process' do
1005
+ r = @icinga2.shutdown_process
1006
+ expect(r).to be_a(Hash)
1007
+ code = r['code']
1008
+ expect(code).to be == 200
1009
+ end
1010
+ end
1011
+
1012
+
1013
+ end