# UNIX statbot script. written Warren Kibbe, 1996 # WAKibbe@nwu.edu # # version 1.0 release 2 # Changes 4/20/96. # - Script is no longer path dependent. # - Script also checks the actual identity of the log # files using bdiff to verify that the log data is the same # in the existing ("old") log file and the new log file. The # old method relied solely on the size of the files in blocks. # # change these variables to use in your statbot log! # # NOTE: Script and logs must be kept in the same directory! # # To start this script, type "sh 'scriptname'" from your login shell # To view the schedule of the script, type "at -l" # To delete a scheduled script, type "at -r 'scriptid'" # # ******* VARIABLES ******* mailbox="wak@nwu.edu" #destination for notifications scriptname="statbot.script" #name of the script file permlogfile="oldlog" #this is where the permanent log is kept logfilename="dailylog" #this is where the daily log file is kept path="/users/wakibbe/stats/" # makes the script run from directories # other than root (login) newlogfile="templog" # ********* Main Body of Script ********** now=`date` exec > /dev/null 2>&1 #this routes all messages into 'nothing' cd $path pwd oldlogfile=$logfilename # messages from "grablog" and "statbot" are routed to null (trash) # replace with pipe to email if you want the output /users/pib/bin/grablog > "$newlogfile" 2> /dev/null /users/pib/statbot/statbot > /dev/null 2>&1 # schedule next run depending on the time of day # currently set to run hourly from 10am to 7pm, and at 11 pm. currenttime=`date +'%H'` currentmin=`date +'%M'` if [ $currenttime -lt 10 ] then nexttime="10:$currentmin" else if [ $currenttime -lt 19 ] then nexttime="now +1 hour" else if [ $currenttime -lt 23 ] then nexttime="23:$currentmin" else nexttime="10:$currentmin" fi fi fi # to run script ever hour regardless of time of day, remove the "#" from # the next line # nexttime="10:$currentmin" at $nexttime < $path$scriptname #example alternative pipe to at (crontab is disabled at pubweb workstations) # echo "sh $path$scriptname >/dev/null 2>&1" | at $nexttime temp="Made the file $newlogfile" temp=$temp"\nExisting File: $oldlogfile " temp=$temp"\nlog grabbed at $now" echo $oldlogfile if [ ! -r $oldlogfile ] then echo "Made it inside oldlogfile test" cat /dev/null > $oldlogfile fi if [ ! -w $permlogfile ] then echo "Made it inside permlogfile test" cat /dev/null > $permlogfile fi #get the actual changed line count difflines=`bdiff $newlogfile $oldlogfile | wc -l` wc -l $newlogfile > temp1 newloglines=`cut -f1 -d" " temp1` wc -l $oldlogfile > temp1 oldloglines=`cut -f1 -d" " temp1` calcdiff=` expr "$newloglines" - $oldloglines"` #this piping method does not work from a batch! #oldloglines=`more $oldlogfile | wc -l` temp=$temp"\nChecking site access stats:" temp=$temp"\n\tLines that differ between current and new logs: $difflines " temp=$temp"\n\tLines in the new log: $newloglines " temp=$temp"\n\tLines in the old log: $oldloglines" temp=$temp"\n\tCalculated difference: $calcdiff" echo $temp if [ "0$difflines" -ge "0$newloglines" ] then # If difflines is greater than or equal to the number of lines in the newlog # then concatenate the permanent log file and the old log file # can't do this in one call, since cat overwrites destination echo "executing diflines test" if [ "0$oldloglines" -gt "0" ] then cat $permlogfile $oldlogfile > log.temp mv log.temp $permlogfile # send notification that the old log file was appended temp=$temp"\nappending old log to $permlogfile " temp=$temp"\nnumber of lines appended=$oldloglines" temp=$temp"\nScript will be run again at $nexttime" echo $temp | mail $mailbox # take the IP numbers out of the old log file and email them to your mailbox cut -f1 -d" " $oldlogfile > visitor.ip mail $mailbox < visitor.ip echo "executing concatenation" fi # end if oldloglines less than 1 else # If the new file is contains the old log, simply rename temp=$temp"\nNew log renamed to $logfilename" temp=$temp"\nScript will be run again at $nexttime" #comment this out to be notified every time script runs #echo $temp | mail $mailbox echo "executing rename file" fi # end if difflines greater than lines in the new log # Final action: # take the new log file and change it to the name of the oldlogfile # This will overwrite the existing log file if it hasn't been added # to the permanent log file. mv $newlogfile $logfilename echo "finished with script"