Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created July 11, 2012 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/3089711 to your computer and use it in GitHub Desktop.
Save boopathi/3089711 to your computer and use it in GitHub Desktop.
Access Log parser
#!/usr/bin/ruby
#require "rubygems"
#require "gruff"
db={}
#g = Gruff::Bar.new("600x480")
#g.minimum_value=0
#g.maximum_value=20000
#g.title = "Access Log Statistics"
File.open(ARGV[0],"r") do |file|
while line=file.gets
contents = line.split(" ")
uri = contents[7].split("?")[0]
if db.has_key?(uri)
db[uri] += 1
else
db[uri] = 1
end
end
end
db.sort_by { |k,v| v }.each do |element|
puts "#{element[0]} : #{element[1]}"
#g.data()
end
#!/usr/bin/ruby
db={}
File.open(ARGV[0],"r") do |file|
while line=file.gets
contents = line.split(" ")
con = line[/(HEAD|GET|POST|DELETE|PUT)(.*)/]
temp = con.split(" ")[1] if not con == nil
uri = temp.split("?")[0]
if db.has_key?(uri)
db[uri] += 1
else
db[uri] = 1
end
end
end
db.sort_by { |k,v| v }.each do |element|
puts "#{element[0]} : #{element[1]}"
end
#!/usr/bin/ruby
#configuration object maker
class Conf
attr_accessor :file, :filter, :type
def initialize(args)
#set default values
self.filter = "HEAD|PUT|GET|DELETE|POST"
self.type = "file"
#extend
args.length.times do |i|
if args[i] == '--file'
self.file = args[i+1]
elsif args[i] == '--filter'
self.filter = args[i+1]
elsif args[i] == '--type'
self.type = args[i+1]
end
end
end
end
#create a new configuration
cfg = Conf.new ARGV
class Database
def initialize
@db = {}
end
def get
return @db
end
def update(key)
if @db.has_key? key
@db[key] += 1
else
@db[key] = 1
end
end
end
#Create a New Hash table for storing the data
db = Database.new
class Filter
attr_accessor :exp
def initialize(type,filter)
if type == "file"
tmp = "(#{filter})(.*)"
elsif type == "ip"
tmp = "(.*)(#{filter})"
end
self.exp = Regexp.new tmp
end
end
#create the filter object to form the required regex
regex = Filter.new cfg.type, cfg.filter
File.open(cfg.file,"r") do |file|
while line=file.gets
con = line[regex.exp]
if con == nil
next
end
if cfg.type == "file"
temp = con.split(" ")[1] if not con == nil
uri = temp.split("?")[0]
db.update(uri)
elsif cfg.type == "ip"
ip = con.split(" ")[0]
db.update(ip)
end
end
end
db.get.sort_by { |k,v| v }.each do |element|
puts "#{element[0]} : #{element[1]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment