diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2008-03-12 12:42:58 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2008-03-12 12:42:58 +0000 |
commit | be42d70ad45a00f7367acc6a7d590980f1ec8f08 (patch) | |
tree | 496b04306127b9993df4c868fe2d3ab99d2ac9ea | |
parent | a9d25227409630770f0f2c58c36d6e21be7a1667 (diff) | |
download | gsc-be42d70ad45a00f7367acc6a7d590980f1ec8f08.tar.gz gsc-be42d70ad45a00f7367acc6a7d590980f1ec8f08.tar.bz2 |
Add to the repository
git-svn-id: file:///svnroot/gsc/trunk@315 d2de0444-eb31-0410-8365-af798a554d48
-rwxr-xr-x | maint/processlogs | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/maint/processlogs b/maint/processlogs new file mode 100755 index 0000000..99ebaab --- /dev/null +++ b/maint/processlogs @@ -0,0 +1,216 @@ +#! /bin/sh +# processlogs -- special processing for webalizer logs. +# Copyright 2006, 2008 Sergey Poznyakoff +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +PATH=$PATH:/usr/local/bin/:/usr/sbin +CONFDIR=/etc/webalizer.d +LOGDIR=/var/log/apache +WEBALIZER=webalizer +FILELIST= +TMPDIR=/tmp +TMPLOGDIR=/tmp/processlogs.$$ +: ${AWK:=awk} + +RUN= +VERBOSE=0 +LROPT= +SKIP_DNS_RESOLVE= + +verbose() { + if [ $VERBOSE -gt 0 ]; then + echo $* + fi +} + +error() { + code=$1 + shift + echo $* >&2 + if [ $code -gt 0 ]; then + exit $code + fi +} + +usage() { + cat <<-EOT + usage: $PROGNAME [OPTIONS] + + OPTIONS are + + -h Display this help list + -v Produce verbose output + -n Dry run. Print what would have be done without doing + anything. Implies -v. + -l DIR Set apache log directory (default $LOGDIR) + -d DIR Set configuration directory (default $CONFDIR) + -f FILE Set file list file (default \$CONFDIR/FILES) + EOT +} + +PROGNAME=$0 +while getopts "hvnl:d:f:s" OPTION +do + case $OPTION in + h) usage + exit 0;; + v) VERBOSE=$(($VERBOSE + 1));; + n) RUN=: + LROPT=-d + VERBOSE=$(($VERBOSE + 1));; + l) LOGDIR=$OPTARG;; + d) CONFDIR=$OPTARG;; + f) FILELIST=$OPTARG;; + s) SKIP_DNS_RESOLVE=yes + esac +done + +test -d $LOGDIR || error 1 "Log directory does not exist" +test -d $CONFDIR || error 1 "Configuration directory does not exist" +if [ -z "$FILELIST" ]; then + FILELIST=$CONFDIR/FILES +fi +test -r $FILELIST || error 1 "Cannot read file list" + +if [ $VERBOSE -gt 0 ]; then + LROPT="$LROPT -v" +fi + +TMP=$CONFDIR/logrotate.$$.conf + +cleanup() { + rm -f $TMP + test -n "$TMPLOG" && rm -f $TMPLOG +} + +trap 'cleanup' 1 2 13 15 + +read_filelist() { + line=0 + while read logname confname filter + do + line=$(($line + 1)) + case "$logname-$confname" in + \#*) ;; + -) ;; + *-) error 0 "$1:$line: Missing confname";; + *) $2 "$logname" "$confname" "$filter" + esac + done < $1 +} + +collect_logs() { + loglist="$loglist +$LOGDIR/$1" +} + +handle_log() { + dir=`sed -n 's/OutputDir[ ]*\(.*\)/\1/p' $CONFDIR/$2` + if [ -n "$dir" ]; then + if [ ! -d "$dir" ]; then + verbose "Creating output directory $dir" + $RUN mkdir -p $dir + fi + fi + filter="${3:-cat}" + LOGFILE=$LOGDIR/$1 + verbose "Running $filter $LOGFILE >> $TMPLOGDIR/$2" + eval "$filter $LOGFILE >> $TMPLOGDIR/$2" + configlist="$configlist $2" +} + +sortlog() { + $AWK ' + BEGIN { + month["Jan"] = 1 + month["Feb"] = 2 + month["Mar"] = 3 + month["Apr"] = 4 + month["May"] = 5 + month["Jun"] = 6 + month["Jul"] = 7 + month["Aug"] = 8 + month["Sep"] = 9 + month["Oct"] = 10 + month["Nov"] = 11 + month["Dec"] = 12 + } + # [14/Feb/2008:05:11:47 +0200 + { date=substr($4,2) " " substr($5,1,length($5)-1) + n = split(date, p, /[/:]/) + ts = mktime(p[3] " " month[p[2]] " " p[1] " " p[4] " " p[5] " " p[6] " " p[7]) + print ts, $0}' $1 | + sort -n -k1,2 | + cut -d ' ' -f2- +} + +loglist= +read_filelist $FILELIST collect_logs +loglist=`echo "$loglist" | sort | uniq | tr '\n' ' '` +verbose "Logfiles: $loglist" + +if [ -z "$SKIP_DNS_RESOLVE" ]; then + verbose "Updating DNS Cache" + if [ -z "$RUN" ]; then + (echo $loglist | xargs cat) | webazolver + fi +fi + +mkdir $TMPLOGDIR || exit 1 +verbose "Processing individual logs" +configlist= +read_filelist $FILELIST handle_log +for file in $configlist +do + verbose "Running sortlog $TMPLOGDIR/$file | $WEBALIZER -c $CONFDIR/$file" + if [ -z "$RUN" ]; then + sortlog $TMPLOGDIR/$file | $WEBALIZER -c $TMPLOGDIR/$file + elif [ $VERBOSE -lt 2 ]; then + sortlog $TMPLOGDIR/$file > $TMPLOGDIR/${file}.sorted + else + sortlog $TMPLOGDIR/$file > /dev/null + fi +done + +if [ $VERBOSE -lt 2 ]; then + rmdir -rf $TMPLOGDIR +else + verbose "Leaving processed logs in directory $TMPLOGDIR" +fi + +cat >$TMP <<EOT +monthly +rotate 6 +create +compress + +$loglist { + sharedscripts + postrotate + /usr/sbin/apachectl restart + endscript +} +EOT + +if [ $VERBOSE -ge 2 ]; then + echo "# Logrotate configuration file:" + cat $TMP + echo "# EOF" +fi + +verbose "Running logrotate --state $CONFDIR/logrotate.status $LROPT $TMP" +logrotate --state $CONFDIR/logrotate.status $LROPT $TMP + +cleanup |