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

Overview

Abstract base class for the API calls. Provides some helper methods

Author:

  • Bodo Schulz

Constant Summary

Constants included from Version

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Usergroups

#add_usergroup, #delete_usergroup, #exists_usergroup?, #usergroups

Methods included from Users

#add_user, #delete_user, #exists_user?, #users

Methods included from Servicegroups

#add_servicegroup, #delete_servicegroup, #exists_servicegroup?, #servicegroups

Methods included from Services

#add_services, #exists_service?, #problem_services, #service_objects, #service_problems, #service_severity, #services, #unhandled_services, #update_host

Methods included from Hostgroups

#add_hostgroup, #delete_hostgroup, #exists_hostgroup?, #hostgroups

Methods included from Hosts

#add_host, #delete_host, #exists_host?, #host_objects, #host_problems, #host_severity, #hosts, #problem_hosts

Methods included from Notifications

#disable_host_notification, #disable_hostgroup_notification, #disable_service_notification, #enable_host_notification, #enable_hostgroup_notification, #enable_service_notification, #notifications

Methods included from Downtimes

#add_downtime, #downtimes

Methods included from Tools

#handled_problems, #object_has_been_checked?, #parse_version

Methods included from Converts

format_service, state_to_color, state_to_string

Methods included from Status

#api_listener, #application_data, #cib_data, #extract_data

Methods included from Network

delete, get, get_with_payload, post, put

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(settings = {}) ⇒ instance, #read

Returns a new instance of Client

Examples:

to create an new Instance

config = {
  icinga: {
    host: '192.168.33.5',
    api: {
      port: 5665,
      user: 'root',
      password: 'icinga'
    },
    cluster: false,
    satellite: true
  }
}

@icinga = Icinga2::Client.new(config)

Parameters:

  • settings (Hash, #read) (defaults to: {})

    the settings for Icinga2

Options Hash (settings):

  • :icinga, (String)

    :host the Icinga2 Hostname (default: ‘localhost’)

  • :icinga, (Integer)

    :api, :port the Icinga2 API Port (default: 5665)

  • :icinga, (String)

    :api, :user the Icinga2 API User

  • :icinga, (String)

    :api, :password the Icinga2 API Password

  • :icinga, (Bool)

    :cluster Icinga2 Cluster Mode

  • :icinga, (Bool)

    :notifications enable Icinga2 Host Notifications (default: false)



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 104

def initialize( settings = {} )

  @icinga_host           = settings.dig(:icinga, :host)           || 'localhost'
  @icinga_api_port       = settings.dig(:icinga, :api, :port)     || 5665
  @icinga_api_user       = settings.dig(:icinga, :api, :user)
  @icinga_api_pass       = settings.dig(:icinga, :api, :password)
  @icinga_cluster        = settings.dig(:icinga, :cluster)        || false
  @icinga_satellite      = settings.dig(:icinga, :satellite)
  @icinga_notifications  = settings.dig(:icinga, :notifications)  || false

  @icinga_api_url_base   = format( 'https://%s:%d', @icinga_host, @icinga_api_port )
  @node_name             = Socket.gethostbyname( Socket.gethostname ).first

  @has_cert   = cert?( user: @icinga_api_user, password: @icinga_api_pass )
  @headers    = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }


  @version = @revision = 0
  @node_name = @start_time = @uptime = ''
  @avg_latency = @avg_execution_time = 0
  @hosts_up = @hosts_down = @hosts_in_downtime = @hosts_acknowledged = 0
  @hosts_all = @hosts_problems = @hosts_handled_warning_problems = @hosts_handled_critical_problems = @hosts_handled_unknown_problems = 0
  @hosts_handled_problems = @hosts_down_adjusted = 0
  @services_ok = @services_warning = @services_critical = @services_unknown = @services_in_downtime = @services_acknowledged = 0
  @services_all = @services_problems = @services_handled_warning_problems = @services_handled_critical_problems = @services_handled_unknown_problems = 0
  @services_warning_adjusted = @services_critical_adjusted = @services_unknown_adjusted = 0
  @hosts_active_checks_1min = @hosts_passive_checks_1min = @services_active_checks_1min = @services_passive_checks_1min = 0

  extract_data

  self
end

Instance Attribute Details

#hosts_acknowledgedObject (readonly)

Returns the value of attribute hosts_acknowledged.



49
50
51
# File 'lib/icinga2.rb', line 49

def hosts_acknowledged
  @hosts_acknowledged
end

#hosts_active_checks_1minObject (readonly)

Returns the value of attribute hosts_active_checks_1min.



56
57
58
# File 'lib/icinga2.rb', line 56

def hosts_active_checks_1min
  @hosts_active_checks_1min
end

#hosts_allObject (readonly)

Returns the value of attribute hosts_all.



50
51
52
# File 'lib/icinga2.rb', line 50

def hosts_all
  @hosts_all
end

#hosts_downObject (readonly)

Returns the value of attribute hosts_down.



49
50
51
# File 'lib/icinga2.rb', line 49

def hosts_down
  @hosts_down
end

#hosts_down_adjustedObject (readonly)

Returns the value of attribute hosts_down_adjusted.



51
52
53
# File 'lib/icinga2.rb', line 51

def hosts_down_adjusted
  @hosts_down_adjusted
end

#hosts_handled_critical_problemsObject (readonly)

Returns the value of attribute hosts_handled_critical_problems.



50
51
52
# File 'lib/icinga2.rb', line 50

def hosts_handled_critical_problems
  @hosts_handled_critical_problems
end

#hosts_handled_problemsObject (readonly)

Returns the value of attribute hosts_handled_problems.



51
52
53
# File 'lib/icinga2.rb', line 51

def hosts_handled_problems
  @hosts_handled_problems
end

#hosts_handled_unknown_problemsObject (readonly)

Returns the value of attribute hosts_handled_unknown_problems.



50
51
52
# File 'lib/icinga2.rb', line 50

def hosts_handled_unknown_problems
  @hosts_handled_unknown_problems
end

#hosts_handled_warning_problemsObject (readonly)

Returns the value of attribute hosts_handled_warning_problems.



50
51
52
# File 'lib/icinga2.rb', line 50

def hosts_handled_warning_problems
  @hosts_handled_warning_problems
end

#hosts_in_downtimeObject (readonly)

Returns the value of attribute hosts_in_downtime.



49
50
51
# File 'lib/icinga2.rb', line 49

def hosts_in_downtime
  @hosts_in_downtime
end

#hosts_passive_checks_1minObject (readonly)

Returns the value of attribute hosts_passive_checks_1min.



56
57
58
# File 'lib/icinga2.rb', line 56

def hosts_passive_checks_1min
  @hosts_passive_checks_1min
end

#hosts_problemsObject (readonly)

Returns the value of attribute hosts_problems.



50
51
52
# File 'lib/icinga2.rb', line 50

def hosts_problems
  @hosts_problems
end

#hosts_upObject (readonly)

Returns the value of attribute hosts_up.



49
50
51
# File 'lib/icinga2.rb', line 49

def hosts_up
  @hosts_up
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



48
49
50
# File 'lib/icinga2.rb', line 48

def node_name
  @node_name
end

#revisionObject (readonly)

Returns the value of attribute revision.



48
49
50
# File 'lib/icinga2.rb', line 48

def revision
  @revision
end

#services_acknowledgedObject (readonly)

Returns the value of attribute services_acknowledged.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_acknowledged
  @services_acknowledged
end

#services_active_checks_1minObject (readonly)

Returns the value of attribute services_active_checks_1min.



56
57
58
# File 'lib/icinga2.rb', line 56

def services_active_checks_1min
  @services_active_checks_1min
end

#services_allObject (readonly)

Returns the value of attribute services_all.



54
55
56
# File 'lib/icinga2.rb', line 54

def services_all
  @services_all
end

#services_criticalObject (readonly)

Returns the value of attribute services_critical.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_critical
  @services_critical
end

#services_critical_adjustedObject (readonly)

Returns the value of attribute services_critical_adjusted.



55
56
57
# File 'lib/icinga2.rb', line 55

def services_critical_adjusted
  @services_critical_adjusted
end

#services_handled_critical_problemsObject (readonly)

Returns the value of attribute services_handled_critical_problems.



54
55
56
# File 'lib/icinga2.rb', line 54

def services_handled_critical_problems
  @services_handled_critical_problems
end

#services_handled_unknown_problemsObject (readonly)

Returns the value of attribute services_handled_unknown_problems.



54
55
56
# File 'lib/icinga2.rb', line 54

def services_handled_unknown_problems
  @services_handled_unknown_problems
end

#services_handled_warning_problemsObject (readonly)

Returns the value of attribute services_handled_warning_problems.



54
55
56
# File 'lib/icinga2.rb', line 54

def services_handled_warning_problems
  @services_handled_warning_problems
end

#services_in_downtimeObject (readonly)

Returns the value of attribute services_in_downtime.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_in_downtime
  @services_in_downtime
end

#services_okObject (readonly)

Returns the value of attribute services_ok.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_ok
  @services_ok
end

#services_passive_checks_1minObject (readonly)

Returns the value of attribute services_passive_checks_1min.



56
57
58
# File 'lib/icinga2.rb', line 56

def services_passive_checks_1min
  @services_passive_checks_1min
end

#services_problemsObject (readonly)

Returns the value of attribute services_problems.



54
55
56
# File 'lib/icinga2.rb', line 54

def services_problems
  @services_problems
end

#services_unknownObject (readonly)

Returns the value of attribute services_unknown.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_unknown
  @services_unknown
end

#services_unknown_adjustedObject (readonly)

Returns the value of attribute services_unknown_adjusted.



55
56
57
# File 'lib/icinga2.rb', line 55

def services_unknown_adjusted
  @services_unknown_adjusted
end

#services_warningObject (readonly)

Returns the value of attribute services_warning.



53
54
55
# File 'lib/icinga2.rb', line 53

def services_warning
  @services_warning
end

#services_warning_adjustedObject (readonly)

Returns the value of attribute services_warning_adjusted.



55
56
57
# File 'lib/icinga2.rb', line 55

def services_warning_adjusted
  @services_warning_adjusted
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



48
49
50
# File 'lib/icinga2.rb', line 48

def start_time
  @start_time
end

#uptimeObject (readonly)

Returns the value of attribute uptime.



48
49
50
# File 'lib/icinga2.rb', line 48

def uptime
  @uptime
end

#versionObject (readonly)

Returns the value of attribute version.



48
49
50
# File 'lib/icinga2.rb', line 48

def version
  @version
end

Instance Method Details

#cert?(params = {}) ⇒ Bool, #read

Examples:

with Certificate

@icinga.cert?(name_name: 'icinga2-dashing')

with User

@icinga.cert?(user: 'root', password: 'icinga')

Parameters:

  • params (Hash, #read) (defaults to: {})

    create a HTTP Header based on a Icinga2 Certificate or an User PAI Login

Options Hash (params):

  • :node_name (String)

    the Icinga2 Hostname (default: ‘localhost’)

  • :user (Integer)

    the Icinga2 API User

  • :password (Integer)

    the Icinga2 API Password

Returns:

  • (Bool, #read)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/icinga2.rb', line 149

def cert?( params = {} )

  node_name    = params.dig(:node_name) || 'localhost'
  user         = params.dig(:user)
  password     = params.dig(:password)

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

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

    ssl_cert_file = File.read( format( 'pki/%s.crt', node_name ) )
    ssl_key_file  = File.read( format( 'pki/%s.key', node_name ) )
    ssl_ca_file   = File.read( 'pki/ca.crt' )

    cert      = OpenSSL::X509::Certificate.new( ssl_cert_file )
    key       = OpenSSL::PKey::RSA.new( ssl_key_file )

    @options   = {
      ssl_client_cert: cert,
      ssl_client_key: key,
      ssl_ca_file: ssl_ca_file,
      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