Skip to content

Instantly share code, notes, and snippets.

@codeprimate
Last active March 3, 2016 17:01
Show Gist options
  • Select an option

  • Save codeprimate/3a1fc4a67a4e820453c0 to your computer and use it in GitHub Desktop.

Select an option

Save codeprimate/3a1fc4a67a4e820453c0 to your computer and use it in GitHub Desktop.
Papertrail Bandwidth Calculator
#!/usr/bin/env ruby
#
# Place this file and PaperTrail gz archives in a folder
#
# Usage: gzcat *.gz | ./papertrail_bandwidth_parser.rb > report.csv
#
BYTES_TO_MB=1024 * 1025
hosts = {}
ARGF.each do |line|
( host = (m = line.match(/host=([^ ]+)/)) ? m[1] : nil ) or next
( bytes = (m = line.match(/bytes=([^ ]+)/)) ? m[1].to_i : nil ) or next
hosts[host] ||= 0
hosts[host] += bytes
end
puts "Host,Bandwidth in MB"
total_mb = hosts.to_a.map{|h| h[1]}.inject(0){|sum, b| sum += b}.to_f / BYTES_TO_MB.to_f
puts "_Total,%0.2f,100%" % [total_mb]
hosts.keys.sort.each do |host|
mb = hosts[host].to_f / BYTES_TO_MB.to_f
puts "%s,%0.2f,%0.2f%" % [host, mb, (mb / total_mb) * 100.0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment