Skip to content

Instantly share code, notes, and snippets.

@Zarthus

Zarthus/r.rb Secret

Last active May 16, 2016 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zarthus/e882cc945418a698a0a9fcd588851604 to your computer and use it in GitHub Desktop.
Save Zarthus/e882cc945418a698a0a9fcd588851604 to your computer and use it in GitHub Desktop.
def run_mtr
@mtr_out = Array.new
h = Hash.new
mtr_cmd = "mtr -i 2 -t #{@options[:host]} --report -c #{@options[:mtr_count]} --report-wide"
stdout = `#{mtr_cmd}`
@mtr_out.push(mtr_cmd)
stdout.each do |line|
if line =~ /^\s+\d/
re = /^\s+(\d*)\.\s*(\?\?\?)|^\s*(\d*).\s*(\S*).*?(\d*?.\d*?)%\s*.*?\d\s+(.*?)\s.*?/
m = re.match(line)
if m.captures.count < 4
STDOUT.puts("encountered ??? on #{$1}")
next
end
id,host,packet_loss,latency_avg = m.captures
h[id] = Hash.new()
h[id]['host'] = host
h[id]['packet_loss'] = packet_loss
h[id]['latency_avg'] = latency_avg
end
@mtr_out.push(line)
end
h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment