Pages

Tuesday, July 28, 2015

Sarg Script for Day wise squid log generation

#!/bin/bash

if [ -f /etc/sysconfig/sarg ]; then
    . /etc/sysconfig/sarg
fi

[ "$SARG_ENABLED" == "on" ] || exit 0

# remove all daily sarg reports older than $SARG_RETENTION days
/usr/bin/find \
    /var/www/sarg/daily \
    -mindepth 1 \
    -maxdepth 1 \
    -mtime +$SARG_RETENTION \
    -type d | xargs /bin/rm -rf

function create_log_argv() {
    local LOG=$(echo "$1" | sed s,/var/log,/var/log/archives,)
    local COUNT=$2
    ls -1 "$LOG"-*gz 2>/dev/null | tail -${COUNT} | while read F; do
echo -n "-l $F "
    done
}

# Get yesterday's date
#YESTERDAY=30/03/2014
#YESTERDAY=$(date --date "yesterday" +%d/%m/%Y)
LOG_FILES=

LOG="/var/log/squid/access.log"
# this is to be backwards compatible
if [ -s "$LOG.1.gz" ]; then
LOG_FILES="$LOG_FILES -l $LOG.1.gz"
fi

LOG_FILES="$LOG_FILES $(create_log_argv $LOG 1)"

if [ -s "$LOG" ]; then
LOG_FILES="$LOG_FILES -l $LOG"
fi

# if no logfiles are present then exit
[ -z "$LOG_FILES" ] && exit 0

#exec /usr/bin/sarg \
#$LOG_FILES \
#-o /var/www/sarg/daily \
#-d $YESTERDAY &>/dev/null


lastdate=$(ls -Art /var/www/sarg/daily | tail -n 1| cut -c 1-9)
year=$(echo $lastdate | cut -c 1-4)
month=$(echo $lastdate | cut -c 5-7)
oldday=$(echo $lastdate | cut -c 8-9)
currentdate=$(date --date "today" +%d)

for (( logdate=oldday; logdate<=currentdate; logdate++ ))
do
        echo    lastdate                $lastdate
        echo    day                     $oldday
        echo    currentdate             $currentdate
        echo    logdate                 $logdate

        YESTERDAY=$(date --date $logdate$month$year  +%d/%m/%Y)
        echo yesterday $YESTERDAY
/usr/bin/sarg $LOG_FILES -o /var/www/sarg/daily -d $YESTERDAY
        echo ============================
done


No comments:

Post a Comment