Module: Icinga2::Hosts

Included in:
Client
Defined in:
lib/icinga2/hosts.rb

Overview

namespace for host handling

Instance Method Summary collapse

Instance Method Details

#add_host(params = {}) ⇒ Hash

add host

Examples:

param = {
  host: 'foo',
  fqdn: 'foo.bar.com',
  display_name: 'test node',
  max_check_attempts: 5,
  notes: 'test node'
}
@icinga.add_host(param)

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :host (String)
  • :fqdn (String)
  • :display_name (String)
  • :enable_notifications (Bool) — default: false
  • :max_check_attempts (Integer) — default: 3
  • :check_interval (Integer) — default: 60
  • :retry_interval (Integer) — default: 45
  • :notes (String)
  • :notes_url (String)
  • :action_url (String)
  • :vars (Hash) — default: {}

Returns:

  • (Hash)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/icinga2/hosts.rb', line 35

def add_host( params = {} )

  host               = params.dig(:host)
  fqdn               = params.dig(:fqdn)
  display_name       = params.dig(:display_name) || host
  notifications      = params.dig(:enable_notifications) || false
  max_check_attempts = params.dig(:max_check_attempts) || 3
  check_interval     = params.dig(:check_interval) || 60
  retry_interval     = params.dig(:retry_interval) || 45
  notes              = params.dig(:notes)
  notes_url          = params.dig(:notes_url)
  action_url         = params.dig(:action_url)
  vars               = params.dig(:vars) || {}

  if( host.nil? )
    return {
      status: 404,
      message: 'missing host name'
    }
  end

  if( fqdn.nil? )
    # build FQDN
    fqdn = Socket.gethostbyname( host ).first
  end

  payload = {
    'templates' => [ 'generic-host' ],
    'attrs'     => {
      'address'              => fqdn,
      'display_name'         => display_name,
      'max_check_attempts'   => max_check_attempts.to_i,
      'check_interval'       => check_interval.to_i,
      'retry_interval'       => retry_interval.to_i,
      'enable_notifications' => notifications,
      'action_url'           => action_url,
      'notes'                => notes,
      'notes_url'            => notes_url
    }
  }

  payload['attrs']['vars'] = vars unless  vars.empty?

  if( @icinga_cluster == true && !@icinga_satellite.nil? )
    payload['attrs']['zone'] = @icinga_satellite
  end

  logger.debug( JSON.pretty_generate( payload ) )

  Network.put(         host: host,
    url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
    headers: @headers,
    options: @options,
    payload: payload )

end

#delete_host(params = {}) ⇒ Hash

delete a host

Examples:

@icinga.delete_host(host: 'foo')

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :host (String)

    host to delete

Returns:

  • (Hash)

    result



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/icinga2/hosts.rb', line 102

def delete_host( params = {} )

  host = params.dig(:host)

  if( host.nil? )

    return {
      status: 404,
      message: 'missing host name'
    }
  end

  Network.delete(         host: host,
    url: format( '%s/v1/objects/hosts/%s?cascade=1', @icinga_api_url_base, host ),
    headers: @headers,
    options: @options )

end

#exists_host?(name) ⇒ Bool

returns true if the host exists

Examples:

@icinga.exists_host?('icinga2')

Parameters:

  • name (String)

Returns:

  • (Bool)


164
165
166
167
168
169
170
171
172
# File 'lib/icinga2/hosts.rb', line 164

def exists_host?( name )

  result = hosts( name: name )
  result = JSON.parse( result ) if  result.is_a?( String )
  status = result.dig(:status)

  return true if  !status.nil? && status == 200
  false
end

#host_objects(params = {}) ⇒ Hash

returns host objects

Examples:

with default attrs and joins

@icinga.host_objects
@icinga.host_objects(attrs: ['name', 'state'])

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :attrs (Array) — default: ['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check']
  • :filter (Array) — default: []
  • :joins (Array) — default: []

Returns:

  • (Hash)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/icinga2/hosts.rb', line 189

def host_objects( params = {} )

  attrs   = params.dig(:attrs)
  filter  = params.dig(:filter)
  joins   = params.dig(:joins)
  payload = {}

  if( attrs.nil? )
    attrs = %w[name state acknowledgement downtime_depth last_check]
  end

  payload['attrs'] = attrs unless  attrs.nil?
  payload['filter'] = filter unless  filter.nil?
  payload['joins'] = joins unless  joins.nil?

  Network.get(         host: nil,
    url: format( '%s/v1/objects/hosts', @icinga_api_url_base ),
    headers: @headers,
    options: @options,
    payload: payload )

end

#host_problemsInteger

return count of hosts with problems

Examples:

@icinga.host_problems

Returns:

  • (Integer)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/icinga2/hosts.rb', line 219

def host_problems

  data     = host_objects
  problems = 0

  data = JSON.parse(data) if  data.is_a?(String)
  nodes = data.dig(:nodes)

  unless !nodes

    nodes.each do |n|

      attrs           = n.last.dig('attrs')
      state           = attrs.dig('state')           || 0
      downtime_depth  = attrs.dig('downtime_depth')  || 0
      acknowledgement = attrs.dig('acknowledgement') || 0

      if( state != 0 && downtime_depth.zero? && acknowledgement.zero? )
        problems += 1
      end

    end
  end
  problems
end

#host_severity(host) ⇒ Hash

calculate a host severity

stolen from Icinga Web 2 ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php

Parameters:

  • host (Hash)

Returns:

  • (Hash)


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/icinga2/hosts.rb', line 295

def host_severity( host )

  attrs           = host.dig('attrs')
  state           = attrs.dig('state')
  acknowledgement = attrs.dig('acknowledgement') || 0
  downtime_depth  = attrs.dig('downtime_depth')  || 0

  severity = 0

  severity +=
    if acknowledgement != 0
      2
    elsif downtime_depth > 0
      1
    else
      4
    end

  severity += 16 if object_has_been_checked?(host)

  unless state.zero?

    severity +=
      if state == 1
        32
      elsif state == 2
        64
      else
        256
      end
  end

  severity
end

#hosts(params = {}) ⇒ Hash

return hosts

Examples:

to get all hosts

@icinga.hosts

to get one host

@icinga.host(host: 'icinga2')

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :host (String)
  • :attrs (String)
  • :filter (String)
  • :joins (String)

Returns:

  • (Hash)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/icinga2/hosts.rb', line 137

def hosts( params = {} )

  host   = params.dig(:host)
  attrs  = params.dig(:attrs)
  filter = params.dig(:filter)
  joins  = params.dig(:joins)

  payload['attrs'] = attrs unless  attrs.nil?
  payload['filter'] = filter unless  filter.nil?
  payload['joins'] = joins unless  joins.nil?

  Network.get(         host: host,
    url: format( '%s/v1/objects/hosts/%s', @icinga_api_url_base, host ),
    headers: @headers,
    options: @options )

end

#problem_hosts(max_items = 5) ⇒ Hash

return a list of host with problems

Examples:

@icinga.problem_hosts

Parameters:

  • max_items (Integer) (defaults to: 5)

    numbers of list entries

Returns:

  • (Hash)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/icinga2/hosts.rb', line 254

def problem_hosts( max_items = 5 )

  @host_problems = {}
  @host_problems_severity = {}

  host_data = host_objects

  host_data = JSON.parse( host_data ) if  host_data.is_a?(String)
  host_data = host_data.dig(:nodes)

  unless !host_data

    host_data.each do |_host,v|

      name  = v.dig('name')
      state = v.dig('attrs','state')

      next if  state.zero?

      @host_problems[name] = host_severity(v)
    end

    # get the count of problems
    #
    @host_problems.keys[1..max_items].each { |k,_v| @host_problems_severity[k] = @host_problems[k] }
  end
  @host_problems_severity

end