#!/bin/sh # # /usr/local/sbin/showlog # Copyright 2002-2006 Jonas Smedegaard # # $Id: showlog,v 1.12 2007-10-23 21:14:51 jonas Exp $ # # Show topic-specific log entries # set -e logfilecount="${logfilecount:-1}" usage() { echo "Usage: $(basename $0) ftp|web|weberror|websuexec|mail|sms|smsbox|bearerbox|system [ [...]]" exit 1 } exit1() { echo "Error: $1" echo "Exiting..." exit 1 } cat_logs() { rootdir="$1" pattern="$2" howmany="${3:-2}" if [ "$howmany" != "all" ]; then files="$(find $rootdir -mindepth 1 -maxdepth 1 -regextype posix-egrep -regex "$pattern" -type f -follow | sort -nr | tail -n "$howmany")" else files="$(find $rootdir -mindepth 1 -maxdepth 1 -regextype posix-egrep -regex "$pattern" -type f -follow | sort -nr)" fi for file in $files; do case $file in *.gz) zcat $file;; *.bz2) bzcat $file;; *) cat $file;; esac done } tail_history="${tail_history:-500}" target=$1 shift || exit1 "Missing argument" case "$target" in ftp) log="xferlog" ;; web) logroot="/var/log/apache" log="access.log" ;; weberror|weberrors) logroot="/var/log/apache" log="error.log" ;; websuexec) logroot="/var/log/apache" log="suexec.log" ;; mail) log="mail.log" ;; sms|smsbox) log="smsbox.log" ;; bearerbox) log="bearerbox.log" ;; system) log="syslog" ;; *) usage ;; esac case "$target" in ftp|mail|system) logroot="/var/log" ;; web|weberror|weberrors|websuexec) logroot="/var/log/apache" ;; sms|smsbox|bearerbox) logroot="/var/log/kannel" ;; *) usage exit1 "Undefined logroot for \"$target\"" ;; esac case "$target" in ftp|web|weberror|weberrors|websuexec|mail|sms|smsbox|bearerbox|system) logpattern="$(printf '.*/%s(\.[[:digit:]]+(\.gz)?)?\n' "$log")" ;; *) exit1 "Undefined logpattern for \"$target\"" ;; esac if [ $# -gt 0 ]; then grep_opts="" for keyword in $@; do grep_opts="$opts -e $keyword" done cat_logs "$logroot" "$logpattern" "$logfilecount" | grep -i $grep_opts | tail -n $tail_history # [ -f $logroot/$log ] && tail -n 0 -F $logroot/$log | grep -i $grep_opts else cat_logs "$logroot" "$logpattern" "$logfilecount" | tail -n $tail_history # [ -f $logroot/$log ] && tail -n 0 -F $logroot/$log fi