Skip to content

Instantly share code, notes, and snippets.

@denvazh
Created February 1, 2017 06:53
Show Gist options
  • Save denvazh/addce4f70c7f62d2a8aad17769b96637 to your computer and use it in GitHub Desktop.
Save denvazh/addce4f70c7f62d2a8aad17769b96637 to your computer and use it in GitHub Desktop.
Convert results of ping output to csv
#!/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