#!/bin/bash
#
# Sample script to generate a weekly 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.
#
# A crontab line such as the one below should be used to trigger this script
# 1 0 * * 7       proxy   /usr/local/sbin/srg.weekly
#
# Keeps 2 months (8 weeks) (57 days) worth of weekly reports in the output
# directory.
#
# Author:       Matt Brown <matt@mattb.net.nz>
# Version:      $Id: srg.weekly 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"

# Working file
TEMPFILE=/tmp/srg.weekly

# Important Dates
lastmon=$(date --date "1 week ago" +%Y%m)
lastweek=$(date --date "1 week ago" +%m)
yesterday=$(date --date "1 day ago" +%m)
WEEKAGO=$(date --date "1 week ago" +%Y-%m-%d)
YESTERDAY=$(date --date "1 day ago" +%Y-%m-%d)

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

# Make the logfile containing the last weeks data
rm -f $TEMPFILE
for log in $LOGS; do
    cat $log >> $TEMPFILE
done
if [ "$yesterday" != "$lastweek" ]; then
    for log in $LOGS; do
        cat $log-$lastmon >> $TEMPFILE
    done
fi
	
# Generate the srg reports
$SRG -C $CONFIGFILE -o $REPORTBASE/weekly -m 57 -f $WEEKAGO -t $YESTERDAY:23:59:59 $TEMPFILE &>/dev/null

# Clean Up
rm -f $TEMPFILE
