Module: Icinga2::Tools

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

Overview

namespache for tools

Instance Method Summary collapse

Instance Method Details

#handled_problems(objects, status) ⇒ Integer

return count of handled problems

Examples:

for host objects

h_objects = @icinga.host_objects
all_hosts = h_objects.dig(:nodes)
warning = @icinga.handled_problems(all_hosts, Icinga2::HANDLED_WARNING)
critical = @icinga.handled_problems(all_hosts, Icinga2::HANDLED_CRITICAL)
unknown = @icinga.handled_problems(all_hosts, Icinga2::HANDLED_UNKNOWN)

for service objects

s_objects = @icinga.service_objects
all_services = s_objects.dig(:nodes)
warning = @icinga.handled_problems(all_services, Icinga2::HANDLED_WARNING)
critical = @icinga.handled_problems(all_services, Icinga2::HANDLED_CRITICAL)
unknown = @icinga.handled_problems(all_services, Icinga2::HANDLED_UNKNOWN)

Parameters:

  • objects (Hash)
  • status (Integer)

Returns:

  • (Integer)


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/tools.rb', line 67

def handled_problems(objects, status)

  problems = 0

  objects = JSON.parse(objects) if  objects.is_a?(String)
  nodes = objects.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 == status && downtime_depth.zero? && acknowledgement.zero? )
        problems += 1
      end

    end
  end
  problems
end

#object_has_been_checked?(object) ⇒ Bool

returns true for the last check

Parameters:

  • object (Hash)

Returns:

  • (Bool)


16
17
18
# File 'lib/icinga2/tools.rb', line 16

def object_has_been_checked?(object)
  object.dig('attrs', 'last_check').positive?
end

#parse_version(version) ⇒ String

parse version string and extract version and revision

Parameters:

  • version (String)

Returns:

  • (String, String)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/icinga2/tools.rb', line 26

def parse_version(version)

  # version = "v2.4.10-504-gab4ba18"
  # version = "v2.4.10"
  version_map = version.split('-', 2)
  version_str = version_map.first
  # strip v2.4.10 (default) and r2.4.10 (Debian)
  version_str = version_str.scan(/^[vr]+(.*)/).last.first

  revision =
  if version_map.size > 1
    version_map.last
  else
    'release'
  end

  [version_str, revision]
end