Class: Icinga2::Client

Inherits:
Object
  • Object
show all
Includes:
Converts, Downtimes, Hostgroups, Hosts, Network, Notifications, Servicegroups, Services, Status, Tools, Usergroups, Users, Version, Logging
Defined in:
lib/icinga2.rb

Constant Summary

Constants included from Version

Version::MAJOR, Version::MINOR, Version::TINY

Instance Method Summary collapse

Methods included from Usergroups

#addUsergroup, #deleteUsergroup, #existsUsergroup?, #listUsergroups

Methods included from Users

#addUser, #deleteUser, #existsUser?, #listUsers

Methods included from Servicegroups

#addServicegroup, #deleteServicegroup, #existsServicegroup?, #listServicegroups

Methods included from Services

#addServices, #existsService?, #listServices, #problemServices, #serviceObjects, #serviceProblems, #serviceSeverity, #unhandledServices, #updateHost

Methods included from Hostgroups

#addHostgroup, #deleteHostgroup, #existsHostgroup?, #listHostgroups

Methods included from Hosts

#addHost, #deleteHost, #existsHost?, #hostObjects, #hostProblems, #hostSeverity, #listHosts, #problemHosts

Methods included from Notifications

#disableHostNotification, #disableHostgroupNotification, #disableServiceNotification, #enableHostNotification, #enableHostgroupNotification, #enableServiceNotification, #hostNotification, #hostgroupNotification, #listNotifications, #serviceNotification

Methods included from Downtimes

#addDowntime, #listDowntimes

Methods included from Tools

#getObjectHasBeenChecked

Methods included from Converts

formatService, stateToColor, stateToString

Methods included from Status

#CIBData, #apiListener, #applicationData

Methods included from Network

delete, get, get2, post, put

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(settings = {}) ⇒ Client



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/icinga2.rb', line 52

def initialize( settings = {} )

  @icingaHost           = settings.dig(:icinga, :host)           || 'localhost'
  @icingaApiPort        = settings.dig(:icinga, :api, :port)     || 5665
  @icingaApiUser        = settings.dig(:icinga, :api, :user)
  @icingaApiPass        = settings.dig(:icinga, :api, :password)
  @icingaCluster        = settings.dig(:icinga, :cluster)        || false
  @icingaSatellite      = settings.dig(:icinga, :satellite)
  @icingaNotifications  = settings.dig(:icinga, :notifications)  || false

  @icingaApiUrlBase     = sprintf( 'https://%s:%d', @icingaHost, @icingaApiPort )
  @nodeName             = Socket.gethostbyname( Socket.gethostname ).first

  date                 = '2017-06-08'

  logger.info( '-----------------------------------------------------------------' )
  logger.info( ' Icinga2 Management' )
  logger.info( "  Version #{VERSION} (#{date})" )
  logger.info( '  Copyright 2016-2017 Bodo Schulz' )
  logger.info( "  Backendsystem #{@icingaApiUrlBase}" )
  logger.info( sprintf( '    cluster enabled: %s', @icingaCluster ? 'true' : 'false' ) )
  logger.info( sprintf( '    notifications enabled: %s', @icingaNotifications ? 'true' : 'false' ) )
  if( @icingaCluster )
    logger.info( sprintf( '    satellite endpoint: %s', @icingaSatellite ) )
  end
  logger.info( '-----------------------------------------------------------------' )
  logger.info( '' )

  logger.debug( sprintf( '  server   : %s', @icingaHost ) )
  logger.debug( sprintf( '  port     : %s', @icingaApiPort ) )
  logger.debug( sprintf( '  api url  : %s', @icingaApiUrlBase ) )
  logger.debug( sprintf( '  api user : %s', @icingaApiUser ) )
  logger.debug( sprintf( '  api pass : %s', @icingaApiPass ) )
  logger.debug( sprintf( '  node name: %s', @nodeName ) )

  @hasCert   = self.checkCert( { :user => @icingaApiUser, :password =>  @icingaApiPass } )
  @headers   = { "Content-Type" => "application/json", "Accept" => "application/json" }

  return self

end

Instance Method Details

#checkCert(params = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/icinga2.rb', line 95

def checkCert( params = {} )

  nodeName     = params.dig(:nodeName) || 'localhost'

  user         = params.dig(:user)     || 'admin'
  password     = params.dig(:password) || ''

  # check whether pki files are there, otherwise use basic auth
  if File.file?( sprintf( 'pki/%s.crt', nodeName ) )

    logger.debug( "PKI found, using client certificates for connection to Icinga 2 API" )

    sslCertFile = File.read( sprintf( 'pki/%s.crt', nodeName ) )
    sslKeyFile  = File.read( sprintf( 'pki/%s.key', nodeName ) )
    sslCAFile   = File.read( 'pki/ca.crt' )

    cert      = OpenSSL::X509::Certificate.new( sslCertFile )
    key       = OpenSSL::PKey::RSA.new( sslKeyFile )

    @options   = {
      :ssl_client_cert => cert,
      :ssl_client_key  => key,
      :ssl_ca_file     => sslCAFile,
      :verify_ssl      => OpenSSL::SSL::VERIFY_NONE
    }

    return true
  else

    logger.debug( "PKI not found, using basic auth for connection to Icinga 2 API" )

    @options = {
      :user       => user,
      :password   => password,
      :verify_ssl => OpenSSL::SSL::VERIFY_NONE
    }

    return false
  end

end