#!/bin/sh # # Apache log helper. # # Copyright (c) 2004 Lorance Stinson # Configuration section. # Directory that log files reside in. logdir="/var/log/httpd/" # Pattern to exclude local hosts. Used with egrep. localip="111\.222\.333\.444|192\.168\.[0-9]*\.[0-9]*|127\.0\.0\.1" # The default log file. logfile="access_log" # The alternate log files. altloga="error_log" altlogb="referer_log" # The extension for the previous version. prevext=".0" # The name of this program. myname="`basename $0`" # Command to cat the log file. catcmd="cat" # The default grep command. grepcmd="grep" #************************************************************************** # usage #************************************************************************** usage(){ cat< Obtain patterns from . -F Use fgrep in place of grep. -h Print this help text and exit. -i Ignore case on all matches. -l Use in place of $logfile. -n Output lines when using -t. -o Use previous version of log file. Eg. $logfile$prevext -p Send output to \$PAGER or more. -t Send the output to tail. -x Filter the output with the inverted pattern . -X Same as -x but obtain patterns from . EOHELP exit $1 } #************************************************************************** # Parse the command line arguments. #************************************************************************** # Process the command line arguments. args=`getopt "aAcdeEf:Fhil:n:optx:X:" "$@" 2>&1` if [ $? != 0 ] ; then # Fix the error message. # Change getopt to $myname and remove any trailing --. echo $args | sed "s/getopt/$myname/;s/\s*--\s*$//" # Print help and exit. echo usage 1 fi eval set -- "$args" # First pass. args="" while true ; do case "$1" in # Use the log file $altloga. -a) logfile=$altloga ; shift ;; # Use the log file $altloga. -A) logfile=$altlogb ; shift ;; # Final count only. -c) pager="wc -l" ; shift ;; # Debug. Print command(s). -d) debug=1 ; shift ;; # Exclude local IP Addresses. -e) filter="| egrep -v '$localip'" ; shift ;; # Use egrep in place of grep. -E) grepcmd="egrep" ; shift ;; # Use egrep in place of grep. -F) grepcmd="fgrep" ; shift ;; # Print the help text. -h) usage 1 ;; # Ignore case. -i) ignorecase="-i" ; shift ;; # Use a different log file. -l) if [ -r "$logdir$2" ] ; then logfile=$2 elif [ -r "$2" ] ; then logfile=$2 logdir="" else echo "Could not find the log file \"$2\"." exit 1 fi shift 2 ;; # The number of lines to output with tail. -n) lines=" -n $2" ; shift 2 ;; # Send the final output to a pager. -p) if [ "$PAGER" ] ; then pager="$PAGER" else pager="more" fi shift;; # End of arguments and start of search patterns. --) for nextarg ; do args="$args $nextarg" done break ;; # Unknown or used in the next pass. *) args="$args $1"; shift ;; esac done if [ $ignorecase ] ; then grepcmd="$grepcmd $ignorecase" fi # Second pass. # These can be are affected by the previous set. eval set -- "$args" while true ; do case "$1" in # Obtain patterns from a file. -f) if [ -r "$2" ] ; then filter="$filter | $grepcmd -f $2" else echo "Could not find the pattern file \"$2\"." exit 1 fi shift 2 ;; # Previous version of log file. -o) logfile="$logfile$prevext" if [ ! -r "$logdir$logfile" ] ; then echo "Could not find the log file \"$logdir$logfile\"." exit 1 fi shift ;; # Send the final output to tail. -t) pager="tail$lines" ; shift ;; # Filter the output with a pattern using grep. -x) filter="$filter | $grepcmd -v $2" ; shift 2 ;; # Obtain inverted patterns from a file. -X) if [ -r "$2" ] ; then filter="$filter | $grepcmd -vf $2" else echo "Could not find the pattern file \"$2\"." exit 1 fi shift 2 ;; # End of arguments and start of search patterns. --) shift ; break ;; # Invalid argument. *) echo "Invalid option $1" usage 1;; esac done #************************************************************************** # Create the command to be evaluated. #************************************************************************** # If there is a pattern use grep instead of cat. if [ "$1" ] ; then catcmd="$grepcmd \"$1\"" # Are there more patterns? shift if [ "$1" ] ; then moregrep="" for pattern ; do moregrep="$moregrep | $grepcmd \"$pattern\"" done fi elif [ "$pager" -a ! "$filter" ] ; then # Special case. The user just wants to use the pager. command="$pager $logdir$logfile" fi # Build the command, unless the special case was chosen. if [ ! "$command" ] ; then command="$catcmd $logdir$logfile $moregrep $filter" if [ "$pager" ] ; then command="$command | $pager" fi fi #************************************************************************** # Run the command. #************************************************************************** # Is debug set? if [ $debug ] ; then # Print the command. echo $command else # Execute the command. eval $command fi