Class: Pong::Host

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ Host

Returns a new instance of Host.



68
69
70
71
72
73
74
75
# File 'lib/pong.rb', line 68

def initialize(hostname)
  @hostname = hostname
  @address = hostname if hostname =~ /^[0-9\.]*$/
  @address = "127.0.0.1" if hostname == "localhost"
  @output = "/tmp/#{hostname}.out"
  FileUtils.rm_rf(@output) if File.exist?(@output)
  FileUtils.touch(@output)
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



66
67
68
# File 'lib/pong.rb', line 66

def hostname
  @hostname
end

#pingsObject (readonly)

Returns the value of attribute pings.



66
67
68
# File 'lib/pong.rb', line 66

def pings
  @pings
end

#statusObject (readonly)

Returns the value of attribute status.



66
67
68
# File 'lib/pong.rb', line 66

def status
  @status
end

Instance Method Details

#addressObject



77
78
79
# File 'lib/pong.rb', line 77

def address
  @address || "?"
end

#at_least(n) ⇒ Object



123
124
125
126
# File 'lib/pong.rb', line 123

def at_least(n)
  # this fills out the array with nils if it needs to
  @pings[n] = @pings[n] unless n < 0
end

#headerObject



149
150
151
152
153
154
155
# File 'lib/pong.rb', line 149

def header
  [
    "Mean     ",
    "Missing  ",
    "Host"
  ].join("\t")
end

#mean(n = nil) ⇒ Object



136
137
138
# File 'lib/pong.rb', line 136

def mean(n=nil)
  pings.mean(n)
end

#missing(n = nil) ⇒ Object



128
129
130
# File 'lib/pong.rb', line 128

def missing(n=nil)
  pings.nils(n)
end

#missing_percent(n = nil) ⇒ Object



132
133
134
# File 'lib/pong.rb', line 132

def missing_percent(n=nil)
  pings.nil_percent(n)
end

#parse(line) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pong.rb', line 109

def parse(line)
  m = /^PING (\S*) \(([0-9.]*)\)/.match(line)
  unless m.nil?
    @address = m[2]
  end

  m = /icmp_seq=([0-9]*).*time=([0-9.]*)/.match(line)
  unless m.nil?
    seq = m[1].to_i
    time = m[2].to_f
    pings[seq] = time
  end
end

#resolve_addressObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pong.rb', line 92

def resolve_address
  while @address.nil?
    begin
      @status = "Resolving..."
      @address = Resolv::DNS.new.getaddresses(hostname).first
      if @address.nil?
        @status = "Resolve failed"
        sleep 5
      else
        @status = nil
      end
    rescue => e
      @status = e.to_s
      sleep 5
    end
  end
end

#startObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/pong.rb', line 81

def start
  @pings = Series.new
  Thread.new do
    resolve_address
    process = IO.popen("ping #{address}")
    while (line = process.gets)
      parse(line) unless line.nil?
    end
  end
end

#stats(n = nil) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/pong.rb', line 140

def stats(n=nil)
  [
    "%8.3f msec" % (mean(n)),
    "%4d (%.2f%%)" % [missing(n), missing_percent(n)],
    "#{hostname} (#{address})",
    "#{status}",
  ].join("\t")
end