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.
@@ -10,7 +10,6 @@ module Icinga2
10
10
  # return statistic data for latency and execution_time
11
11
  #
12
12
  # @example
13
- # @icinga.cib_data
14
13
  # latency, execution_time = @icinga.average_statistics.values
15
14
  #
16
15
  # h = @icinga.average_statistics
@@ -21,6 +20,9 @@ module Icinga2
21
20
  # * execution_time (Float)
22
21
  #
23
22
  def average_statistics
23
+
24
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
25
+
24
26
  avg_latency = @avg_latency.nil? ? 0 : @avg_latency
25
27
  avg_execution_time = @avg_execution_time.nil? ? 0 : @avg_execution_time
26
28
 
@@ -33,7 +35,6 @@ module Icinga2
33
35
  # return statistic data for intervall data
34
36
  #
35
37
  # @example
36
- # @icinga.cib_data
37
38
  # hosts_active_checks, hosts_passive_checks, services_active_checks, services_passive_checks = @icinga.interval_statistics.values
38
39
  #
39
40
  # i = @icinga.interval_statistics
@@ -47,6 +48,8 @@ module Icinga2
47
48
  #
48
49
  def interval_statistics
49
50
 
51
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
52
+
50
53
  # take a look into https://github.com/Icinga/pkg-icinga2-debian/blob/master/lib/icinga/cib.cpp
51
54
 
52
55
  hosts_active_checks = @hosts_active_checks_1min.nil? ? 0 : @hosts_active_checks_1min
@@ -65,7 +68,6 @@ module Icinga2
65
68
  # return statistic data for services
66
69
  #
67
70
  # @example
68
- # @icinga.cib_data
69
71
  # ok, warning, critical, unknown, pending, in_downtime, ack = @icinga.service_statistics.values
70
72
  #
71
73
  # s = @icinga.service_statistics
@@ -82,6 +84,8 @@ module Icinga2
82
84
  #
83
85
  def service_statistics
84
86
 
87
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
88
+
85
89
  services_ok = @services_ok.nil? ? 0 : @services_ok
86
90
  services_warning = @services_warning.nil? ? 0 : @services_warning
87
91
  services_critical = @services_critical.nil? ? 0 : @services_critical
@@ -104,7 +108,6 @@ module Icinga2
104
108
  # return statistic data for hosts
105
109
  #
106
110
  # @example
107
- # @icinga.cib_data
108
111
  # up, down, pending, unreachable, in_downtime, ack = @icinga.host_statistics.values
109
112
  #
110
113
  # h = @icinga.host_statistics
@@ -120,6 +123,8 @@ module Icinga2
120
123
  #
121
124
  def host_statistics
122
125
 
126
+ cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
127
+
123
128
  hosts_up = @hosts_up.nil? ? 0 : @hosts_up
124
129
  hosts_down = @hosts_down.nil? ? 0 : @hosts_down
125
130
  hosts_pending = @hosts_pending.nil? ? 0 : @hosts_pending
@@ -148,7 +153,7 @@ module Icinga2
148
153
  def work_queue_statistics
149
154
 
150
155
  stats = {}
151
- data = Network.api_data(
156
+ data = api_data(
152
157
  url: format( '%s/status', @icinga_api_url_base ),
153
158
  headers: @headers,
154
159
  options: @options
@@ -156,12 +161,11 @@ module Icinga2
156
161
 
157
162
  return stats if data.nil?
158
163
 
159
- if( data.dig(:status).nil? )
160
- results = data.dig('results')
164
+ if( data.is_a?(Array) )
161
165
 
162
- json_rpc_data = results.find { |k| k['name'] == 'ApiListener' }
163
- graphite_data = results.find { |k| k['name'] == 'GraphiteWriter' }
164
- ido_mysql_data = results.find { |k| k['name'] == 'IdoMysqlConnection' }
166
+ json_rpc_data = data.find { |k| k['name'] == 'ApiListener' }
167
+ graphite_data = data.find { |k| k['name'] == 'GraphiteWriter' }
168
+ ido_mysql_data = data.find { |k| k['name'] == 'IdoMysqlConnection' }
165
169
 
166
170
  json_rpc_data = json_rpc_data.dig('status', 'api', 'json_rpc') unless( json_rpc_data.nil? )
167
171
  graphite_data = graphite_data.dig('status', 'graphitewriter', 'graphite') unless( graphite_data.nil? )
@@ -14,7 +14,7 @@ module Icinga2
14
14
  # @return [Bool]
15
15
  #
16
16
  def object_has_been_checked?( object )
17
- object.dig('attrs', 'last_check').positive?
17
+ object.dig('attrs', 'last_check') > 0
18
18
  end
19
19
 
20
20
  # parse version string and extract version and revision
@@ -19,8 +19,8 @@ module Icinga2
19
19
  #
20
20
  def add_usergroup( params )
21
21
 
22
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
23
- raise ArgumentError.new('missing params') if( params.size.zero? )
22
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
23
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
24
24
 
25
25
  user_group = params.dig(:user_group)
26
26
  display_name = params.dig(:display_name)
@@ -33,7 +33,7 @@ module Icinga2
33
33
  }
34
34
  }
35
35
 
36
- Network.put(
36
+ put(
37
37
  url: format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group ),
38
38
  headers: @headers,
39
39
  options: @options,
@@ -53,14 +53,14 @@ module Icinga2
53
53
  #
54
54
  def delete_usergroup( params )
55
55
 
56
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
57
- raise ArgumentError.new('missing params') if( params.size.zero? )
56
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
57
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
58
58
 
59
59
  user_group = params.dig(:user_group)
60
60
 
61
61
  raise ArgumentError.new('Missing user_group') if( user_group.nil? )
62
62
 
63
- Network.delete(
63
+ delete(
64
64
  url: format( '%s/objects/usergroups/%s?cascade=1', @icinga_api_url_base, user_group ),
65
65
  headers: @headers,
66
66
  options: @options
@@ -91,15 +91,11 @@ module Icinga2
91
91
  format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group )
92
92
  end
93
93
 
94
- data = Network.api_data(
94
+ api_data(
95
95
  url: url,
96
96
  headers: @headers,
97
97
  options: @options
98
98
  )
99
-
100
- return data.dig('results') if( data.dig(:status).nil? )
101
-
102
- nil
103
99
  end
104
100
 
105
101
  # returns true if the usergroup exists
@@ -113,15 +109,16 @@ module Icinga2
113
109
  #
114
110
  def exists_usergroup?( user_group )
115
111
 
116
- raise ArgumentError.new('only String are allowed') unless( user_group.is_a?(String) )
117
- raise ArgumentError.new('Missing user_group') if( user_group.size.zero? )
112
+ raise ArgumentError.new(format('wrong type. \'user_group\' must be an String, given \'%s\'', user_group.class.to_s)) unless( user_group.is_a?(String) )
113
+ raise ArgumentError.new('Missing \'user_group\'') if( user_group.size.zero? )
118
114
 
119
115
  result = usergroups( user_group: user_group )
120
116
  result = JSON.parse( result ) if result.is_a?( String )
117
+ result = result.first if( result.is_a?(Array) )
121
118
 
122
- return true if !result.nil? && result.is_a?(Array)
119
+ return false if( result.is_a?(Hash) && result.dig('code') == 404 )
123
120
 
124
- false
121
+ true
125
122
  end
126
123
 
127
124
  end
@@ -23,8 +23,8 @@ module Icinga2
23
23
  #
24
24
  def add_user( params )
25
25
 
26
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
27
- raise ArgumentError.new('missing params') if( params.size.zero? )
26
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
27
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
28
28
 
29
29
  user_name = params.dig(:user_name)
30
30
  display_name = params.dig(:display_name)
@@ -64,7 +64,7 @@ module Icinga2
64
64
  }
65
65
  end
66
66
 
67
- Network.put(
67
+ put(
68
68
  url: format( '%s/objects/users/%s', @icinga_api_url_base, user_name ),
69
69
  headers: @headers,
70
70
  options: @options,
@@ -84,14 +84,14 @@ module Icinga2
84
84
  #
85
85
  def delete_user( params )
86
86
 
87
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
88
- raise ArgumentError.new('missing params') if( params.size.zero? )
87
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
88
+ raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
89
89
 
90
90
  user_name = params.dig(:user_name)
91
91
 
92
92
  raise ArgumentError.new('Missing user_name') if( user_name.nil? )
93
93
 
94
- Network.delete(
94
+ delete(
95
95
  url: format( '%s/objects/users/%s?cascade=1', @icinga_api_url_base, user_name ),
96
96
  headers: @headers,
97
97
  options: @options
@@ -109,10 +109,12 @@ module Icinga2
109
109
  # @example to get one user
110
110
  # @icinga.users(user_name: 'icingaadmin')
111
111
  #
112
- # @return [Hash] returns a hash
112
+ # @return [Array]
113
113
  #
114
114
  def users( params = {} )
115
115
 
116
+ raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
117
+
116
118
  user_name = params.dig(:user_name)
117
119
 
118
120
  url =
@@ -122,15 +124,11 @@ module Icinga2
122
124
  format( '%s/objects/users/%s', @icinga_api_url_base, user_name )
123
125
  end
124
126
 
125
- data = Network.api_data(
127
+ api_data(
126
128
  url: url,
127
129
  headers: @headers,
128
130
  options: @options
129
131
  )
130
-
131
- return data.dig('results') if( data.dig(:status).nil? )
132
-
133
- nil
134
132
  end
135
133
 
136
134
  # checks if the user exists
@@ -144,15 +142,16 @@ module Icinga2
144
142
  #
145
143
  def exists_user?( user_name )
146
144
 
147
- raise ArgumentError.new('only String are allowed') unless( user_name.is_a?(String) )
148
- raise ArgumentError.new('Missing user_name') if( user_name.size.zero? )
145
+ raise ArgumentError.new(format('wrong type. \'user_name\' must be an String, given \'%s\'', user_name.class.to_s)) unless( user_name.is_a?(String) )
146
+ raise ArgumentError.new('Missing \'user_name\'') if( user_name.size.zero? )
149
147
 
150
148
  result = users( user_name: user_name )
151
- result = JSON.parse( result ) if( result.is_a?(String) )
149
+ result = JSON.parse( result ) if result.is_a?( String )
150
+ result = result.first if( result.is_a?(Array) )
152
151
 
153
- return true if( !result.nil? && result.is_a?(Array) )
152
+ return false if( result.is_a?(Hash) && result.dig('code') == 404 )
154
153
 
155
- false
154
+ true
156
155
  end
157
156
 
158
157
  end
@@ -3,21 +3,7 @@
3
3
 
4
4
  module Icinga2
5
5
 
6
- # namespace for version information
7
- module Version
8
-
9
- # major part of version
10
- MAJOR = 0
11
- # minor part of version
12
- MINOR = 9
13
- # tiny part of version
14
- TINY = 0
15
- # patch part
16
- PATCH = 1
17
-
18
- end
19
-
20
6
  # Current version of gem.
21
- VERSION = [Version::MAJOR, Version::MINOR, Version::TINY, Version::PATCH].compact * '.'
7
+ VERSION = '0.9.2.1' # [Version::MAJOR, Version::MINOR, Version::TINY, Version::PATCH].compact * '.'
22
8
 
23
9
  end
@@ -0,0 +1,89 @@
1
+
2
+ # -----------------------------------------------------------------------------
3
+ # Monkey patches
4
+
5
+ # Modify `Object` (https://gist.github.com/Integralist/9503099)
6
+
7
+ # None of the above solutions work with a multi-level hash
8
+ # They only work on the first level: {:foo=>"bar", :level1=>{"level2"=>"baz"}}
9
+ # The following two variations solve the problem in the same way
10
+ # transform hash keys to symbols
11
+ # multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } }
12
+ # multi_hash = multi_hash.deep_string_keys
13
+
14
+ class Object
15
+
16
+ def deep_symbolize_keys
17
+
18
+ if( is_a?( Hash ) )
19
+ return inject({}) do |memo, (k, v)|
20
+ memo.tap { |m| m[k.to_sym] = v.deep_string_keys }
21
+ end
22
+ elsif( is_a?( Array ) )
23
+ return map(&:deep_string_keys)
24
+ end
25
+
26
+ self
27
+ end
28
+
29
+ def deep_string_keys
30
+
31
+ if( is_a?( Hash ) )
32
+ return inject({}) do |memo, (k, v)|
33
+ memo.tap { |m| m[k.to_s] = v.deep_string_keys }
34
+ end
35
+ elsif( is_a?( Array ) )
36
+ return map(&:deep_string_keys)
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ end
43
+
44
+ # -----------------------------------------------------------------------------
45
+
46
+ class Array
47
+ def compare( comparate )
48
+ to_set == comparate.to_set
49
+ end
50
+ end
51
+
52
+ # -----------------------------------------------------------------------------
53
+
54
+ # filter hash
55
+ # example:
56
+ # tags = [ 'foo', 'bar', 'fii' ]
57
+ # useableTags = tags.filter( 'fii' )
58
+
59
+ class Hash
60
+ def filter( *args )
61
+ if( args.size == 1 )
62
+ args[0] = args[0].to_s if args[0].is_a?( Symbol )
63
+ select { |key| key.to_s.match( args.first ) }
64
+ else
65
+ select { |key| args.include?( key ) }
66
+ end
67
+ end
68
+ end
69
+
70
+ # -----------------------------------------------------------------------------
71
+
72
+ class Time
73
+ def add_minutes(m)
74
+ self + (60 * m)
75
+ end
76
+ end
77
+
78
+ # -----------------------------------------------------------------------------
79
+
80
+ # https://stackoverflow.com/questions/3028243/check-if-ruby-object-is-a-boolean/3028378#3028378
81
+
82
+ module Boolean; end
83
+ class TrueClass; include Boolean; end
84
+ class FalseClass; include Boolean; end
85
+
86
+ true.is_a?(Boolean) #=> true
87
+ false.is_a?(Boolean) #=> true
88
+
89
+ # -----------------------------------------------------------------------------
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: icinga2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.1
4
+ version: 0.9.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bodo Schulz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
14
+ name: ruby_dig
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: openssl
28
+ name: rest-client
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
@@ -150,7 +150,7 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- description: Ruby Class for the Icinga2 API
153
+ description: An enhanced ruby gem to communicate with Icinga2 API
154
154
  email: bodo@boone-schulz.de
155
155
  executables: []
156
156
  extensions: []
@@ -186,8 +186,20 @@ files:
186
186
  - doc/top-level-namespace.html
187
187
  - doc/usergroups.md
188
188
  - doc/users.md
189
+ - examples/_blank.rb
190
+ - examples/downtimes.rb
191
+ - examples/hostgroups.rb
192
+ - examples/hosts.rb
193
+ - examples/informations.rb
194
+ - examples/notifications.rb
195
+ - examples/servicegroups.rb
196
+ - examples/services.rb
197
+ - examples/statistics.rb
189
198
  - examples/test.rb
199
+ - examples/usergroups.rb
200
+ - examples/users.rb
190
201
  - lib/icinga2.rb
202
+ - lib/icinga2/client.rb
191
203
  - lib/icinga2/converts.rb
192
204
  - lib/icinga2/downtimes.rb
193
205
  - lib/icinga2/hostgroups.rb
@@ -203,6 +215,7 @@ files:
203
215
  - lib/icinga2/users.rb
204
216
  - lib/icinga2/version.rb
205
217
  - lib/logging.rb
218
+ - lib/monkey_patches.rb
206
219
  homepage: https://github.com/bodsch/ruby-icinga2
207
220
  licenses:
208
221
  - LGPL-2.1+
@@ -215,7 +228,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
215
228
  requirements:
216
229
  - - "~>"
217
230
  - !ruby/object:Gem::Version
218
- version: '2.3'
231
+ version: '2.0'
219
232
  required_rubygems_version: !ruby/object:Gem::Requirement
220
233
  requirements:
221
234
  - - ">="