#!/usr/bin/env ruby ############ connectdurations -- brent@mbari.org ##################### # Copyright (C) 2008 MBARI # MBARI Proprietary Information. All rights reserved. # # Exctract pppd connection durations from /var/log/messages # ######################################################################## require 'getoptlong' require 'timehash' opts = GetoptLong.new( ["--help", "-h", GetoptLong::NO_ARGUMENT], ["--minup", "-u", GetoptLong::REQUIRED_ARGUMENT], ["--mindown", "-d", GetoptLong::REQUIRED_ARGUMENT], ["--year", '-y', GetoptLong::REQUIRED_ARGUMENT], ["--gmt", '-g', GetoptLong::NO_ARGUMENT], ["--UTC", '-U', GetoptLong::NO_ARGUMENT] ) progName = File.basename $0 minUp = minDown = 0 baseYear = Time.now.year tz = '' tzMethod = :localtime form = '%.2f' logDate = '([[:alpha:]]+\s+\d+\s+\d+:\d+:\d+)\s+' logProcess = '(\w+)(\[(\d+)\])?' logLine = Regexp.new(logDate+logProcess+':\s+(.*)') # [1] == date # [2] == process name # [4] == PID (optional) # [-1] == message text opts.each do |opt, arg| case opt when '--minup' minUp = Float(arg) when '--mindown' minDown = Float(arg) when '--year' baseYear = Integer(arg) when '--gmt' tz = ' UTC' when '--UTC' tzMethod=:utc else $stderr.puts "\ Extract ppp connection durations from syslog -- brent@mbari.org 6/12/08 Options: --help #displays this text --minup=seconds #don't report connections shorter than this --mindown=seconds #don't report connection interruptions shorter than this --year=#{baseYear} #assume log entries begin in specified year --gmt #assume log times are UTC (default is local time) --UTC #convert times to UTC Expects to read a syslog (i.e. /var/log/messages) via STDIN Examples: #{progName} < /var/log/messages " exit end end downSince = upSince = nil Time.last = Time("0:0:0.0 1/1/#{baseYear}"<= minUp upSince = nil end when /local.*address.*/ upSince ||= parseTime(field[1]<= minDown downSince = nil end end end else STDERR.puts "parse error on line #{STDIN.lineno}" end end