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(host, services = {}) ⇒ Hash

add services

Parameters:

  • host (String)
  • services (Hash) (defaults to: {})

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/icinga2/services.rb', line 17

def add_services( host, services = {} )

  services.each do |s,v|

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

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

    logger.debug( JSON.pretty_generate( payload ) )

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

    logger.debug( result )

  end

end

#exists_service?(params = {}) ⇒ Bool

returns true if the service exists

Examples:

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

Parameters:

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

Options Hash (params):

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

Returns:

  • (Bool)


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

def exists_service?( params = {} )

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

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

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

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

#problem_services(max_items = 5) ⇒ Hash

return a list of services with problems

Examples:

@icinga.problem_services

Parameters:

  • max_items (Integer) (defaults to: 5)

    numbers of list entries

Returns:

  • (Hash)


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

def problem_services( max_items = 5 )

  @service_problems = {}
  @service_problems_severity = {}

  # only fetch the minimal attribute set required for severity calculation
  services_data = service_objects

  if( services_data.is_a?(String) )
    services_data = JSON.parse( services_data )
  end

  services_data = services_data.dig(:nodes)

  unless !services_data

    services_data.each do |_service,v|

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

      @service_problems[name] = service_severity(v)
    end

    @service_problems.sort.reverse!
    @service_problems.keys[1..max_items].each { |k,_v| @service_problems_severity[k] = @service_problems[k] }
  end
  @service_problems_severity
end

#service_objects(params = {}) ⇒ Hash

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:

  • (Hash)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/icinga2/services.rb', line 140

def service_objects( params = {} )

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

  if( attrs.nil? )
    attrs = ['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?

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

end

#service_problemsInteger

return count of services with problems

Examples:

@icinga.service_problems

Returns:

  • (Integer)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/icinga2/services.rb', line 174

def service_problems

  data     = service_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

#service_severity(service) ⇒ Hash

calculate a service severity

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

Parameters:

  • service (Hash)

Returns:

  • (Hash)


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

def service_severity(service)

  attrs           = service.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?(service)

  unless state.zero?

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

    # requires joins
    host_attrs = service.dig('joins','host')
    host_state           = host_attrs.dig('state')
    host_acknowledgement = host_attrs.dig('acknowledgement')
    host_downtime_depth  = host_attrs.dig('downtime_depth')

    severity +=
      if host_state > 0
        1024
      elsif host_acknowledgement
        512
      elsif host_downtime_depth > 0
        256
      else
        2048
      end

  end

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/icinga2/services.rb', line 75

def services( params = {} )

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

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

  Network.get(         host: name,
    url: url,
    headers: @headers,
    options: @options )

end

#unhandled_services(params = {}) ⇒ Nil

TODO:

this function are not operable need help, time or beer

return all unhandled services

Parameters:

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

Returns:

  • (Nil)


53
54
55
56
57
58
59
# File 'lib/icinga2/services.rb', line 53

def unhandled_services( params = {} )

  # 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/v1/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)


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/icinga2/services.rb', line 248

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