Guest User

Untitled

a guest
Jul 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. require 'rubygems'
  2. require 'activesupport'
  3. require 'sequel'
  4.  
  5. DB = Sequel.connect('sqlite://log.db')
  6.  
  7. DB.create_table :loglines do
  8. primary_key :id
  9. String :client_ip
  10. String :client_identd
  11. String :client_userid
  12. DateTime :request_started
  13. String :request_line
  14. String :response_status
  15. Integer :response_size
  16. Integer :request_microsec
  17. Integer :request_seconds
  18. DateTime :request_finished
  19. end
  20.  
  21. LOG_REGEX = /^([\d.]+) (\S+) (\S+) \[([\w:\/]+\s[+\\-]\d{4})\] \"(.+?)\" (\d{3}) (\d+) (\d+)/
  22.  
  23. class LogLine < Struct.new(
  24. :client_ip, :client_identd, :client_userid,
  25. :request_started, :request_line,
  26. :response_status, :response_size,
  27. :request_microsec, :request_seconds, :request_finished)
  28. end
  29.  
  30. loglines = DB[:loglines]
  31.  
  32. File.open('wlt_pricing.access').each do |line|
  33. if m = LOG_REGEX.match(line)
  34. entry = LogLine.new
  35. entry.client_ip = m[1]
  36. entry.client_identd = m[2]
  37. entry.client_userid = m[3]
  38. entry.request_started = DateTime.strptime(str=m[4].gsub(/-\d{4}$/,''), fmt='%d/%b/%Y:%H:%M:%S')
  39. entry.request_line = m[5]
  40. entry.response_status = m[6]
  41. entry.response_size = m[7]
  42. entry.request_microsec = m[8].to_i
  43. entry.request_seconds = entry.request_microsec / 1000
  44. entry.request_finished = entry.request_started + entry.request_seconds.seconds
  45.  
  46. loglines.insert( :client_ip => entry.client_ip, :client_identd => entry.client_identd, :client_userid => entry.client_userid,
  47. :request_started => entry.request_started, :request_line => entry.request_line, :response_status => entry.response_status,
  48. :response_size => entry.response_size, :request_microsec => entry.request_microsec, :request_seconds => entry.request_seconds,
  49. :request_finished => entry.request_finished )
  50. end
  51. end
Add Comment
Please, Sign In to add comment