Skip to content

Instantly share code, notes, and snippets.

@allolex
Created August 24, 2012 16:05
Show Gist options
  • Select an option

  • Save allolex/3452327 to your computer and use it in GitHub Desktop.

Select an option

Save allolex/3452327 to your computer and use it in GitHub Desktop.
Parse ping output files in the format ping-YYYYMMDDhhmm.log and turn them into a huge JSON array
#!/usr/bin/env ruby
require 'json'
lines = DATA.readlines
stats = []sent = received = loss = time = minimum = average = maximum = deviation = 0files = Dir.glob "data/*.log"files.each do |log|
lines = File.open(log).readlines
log =~ /ping-(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/
year,month,day,hour,minute = $1,$2,$3,$4,$5
lines.each do |line|
if line =~ /packets transmitted/
sent, received, loss, time = line.scan(/\d+/)
elsif line =~ /min\/avg\/max/
minimum, average, maximum, deviation = line.scan /[\d\.]+/
end
end
stats << {
date: [year,month,day].join('-'),
time: [hour,minute,'00'].join(':'),
packets: {
sent: sent,
received: received,
loss: loss,
time: time,
},
times: {
minimum: minimum,
average: average,
maximum: maximum,
deviation: deviation,
}
}
end
puts stats.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment