#readlog.rb reads production.log, searches for lines containing "teacher:" and #prints out the part of the line that follows require 'optparse' class CatArguments < Hash def initialize(args) super() self[:logfile] = "production.log" self[:controlstr]= "teacher:" self[:ip]=nil opts = OptionParser.new do |opts| opts.banner = "Usage: #$0 [options] " opts.on('-l','--logfile [STRING]', 'default is production.log') do |s| self[:logfile]=s end opts.on('-s', '--controlstr [STRING]', 'default is teacher:') do |s| self[:controlstr] = s end opts.on('-i','--ip [STRING]', 'default is all') do |s| self[:ip]=s end opts.on_tail('-h', '--help', 'display this help and exit') do puts opts exit end end opts.parse!(args) end end arguments = CatArguments.new(ARGV) logfile=arguments[:logfile] controlstr=arguments[:controlstr] limitip=arguments[:ip] r=%r~#{controlstr}(.*)~ newsum={} IO.foreach(logfile) do |nk| # p nk if nk.match(r) #controlstr found in string # p "raw #{$1}" s=$1 if s=~ /for ([\d\.]+).*\/(\d+)$/ || s=~ /from ([\d\.]+).*\/(\d+)$/ ip=$1 secs=$2 # p ip+secs if !limitip || (limitip == ip) if s=~/new problem/ #a new sum was generated for an ip of interest newsum[ip]= secs # p s elsif start=newsum[ip] time_spent= secs.to_i - start.to_i p "After #{time_spent} seconds: #{s}" end end end end end