#!/bin/bash # Squid reports maker and log backup tool # Written by Zhaolei # I am a programmer. # Because it is my first time writing shell-script without learning it, maybe some bug in it. # I wish you can improve it. # -------------------------------------------------- # Zhao Lei # Dept. of Technology and Development, # Nanjing Fujitsu Nanda Software Tech. Co., Ltd.(FNST) # No.189, Guangzhou Rd.,Nanjing, P.R. China # Civil Defense Mansion 8F # PHONE」コ+86+25-86630566-838 # FUJITSU INTERNAL」コ79955838 # FAX」コ+86+25-83317685 # Mail」コzl@nanjing-fnst.com # -------------------------------------------------- VER="1.0 20050127" # Following setting must check and modify before first time of running SQUID_LOGPATH=/var/log/squid SARG_CONFIGFILE=/root/proxy/sargtop10.conf SARG_REPORTPATH=/var/www/html/reports REPORT_FILE=/root/proxy/top10.log # Following setting is advanced config, keep default is no problem PACKBACKUPFILES=YES # YES/NO ROTATEDLOGWAITCOUNTMAX=50 # Unit: 0.1s DETAILLOG=YES # YES/NO # Check is report file exist, this must be done first if [ ! -f $REPORT_FILE ]; then touch $REPORT_FILE 2>/dev/null if [ $? != 0 ]; then echo Report file not exist, ignore report! REPORT_FILE=/dev/null fi fi # Output detail log of "Begin report and backup" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Begin report and backup" echo $ERRORMSG >> $REPORT_FILE fi # Check is squid log file path exist if [ ! -d $SQUID_LOGPATH ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Squid log file path not exist! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Check is sarg config file exist if [ ! -f $SARG_CONFIGFILE ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Sarg config file not exist! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Check is sarg report path exist if [ ! -d $SARG_REPORTPATH ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Sarg report path not exist! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Check is squid running /etc/init.d/squid status > /dev/null if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Squid is not running! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Setting Necessary Variables SQUID_ACCESS_LOG=${SQUID_LOGPATH}/access.log SQUID_ACCESS_ROTATEDLOG=${SQUID_LOGPATH}/access.log.0 NOW=$(date +%Y%m%d%H%M%S) BACKUPFILESINGLENAME=access.log.bak${NOW} BACKUPFILENAME=${SQUID_LOGPATH}/$BACKUPFILESINGLENAME BACKUPPACKAGENAME=${SQUID_LOGPATH}/access.log.bak.tar # Generate local sleep args. If current system do not support float sleep arg, we adjust sleep args into int value sleep 0.001 2> /dev/null if [ $? != 0 ]; then ROTATEDLOGWAITCOUNTMAX_LOCAL=`expr \( $ROTATEDLOGWAITCOUNTMAX + 9 \) / 10` ROTATEDLOGWAITTIMEMAX_LOCAL=1 else ROTATEDLOGWAITCOUNTMAX_LOCAL=$ROTATEDLOGWAITCOUNTMAX ROTATEDLOGWAITTIMEMAX_LOCAL=0.1 fi # Check is squid log file exist if [ ! -f $SQUID_ACCESS_LOG ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Squid log file "${SQUID_ACCESS_LOG}" not exist! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # If rotated log file exist, delete it if [ -f $SQUID_ACCESS_ROTATEDLOG ]; then # Delete existing rotated log rm -f $SQUID_ACCESS_ROTATEDLOG 2> /dev/null if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Delete existing rotate log file fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Add into log ERRORMSG=$(date +%Y%m%d%H%M%S)": Old rotated log exist, delete it now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE fi # Output detail log of "Begin rotate" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Begin rotate" echo $ERRORMSG >> $REPORT_FILE fi # Call squid to rotate log squid -k rotate if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Squid rotate operation fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Wait for exist of rotated log file # By test, wait time normally 0.8-1.6s count=$ROTATEDLOGWAITCOUNTMAX_LOCAL while ( test $count -gt 0 ) do # Speep one unit time sleep $ROTATEDLOGWAITTIMEMAX_LOCAL 2> /dev/null # If rotated log file generated, goto next step if [ -f $SQUID_ACCESS_ROTATEDLOG ]; then break fi count=`expr $count - 1` done # Check is rotated log file exist if [ ! -f $SQUID_ACCESS_ROTATEDLOG ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Watting for squid rotated log file reached max time! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Output detail log of "Rotate complished" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Rotate complished" echo $ERRORMSG >> $REPORT_FILE fi # Output detail log of "Begin generate log report" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Begin generate log report" echo $ERRORMSG >> $REPORT_FILE fi # Use sarg to generate log report sarg -f $SARG_CONFIGFILE -l $SQUID_ACCESS_ROTATEDLOG -o $SARG_REPORTPATH if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Sarg generate log report fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Output detail log of "Generate log report complished" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Generate log report complished" echo $ERRORMSG >> $REPORT_FILE fi # Rename squid rotated log file mv -f $SQUID_ACCESS_ROTATEDLOG $BACKUPFILENAME 2> /dev/null if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Rename squid rotated log file fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Output detail log of "Begin compress squid rotated log" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Begin compress squid rotated log" echo $ERRORMSG >> $REPORT_FILE fi # Compress squid rotated log file gzip $BACKUPFILENAME 2> /dev/null if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Compress squid rotated log file fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Output detail log of "Compress squid rotated log complished" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Compress squid rotated log complished" echo $ERRORMSG >> $REPORT_FILE fi # Pack backup file if [ $PACKBACKUPFILES = "YES" ]; then # Output detail log of "Begin pack compressed squid rotated log file" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Begin pack compressed squid rotated log file" echo $ERRORMSG >> $REPORT_FILE fi tar -rf ${BACKUPPACKAGENAME} -C$SQUID_LOGPATH $BACKUPFILESINGLENAME.gz if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Pack compressed squid rotated log file fail! Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi # Output detail log of "Pack compressed squid rotated log file complished" if [ $DETAILLOG = "YES" ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": ----Pack compressed squid rotated log file complished" echo $ERRORMSG >> $REPORT_FILE fi rm -rf ${BACKUPFILENAME}.gz if [ $? != 0 ]; then ERRORMSG=$(date +%Y%m%d%H%M%S)": Delete compressed squid rotated log file fail!(It is already packed into pack file) Exit now." echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 1 fi fi ERRORMSG=$(date +%Y%m%d%H%M%S)": Report and backup success!" echo $ERRORMSG echo $ERRORMSG >> $REPORT_FILE exit 0