Module: Icinga2::Tools
- Included in:
- Client
- Defined in:
- lib/icinga2/tools.rb
Overview
namespache for tools
Instance Method Summary collapse
-
#count_problems(objects, state = nil) ⇒ Integer
return count of handled problems.
-
#object_has_been_checked?(object) ⇒ Bool
returns true for the last check.
-
#parse_version(version) ⇒ String
parse version string and extract version and revision.
-
#valid_json?(json) ⇒ Bool
check, it an String a valid Json.
Instance Method Details
#count_problems(objects, state = nil) ⇒ Integer
return count of handled problems
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
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
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
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 |