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 = host_objects
all = count_problems(h_objects)
down = count_problems(h_objects, Icinga2::HOSTS_DOWN)
critical = count_problems(h_objects, Icinga2::HOSTS_CRITICAL)
unknown = count_problems(h_objects, Icinga2::HOSTS_UNKNOWN)

for service objects

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

Parameters:

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

    (nil)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


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

def count_problems( objects, state = nil )

  raise ArgumentError.new(format('wrong type. \'objects\' must be an Array, given \'%s\'', objects.class.to_s)) unless( objects.is_a?(Array) )
  #raise ArgumentError.new('missing objects') if( objects.size.zero? )

  raise ArgumentError.new(format('wrong type. \'state\' must be an Integer, given \'%s\'', state.class.to_s)) unless( state.nil? || state.is_a?(Integer) )

  # 0 = "Up"   or "OK"
  # 1 = "Down" or "Warning"
  # 2 = "Critical"
  # 3 = "Unknown"
  compare_states = []
  compare_states = [1, 2, 3] unless( state.nil? )

  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:

Returns:



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

#valid_json?(json) ⇒ Bool

check, it an String a valid Json

Examples:

valid_json?( json )

Parameters:

Returns:

  • (Bool)


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/icinga2/tools.rb', line 104

def valid_json?( json )

  begin
    JSON.parse( json )
    return true
  rescue JSON::ParserError => e
    @logger.error("json parse error: #{e}") if @debug
    return false
  end

end