Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

strip rest, add "round trip time", to strict

  • Loading branch information...
commit 22a806307546500a8ed038f374024c217c6c1cb6 1 parent ad0491c
@kachick authored
Showing with 41 additions and 13 deletions.
  1. +36 −8 lib/mswindows/ping.rb
  2. +5 −5 lib/wrapingr.rb
View
44 lib/mswindows/ping.rb
@@ -6,6 +6,8 @@ module MSWindows
module Ping
+ class MalformedDataError < TypeError; end
+
class RecordRoute < Striuct
class << self
@@ -15,11 +17,29 @@ class << self
def parse(str)
define {|pr|
source = str.dup
- source.slice!(/^Pinging.+:\n/)
- pr.summary = source.slice!(/^((?:Reply|Request|Destination).+)\n/, 1)
- source.slice!(/\n+/)
- source.slice!(/^Ping statistics.*?\n/)
+ if source.slice!(/^Pinging.+:\n/)
+ unless pr.summary = source.slice!(/^((?:Reply|Request|Destination).+)\n/, 1)
+ raise MalformedDataError
+ end
+ else
+ raise MalformedDataError
+ end
+
+ source.slice!(/\n+/)
+
+ if source.slice!(/^Approximate round trip.*?\n+\s*Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms.*?\n?/)
+ pr.round_trip_min, pr.round_trip_max, pr.round_trip_ave = $1.to_i, $2.to_i, $3.to_i
+ else
+ raise MalformedDataError
+ end
+ if source.slice!(/^Ping statistics.*?\n+\s*Packets: Sent = (\d+), Received = (\d+), Lost = (\d+).+loss.+\n?/)
+ pr.sent_count, pr.recived_count = $1.to_i, $2.to_i
+ raise MalformedDataError unless pr.lost_count == $3.to_i
+ else
+ raise MalformedDataError
+ end
+
1.upto 9 do |i|
pr[:"route#{i}"] = (
if source.slice!(/^\s+(?:Route:)?\s*(\S+)\s*(?:->)?\s*$/)
@@ -30,15 +50,15 @@ def parse(str)
)
end
- unless source.empty?
- pr.rest = source.gsub("\n", '\n')
- end
+ pr.rest = source.empty? ? nil : source.strip.gsub("\n", '\n')
}
end
end
member :summary, String
+ member :sent_count, Integer
+ member :recived_count, Integer
member :route1, OR(nil, String)
member :route2, OR(nil, String)
member :route3, OR(nil, String)
@@ -48,8 +68,16 @@ def parse(str)
member :route7, OR(nil, String)
member :route8, OR(nil, String)
member :route9, OR(nil, String)
+ member :round_trip_min, Integer
+ member :round_trip_max, Integer
+ member :round_trip_ave, Integer
member :rest, OR(nil, String)
close_member
+
+ # @return [Integer]
+ def lost_count
+ sent_count - recived_count
+ end
# @return [Array<String>]
def hops
@@ -59,7 +87,7 @@ def hops
alias_method :route, :hops
def pass?
- !!route1
+ lost_count == 0
end
end
View
10 lib/wrapingr.rb
@@ -9,10 +9,10 @@ module Wrapingr
CSV_OPTIONS = {
write_headers: true,
- headers: %w[Time Pass? RouteChanged? 1 2 3 4 5 6 7 8 9 Summary Rest]
+ headers: %w[Time Pass? RouteChanged? 1 2 3 4 5 6 7 8 9 RoundTrip(msec) Summary Rest]
}.freeze
- VERSION = '0.2.1'.freeze
+ VERSION = '0.2.2'.freeze
# @param [String] dest_addr
# @param [Numeric] interval
@@ -38,8 +38,8 @@ def run(dest_addr, interval)
loop do
time, cmd_output = Time.now, `#{command}`
ftime = ftime time
- last = result = Ping::RecordRoute.parse cmd_output
-
+ last = result = MSWindows::Ping::RecordRoute.parse cmd_output
+
if result.pass?
if last_passed
route_changed = (result.route != last_passed.route)
@@ -60,7 +60,7 @@ def run(dest_addr, interval)
route_summaries[4..9].join(' -> ')
)
- csv << [ftime, result.pass?, route_changed, *result.route, result.summary, result.rest]
+ csv << [ftime, result.pass?, route_changed, *result.route, result.round_trip_ave, result.summary, result.rest]
sleep interval
end
Please sign in to comment.
Something went wrong with that request. Please try again.