#!/bin/bash
#
# Sample script to generate a daily SRG report. This script assumes that you
# rotate your squid logfiles on a monthly basis using the srg.monthly 
# script provided along with this one. If you have a different scheme you
# may need to modify this script.
#
# This script is designed to be run close after midnight to generate a report
# for the previous day.
# Use a crontab line such as the one below to trigger this script.
# 1 0 * * *       proxy   /usr/local/sbin/srg.daily
#
# Keeps 1 months (31 days) worth of daily reports in the output directory.
#
# Author:       Matt Brown <matt@mattb.net.nz>
# Version:      $Id: srg.daily 243 2008-01-19 18:27:24Z matt $

# Path to SRG binary
SRG=/usr/bin/srg

# Utility to use for sending mail
MAIL_UTIL=/usr/bin/mail

# Top level directory for output reports
REPORTBASE=/var/www/srg_reports

# Configuration file location
CONFIGFILE=/etc/srg/srg.conf

# If you would like a summary report emailed to you each night, specify 
# your email address here
# eg. MAILUSER="srg-daily@yourdomain.com"
MAILUSER=""

# Log Files - Space separated list of logfiles to process
# eg. LOGS="access1.log access2.log access3.log"
LOGS="/var/log/squid/access.log"

# Get the date range
YESTERDAY=$(date --date "1 day ago" +%Y-%m-%d)

# Check that the SRG binary exists and is executable
test -x $SRG || exit 0

# Check that at least one of the specified logfiles exists
found=0
for log in $LOGS; do
    if [ -e $log ]; then
        found=1
    fi
done
if [ "$found" -eq "0" ]; then
    exit 0
fi

# Generate the srg reports
if [ -z $MAILUSER ]; then
    $SRG -C $CONFIGFILE -m 31 -f $YESTERDAY -t $YESTERDAY -o $REPORTBASE/daily $LOGS
else
    $SRG -M -C $CONFIGFILE -m 31 -f $YESTERDAY -t $YESTERDAY -o $REPORTBASE/daily $LOGS | $MAIL_UTIL -s 'Squid Traffic Report' $MAILUSER
fi
