Created
February 1, 2017 06:53
-
-
Save denvazh/addce4f70c7f62d2a8aad17769b96637 to your computer and use it in GitHub Desktop.
Convert results of ping output to csv
This file contains 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 'csv' | |
class Entry | |
attr_reader :id, :text | |
def initialize(text) | |
@id, @text = parse_text(text) | |
end | |
private | |
def parse_text(text) | |
[text.split.reverse.first, text] | |
end | |
end | |
class Timeout < Entry | |
end | |
class Passed < Entry | |
attr_reader :id, :text, :ttl, :time | |
def initialize(text) | |
@id, @text, @ttl, @time = parse_text(text) | |
end | |
private | |
def parse_text(text) | |
params = text.sub(/^[\d]{2}[ ]bytes[ ][\w\s.]*:[ ]/, '').split.select { |e| e.include?('=') } | |
id, ttl, time = params.map { |p| p.split('=').last } | |
[id, text, ttl, time] | |
end | |
end | |
requests = File.read('/tmp/graph/requests.txt').split("\n") | |
parsed_entries = requests.map do |req| | |
if req.start_with?('Request timeout') | |
Timeout.new(req) | |
else | |
Passed.new(req) | |
end | |
end | |
csv_string = CSV.generate do |csv| | |
parsed_entries.each do |entry| | |
row = if entry.is_a?(Timeout) | |
[entry.id, 99999] | |
elsif entry.is_a?(Passed) | |
[entry.id, entry.time] | |
else | |
[] | |
end | |
csv << row | |
end | |
end | |
File.write('plot.csv', csv_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment