Skip to content

Instantly share code, notes, and snippets.

@lloyd
Created November 13, 2009 19:44
Show Gist options
  • Save lloyd/234098 to your computer and use it in GitHub Desktop.
Save lloyd/234098 to your computer and use it in GitHub Desktop.
#!/home/y/bin/ruby1.8
# constant
delim = [ 5 ].pack("c")
# how often do we output
period = 60
def initStats
s = Hash.new
s[:start] = Time.now
s[:vers] = Hash.new
s
end
File.open("/home/y/logs/yapache/access", "r") { |f|
f.seek(0, IO::SEEK_END)
puts "Moved to end of access log (offset is #{f.pos}) - will output every #{period}s"
stats = initStats
while true
# is time up?
if ((Time.now - stats[:start]) > period)
op = stats[:vers].keys.collect { |k| "(#{k}: #{stats[:vers][k]})" }.join("\t")
cnt = 0
stats[:vers].each { |k,v| cnt += v }
puts "#{cnt} -- #{op}"
stats = initStats
end
cur = f.pos
while true
f.seek(0, IO::SEEK_END)
if f.pos != cur
f.seek(cur, IO::SEEK_SET)
break
end
sleep 0.1
end
while s = f.gets
hostid = s[0,8]
time = s[8,8].to_i(16)
size = s[24,8].to_i(16)
url = s[32, s.index(delim, 32) - 32]
next if url !~ /^\/usage/
dat = Hash.new
url.sub(/^.*\?/, '').split('&').each { |v|
ar = v.split('=')
dat[ar[0]] = ar[1]
}
next if !dat.has_key?('bp')
v = dat['bp']
stats[:vers][v] = 0 if !(stats[:vers].has_key?(v))
stats[:vers][v] += 1
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment