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.
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # 07.10.2017 - Bodo Schulz
5
+ #
6
+ #
7
+ # Examples for Hostgroups
8
+
9
+ # -----------------------------------------------------------------------------
10
+
11
+ require_relative '../lib/icinga2'
12
+
13
+ # -----------------------------------------------------------------------------
14
+
15
+ icinga_host = ENV.fetch( 'ICINGA_HOST' , 'icinga2' )
16
+ icinga_api_port = ENV.fetch( 'ICINGA_API_PORT' , 5665 )
17
+ icinga_api_user = ENV.fetch( 'ICINGA_API_USER' , 'admin' )
18
+ icinga_api_pass = ENV.fetch( 'ICINGA_API_PASSWORD' , nil )
19
+ icinga_api_pki_path = ENV.fetch( 'ICINGA_API_PKI_PATH' , '/etc/icinga2' )
20
+ icinga_api_node_name = ENV.fetch( 'ICINGA_API_NODE_NAME' , nil )
21
+ icinga_cluster = ENV.fetch( 'ICINGA_CLUSTER' , false )
22
+ icinga_satellite = ENV.fetch( 'ICINGA_CLUSTER_SATELLITE', nil )
23
+
24
+
25
+ # convert string to bool
26
+ icinga_cluster = icinga_cluster.to_s.eql?('true') ? true : false
27
+
28
+ config = {
29
+ icinga: {
30
+ host: icinga_host,
31
+ api: {
32
+ port: icinga_api_port,
33
+ user: icinga_api_user,
34
+ password: icinga_api_pass,
35
+ pki_path: icinga_api_pki_path,
36
+ node_name: icinga_api_node_name
37
+ },
38
+ cluster: icinga_cluster,
39
+ satellite: icinga_satellite
40
+ }
41
+ }
42
+
43
+ # ---------------------------------------------------------------------------------------
44
+
45
+ i = Icinga2::Client.new( config )
46
+
47
+ unless( i.nil? )
48
+
49
+ # run tests ...
50
+ #
51
+ #
52
+
53
+ begin
54
+
55
+ puts ' ------------------------------------------------------------- '
56
+ puts ''
57
+
58
+ puts ' ==> USERGROUPS'
59
+ puts ''
60
+
61
+ ['icingaadmins', 'linux-admins'].each do |h|
62
+ e = i.exists_usergroup?( h ) ? 'true' : 'false'
63
+ puts format( '= check if Usergroup \'%s\' exists : %s', h, e )
64
+ end
65
+ puts ''
66
+
67
+ puts '= add Usergroup \'foo\''
68
+ puts i.add_usergroup( user_group: 'foo', display_name: 'FOO' )
69
+ puts ''
70
+
71
+ puts 'list named Usergroup \'foo\''
72
+ puts i.usergroups( user_group: 'foo' )
73
+ puts ''
74
+
75
+ puts '= delete Usergroup \'foo\''
76
+ puts i.delete_usergroup( user_group: 'foo' )
77
+ puts ''
78
+
79
+
80
+ puts 'list all Usergroup'
81
+ puts i.usergroups
82
+ puts ''
83
+ puts ' ------------------------------------------------------------- '
84
+ puts ''
85
+
86
+ rescue => e
87
+ $stderr.puts( e )
88
+ $stderr.puts( e.backtrace.join("\n") )
89
+ end
90
+ end
91
+
92
+
93
+ # -----------------------------------------------------------------------------
94
+
95
+ # EOF
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # 07.10.2017 - Bodo Schulz
5
+ #
6
+ #
7
+ # Examples for Hostgroups
8
+
9
+ # -----------------------------------------------------------------------------
10
+
11
+ require_relative '../lib/icinga2'
12
+
13
+ # -----------------------------------------------------------------------------
14
+
15
+ icinga_host = ENV.fetch( 'ICINGA_HOST' , 'icinga2' )
16
+ icinga_api_port = ENV.fetch( 'ICINGA_API_PORT' , 5665 )
17
+ icinga_api_user = ENV.fetch( 'ICINGA_API_USER' , 'admin' )
18
+ icinga_api_pass = ENV.fetch( 'ICINGA_API_PASSWORD' , nil )
19
+ icinga_api_pki_path = ENV.fetch( 'ICINGA_API_PKI_PATH' , '/etc/icinga2' )
20
+ icinga_api_node_name = ENV.fetch( 'ICINGA_API_NODE_NAME' , nil )
21
+ icinga_cluster = ENV.fetch( 'ICINGA_CLUSTER' , false )
22
+ icinga_satellite = ENV.fetch( 'ICINGA_CLUSTER_SATELLITE', nil )
23
+
24
+
25
+ # convert string to bool
26
+ icinga_cluster = icinga_cluster.to_s.eql?('true') ? true : false
27
+
28
+ config = {
29
+ icinga: {
30
+ host: icinga_host,
31
+ api: {
32
+ port: icinga_api_port,
33
+ user: icinga_api_user,
34
+ password: icinga_api_pass,
35
+ pki_path: icinga_api_pki_path,
36
+ node_name: icinga_api_node_name
37
+ },
38
+ cluster: icinga_cluster,
39
+ satellite: icinga_satellite
40
+ }
41
+ }
42
+
43
+ # ---------------------------------------------------------------------------------------
44
+
45
+ i = Icinga2::Client.new( config )
46
+
47
+ unless( i.nil? )
48
+
49
+ # run tests ...
50
+ #
51
+ #
52
+
53
+ begin
54
+
55
+ puts ' ------------------------------------------------------------- '
56
+ puts ''
57
+
58
+ puts ' ==> USERS'
59
+ puts ''
60
+
61
+ ['icingaadmin', 'icinga-admin'].each do |h|
62
+ e = i.exists_user?( h ) ? 'true' : 'false'
63
+ puts format( '= check if User \'%s\' exists : %s', h, e )
64
+ end
65
+ puts ''
66
+
67
+ puts '= add User \'foo\''
68
+ puts i.add_user( user_name: 'foo', display_name: 'FOO', email: 'foo@bar.com', pager: '0000', groups: ['icingaadmins'] )
69
+ puts ''
70
+ puts '= add User \'foo\' (again)'
71
+ puts i.add_user( user_name: 'foo', display_name: 'FOO', email: 'foo@bar.com', pager: '0000', groups: ['icingaadmins'] )
72
+ puts ''
73
+
74
+ puts '= list named User \'foo\''
75
+ puts i.users( user_name: 'foo' )
76
+ puts ''
77
+
78
+ puts '= delete User \'foo\''
79
+ puts i.delete_user( user_name: 'foo' )
80
+ puts ''
81
+
82
+ puts '= list all User'
83
+ puts i.users
84
+ puts ''
85
+
86
+ puts ' ------------------------------------------------------------- '
87
+ puts ''
88
+
89
+ rescue => e
90
+ $stderr.puts( e )
91
+ $stderr.puts( e.backtrace.join("\n") )
92
+ end
93
+ end
94
+
95
+
96
+ # -----------------------------------------------------------------------------
97
+
98
+ # EOF
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ruby_dig' if RUBY_VERSION < '2.3'
4
+
3
5
  require 'rest-client'
4
6
  require 'openssl'
5
7
 
@@ -8,400 +10,8 @@ require 'net/http'
8
10
  require 'uri'
9
11
 
10
12
  require_relative 'logging'
11
- require_relative 'icinga2/version'
12
- require_relative 'icinga2/network'
13
- require_relative 'icinga2/statistics'
14
- require_relative 'icinga2/converts'
15
- require_relative 'icinga2/tools'
16
- require_relative 'icinga2/downtimes'
17
- require_relative 'icinga2/notifications'
18
- require_relative 'icinga2/hosts'
19
- require_relative 'icinga2/hostgroups'
20
- require_relative 'icinga2/services'
21
- require_relative 'icinga2/servicegroups'
22
- require_relative 'icinga2/users'
23
- require_relative 'icinga2/usergroups'
13
+ require_relative 'monkey_patches'
14
+ require_relative 'icinga2/client'
24
15
 
25
- # -------------------------------------------------------------------------------------------------------------------
26
- #
27
- # @abstract # Namespace for classes and modules that handle all Icinga2 API calls
28
- #
29
- # @author Bodo Schulz <bodo@boone-schulz.de>
30
- #
31
- #
32
16
  module Icinga2
33
-
34
- # static variable for hosts down
35
- HOSTS_DOWN = 1
36
- # static variable for hosts critical
37
- HOSTS_CRITICAL = 2
38
- # static variable for hosts unknown
39
- HOSTS_UNKNOWN = 3
40
-
41
- # static variables for handled warning
42
- SERVICE_STATE_WARNING = 1
43
- # static variables for handled critical
44
- SERVICE_STATE_CRITICAL = 2
45
- # static variables for handled unknown
46
- SERVICE_STATE_UNKNOWN = 3
47
-
48
- # Abstract base class for the API calls.
49
- # Provides some helper methods
50
- #
51
- # @author Bodo Schulz
52
- #
53
- class Client
54
-
55
- include Logging
56
-
57
- include Icinga2::Version
58
- include Icinga2::Network
59
- include Icinga2::Statistics
60
- include Icinga2::Converts
61
- include Icinga2::Tools
62
- include Icinga2::Downtimes
63
- include Icinga2::Notifications
64
- include Icinga2::Hosts
65
- include Icinga2::Hostgroups
66
- include Icinga2::Services
67
- include Icinga2::Servicegroups
68
- include Icinga2::Users
69
- include Icinga2::Usergroups
70
-
71
- # Returns a new instance of Client
72
- #
73
- # @param [Hash, #read] settings the settings for Icinga2
74
- # @option settings [String] :host ('localhost') the Icinga2 Hostname
75
- # @option settings [Integer] :port (5665) the Icinga2 API Port
76
- # @option settings [String] :user the Icinga2 API User
77
- # @option settings [String] :password the Icinga2 API Password
78
- # @option settings [Integer] :version (1) the Icinga2 API Version
79
- # @option settings [Bool] :cluster Icinga2 Cluster Mode
80
- # @option settings [Bool] :notifications (false) enable Icinga2 Host Notifications
81
- #
82
- # @example to create an new Instance
83
- # config = {
84
- # icinga: {
85
- # host: '192.168.33.5',
86
- # api: {
87
- # port: 5665,
88
- # user: 'root',
89
- # password: 'icinga',
90
- # version: 1
91
- # },
92
- # cluster: false,
93
- # satellite: true
94
- # }
95
- # }
96
- #
97
- # @icinga = Icinga2::Client.new(config)
98
- #
99
- # @return [instance, #read]
100
- #
101
- def initialize( settings )
102
-
103
- raise ArgumentError.new('only Hash are allowed') unless( settings.is_a?(Hash) )
104
- raise ArgumentError.new('missing settings') if( settings.size.zero? )
105
-
106
- icinga_host = settings.dig(:icinga, :host) || 'localhost'
107
- icinga_api_port = settings.dig(:icinga, :api, :port) || 5665
108
- icinga_api_user = settings.dig(:icinga, :api, :user)
109
- icinga_api_pass = settings.dig(:icinga, :api, :password)
110
- icinga_api_version = settings.dig(:icinga, :api, :version) || 1
111
- icinga_api_pki_path = settings.dig(:icinga, :api, :pki_path)
112
- icinga_api_node_name = settings.dig(:icinga, :api, :node_name)
113
- @icinga_cluster = settings.dig(:icinga, :cluster) || false
114
- @icinga_satellite = settings.dig(:icinga, :satellite)
115
- @icinga_notifications = settings.dig(:icinga, :notifications) || false
116
-
117
- @icinga_api_url_base = format( 'https://%s:%d/v%s', icinga_host, icinga_api_port, icinga_api_version )
118
-
119
- _has_cert, @options = cert?(
120
- pki_path: icinga_api_pki_path,
121
- node_name: icinga_api_node_name,
122
- user: icinga_api_user,
123
- password: icinga_api_pass
124
- )
125
-
126
- @headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
127
-
128
- self
129
- end
130
-
131
- # create a HTTP Header based on a Icinga2 Certificate or an User API Login
132
- #
133
- # @param [Hash, #read] params
134
- # @option params [String] :pki_path the location of the Certificate Files
135
- # @option params [String] :node_name the Icinga2 Hostname
136
- # @option params [String] :user the Icinga2 API User
137
- # @option params [String] :password the Icinga2 API Password
138
- #
139
- # @example with Certificate
140
- # @icinga.cert?(pki_path: '/etc/icinga2', node_name: 'icinga2-dashing')
141
- #
142
- # @example with User
143
- # @icinga.cert?(user: 'root', password: 'icinga')
144
- #
145
- # @return [Bool, #read]
146
- #
147
- def cert?( params )
148
-
149
- raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
150
- raise ArgumentError.new('missing params') if( params.size.zero? )
151
-
152
- pki_path = params.dig(:pki_path)
153
- node_name = params.dig(:node_name)
154
- user = params.dig(:user)
155
- password = params.dig(:password)
156
-
157
- if( node_name.nil? )
158
- begin
159
- node_name = Socket.gethostbyname(Socket.gethostname).first
160
- logger.debug(format('node name: %s', node_name))
161
- rescue SocketError => e
162
-
163
- raise format("can't resolve hostname (%s)", e)
164
- end
165
- end
166
-
167
- ssl_cert_file = format( '%s/%s.crt', pki_path, node_name )
168
- ssl_key_file = format( '%s/%s.key', pki_path, node_name )
169
- ssl_ca_file = format( '%s/ca.crt', pki_path )
170
-
171
- if( File.file?( ssl_cert_file ) && File.file?( ssl_key_file ) && File.file?( ssl_ca_file ) )
172
-
173
- logger.debug( 'PKI found, using client certificates for connection to Icinga 2 API' )
174
-
175
- ssl_cert_file = File.read( ssl_cert_file )
176
- ssl_key_file = File.read( ssl_key_file )
177
- ssl_ca_file = File.read( ssl_ca_file )
178
-
179
- cert = OpenSSL::X509::Certificate.new( ssl_cert_file )
180
- key = OpenSSL::PKey::RSA.new( ssl_key_file )
181
-
182
- [true, {
183
- ssl_client_cert: cert,
184
- ssl_client_key: key,
185
- ssl_ca_file: ssl_ca_file,
186
- verify_ssl: OpenSSL::SSL::VERIFY_NONE
187
- } ]
188
-
189
- else
190
-
191
- logger.debug( 'PKI not found, using basic auth for connection to Icinga 2 API' )
192
-
193
- raise ArgumentError.new('Missing user_name') if( user.nil? )
194
- raise ArgumentError.new('Missing password') if( password.nil? )
195
-
196
- [false, {
197
- user: user,
198
- password: password,
199
- verify_ssl: OpenSSL::SSL::VERIFY_NONE
200
- } ]
201
- end
202
-
203
- end
204
-
205
- # return Icinga2 Application data
206
- #
207
- # @example
208
- # @icinga.application_data
209
- #
210
- # @return [Hash]
211
- #
212
- def application_data
213
-
214
- data = Network.application_data(
215
- url: format( '%s/status/IcingaApplication', @icinga_api_url_base ),
216
- headers: @headers,
217
- options: @options
218
- )
219
-
220
- return nil if( data.nil? )
221
-
222
- app_data = data.dig('icingaapplication','app')
223
-
224
- # version and revision
225
- @version, @revision = parse_version(app_data.dig('version'))
226
-
227
- # - node_name
228
- @node_name = app_data.dig('node_name')
229
-
230
- # - start_time
231
- @start_time = Time.at(app_data.dig('program_start').to_f)
232
-
233
- data
234
- end
235
-
236
- # return Icinga2 CIB
237
- #
238
- # @example
239
- # @icinga.cib_data
240
- #
241
- # @return [Hash]
242
- #
243
- def cib_data
244
-
245
- data = Network.application_data(
246
- url: format( '%s/status/CIB', @icinga_api_url_base ),
247
- headers: @headers,
248
- options: @options
249
- )
250
-
251
- unless( data.nil? )
252
-
253
- cib_data = data.clone
254
-
255
- # extract
256
- # - uptime
257
- uptime = cib_data.dig('uptime').round(2)
258
- @uptime = Time.at(uptime).utc.strftime('%H:%M:%S')
259
-
260
- # - avg_latency / avg_execution_time
261
- @avg_latency = cib_data.dig('avg_latency').round(2)
262
- @avg_execution_time = cib_data.dig('avg_execution_time').round(2)
263
-
264
- # - hosts
265
- @hosts_up = cib_data.dig('num_hosts_up').to_i
266
- @hosts_down = cib_data.dig('num_hosts_down').to_i
267
- @hosts_pending = cib_data.dig('num_hosts_pending').to_i
268
- @hosts_unreachable = cib_data.dig('num_hosts_unreachable').to_i
269
- @hosts_in_downtime = cib_data.dig('num_hosts_in_downtime').to_i
270
- @hosts_acknowledged = cib_data.dig('num_hosts_acknowledged').to_i
271
-
272
- # - services
273
- @services_ok = cib_data.dig('num_services_ok').to_i
274
- @services_warning = cib_data.dig('num_services_warning').to_i
275
- @services_critical = cib_data.dig('num_services_critical').to_i
276
- @services_unknown = cib_data.dig('num_services_unknown').to_i
277
- @services_pending = cib_data.dig('num_services_pending').to_i
278
- @services_in_downtime = cib_data.dig('num_services_in_downtime').to_i
279
- @services_acknowledged = cib_data.dig('num_services_acknowledged').to_i
280
-
281
- # - check stats
282
- @hosts_active_checks_1min = cib_data.dig('active_host_checks_1min')
283
- @hosts_passive_checks_1min = cib_data.dig('passive_host_checks_1min')
284
- @services_active_checks_1min = cib_data.dig('active_service_checks_1min')
285
- @services_passive_checks_1min = cib_data.dig('passive_service_checks_1min')
286
-
287
- end
288
-
289
- data
290
- end
291
-
292
- # return Icinga2 Status Data
293
- #
294
- # @example
295
- # @icinga.status_data
296
- #
297
- # @return [Hash]
298
- #
299
- def status_data
300
-
301
- Network.application_data(
302
- url: format( '%s/status', @icinga_api_url_base ),
303
- headers: @headers,
304
- options: @options
305
- )
306
- end
307
-
308
- # return Icinga2 API Listener
309
- #
310
- # @example
311
- # @icinga.api_listener
312
- #
313
- # @return [Hash]
314
- #
315
- def api_listener
316
-
317
- Network.application_data(
318
- url: format( '%s/status/ApiListener', @icinga_api_url_base ),
319
- headers: @headers,
320
- options: @options
321
- )
322
- end
323
-
324
- # check the availability of a Icinga network connect
325
- #
326
- # @example
327
- # @icinga.available?
328
- #
329
- # @return [Bool]
330
- #
331
- def available?
332
-
333
- data = application_data
334
-
335
- return true unless( data.nil? )
336
-
337
- false
338
- end
339
-
340
- # return Icinga2 version and revision
341
- #
342
- # @example
343
- # @icinga.application_data
344
- # @icinga.version.values
345
- #
346
- # v = @icinga.version
347
- # version = v.dig(:version)
348
- #
349
- # @return [Hash]
350
- # * version
351
- # * revision
352
- #
353
- def version
354
- version = @version.nil? ? 0 : @version
355
- revision = @revision.nil? ? 0 : @revision
356
-
357
- {
358
- version: version.to_s,
359
- revision: revision.to_s
360
- }
361
- end
362
-
363
- # return Icinga2 node_name
364
- #
365
- # @example
366
- # @icinga.application_data
367
- # @icinga.node_name
368
- #
369
- # @return [String]
370
- #
371
- def node_name
372
- return @node_name if( @node_name )
373
-
374
- nil
375
- end
376
-
377
- # return Icinga2 start time
378
- #
379
- # @example
380
- # @icinga.application_data
381
- # @icinga.start_time
382
- #
383
- # @return [String]
384
- #
385
- def start_time
386
- return @start_time if( @start_time )
387
-
388
- nil
389
- end
390
-
391
- # return Icinga2 uptime
392
- #
393
- # @example
394
- # @icinga.cib_data
395
- # @icinga.uptime
396
- #
397
- # @return [String]
398
- #
399
- def uptime
400
- return @uptime if( @uptime )
401
-
402
- nil
403
- end
404
-
405
- end
406
17
  end
407
-