Guest User

Untitled

a guest
Aug 15th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # coding: utf-8
  3. #require 'profile'
  4. require 'rubygems'
  5. require 'ar-extensions'
  6. require 'active_record'
  7.  
  8.  
  9. ActiveRecord::Base.establish_connection(
  10. :adapter => "mysql",
  11. :host => "localhost",
  12. :username => "rusqulog",
  13. :password => "rusqulog",
  14. :database => "squidlog"
  15. )
  16.  
  17. columns = [
  18. :time,
  19. :response_time,
  20. :src_ip,
  21. :request_status,
  22. :http_status,
  23. :reply_size,
  24. :request_method,
  25. :request_url,
  26. :username,
  27. :squid_status,
  28. :server_ip,
  29. :mime_type
  30. ]
  31.  
  32. options = {
  33. :validate => false,
  34. :timestamps => false
  35. }
  36.  
  37. $calls = 0
  38.  
  39. class LogEntry < ActiveRecord::Base
  40. end
  41.  
  42. def parse str
  43. $calls+=1
  44. fields = str.split
  45. fields[0] = Time.at fields[0].to_i
  46. fields[3] = fields[3].split "/"
  47. fields[8] = fields[8].split "/"
  48. fields = fields.flatten
  49. if fields.size == 12 then
  50. return fields
  51. else
  52. raise ArgumentError, "Неверное число полей в строке лога: #{fields.size}, #{str}"
  53. end
  54. end
  55.  
  56. values=STDIN.inject([]) do |result, line|
  57. result << parse(line)
  58. end
  59.  
  60. puts "#{$calls} lines read and parsed. Importing to DB..."
  61.  
  62. LogEntry.import columns, values, options
Add Comment
Please, Sign In to add comment