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 = {
  name: 'foo',
  address: 'foo.bar.com',
  display_name: 'test node',
  max_check_attempts: 5,
  notes: 'test node',
  vars: {
    description: 'host foo',
    os: 'Linux',
    partitions: {
      '/' => {
        crit: '95%',
        warn: '90%'
      }
    }
  }
}
add_host(param)

Parameters:

Options Hash (params):

  • name (String)
  • address (String)
  • address6 (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)
  • check_command (String)
  • check_interval (Integer)
  • check_period (String)
  • check_timeout (Integer)
  • command_endpoint (String)
  • enable_active_checks (Bool)
  • enable_event_handler (Bool)
  • enable_flapping (Bool)
  • enable_passive_checks (Bool)
  • enable_perfdata (Bool)
  • event_command (String)
  • flapping_threshold (Integer)
  • flapping_threshold_high (Integer)
  • flapping_threshold_low (Integer)
  • icon_image (String)
  • icon_image_alt (String)
  • retry_interval (Integer)
  • volatile (Bool)
  • vars (Hash) — default: {}

Returns:

Raises:

  • (ArgumentError)


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
93
94
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
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/icinga2/hosts.rb', line 64

def add_host( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing params') if( params.size.zero? )

  name    = validate( params, required: true, var: 'name', type: String )
  action_url = validate( params, required: false, var: 'action_url', type: String )
  address    = validate( params, required: false, var: 'address', type: String )
  address6   = validate( params, required: false, var: 'address6', type: String )
  check_command = validate( params, required: false, var: 'check_command', type: String )
  check_interval = validate( params, required: false, var: 'check_interval', type: Integer ) || 60
  check_period   = validate( params, required: false, var: 'check_period', type: Integer )
  check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
  command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
  display_name   = validate( params, required: false, var: 'display_name', type: String )
  enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
  enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
  enable_flapping   = validate( params, required: false, var: 'enable_flapping', type: Boolean )
  enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
  enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
  volatile = validate( params, required: false, var: 'volatile', type: Boolean )
  enable_perfdata   = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
  event_command = validate( params, required: false, var: 'event_command', type: String )
  flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
  groups   = validate( params, required: false, var: 'groups', type: Array )
  icon_image    = validate( params, required: false, var: 'icon_image', type: String )
  icon_image_alt   = validate( params, required: false, var: 'icon_image_alt', type: String )
  notes    = validate( params, required: false, var: 'notes', type: String )
  notes_url   = validate( params, required: false, var: 'notes_url', type: String )
  max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer ) || 3
  retry_interval   = validate( params, required: false, var: 'retry_interval', type: Integer ) || 45
  templates   = validate( params, required: false, var: 'templates', type: Array ) || [ 'generic-host' ]
  vars   = validate( params, required: false, var: 'vars', type: Hash ) || {}
  zone   = validate( params, required: false, var: 'zone', type: String )

  address = Socket.gethostbyname( name ).first if( address.nil? )

  payload = {
    templates: templates,
    attrs: {
      action_url: action_url,
      address: address,
      address6: address6,
      check_period: check_period,
      check_command: check_command,
      check_interval: check_interval,
      check_timeout: check_timeout,
      command_endpoint: command_endpoint,
      display_name: display_name,
      enable_active_checks: enable_active_checks,
      enable_event_handler: enable_event_handler,
      enable_flapping: enable_flapping,
      enable_notifications: enable_notifications,
      enable_passive_checks: enable_passive_checks,
      enable_perfdata: enable_perfdata,
      event_command: event_command,
      flapping_threshold: flapping_threshold,
      groups: groups,
      icon_image: icon_image,
      icon_image_alt: icon_image_alt,
      max_check_attempts: max_check_attempts,
      notes: notes,
      notes_url: notes_url,
      retry_interval: retry_interval,
      volatile: volatile,
      zone: zone,
      vars: vars
    }
  }

  # remove all empty attrs
  payload.reject!{ |_k, v| v.nil? }
  payload[:attrs].reject!{ |_k, v| v.nil? }

#       puts JSON.pretty_generate  payload

  put(
    url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#count_hosts_with_problemsInteger

return count of hosts with problems

Examples:

count_hosts_with_problems

Returns:

  • (Integer)


507
508
509
510
511
512
513
514
515
# File 'lib/icinga2/hosts.rb', line 507

def count_hosts_with_problems

  host_data = host_objects
  host_data = JSON.parse(host_data) if  host_data.is_a?(String)

  f = host_data.select { |t| t.dig('attrs','state') != 0 && t.dig('attrs','downtime_depth').zero? && t.dig('attrs','acknowledgement').zero? }

  f.size
end

#delete_host(params) ⇒ Hash

delete a host

Examples:

delete_host(name: 'foo')
delete_host(name: 'foo', cascade: true)

Parameters:

Options Hash (params):

  • name (String)

    host to delete

  • cascade (Bool) — default: false

    delete host also when other objects depend on it

Returns:

Raises:

  • (ArgumentError)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/icinga2/hosts.rb', line 161

def delete_host( params )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing params') if( params.size.zero? )

  name    = validate( params, required: true, var: 'name', type: String )
  cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false

  url = format( '%s/objects/hosts/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )

  delete(
    url: url,
    headers: @headers,
    options: @options
  )
end

#exists_host?(host_name) ⇒ Bool

returns true if the host exists

Examples:

exists_host?('icinga2')

Parameters:

Returns:

  • (Bool)

Raises:

  • (ArgumentError)


395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/icinga2/hosts.rb', line 395

def exists_host?( host_name )

  raise ArgumentError.new(format('wrong type. \'host_name\' must be an String, given \'%s\'',host_name.class.to_s)) unless( host_name.is_a?(String) )
  raise ArgumentError.new('Missing host_name') if( host_name.size.zero? )

  result = hosts( name: host_name )
  result = JSON.parse( result ) if( result.is_a?(String) )
  result = result.first if( result.is_a?(Array) )

  return false if( result.is_a?(Hash) && result.dig('code') == 404 )

  true
end

#host_objects(params = {}) ⇒ Hash

returns host objects

Examples:

with default attrs and joins

host_objects
host_objects(attrs: ['name', 'state'])

Parameters:

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

Options Hash (params):

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

Returns:



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/icinga2/hosts.rb', line 424

def host_objects( params = {} )

  attrs   = validate( params, required: false, var: 'attrs', type: Array ) || %w[name state acknowledgement downtime_depth last_check]
  filter  = validate( params, required: false, var: 'filter', type: String )
  joins   = validate( params, required: false, var: 'joins', type: Array )

  payload = {
    attrs: attrs,
    filter: filter,
    joins: joins
  }
  payload.reject!{ |_k, v| v.nil? }

  data = api_data(
    url: format( '%s/objects/hosts', @icinga_api_url_base ),
    headers: @headers,
    options: @options,
    payload: payload
  )

  @last_host_objects_called = Time.now.to_i

  if( !data.nil? && data.is_a?(Array) )
    all_hosts = data.clone
    unless( all_hosts.nil? )
      # global var for count of all hosts
      @hosts_all               = all_hosts.size
      # global var for count of all host with a problem
      @hosts_problems          = count_problems(all_hosts)
      # global var for count of all gost with state HOSTS_DOWN
      @hosts_problems_down     = count_problems(all_hosts, Icinga2::HOSTS_DOWN)
      @hosts_problems_critical = count_problems(all_hosts, Icinga2::HOSTS_CRITICAL)
      @hosts_problems_unknown  = count_problems(all_hosts, Icinga2::HOSTS_UNKNOWN)
    end
  end

  data
end

#host_problemsHash

returns data with host problems

Examples:

host_objects
all, down, critical, unknown, handled, adjusted = host_problems.values

p = host_problems
down = h.dig(:down)

Returns:

  • (Hash)
    • all

    • down

    • critical

    • unknown

Raises:

  • (ArgumentError)


581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/icinga2/hosts.rb', line 581

def host_problems

  cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
  host_objects if((Time.now.to_i - @last_host_objects_called).to_i > @last_call_timeout)

  raise ArgumentError.new(format('wrong type. \'@hosts_problems_down\' must be an Integer, given \'%s\'', @hosts_problems_down.class.to_s)) unless( @hosts_problems_down.is_a?(Integer))
  raise ArgumentError.new(format('wrong type. \'@hosts_problems_critical\' must be an Integer, given \'%s\'', @hosts_problems_critical.class.to_s)) unless( @hosts_problems_critical.is_a?(Integer))
  raise ArgumentError.new(format('wrong type. \'@hosts_problems_critical\' must be an Integer, given \'%s\'', @hosts_problems_critical.class.to_s)) unless( @hosts_problems_critical.is_a?(Integer))
  raise ArgumentError.new(format('wrong type. \'@hosts_down\' must be an Integer, given \'%s\'', @hosts_down.class.to_s)) unless( @hosts_down.is_a?(Integer))

  problems_all      = @hosts_problems.nil?           ? 0 : @hosts_problems
  problems_down     = @hosts_problems_down.nil?      ? 0 : @hosts_problems_down
  problems_critical = @hosts_problems_critical.nil?  ? 0 : @hosts_problems_critical
  problems_unknown  = @hosts_problems_unknown.nil?   ? 0 : @hosts_problems_unknown

  # calculate host problems adjusted by handled problems
  # count togther handled host problems
  problems_handled  = @hosts_problems_down + @hosts_problems_critical + @hosts_problems_unknown
  problems_adjusted = @hosts_down - problems_handled

  {
    all: problems_all.to_i,
    down: problems_down.to_i,
    critical: problems_critical.to_i,
    unknown: problems_unknown.to_i,
    handled: problems_handled.to_i,
    adjusted: problems_adjusted.to_i
  }
end

#hosts(params = {}) ⇒ Array

return hosts

Examples:

to get all hosts

hosts

to get one host

hosts( name: 'icinga2')

Parameters:

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

Options Hash (params):

Returns:

Raises:

  • (ArgumentError)


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/icinga2/hosts.rb', line 362

def hosts( params = {} )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )

  name    = validate( params, required: false, var: 'name', type: String )
  attrs   = validate( params, required: false, var: 'attrs', type: Array )
  filter  = validate( params, required: false, var: 'filter', type: String )
  joins   = validate( params, required: false, var: 'joins', type: Array )

  payload = {
    attrs: attrs,
    filter: filter,
    joins: joins
  }
  payload.reject!{ |_k, v| v.nil? }

  api_data(
    url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#hosts_adjustedHash

returns adjusted hosts state OBSOLETE

Examples:

handled, down = hosts_adjusted.values

h = hosts_adjusted
down = h.dig(:down_adjusted)

Returns:

  • (Hash)
    • handled_problems

    • down_adjusted

Raises:

  • (ArgumentError)


476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/icinga2/hosts.rb', line 476

def hosts_adjusted

  puts 'function hosts_adjusted() is obsolete'
  puts 'Please use host_problems()'

  cib_data if((Time.now.to_i - @last_cib_data_called).to_i > @last_call_timeout)
  host_objects if((Time.now.to_i - @last_host_objects_called).to_i > @last_call_timeout)

  raise ArgumentError.new('Integer for @hosts_problems_down needed') unless( @hosts_problems_down.is_a?(Integer) )
  raise ArgumentError.new('Integer for @hosts_problems_critical needed') unless( @hosts_problems_critical.is_a?(Integer) )
  raise ArgumentError.new('Integer for @hosts_problems_unknown needed') unless( @hosts_problems_unknown.is_a?(Integer) )
  raise ArgumentError.new('Integer for @hosts_down needed') unless( @hosts_down.is_a?(Integer) )

  # calculate host problems adjusted by handled problems
  # count togther handled host problems
  handled_problems = @hosts_problems_down + @hosts_problems_critical + @hosts_problems_unknown
  down_adjusted    = @hosts_down - handled_problems

  {
    handled_problems: handled_problems.to_i,
    down_adjusted: down_adjusted.to_i
  }
end

#hosts_allInteger

returns a counter of all hosts

Examples:

hosts_all

Returns:

  • (Integer)


561
562
563
564
# File 'lib/icinga2/hosts.rb', line 561

def hosts_all
  host_objects if( @hosts_all.nil? || @hosts_all.zero? )
  @hosts_all
end

#list_hosts_with_problems(max_items = 5) ⇒ Hash

return a list of hosts with problems

Examples:

list_hosts_with_problems

Parameters:

  • max_items (Integer) (defaults to: 5)

    numbers of list entries

Returns:

Raises:

  • (ArgumentError)


526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/icinga2/hosts.rb', line 526

def list_hosts_with_problems( max_items = 5 )

  raise ArgumentError.new(format('wrong type. \'max_items\' must be an Integer, given \'%s\'', max_items.class.to_s)) unless( max_items.is_a?(Integer) )

  host_problems = {}
  host_problems_severity = {}

  host_data = host_objects
  host_data = JSON.parse( host_data ) if host_data.is_a?(String)

  unless( host_data.nil? )
    host_data.each do |h,_v|
      name  = h.dig('name')
      state = h.dig('attrs','state')

      next if state.to_i.zero?

      host_problems[name] = host_severity(h)
    end
  end

  # get the count of problems
  #
  host_problems.keys[1..max_items].each { |k| host_problems_severity[k] = host_problems[k] } if( host_problems.count != 0 )

  host_problems_severity
end

#modify_host(params) ⇒ Hash

modify a host

Examples:

param = {
  name: 'foo',
  address: 'foo.bar.com',
  display_name: 'Host for an example Problem',
  max_check_attempts: 10,
}

param = {
  name: 'foo',
  address: 'foo.bar.com',
  notes: 'an demonstration object',
  vars: {
    description: 'schould be delete ASAP',
    os: 'Linux',
    partitions: {
      '/' => {
        crit: '98%',
        warn: '95%'
      }
    }
  },
  merge_vars: true
}

param = {
  name: 'foo',
  address: 'foo.bar.com',
  vars: {
    description: 'removed all other custom vars',
  }
}

add_host(param)

Parameters:

Options Hash (params):

  • name (String)
  • name (String)
  • address (String)
  • address6 (String)
  • display_name (String)
  • enable_notifications (Bool)
  • max_check_attempts (Integer)
  • check_interval (Integer)
  • retry_interval (Integer)
  • notes (String)
  • notes_url (String)
  • action_url (String)
  • check_command (String)
  • check_interval (Integer)
  • check_period (String)
  • check_timeout (Integer)
  • command_endpoint (String)
  • enable_active_checks (Bool)
  • enable_event_handler (Bool)
  • enable_flapping (Bool)
  • enable_passive_checks (Bool)
  • enable_perfdata (Bool)
  • event_command (String)
  • flapping_threshold (Integer)
  • flapping_threshold_high (Integer)
  • flapping_threshold_low (Integer)
  • icon_image (String)
  • icon_image_alt (String)
  • retry_interval (Integer)
  • volatile (Bool)
  • vars (Hash) — default: {}
  • merge_vars (Bool) — default: false

Returns:

Raises:

  • (ArgumentError)


251
252
253
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
283
284
285
286
287
288
289
290
291
292
293
294
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/icinga2/hosts.rb', line 251

def modify_host( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing params') if( params.size.zero? )

  name    = validate( params, required: true, var: 'name', type: String )
  action_url = validate( params, required: false, var: 'action_url', type: String )
  address    = validate( params, required: false, var: 'address', type: String )
  address6   = validate( params, required: false, var: 'address6', type: String )
  check_command = validate( params, required: false, var: 'check_command', type: String )
  check_interval = validate( params, required: false, var: 'check_interval', type: Integer )
  check_period   = validate( params, required: false, var: 'check_period', type: Integer )
  check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
  command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
  display_name   = validate( params, required: false, var: 'display_name', type: String )
  enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
  enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
  enable_flapping   = validate( params, required: false, var: 'enable_flapping', type: Boolean )
  enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean )
  enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
  volatile = validate( params, required: false, var: 'volatile', type: Boolean )
  enable_perfdata   = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
  event_command = validate( params, required: false, var: 'event_command', type: String )
  flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
  groups   = validate( params, required: false, var: 'groups', type: Array )
  icon_image    = validate( params, required: false, var: 'icon_image', type: String )
  icon_image_alt   = validate( params, required: false, var: 'icon_image_alt', type: String )
  notes    = validate( params, required: false, var: 'notes', type: String )
  notes_url   = validate( params, required: false, var: 'notes_url', type: String )
  max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer )
  retry_interval   = validate( params, required: false, var: 'retry_interval', type: Integer )
  templates   = validate( params, required: false, var: 'templates', type: Array ) || [ 'generic-host' ]
  vars   = validate( params, required: false, var: 'vars', type: Hash ) || {}
  zone   = validate( params, required: false, var: 'zone', type: String )
  merge_vars = validate( params, required: false, var: 'merge_vars', type: Boolean ) || false

  # check if host exists
  return { 'code' => 404, 'name' => name, 'status' => 'Object not Found' } unless( exists_host?( name ) )

  # merge the new with the old vars
  if( merge_vars == true )
    current_host = hosts( name: name )
    current_host_vars = current_host.first
    current_host_vars = current_host_vars.dig('attrs','vars')
    current_host_vars = current_host_vars.deep_string_keys
    vars = vars.deep_string_keys unless( vars.empty? )
    vars = current_host_vars.merge( vars )
  end

  payload = {
    templates: templates,
    attrs: {
      action_url: action_url,
      address: address,
      address6: address6,
      check_period: check_period,
      check_command: check_command,
      check_interval: check_interval,
      check_timeout: check_timeout,
      command_endpoint: command_endpoint,
      display_name: display_name,
      enable_active_checks: enable_active_checks,
      enable_event_handler: enable_event_handler,
      enable_flapping: enable_flapping,
      enable_notifications: enable_notifications,
      enable_passive_checks: enable_passive_checks,
      enable_perfdata: enable_perfdata,
      event_command: event_command,
      flapping_threshold: flapping_threshold,
      groups: groups,
      icon_image: icon_image,
      icon_image_alt: icon_image_alt,
      max_check_attempts: max_check_attempts,
      notes: notes,
      notes_url: notes_url,
      retry_interval: retry_interval,
      volatile: volatile,
      zone: zone,
      vars: vars
    }
  }

  # remove all empty attrs
  payload.reject!{ |_k, v| v.nil? }
  payload[:attrs].reject!{ |_k, v| v.nil? }

  post(
    url: format( '%s/objects/hosts/%s', @icinga_api_url_base, name ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end