dcav-stats/bandwidth.rb

26 lines
580 B
Ruby
Raw Permalink Normal View History

2017-05-22 22:09:10 +00:00
require 'server_log_parser'
require 'filesize'
require 'json'
load 'config.rb'
parser = ServerLogParser::Parser.new(ServerLogParser::COMBINED)
bw_sum = Hash.new(0)
log = `zcat /var/shots/logs/access.log.gz`
log.split("\n").each do |line|
parsed = parser.parse(line)
request = parsed['%r'].split(" ")[1]
bytes = parsed['%b'].to_i
status = parsed['%>s']
2017-10-13 16:51:34 +00:00
next if request == nil
2017-05-22 22:09:10 +00:00
user = request[1]
bw_sum[user] += bytes unless user == nil or status != "200"
end
puts JSON.generate(bw_sum.map{|k,v| [NAMES_SHO[k],Filesize.from(v.to_s + 'B').pretty]}.to_h)