Guest User

Untitled

a guest
Apr 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/home/y/bin/ruby1.8
  2.  
  3. # constant
  4. delim = [ 5 ].pack("c")
  5. # how often do we output
  6. period = 60
  7.  
  8. def initStats
  9. s = Hash.new
  10. s[:start] = Time.now
  11. s[:vers] = Hash.new
  12. s
  13. end
  14.  
  15. File.open("/home/y/logs/yapache/access", "r") { |f|
  16. f.seek(0, IO::SEEK_END)
  17. puts "Moved to end of access log (offset is #{f.pos}) - will output every #{period}s"
  18.  
  19. stats = initStats
  20.  
  21. while true
  22. # is time up?
  23. if ((Time.now - stats[:start]) > period)
  24. op = stats[:vers].keys.collect { |k| "(#{k}: #{stats[:vers][k]})" }.join("\t")
  25. cnt = 0
  26. stats[:vers].each { |k,v| cnt += v }
  27. puts "#{cnt} -- #{op}"
  28. stats = initStats
  29. end
  30.  
  31. cur = f.pos
  32. while true
  33. f.seek(0, IO::SEEK_END)
  34. if f.pos != cur
  35. f.seek(cur, IO::SEEK_SET)
  36. break
  37. end
  38. sleep 0.1
  39. end
  40.  
  41. while s = f.gets
  42. hostid = s[0,8]
  43. time = s[8,8].to_i(16)
  44. size = s[24,8].to_i(16)
  45. url = s[32, s.index(delim, 32) - 32]
  46. next if url !~ /^\/usage/
  47. dat = Hash.new
  48. url.sub(/^.*\?/, '').split('&').each { |v|
  49. ar = v.split('=')
  50. dat[ar[0]] = ar[1]
  51. }
  52. next if !dat.has_key?('bp')
  53. v = dat['bp']
  54. stats[:vers][v] = 0 if !(stats[:vers].has_key?(v))
  55. stats[:vers][v] += 1
  56. end
  57. end
  58. }
Add Comment
Please, Sign In to add comment