Created
August 24, 2012 16:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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