#!/usr/local/bin/ksh ################################################################################# # Script ecs.msg.mgr # # Description Designed to be run via rsh from an NT Workstation/Server to # # manage the message log for ECS (Event Control System). This # # log often times gets too long and takes too long for ECS to # # manage. The NT Server that ECS runs on will copy the msg.log # # to /usr/ECS/msg.log.tmp and run this script. # # Written On 03/29/01 # # Modifications 04/05/01 - Restructured script so it could be run at any time # # of the day. HOBBES will now run this script remoteley every # # time ECS is started so the number of logs will always be 5000 # # prior to ECS starting. # # 04/06/01 - Still can't find a good way to sort... Serial # # numbers wrap and become unsortable, Date wraps and becomes # # unsortable, and the time is AM/PM so it sorts incorrectly as # # well. I have come to the conclusion the only way to do this is # # to truncate the msg.log file and send it back. We will keep # # the stripped logs in msg.log.$SERIAL so each time ECS is # # restarted, there will be a new file and ECS will start with a # # log file with only 5000 lines providing a quick start every # # time. At 23:59, HOBBES will kick off this script to clean out # # the msg.log. We (this script) touch $1.$SERIAL every time this # # script is run so we can keep track of reboots and make sure the # # cron on HOBBES is running every night. The cleanup section was # # changed so it will cleanup anything with the date 30 days ago # # in the filename. This should be the end of this for a while... # # 04/08/01 - Changed $msg_returned from 5000 to 4080. Messages # # were getting purged by Mary which is undesirable since we want # # to keep all the purged messages here. # # 04/16/01 - Function 'hexinc' was trying to compare a hex number # # with a decimal number ($NEWMSGIDHEX). It was supposed to be # # comparing $NEWMSGIDDEC with a decimal number and now it is. # # 04/16/01 - Changed how we log. We also log to an activity log. # # 04/16/01 - Had a big boo boo. Another script wiped out the # # entire /home/bin directory tree which included this file. # # Since I had been working on this file, I had a good chunk of it # # in display memory so it was quickly dumped to a file... Seems # # to be working ok... # # 01/28/03 - Added the ability to keep logs for any given day in # # a separate file with a file name that indicates the day the # # logs belong to. This script will continue as it has in the # # past but will now have this added functionality. The log files # # created will be behind by about 24 hours but will contain only # # logs for that day and they will be in order even if the log # # indicator rolls (ffff-0000) over (which was the reason this was # # never done before. It was also noticed that the $msg_returned # # tag was set at 4080 which allows for 980 logs to be accumulated # # in two minutes. A decision was made to decrease this number to # # an even 4000 since it really doesn't matter anyway as long as # # the script runs and the number of logs is less than 5000 at # # midnight on HOBBES. - rmorten # # 01/29/03 - Oops! The 'if' statement that contains the code to # # watch the log indicator for rollover caused the rest of the # # code to be ignored if the log indicator wasn't going to roll # # over. Else statement added so check back tomorrow. - rmorten # # 01/31/03 - Logging started for each individual light sensor. # # There is a tag "{LOGGER}" included in every type of log we want # # broken out so if the log appears in the main log with that tag # # it will have it's own log when this script is run in cleanup # # mode. - rmorten # # 02/05/03 - Just so there will be a log that Hobbes will have # # online we are now storing the number of messages purged where # # Hobbes can find it. Hobbes will in turn use the number to # # create a log entry and display both the reason and the number # # logs purged on a maintenance page. - rmorten # # 06/27/04 - A recent boo boo in /home/bin deleted all scripts # # since March of last year. Re-added section to discern between # # shutdowns and restarts. - rmorten # # 07/07/04 - The recent change in light level logging caused too # # many logs for Mary to be able to keep them in the main log so # # another log file was created (logger1.log) to keep them in. # # Processing had to be changed to match here... - rmorten # # 07/10/04 - Correcting a couple of scripting problems associated # # with the latest change. Also now copy the last couple of logs # # which belong to the new day back to the file that is copied # # back to hobbes so there are no missing logs. - rmorten # # 07/13/04 - Added a line to keep yesterday's light logs in a file# # so Hobbes can keep a copy available on a MS platform. Until # # now we have been using FTP to move files around and that is # # getting old. - rmorten # # Written By Rick Mortensen # ################################################################################# # Program Variables msg_returned=4000 bkdate=/bin/bkdate awk=/usr/bin/awk htod=/home/bin/htod dtoh=/home/bin/dtoh DAYMINUS30=`$bkdate -d30 '+%y%m%d'` YESTERDATE=`$bkdate -d1 '+%m/%d'` YESTERDAY=`$bkdate -d1 '+%y%m%d'` DATE=`date '+%m/%d'` DAY=`date '+%y%m%d'` SERIAL=`date '+%y%m%d%H%M%S'` PARAMETERS="restart, cleanup, or log" # Directory Variables ECSDIR=/usr/ECS # File Variables tmp=/tmp/ecs.tmp LOG=$ECSDIR/msg.log BAK=$LOG.bak TMP=$LOG.tmp ACTIVITYLOG=$ECSDIR/activity.ecs.msg.log BACKLOGS="$LOG.$DAY* $LOG.$YESTERDAY*" LOGGER1="$ECSDIR/logger1.log" # Functions hexinc () { # Incr hex number OLDMSGIDDEC=`$htod $OLDMSGIDHEX` let NEWMSGIDDEC=$OLDMSGIDDEC+1 NEWMSGIDHEX=`$dtoh $NEWMSGIDDEC | tr "[A-Z]" "[a-z]"` if [ $NEWMSGIDDEC -ge 65537 ] then NEWMSGIDHEX=0000 fi OLDMSGIDHEX=$NEWMSGIDHEX case $NEWMSGIDHEX in ?)NEWMSGIDHEX=000$NEWMSGIDHEX ;; ??)NEWMSGIDHEX=00$NEWMSGIDHEX ;; ???)NEWMSGIDHEX=0$NEWMSGIDHEX ;; ????)NEWMSGIDHEX=$NEWMSGIDHEX ;; esac } truncate () { # Truncate log file and send result to msg.log # echo Tail 1 tail -$msg_returned $TMP > $LOG OLD=`wc -l $TMP | $awk '{print $1}'` let PURGED=$OLD-$msg_returned } storelog () { # Get a copy of all logs purged and keep them in a local file head -$PURGED $TMP > $LOG.$SERIAL } makelogentry () { # 60db O 04/16 6:59 PM:cliff purges 404 messages from log # Make an entry in the local log file to show we were here. # echo Tail 2 OLDMSGIDHEX=`tail -1 $LOG.$SERIAL | $awk '{print $1}'` hexinc echo "$NEWMSGIDHEX O `date '+%m/%d %l:%M %p'`:cliff purges $PURGED messages from log for system $1" >> $LOG.$SERIAL echo "$NEWMSGIDHEX O `date '+%m/%d %l:%M %p'`:cliff purges $PURGED messages from log for system $1" >> $ACTIVITYLOG echo "$NEWMSGIDHEX O `date '+%m/%d %l:%M %p'`:cliff purges $PURGED messages from log for system $1" } # We now expect the user to use a variable to show the type of activity. # We will test for that here. if [ $# -ne 1 ] then echo "\n\nYou must either enter $PARAMETERS.\nExiting...\n\n";exit fi # Test for tmp file and if it missing, exit if [ -s $TMP ] then : else echo "\ntmp file missing; exiting...\n" exit fi # If user input matches acceptable parameters act upon them, otherwise let them know what they did wrong. case $1 in restart) truncate storelog makelogentry $1 cp $LOGGER1.tmp $LOGGER1 ;; shutdown) truncate storelog makelogentry $1 cp $LOGGER1.tmp $LOGGER1 ;; cleanup) truncate storelog makelogentry $1 cp $LOGGER1.tmp $LOGGER1 ;; log) truncate storelog makelogentry $1 # Build a msg.log file that contains only the logs for that day (hopefully in order) cat $TMP $BACKLOGS | grep "$YESTERDATE" | sort | uniq | grep -v "messages from log for" > $TMP.backlog awk ' $1 ~ /^ffff/ {print $0}' $TMP.backlog > $TMP.backlog.test if [ -s $TMP.backlog.test ] then awk ' $1 ~ /^[5-9a-f]/ {print $0}' $TMP.backlog > $LOG.$YESTERDAY.history awk ' $1 ~ /^[0-4]/ {print $0}' $TMP.backlog >> $LOG.$YESTERDAY.history else mv $TMP.backlog $LOG.$YESTERDAY.history fi # Copy the latest and greatest logger log file to a working file cp $LOGGER1.tmp $LOGGER1.$YESTERDAY grep "^$YESTERDATE" $LOGGER1.tmp > $LOGGER1.$YESTERDAY grep -v "^$YESTERDATE" $LOGGER1.tmp > $LOGGER1 # Build a list of logged items and dump to tracking file LOGLIST=`nawk -F '~' '{print $2} ' $LOGGER1.$YESTERDAY | awk ' /LOGGER/ {print $0}' | sed 's/is [0-9].*//g'|sort|uniq|sed 's/ /_/g'` for log in $LOGLIST do Log=`echo $log | sed 's/_/ /g'` filelog=`echo $log | sed 's/,//g'|sed 's/_/./g'` grep "${Log}is" $LOGGER1.$YESTERDAY | sed 's/~/ ~ /g'|awk '{print $1,$2",""-"$(NF-1)}' > $LOGGER1.$YESTERDAY.${filelog}csv grep "${Log}is" $LOGGER1.$YESTERDAY | sed 's/~/ ~ /g'|awk '{print $1,$2",""-"$(NF-1)}' > $ECSDIR/Logger1/${filelog}csv done ;; *) echo "\n\nThe only choices are $PARAMETERS.\nExiting...\n\n" ; exit ;; esac # Store number of messages purged in a file for hobbes to copy echo "$PURGED\n" > $ECSDIR/purged echo "$1\n" > $ECSDIR/reason # Cleanup rm -fr $TMP*