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)

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)

Raises:

  • (ArgumentError)


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
91
92
93
94
95
# File 'lib/icinga2/hosts.rb', line 35

def add_host( params )

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

  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) || {}

  raise ArgumentError.new('Missing host') if( host.nil? )
  raise ArgumentError.new('only true or false for notifications are allowed') unless( notifications.is_a?(TrueClass) || notifications.is_a?(FalseClass) )
  raise ArgumentError.new('only Integer for max_check_attempts are allowed') unless( max_check_attempts.is_a?(Integer) )
  raise ArgumentError.new('only Integer for check_interval are allowed') unless( check_interval.is_a?(Integer) )
  raise ArgumentError.new('only Integer for retry_interval are allowed') unless( retry_interval.is_a?(Integer) )
  raise ArgumentError.new('only String for notes are allowed') unless( notes.is_a?(String) || notes.nil? )
  raise ArgumentError.new('only String for notes_url are allowed') unless( notes_url.is_a?(String) || notes_url.nil? )
  raise ArgumentError.new('only Hash for vars are allowed') unless( vars.is_a?(Hash) )

  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(
    url: format( '%s/objects/hosts/%s', @icinga_api_url_base, host ),
    headers: @headers,
    options: @options,
    payload: payload
  )
end

#count_hosts_with_problemsInteger

return count of hosts with problems

Examples:

@icinga.count_hosts_with_problems

Returns:

  • (Integer)


293
294
295
296
297
298
299
300
301
# File 'lib/icinga2/hosts.rb', line 293

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:

@icinga.delete_host(host: 'foo')

Parameters:

  • params (Hash)

Options Hash (params):

  • :host (String)

    host to delete

Returns:

  • (Hash)

    result

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/icinga2/hosts.rb', line 107

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? )

  host = params.dig(:host)

  raise ArgumentError.new('Missing host') if( host.nil? )

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

#exists_host?(host_name) ⇒ Bool

returns true if the host exists

Examples:

@icinga.exists_host?('icinga2')

Parameters:

  • host_name (String)

Returns:

  • (Bool)

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/icinga2/hosts.rb', line 170

def exists_host?( host_name )

  raise ArgumentError.new('only String are allowed') unless( host_name.is_a?(String) )
  raise ArgumentError.new('Missing host_name') if( host_name.size.zero? )

  result = hosts( host: host_name )
  result = JSON.parse( result ) if  result.is_a?( String )

  return true if  !result.nil? && result.is_a?(Array)

  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)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
244
245
246
247
248
249
250
251
252
# File 'lib/icinga2/hosts.rb', line 198

def host_objects( params = {} )

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

#       raise ArgumentError.new('only Array for attrs are allowed') unless( attrs.is_a?(Hash) )
#       raise ArgumentError.new('only Array for filter are allowed') unless( filter.is_a?(Hash) )
#       raise ArgumentError.new('only Array for joins are allowed') unless( joins.is_a?(Hash) )

  payload = {}
  results = nil

  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?

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

  status  = data.dig(:status)

  if( status.nil? )

    results = data.dig('results')

    unless( results.nil? )

      all_hosts = results.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(results)
        # global var for count of all gost with state HOSTS_DOWN
        @hosts_problems_down     = count_problems(results, Icinga2::HOSTS_DOWN)
        @hosts_problems_critical = count_problems(results, Icinga2::HOSTS_CRITICAL)
        @hosts_problems_unknown  = count_problems(results, Icinga2::HOSTS_UNKNOWN)

      end
    end
  end

  results
end

#host_problemsHash

returns data with host problems

Examples:

@icinga.host_objects
all, down, critical, unknown = @icinga.host_problems.values

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

Returns:

  • (Hash)
    • all

    • down

    • critical

    • unknown



370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/icinga2/hosts.rb', line 370

def host_problems

  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

  {
    all: problems_all.to_i,
    down: problems_down.to_i,
    critical: problems_critical.to_i,
    unknown: problems_unknown.to_i
  }
end

#hosts(params = {}) ⇒ Array

return hosts

Examples:

to get all hosts

@icinga.hosts

to get one host

@icinga.hosts(host: 'icinga2')

Parameters:

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

Options Hash (params):

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

Returns:

  • (Array)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/icinga2/hosts.rb', line 139

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?

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

  return data.dig('results') if( data.dig(:status).nil? )

  nil
end

#hosts_adjustedHash

returns adjusted hosts state

Examples:

@icinga.cib_data
@icinga.host_objects
handled, down = @icinga.hosts_adjusted.values

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

Returns:

  • (Hash)
    • handled_problems

    • down_adjusted

Raises:

  • (ArgumentError)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/icinga2/hosts.rb', line 268

def hosts_adjusted

  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:

@icinga.host_objects
@icinga.hosts_all

Returns:

  • (Integer)


351
352
353
# File 'lib/icinga2/hosts.rb', line 351

def hosts_all
  @hosts_all
end

#list_hosts_with_problems(max_items = 5) ⇒ Hash

return a list of hosts with problems

Examples:

@icinga.list_hosts_with_problems

Parameters:

  • max_items (Integer) (defaults to: 5)

    numbers of list entries

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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
# File 'lib/icinga2/hosts.rb', line 312

def list_hosts_with_problems( max_items = 5 )

  raise ArgumentError.new('only Integer for max_items are allowed') 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
  #
  if( host_problems.count != 0 )
    host_problems.keys[1..max_items].each { |k,_v| host_problems_severity[k] = host_problems[k] }
  end

  host_problems_severity
end