#!/usr/local/bin/ruby # #### logana.rb 1.0.0 # # Copyright (C) 2005 phonondrive # You can redistribute it and/or modify it under GPL2. # #### How to use # # http://example.net/logana.rb?file=/usr/foo/log/expamle.com.log®exp=POST # # You may use regular expression. # http://example.net/logana.rb?file=/usr/foo/log/expamle.com.log®exp=.jpg|.gif # http://example.net/logana.rb?file=/usr/foo/log/expamle.com.log®exp=GET|404 # # *NOTE* Use '%3B' instead of ';'. # http://example.net/logana.rb?file=/usr/foo/log/expamle.com.log®exp=Mozilla/4.76%20[en]%20(Win98%3B%20U) # BEGIN { $defout.binmode } $KCODE = 'n' # Set a path to your Apache's log file here if you don't want to use CGI parameters. FILE_PATH = "example.com.log" begin require 'cgi' @cgi = CGI::new print "Content-type: text/html\n\n" if @cgi.params['regexp'][0] file = @cgi.params['file'][0] || FILE_PATH regexp = Regexp.new( "#{ Regexp.quote( CGI::escapeHTML( @cgi.params['regexp'][0] ) ) }", Regexp::IGNORECASE ) output = "" url_h = {} url_r = {} open( CGI::escapeHTML( file ) ) {|line| while l = line.gets if regexp =~ l url = l.split(" ").first if url_h[url] url_h[url] += 1 url_r[url] += "#{l}
" else url_h[url] = 1 url_r[url] = "#{l}
" end output += l end if regexp end } print "

Logs matching to '#{regexp.source}'

\n" url_h.sort {|i, j| ( j[1] <=> i[1] ).nonzero? or i[0] <=> j[0] }.each {|key, num| print "

#{num} : #{key}

\n

#{url_r[key]}

\n" } end print "complete!" rescue Exception puts 'Content-Type: text/plain' puts puts "#$! (#{$!.class})" puts puts $@.join("\n") end