lib/arborist/monitor/fping.rb
changeset 0 d99e1dffbc72
child 1 69f2ba4d4d93
equal deleted inserted replaced
-1:000000000000 0:d99e1dffbc72
       
     1 # -*- ruby -*-
       
     2 # vim: set noet nosta sw=4 ts=4 :
       
     3 # encoding: utf-8
       
     4 #
       
     5 # This library parses fping output when provided a list of nodes to
       
     6 # monitor.  Require it from your monitor file(s), and call the Arborist
       
     7 # exec() with this class -- that's it.
       
     8 #
       
     9 #    require 'arborist/monitor/fping'
       
    10 #
       
    11 #    Arborist::Monitor 'ping check' do
       
    12 #        every 20.seconds
       
    13 #        match type: 'host'
       
    14 #        include_down true
       
    15 #        use :addresses
       
    16 #        exec 'fping', '-e', '-t', '150'
       
    17 #        exec_callbacks( Arborist::Monitor::FPing )
       
    18 #    end
       
    19 
       
    20 
       
    21 require 'loggability'
       
    22 require 'arborist/monitor' unless defined?( Arborist::Monitor )
       
    23 
       
    24 # Parse Fping output for better batch ICMP checks.
       
    25 #
       
    26 module Arborist::Monitor::FPing
       
    27 	extend Loggability
       
    28 	log_to :arborist
       
    29 
       
    30 	# The version of this library.
       
    31 	VERSION = '0.1.0'
       
    32 
       
    33 	attr_accessor :identifiers
       
    34 
       
    35 	def exec_arguments( nodes )
       
    36 		self.log.debug "Building fping arguments for %d nodes" % [ nodes.size ]
       
    37 		self.identifiers = nodes.each_with_object({}) do |(identifier, props), hash|
       
    38 			next unless props.key?( 'addresses' )
       
    39 			address = props[ 'addresses' ].first
       
    40 			hash[ address ] = identifier
       
    41 		end
       
    42 
       
    43 		return {} if self.identifiers.empty?
       
    44 		return self.identifiers.keys
       
    45 	end
       
    46 
       
    47 	def handle_results( pid, stdout, stderr )
       
    48 		# 8.8.8.8 is alive (32.1 ms)
       
    49 		# 8.8.4.4 is alive (14.9 ms)
       
    50 		# 8.8.0.1 is unreachable
       
    51 
       
    52 		return stdout.each_line.with_object({}) do |line, hash|
       
    53 			address, remainder = line.split( ' ', 2 )
       
    54 			identifier = self.identifiers[ address ] or next
       
    55 
       
    56 			if remainder =~ /is alive \((\d+\.\d+) ms\)/
       
    57 				hash[ identifier ] = { rtt: Float( $1 ) }
       
    58 			else
       
    59 				hash[ identifier ] = { error: remainder.chomp }
       
    60 			end
       
    61 		end
       
    62 	end
       
    63 end # Arborist::Monitor::FPing
       
    64