Pages

Tuesday, July 28, 2015

Sarg Script for Week wise squid log generation


#!/bin/bash

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

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

# remove all weekly sarg reports older than $SARG_RETENTION days
/usr/bin/find \
    /var/www/sarg/weekly \
    -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
}

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 7)"

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


[ -z "$LOG_FILES" ] && exit 0


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


for (( logdate=oldweek; logdate<currentdate; logdate=week ))
do


# Get epoch seconds
USERDATE=$(date +%s -d $logdate)
# Subtract 1 week
((USERDATE += (60*60*24*7) ))

# Convert from epoch seconds into YYYYMMDD.
# -d assumes strings beginning with @ are epoch seconds.
week=$(date -d "@$USERDATE" +"%Y%m%d")
echo Week  $week

WEEKAGO=$(date -d "$week 1 week ago"  +%d/%m/%Y)

        echo    currentdate             $currentdate
        echo    logdate                 $logdate
        echo    WEEKAGO                 $WEEKAGO
        YESTERDAY=$(date --date $week  +%d/%m/%Y)
        echo yesterday $YESTERDAY
        echo ============================
/usr/bin/sarg $LOG_FILES -o /var/www/sarg/weekly -d $WEEKAGO-$YESTERDAY


done

No comments:

Post a Comment