Module: Icinga2::Services

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

Overview

namespace for service handling

Instance Method Summary collapse

Instance Method Details

#add_services(params = {}) ⇒ Hash

TODO:

this function is not operable need help, time or beer

add services

Parameters:

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

Options Hash (params):

  • :host (String)
  • :services (String)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/icinga2/services.rb', line 21

def add_services( params = {} )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )

  # TODO
  puts 'add_services() ToDo'

  host_name = params.dig(:host)
  services  = params.dig(:services)

  services.each do |s,v|

    payload = {
      'templates' => [ 'generic-service' ],
      'attrs'     => update_host( v, host_name )
    }

    logger.debug( s )
    logger.debug( v.to_json )

    logger.debug( JSON.pretty_generate( payload ) )

    Network.put(
      url: format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, s ),
      headers: @headers,
      options: @options,
      payload: payload
    )
  end

end

#count_services_with_problemsInteger

return count of services with problems

Examples:

@icinga.count_services_with_problems

Returns:

  • (Integer)


255
256
257
258
259
260
261
262
263
# File 'lib/icinga2/services.rb', line 255

def count_services_with_problems

  service_data = service_objects
  service_data = JSON.parse(service_data) if service_data.is_a?(String)

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

  f.size
end

#exists_service?(params) ⇒ Bool

returns true if the service exists

Examples:

@icinga.exists_service?(host: 'icinga2', service: 'users' )

Parameters:

  • params (Hash)

Options Hash (params):

  • :host (String)
  • :service (String)

Returns:

  • (Bool)

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/icinga2/services.rb', line 128

def exists_service?( 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)
  service = params.dig(:service)

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

  result = services( host: host, service: service )
  result = JSON.parse( result ) if  result.is_a?( String )

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

  false
end

#list_services_with_problems(max_items = 5) ⇒ Hash

return a list of services with problems

Examples:

problems, problems_and_severity = @icinga.list_services_with_problems.values

l = @icinga.list_services_with_problems
problems_and_severity = l.dig(:services_with_problems_and_severity)

Parameters:

  • max_items (Integer) (defaults to: 5)

    numbers of list entries

Returns:

  • (Hash)
    • Array (services_with_problems)

    • Hash (services_with_problems_and_severity)



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

def list_services_with_problems( max_items = 5 )

  services_with_problems = {}
  services_with_problems_and_severity = {}

  # only fetch the minimal attribute set required for severity calculation
  services_data = service_objects
  services_data = JSON.parse( services_data ) if services_data.is_a?(String)

  unless( services_data.nil? )

    services_data.each do |s,_v|

      name  = s.dig('name')
      state = s.dig('attrs','state')
      next if  state.zero?

      services_with_problems[name] = service_severity(s)
    end

    if( services_with_problems.count != 0 )
      services_with_problems.sort.reverse!
      services_with_problems = services_with_problems.keys[1..max_items].each { |k,_v| services_with_problems_and_severity[k] = services_with_problems[k] }
    end
  end

  {
    services_with_problems: services_with_problems,
    services_with_problems_and_severity: services_with_problems_and_severity
  }
end

#service_objects(params = {}) ⇒ Array

returns service objects

Examples:

with default attrs and joins

@icinga.service_objects
@icinga.service_objects(attrs: ['name', 'state'], joins: ['host.name','host.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: ['host.name', 'host.state', 'host.acknowledgement', 'host.downtime_depth', 'host.last_check']

Returns:

  • (Array)


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
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/services.rb', line 162

def service_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

  if( joins.nil? )
    joins = ['host.name', 'host.state', 'host.acknowledgement', 'host.downtime_depth', 'host.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/services', @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_services = results.clone

      unless( all_services.nil? )

        @services_all              = all_services.size
        @services_problems         = count_problems(results)
        @services_handled_warning  = count_problems(results, Icinga2::SERVICE_STATE_WARNING)
        @services_handled_critical = count_problems(results, Icinga2::SERVICE_STATE_CRITICAL)
        @services_handled_unknown  = count_problems(results, Icinga2::SERVICE_STATE_UNKNOWN)
      end
    end
  end

  results
end

#service_problems_handledHash

returns data with service problems they be handled (acknowledged or in downtime)

Examples:

@icinga.cib_data
@icinga.service_objects
all, critical, warning, unknown = @icinga.service_problems_handled.values

p = @icinga.service_problems_handled
warning = p.dig(:warning)

Returns:

  • (Hash)
    • all

    • critical

    • warning

    • unknown



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

def service_problems_handled

  problems_all      = @services_handled.nil?          ? 0 : @services_handled
  problems_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
  problems_warning  = @services_handled_warning.nil?  ? 0 : @services_handled_warning
  problems_unknown  = @services_handled_unknown.nil?  ? 0 : @services_handled_unknown

  {
    all: problems_all.to_i,
    critical: problems_critical.to_i,
    warning: problems_warning.to_i,
    unknown: problems_unknown.to_i
  }

#      [problems_all,problems_critical,problems_warning,problems_unknown]
end

#services(params = {}) ⇒ Hash

return services

Examples:

to get all services

@icinga.services

to get one service for host

@icinga.services( host: 'icinga2', service: 'ping4' )

Parameters:

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

Options Hash (params):

  • :host (String)
  • :service (String)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/icinga2/services.rb', line 92

def services( params = {} )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )

  host_name = params.dig(:host)
  service   = params.dig(:service)

  url =
  if( service.nil? )
    format( '%s/objects/services/%s', @icinga_api_url_base, host_name )
  else
    format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, service )
  end

  data = Network.api_data(
    url: url,
    headers: @headers,
    options: @options
  )

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

  nil
end

#services_adjustedHash

returns adjusted service state

Examples:

@icinga.cib_data
@icinga.service_objects
warning, critical, unknown = @icinga.services_adjusted.values

s = @icinga.services_adjusted
unknown = s.dig(:unknown)

Returns:

  • (Hash)
    • warning

    • critical

    • unknown



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/icinga2/services.rb', line 227

def services_adjusted

  service_warning          = @services_warning.nil?          ? 0 : @services_warning
  service_critical         = @services_critical.nil?         ? 0 : @services_critical
  service_unknown          = @services_unknown.nil?          ? 0 : @services_unknown
  service_handled_warning  = @services_handled_warning.nil?  ? 0 : @services_handled_warning
  service_handled_critical = @services_handled_critical.nil? ? 0 : @services_handled_critical
  service_handled_unknown  = @services_handled_unknown.nil?  ? 0 : @services_handled_unknown

  # calculate service problems adjusted by handled problems
  service_adjusted_warning  = service_warning  - service_handled_warning
  service_adjusted_critical = service_critical - service_handled_critical
  service_adjusted_unknown  = service_unknown  - service_handled_unknown

  {
    warning: service_adjusted_warning.to_i,
    critical: service_adjusted_critical.to_i,
    unknown: service_adjusted_unknown.to_i
  }
end

#services_allInteger

returns a counter of all services

Examples:

@icinga.cib_data
@icinga.service_objects
@icinga.services_all

Returns:

  • (Integer)


350
351
352
# File 'lib/icinga2/services.rb', line 350

def services_all
  @services_all
end

#unhandled_services(params = {}) ⇒ Nil

TODO:

this function is not operable need help, time or beer

return all unhandled services

Parameters:

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

Returns:

  • (Nil)

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/icinga2/services.rb', line 63

def unhandled_services( params = {} )

  raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )

  # TODO
  puts 'unhandled_services() ToDo'

  # taken from https://blog.netways.de/2016/11/18/icinga-2-api-cheat-sheet/
  # 5) Anzeige aller Services die unhandled sind und weder in Downtime, noch acknowledged sind
  # /usr/bin/curl -k -s -u 'root:icinga' -H 'X-HTTP-Method-Override: GET' -X POST
  # 'https://127.0.0.1:5665/objects/services' #
  # -d '{ "attrs": [ "__name", "state", "downtime_depth", "acknowledgement" ], "filter": "service.state != ServiceOK && service.downtime_depth == 0.0 && service.acknowledgement == 0.0" }''' | jq

end

#update_host(hash, host) ⇒ Hash

TODO:

this function are not operable need help, time or beer

update host

Parameters:

  • hash (Hash)
  • host (String)

Returns:

  • (Hash)


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/icinga2/services.rb', line 322

def update_host( hash, host )

  hash.each do |k, v|

    if( k == 'host' && v.is_a?( String ) )
      v.replace( host )

    elsif( v.is_a?( Hash ) )
      update_host( v, host )

    elsif( v.is_a?(Array) )

      v.flatten.each { |x| update_host( x, host ) if x.is_a?( Hash ) }
    end
  end

  hash
end