Module: Icinga2::Tools

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

Overview

namespache for tools

Instance Method Summary collapse

Instance Method Details

#count_problems(objects, state = nil) ⇒ Integer

return count of handled problems

Examples:

for host objects

h_objects = @icinga.host_objects
all = @icinga.count_problems(h_objects)
down = @icinga.count_problems(h_objects, Icinga2::HOSTS_DOWN)
critical = @icinga.count_problems(h_objects, Icinga2::HOSTS_CRITICAL)
unknown = @icinga.count_problems(h_objects, Icinga2::HOSTS_UNKNOWN)

for service objects

s_objects = @icinga.service_objects
all = @icinga.count_problems(s_objects)
warning = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_WARNING)
critical = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_CRITICAL)
unknown = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_UNKNOWN)

Parameters:

  • objects (Hash)
  • state (Integer) (defaults to: nil)

    (nil)

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
91
# File 'lib/icinga2/tools.rb', line 67

def count_problems( objects, state = nil )

  compare_states = []

  unless( state.nil? )

    # 0 = "Up"   or "OK"
    # 1 = "Down" or "Warning"
    # 2 = "Critical"
    # 3 = "Unknown"
    compare_states = [1, 2, 3]
  end

  compare_states.push(state) if( state.is_a?(Integer) )

  objects = JSON.parse(objects) if objects.is_a?(String)

  f = objects.select do |t|
    t.dig('attrs','state') == state && \
        ( !t.dig('attrs','downtime_depth').nil? && t.dig('attrs','downtime_depth').zero?) && \
        ( !t.dig('attrs','acknowledgement').nil? && t.dig('attrs','acknowledgement').zero? )
  end

  f.size
end

#object_has_been_checked?(object) ⇒ Bool

returns true for the last check

Parameters:

Returns:

  • (Bool)


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

def object_has_been_checked?( object )
  object.dig('attrs', 'last_check') > 0
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