class Scriptroute::LivenessTest

Attributes

broken[R]
pingfiltering[R]
responsives[R]

whether a host is responsive for measurement; try_icmp or try_tcp.

unresponsives[R]

Public Class Methods

new(destinations) click to toggle source
# File lib/scriptroute/liveness.rb, line 89
def initialize(destinations)
  @responsives = []
  @unresponsives = []
  @broken = []
  if(destinations.is_a?(String)) then
    # promote to an array
    destinations = [ destinations ]
  end
  if(destinations.length == 0) then
    raise "Empty array passed to LivenessTest.new()"
  end
  icmp_responsive, icmp_unresponsive = LivenessTest.try_icmp(destinations);
  @pingfiltering, @unresponsives = if(icmp_unresponsive.length > 0) then
                                     LivenessTest.try_tcp(icmp_unresponsive, false);
                                   else
                                     [[], [] ]
                                   end
  @responsives, @broken = if(icmp_responsive.length > 0) then
                            LivenessTest.try_tcp(icmp_responsive, true);
                          else
                            [[], []]
                          end
end
try_icmp(destinations) click to toggle source
# File lib/scriptroute/liveness.rb, line 9
  def LivenessTest.try_icmp(destinations)
    retvalResponsives = []
    retvalUnresponsives = []
    begin 
      responses = Scriptroute::send_train( destinations.map { |dst|
          begin
            probe = Scriptroute::ICMPecho.new
            probe.ip_dst = dst
            Struct::DelayedPacket.new(0.01,probe)
          rescue RuntimeError => e
            # happens if dns lookup doesn't work; we'll mark it dead.
            # 2013-aug added to handle dns fail.
            retvalUnresponsives.push(dst)
            nil
          rescue => e
            # happens if dns lookup doesn't work; we'll mark it dead.
            retvalUnresponsives.push(dst)
            nil
          end
        }.compact )
      responses.each_with_index { |tuple,i|
        if(tuple.response && 
            tuple.response.packet.is_a?(Scriptroute::ICMP) && 
            tuple.response.packet.icmp_type == Scriptroute::ICMP::ICMP_ECHOREPLY) then
          retvalResponsives.push(destinations[i])
        else
          retvalUnresponsives.push(destinations[i])
        end
      }
    rescue => e
      if(e=~/packet (%d) of %d/) then
        puts "LivenessTest.try_icmp(#{destinations.join(', ')}) took #{e} when 
contacting %s (or maybe %s)" % [ destinations[$1.to_i], destinations[$1.to_i-1] ]
      else
        puts "LivenessTest.try_icmp(#{destinations.join(', ')}) took #{e}"
      end
    end
    return retvalResponsives, retvalUnresponsives;
  end
try_tcp(destinations, use_syn=false) click to toggle source
# File lib/scriptroute/liveness.rb, line 49
def LivenessTest.try_tcp(destinations, use_syn=false) 
  retvalResponsives = []
  retvalUnresponsives = []
  if (destinations.length < 1) then
    raise "you want to give a list of destinations to probe"
  end
  begin
    responses = Scriptroute::send_train( destinations.map { |dst|
        probe = Scriptroute::TCP.new(0) 
        probe.ip_dst = dst
        probe.flag_syn = use_syn
        probe.flag_ack = !use_syn
        if(use_syn) then 
          probe.th_dport = 80
        end
        Struct::DelayedPacket.new(0.01,probe)
      } )
    responses.each_with_index { |tuple,i|
      puts tuple.probe.inspect
      puts (tuple.response.inspect or "no response")
      # puts tuple.to_s
      if(tuple.response && tuple.response.packet.is_a?(Scriptroute::TCP) && 
          ( (use_syn && tuple.response.packet.flag_syn) || 
            (!use_syn && tuple.response.packet.flag_rst)) ) then
        retvalResponsives.push(destinations[i])
      else
        retvalUnresponsives.push(destinations[i])
      end
    }
  rescue => e
    if(e.to_s =~ /packet (\d+) of \d+/) then
      puts "LivenessTest.try_tcp(...) took #{e} (#{$1} is to #{destinations[$1.to_i]} maybe #{destinations[$1.to_i - 1]} "
    else
      puts "LivenessTest.try_tcp(#{destinations.join(', ')}) took #{e} #{e.backtrace[0]}"
    end
    return destinations, []; 
  end
  return retvalResponsives, retvalUnresponsives;
end

Public Instance Methods

summary() click to toggle source
# File lib/scriptroute/liveness.rb, line 119
def summary
  "%d responsive, %d unresponsive" % [ @responsives.length, @unresponsives.length ]
end
to_s() click to toggle source
# File lib/scriptroute/liveness.rb, line 113
def to_s
  "Responsives:   " + @responsives.join(" ") + 
    "\nUnresponsivees: " + @unresponsives.join(" ") +
    "\nPing Filtering: " + @pingfiltering.join(" ") +
    "\nBroken (ping not http): " + @broken.join(" ")
end