Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created March 9, 2010 00:05
Show Gist options
  • Save snusnu/325971 to your computer and use it in GitHub Desktop.
Save snusnu/325971 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Collects trace stats using mtr. Accepts the following environment variables
# and delegates all real arguments to the underlying call to the mtr program.
#
# VERBOSE=true REPORT_DIR=/path/to/report ENDPOINTS=foo.com,bar.com THRESHOLD=10.0 ./rmtr.rb foo.com -r -c 1000
require 'fileutils'
THRESHOLD = ENV['THRESHOLD' ].to_i || 10
ENDPOINTS = ENV['ENDPOINTS' ].split(',')
REPORT_DIR = ENV['REPORT_DIR'] || File.expand_path('../rmtr-report', __FILE__)
VERBOSE = ENV['VERBOSE' ] || false
FileUtils.mkdir_p(REPORT_DIR)
while true do
result = %x[mtr #{ARGV.join(' ')}]
if VERBOSE
puts '-'*80
puts result
puts '-'*80
end
packet_loss = (ENDPOINTS.any? { |endpoint| result =~ /#{endpoint}\s*(\d*.\d*)%/ } ? $1.to_f : 100.0)
if packet_loss >= THRESHOLD
puts "WARNING: #{packet_loss}% packet loss"
file = "#{REPORT_DIR}/#{Time.now.strftime('%Y_%m_%d__%H-%M-%S')}__#{packet_loss}.txt"
File.open(file, 'w') do |f|
f.puts result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment