Class: Scriptroute::LivenessTest

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptroute/liveness.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destinations) ⇒ LivenessTest

Returns a new instance of LivenessTest.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

Instance Attribute Details

#brokenObject (readonly)

do ping, but not http.



7
8
9
# File 'lib/scriptroute/liveness.rb', line 7

def broken
  @broken
end

#pingfilteringObject (readonly)

Returns the value of attribute pingfiltering.



6
7
8
# File 'lib/scriptroute/liveness.rb', line 6

def pingfiltering
  @pingfiltering
end

#responsivesObject (readonly)

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



5
6
7
# File 'lib/scriptroute/liveness.rb', line 5

def responsives
  @responsives
end

#unresponsivesObject (readonly)

Returns the value of attribute unresponsives.



8
9
10
# File 'lib/scriptroute/liveness.rb', line 8

def unresponsives
  @unresponsives
end

Class Method Details

.try_icmp(destinations) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

Instance Method Details

#summaryObject



119
120
121
# File 'lib/scriptroute/liveness.rb', line 119

def summary
  "%d responsive, %d unresponsive" % [ @responsives.length, @unresponsives.length ]
end

#to_sObject



113
114
115
116
117
118
# 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