1 #!/bin/bash 2 # 3 # Sample script to generate a monthly SRG report. This script assumes that 4 # your logs are not already rotated. It handles log rotation before it 5 # generates the report for the previous month 6 # 7 # A crontab line such as the one below should be used to trigger this script 8 # 1 0 1 * * root /usr/local/sbin/srg.monthly 9 # 10 # Keeps an unlimited number of monthly reports in the output directory! 11 # I hope you have lots of diskspace!!! 12 # 13 # NOTE: This script doesn't necessarily do what you want, it is provided 14 # only as an example of what you may want to do at the end of the month. 15 # Please review it carefully before using it! 16 # 17 # Author: Matt Brown <matt@mattb.net.nz> 18 # Version: $Id: srg.monthly 243 2008-01-19 18:27:24Z matt $ 19 # 20 21 # Path to SRG binary 22 SRG=/usr/bin/srg 23 24 # Utility to use for sending mail 25 MAIL_UTIL=/usr/bin/mail 26 27 # Top level directory for output reports 28 REPORTBASE=/var/www/srg_reports 29 30 # Configuration file location 31 CONFIGFILE=/etc/srg/srg.conf 32 33 # If you would like a summary report emailed to you each night, specify 34 # your email address here 35 # eg. MAILUSER="srg-daily@yourdomain.com" 36 MAILUSER="" 37 38 # Log Files - Space separated list of logfiles to process 39 # eg. LOGS="access1.log access2.log access3.log" 40 ALOGS="/var/log/squid/access.log" 41 CLOGS="/var/log/squid/cache.log" 42 43 # Working file 44 TEMPFILE=/tmp/srg.monthly 45 46 # Permissions the Squid Logfiles should have 47 PERMS=640 48 49 # Check that the SRG binary exists and is executable 50 test -x $SRG || exit 0 51 52 # Make the logfile containing the last months data 53 rm -f $TEMPFILE 54 for log in $ALOGS; do 55 cat $log >> $TEMPFILE 56 done 57 58 lastmon=$(date --date "1 day ago" +%Y%m) 59 60 # Stop squid 61 /etc/init.d/squid stop &> /dev/null 62 63 # Rotate access logs 64 for log in $ALOGS; do 65 mv $log $log-$lastmon 66 touch $log 67 chown proxy: $log 68 chmod $PERMS $log 69 done 70 # Rotate cache logs 71 for log in $CLOGS; do 72 mv $log $log-$lastmon 73 touch $log 74 chown proxy: $log 75 chmod $PERMS $log 76 done 77 78 # Start squid again 79 /etc/init.d/squid start &>/dev/null 80 81 # Gzip last months logs 82 for log in $ALOGS; do 83 gzip $log-$lastmon 84 chmod $PERMS $log-$lastmon.gz 85 done 86 for log in $CLOGS; do 87 gzip $log-$lastmon 88 chmod $PERMS $log-$lastmon.gz 89 done 90 91 # Get the date range 92 YEAR=$(date --date "last month" +%Y) 93 LASTMONTH=$(date --date "last month" +%m) 94 LASTDAY=$(date --date "1 day ago" +%d) 95 FIRSTDAY="$YEAR-$LASTMONTH-01" 96 LASTDAY="$YEAR-$LASTMONTH-$LASTDAY" 97 98 # Generate the srg reports 99 $SRG -C $CONFIGFILE -o $REPORTBASE/monthly -f $FIRSTDAY -t $LASTDAY:23:59:59 $TEMPFILE &>/dev/null 100 101 # Clean Up 102 rm -f $TEMPFILE