Class: Pong::Host
- Inherits:
-
Object
- Object
- Pong::Host
- Defined in:
- lib/pong.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#pings ⇒ Object
readonly
Returns the value of attribute pings.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #address ⇒ Object
- #at_least(n) ⇒ Object
- #header ⇒ Object
-
#initialize(hostname) ⇒ Host
constructor
A new instance of Host.
- #mean(n = nil) ⇒ Object
- #missing(n = nil) ⇒ Object
- #missing_percent(n = nil) ⇒ Object
- #parse(line) ⇒ Object
- #resolve_address ⇒ Object
- #start ⇒ Object
- #stats(n = nil) ⇒ Object
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
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
66 67 68 |
# File 'lib/pong.rb', line 66 def hostname @hostname end |
#pings ⇒ Object (readonly)
Returns the value of attribute pings.
66 67 68 |
# File 'lib/pong.rb', line 66 def pings @pings end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
66 67 68 |
# File 'lib/pong.rb', line 66 def status @status end |
Instance Method Details
#address ⇒ Object
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 |
#header ⇒ Object
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_address ⇒ Object
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 |
#start ⇒ Object
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 |