Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # coding: utf-8
- #require 'profile'
- require 'rubygems'
- require 'ar-extensions'
- require 'active_record'
- ActiveRecord::Base.establish_connection(
- :adapter => "mysql",
- :host => "localhost",
- :username => "rusqulog",
- :password => "rusqulog",
- :database => "squidlog"
- )
- columns = [
- :time,
- :response_time,
- :src_ip,
- :request_status,
- :http_status,
- :reply_size,
- :request_method,
- :request_url,
- :username,
- :squid_status,
- :server_ip,
- :mime_type
- ]
- options = {
- :validate => false,
- :timestamps => false
- }
- $calls = 0
- class LogEntry < ActiveRecord::Base
- end
- def parse str
- $calls+=1
- fields = str.split
- fields[0] = Time.at fields[0].to_i
- fields[3] = fields[3].split "/"
- fields[8] = fields[8].split "/"
- fields = fields.flatten
- if fields.size == 12 then
- return fields
- else
- raise ArgumentError, "Неверное число полей в строке лога: #{fields.size}, #{str}"
- end
- end
- values=STDIN.inject([]) do |result, line|
- result << parse(line)
- end
- puts "#{$calls} lines read and parsed. Importing to DB..."
- LogEntry.import columns, values, options
Add Comment
Please, Sign In to add comment