#!/bin/bash
#
# SRG - Debian Daily Report Generation 
# This script generates a report for the previous day and outputs it into
# the path specified in /etc/srg/srg.conf (/var/www/srg_reports by default).
#
# It assumes that your logfiles have not been rotated before this script runs.
#
# If you would like a summary report emailed to you each night, enter your
# email address in the MAILUSER field below
#
# Keeps 1 months (31 days) worth of daily reports in the output directory.
#
# Author:       Matt Brown <matt@crc.net.nz>
# Version:      $Id: srg.daily 222 2005-05-11 05:04:46Z matt $

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

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

# 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 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 $LOGS
else
    $SRG -M -C $CONFIGFILE -m 31 -f $YESTERDAY -t $YESTERDAY $LOGS | $MAIL_UTIL -s 'Squid Traffic Report' $MAILUSER
fi
