Guest User

Untitled

a guest
Mar 5th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. require 'rubygems'
  2. require 'date'
  3. require 'dm-core'
  4. require 'dm-more'
  5.  
  6. #DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/test.db")
  7. #DataMapper.setup(:default, 'postgres://localhost/logfile_analysis')
  8. DataMapper.setup(:default, :host => 'localhost', :adapter => 'postgres', :database => 'logfile_analysis_development', :username => 'logfile_analysis', :password => '')
  9.  
  10. class LogfileLine
  11. include DataMapper::Resource
  12. include DataMapper::Validate
  13. property :id, Serial
  14. property :remote_addr, IPAddress, :nullable => false, :index => true
  15. property :remote_user, String, :nullable => true, :length => 255
  16. property :day, Integer, :nullable => false, :index => true
  17. property :month, Integer, :nullable => false, :index => true
  18. property :year, Integer, :nullable => false, :index => true
  19. property :hour, Integer, :nullable => false, :index => true
  20. property :min, Integer, :nullable => false
  21. property :sec, Integer, :nullable => false
  22. property :request_verb, String, :nullable => true, :length => 7
  23. property :request_uri, String, :nullable => true, :length => 2048
  24. property :http_version, String, :nullable => true, :length => 8
  25. property :status, Integer, :nullable => false, :index => true
  26. property :size, Integer, :nullable => false
  27. property :http_referer, String, :nullable => true, :length => 2048
  28. property :http_user_agent, String, :nullable => true, :length => 2048
  29. property :http_x_forwarded_for, String, :nullable => true, :length => 2048
  30. timestamps :at
  31. end
  32.  
  33. DataMapper.auto_migrate!
  34.  
  35. partial_pattern = /^(?:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))\s+-(.*?)?\s+-\s+\[((\d{1,2})\/(\w+)\/(\d{4}):0?(\d{1,2}):0?(\d{1,2}):0?(\d{1,2})\s+(.*?))\]\s+"(?:-|(GET|HEAD|PUT|POST|DELETE|TRACE|CONNECT)\s+(.*?)\s+(.*?))"\s+(\d{1,3})\s+(\d+)\s+"(.*?)"\s+"(.*?)"\s+"(.*?)"$/i
  36. start_time = Time.now
  37.  
  38. File.open("nginx.access.log") do |file|
  39. current_line = 0
  40. total_lines = file.lines.count
  41. file.rewind
  42. file.each_line { |line|
  43. line.chomp!
  44. current_line += 1
  45. next if line.nil? || line.empty?
  46. raise line.inspect unless (matches = partial_pattern.match(line))
  47. remote_addr = matches[1]
  48. remote_user = matches[2]
  49. day = Integer(matches[4])
  50. str_month = matches[5]
  51. i = 0
  52. month = Date::ABBR_MONTHNAMES.compact.collect{|name|i = i + 1;[i,name]}.reject{ |i,name| name != str_month }.flatten[0]
  53. year = Integer(matches[6])
  54. hour = Integer(matches[7])
  55. min = Integer(matches[8])
  56. sec = Integer(matches[9])
  57. offset = Integer((Float(matches[10])*0.01).floor)
  58. request_verb = matches[11]
  59. request_uri = matches[12]
  60. http_version = matches[13]
  61. status = Integer(matches[14])
  62. size = Integer(matches[15])
  63. http_referer = matches[16]
  64. http_user_agent = matches[17]
  65. http_x_forwarded_for = matches[18]
  66. logfile_line = LogfileLine.find_or_create({
  67. :status=>status,
  68. :request_verb=>request_verb,
  69. :day=>day,
  70. :request_uri=>request_uri,
  71. :remote_addr=>remote_addr,
  72. :http_version=>http_version,
  73. :month=>month,
  74. :sec=>sec,
  75. :http_referer=>http_referer,
  76. :min=>min,
  77. :year=>year,
  78. :size=>size,
  79. :hour=>hour,
  80. :http_user_agent=>http_user_agent,
  81. :remote_user=>remote_user,
  82. :http_x_forwarded_for=>http_x_forwarded_for})
  83. logfile_line.save
  84. p "id #{logfile_line.id} created - line #{current_line} of #{total_lines} (#{sprintf( "%0.02f", (current_line.to_f / total_lines.to_f) * 100.00 )} % complete)"
  85. time_now = Time.now
  86. time_elapsed = time_now - start_time
  87. lines_left = total_lines - current_line
  88. lines_per_second = current_line.to_f / time_elapsed
  89. p "#{time_elapsed} seconds elapsed. #{sprintf( "%0.02f", lines_per_second)} lines per second. Estimate finished: #{Time.now + (lines_left / lines_per_second)}"
  90. p logfile_line.errors unless logfile_line.errors.empty?
  91. }
  92. end
Add Comment
Please, Sign In to add comment