#!/bin/sh

BCNUNOLOG="yes"
#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

DATALOG=${DATALOG:=20}		# set in bcnulocal
LIMIT=${LIMIT:=5}
TRANSIENT=${TRANSIENT:=1}

LOGFILES="bcnu retry message pager mail ckbcnu snmptrap weback win32 run" # names of logfiles


RMACTION="rm -f {} ;"			# action to remove files
RMDIRACTION="rmdir {} ;"		# action to remove directories

# use these for testing
#RMACTION="cp {} /tmp/bucket ;"		# action to remove files
#RMACTION="false ;"
#RMDIRACTION="false ;"			# action to remove directories

LOGDATE=`date +%Y%m%d`
MYHOST=`uname -n`

numhosts ()
{
MONHOSTS=`ls $BCNUHOME/data/hosts | wc -l`
}

#set -x
#
# daily admin for bcnu
#


{
echo admin agent starting
date
# Produce error/warning report

echo "bcnu Error/Warning Report for $LOGDATE for $MYHOST"

$AWK -f $BCNUHOME/agent/report.awk -v myhost=$MYHOST $BCNUHOME/logs/message.log

# Tidy up log files 
for logfile in $LOGFILES
do
	echo Copying $logfile.log to $logfile.log.$LOGDATE and initializing
	cat $BCNUHOME/logs/$logfile.log  >>$BCNUHOME/logs/$logfile.log.$LOGDATE
	echo "Initialized `date`" >$BCNUHOME/logs/$logfile.log
	chmod 777 $BCNUHOME/logs/$logfile.log
done

ls $BCNUHOME/data/hosts >$BCNUTMP/hosts.before
HOSTSBEFORE=`wc -l <$BCNUTMP/hosts.before`

echo Currently $HOSTSBEFORE hosts being monitored

touch $BCNUHOME/logs/bcnud.log

echo Clean up any log files older than $LIMIT days
find $BCNUHOME/logs/ -type f -mtime +$LIMIT -exec $RMACTION
echo Clean up any retry files older than $LIMIT days
find $BCNUHOME/retry/ -type f -mtime +$LIMIT -exec $RMACTION
echo Clean up any files older than $DATALOG days in the data directory
find $BCNUHOME/data/ -type f -mtime +$DATALOG -exec $RMACTION

echo Clean up any files older than $TRANSIENT days in the data/files directory
find $BCNUHOME/data/files -type f -mtime +$TRANSIENT -exec $RMACTION

echo Clean up any files older than $TRANSIENT days in the data/hosts directory
find $BCNUHOME/data/hosts -type f -mtime +$TRANSIENT -exec $RMACTION

# Clean up any empty directories (messy but effective!)
find $BCNUHOME/data/hosts/* -type d -exec $RMDIRACTION 2>/dev/null

echo Clean up any files older than $TRANSIENT days in the data/agents directory
find $BCNUHOME/data/agents -type f -mtime +$TRANSIENT -exec $RMACTION

echo Clean up any files older than $TRANSIENT days in the www/hosts directory
find $BCNUHOME/www/hosts -type f -mtime +$TRANSIENT -exec $RMACTION

echo Clean up any files older than $TRANSIENT days in the flags directory
find $BCNUHOME/flags/ -type f -mtime +$TRANSIENT -exec $RMACTION

# remove mail-alerts flags
rm $BCNUHOME/flags/sentalert.* 2>/dev/null

# remove win32alerts 
rm $BCNUHOME/data/win32alert/* 2>/dev/null

# remove ignore flags unless otherwise specified
if [ -z "$KEEPIGNORE" ]
	then
		rm $BCNUHOME/flags/*.ignore 2>/dev/null
fi

# move any defered alerts to the alert file
if [ -r $BCNUHOME/flags/bcnud.alert.defer ]
then
	cat $BCNUHOME/flags/bcnud.alert.defer >>$BCNUHOME/flags/bcnud.alert
	rm $BCNUHOME/flags/bcnud.alert.defer
fi

ls $BCNUHOME/data/hosts >$BCNUTMP/hosts.after
HOSTSAFTER=`wc -l <$BCNUTMP/hosts.after`

echo Now $HOSTSAFTER hosts being monitored

if [ "$MYHOST" = "$BCNUHOST" -a $HOSTSAFTER -lt $HOSTSBEFORE ]
	then
		diff -C0 $BCNUTMP/hosts.before $BCNUTMP/hosts.after >$BCNUTMP/hosts.diff
		BCNUMSG="$BCNU -m 'ADMIN Monitoring $HOSTSAFTER hosts was ($HOSTSBEFORE)' -f $BCNUTMP/hosts.diff -e $BCNU_ALERT -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send		
fi

rm $BCNUTMP/hosts.*

date
echo admin agent complete
}  >$BCNUHOME/logs/admin.log 2>&1


# do some things e.g. rebuild web pages on master only
if [ "$MYHOST" = "$BCNUHOST" ]
	then
	# build stuff needed for web interface
	$BCNUHOME/agent/bcnuinfo

	$BCNUHOME/scripts/buildweb -ad
#set -x	
	if [ $RESTART_BCNU ]
	then
		# restart bcnu server - prevents some problems on busy systems
		$BCNUHOME/etc/bcnud_server stop
		(sleep 60 ; $BCNUHOME/etc/bcnud_server start) >&- 2>&- &
	fi

fi

