#! /bin/ksh
#
# Dailystuff - local commands to be run once a day (from cron).
#
# Version: 1995/05/04.
#
# Usage: Dailystuff
#
# Problems found by this script should be mailed to a system
# administrator.
#
host=`hostname`
admin=system@alchemy
newsadmin=usenet
spool=usr/spool
#
# Set registry master node name.
#
rgymaster=aurum
#
# Set rdate time master node name.
#
timemaster=alchemy
#
umask 22
#
# Get the time of day via rdate if timed is not running.
# ------------------------------------------------------
#
if [ ! -f /etc/daemons/timed -a -x /usr/local/bin/rdate ]; then
   /usr/local/bin/rdate -s $timemaster
fi
#
# Synchronize the local registry with the network registry.
# ---------------------------------------------------------
#
/etc/edrgy -synch
#
# Do the accounting summary if accounting is being used on this node.
# -------------------------------------------------------------------
#
acctfile=/usr/adm/acct
if [ -f $acctfile.old ]; then
   mail -s "Trouble: $acctfile.old already exists" $admin <<EOF1
$0 had a problem since the accounting file
$acctfile.old already exists on `date` on $host.
EOF1
elif [ -f $acctfile ]; then
   mv $acctfile $acctfile.old
   touch $acctfile
   chmod 644 $acctfile
   /etc/accton $acctfile
   /etc/sa -s $acctfile.old >/dev/null
   if [ "$?" != 0 ]; then
      mail -s "Trouble with sa -s $acctfile.old" $admin <<EOF2
$0 had a problem with the accounting file
compression on `date` on $host.
EOF2
   else
      /bin/rm -f $acctfile.old
   fi
fi
#
if [ -f /usr/adm/savacct ]; then
   chmod 644 /usr/adm/savacct
fi
if [ -f /usr/adm/usracct ]; then
   chmod 644 /usr/adm/usracct
fi
#
# NNTP checks.
# ------------
#
# Check that 'nntpsend' has not had any problems, and clean up the log
# file /usr/adm/nntpsend.log.
#
logfile=/usr/adm/nntpsend.log
if [ -f $logfile ]; then
   mv $logfile $logfile.old
   grep 'locked' $logfile.old >/dev/null 2>&1
   if [ "$?" = 0 ]; then
      mail -s "Trouble with lock file(s) for nntpsend" $newsadmin <$logfile.old
   fi
   grep 'failed' $logfile.old >/dev/null 2>&1
   if [ "$?" = 0 ]; then
      grep ' 0 rejected 0 failed$' $logfile.old >/dev/null 2>&1
      if [ "$?" != 0 ]; then
         mail -s "Trouble with nntpsend transmission" $newsadmin <$logfile.old
      fi
   fi
   /bin/rm -f $logfile.old
fi
#
# News checks.
# ------------
#
# Check that news has not had any history file problems.
# If the history file has been trashed, rebuild it with 'mkhistory'.
#
newsspool=/$spool/news
logfile=/usr/local/lib/news/errlog
tmpfile=/tmp/tmpmkhistory
if [ -f $logfile ]; then
   nbadhist=`grep -c 'database files .* incomprehensible' $logfile`
   if [ "$nbadhist" -ge "5" ]; then
      mail -s "Trouble with history database on $host" $newsadmin <<EOF3
The history database on $host has reported $nbadhist errors;
attempting to rebuild it with 'mkhistory'.

Rejected batches have been moved from '$newsspool/in.coming/bad'
to '$newsspool/in.coming' where they will be processed if/when
news processing resumes.
EOF3
      if [ ! -f $tmpfile ]; then
         cat <<EOF4 >$tmpfile
#! /bin/sh
/usr/local/lib/newsbin/expire/mkhistory
/usr/local/lib/newsbin/maint/newsdaily
/bin/rm -f $tmpfile
cd $newsspool/in.coming
mv bad/[0-9]* .
exit 0
EOF4
         chmod +x $tmpfile
         su news -c $tmpfile | mail -s "Mkhistory output from $host" $newsadmin &
      else
         mail -s "Trouble with mkhistory on $host" $newsadmin <<EOF5
The history database on $host has reported $nbadhist errors,
but 'mkhistory' is already running (or has been run).

If 'mkhistory' has completed, and news is not now working smoothly,
I suggest that you do 'newsrunning off' (as user 'news') to stop more
news from being accepted by the system, as it will just be thrown away.
EOF5
      fi
   fi
# Move rejected batches back to the 'in.coming' directory, since
# they are almost always processed properly the second time around.
   if [ -d $newsspool/in.coming ]; then
      cd $newsspool/in.coming
      mv bad/[0-9]* .
      cd /
   fi
fi
#
# /usr/tmp checks.
# ----------------
#
# Check that there aren't any files lying around in /usr/tmp.
# If the files belong to g88 jobs, just blow them away, else just
# complain to the system administrator.
#
if [ -d /usr/tmp ]; then
   cd /usr/tmp
   oldfilelist=/tmp/oldfilelist.$$
   ls -l | grep -v '^total ' >$oldfilelist 
   if [ -s $oldfilelist ]; then
      echo "" >>$oldfilelist 
      echo "Any 'g88-' files on the above list have been deleted." >>$oldfilelist 
      mail -s "/usr/tmp files found on $host" $admin <$oldfilelist
   fi
   /bin/rm -f $oldfilelist g88-*
   cd /
fi
#
# Clean up /$spool/rwho files.
# ----------------------------
#
# Check for old rwho files lying around in /$spool/rwho.
# If the files are more than 7 days old, just blow them away.
#
find /$spool/rwho -type f -mtime +7 -exec rm -f {} \; >/dev/null 2>&1
#
# Do a check of the system error log.
# -----------------------------------
#
errorlog=/usr/adm/Dailystuff.lsyserr
errordiff=/tmp/errorlog.$$
/systest/ssr_util/lsyserr >$errorlog.new
chmod 644 $errorlog.new
if [ -f $errorlog ]; then
   diff $errorlog $errorlog.new >$errordiff
   if [ -s $errordiff ]; then
      mail -s "New system error log entries on $host" $admin <<EOF30
Dailystuff found the following new error messages in the lsyserr log
on $host:

`cat $errordiff`

`ll $errorlog $errorlog.new`
EOF30
      mv $errorlog.new $errorlog
   else
      /bin/rm -f $errorlog.new
   fi
else
   mv $errorlog.new $errorlog
   mail -s "System error log on $host" $admin <<EOF31
Dailystuff created a new 'lsyserr' log on $host:

`cat $errorlog`
EOF31
fi
/bin/rm -f $errordiff
#
# Do a check of the syslog log for failed logins.
# -----------------------------------------------
#
errorlog=/usr/adm/Dailystuff.syslog
errordiff=/tmp/errorlog.$$
grep 'LOGIN FAILURE' /usr/adm/critical >$errorlog.new
chmod 640 $errorlog.new
if [ -f $errorlog ]; then
   diff $errorlog $errorlog.new >$errordiff
   if [ -s $errordiff ]; then
      mail -s "New syslog entries on $host" $admin <<EOF32
Dailystuff found the following new login failures in the 'critical' syslog
on $host:

`cat $errordiff`

`ll $errorlog $errorlog.new`
EOF32
      mv $errorlog.new $errorlog
   else
      /bin/rm -f $errorlog.new
   fi
else
   mv $errorlog.new $errorlog
   mail -s "Syslog entries on $host" $admin <<EOF33
Dailystuff created a new login failure log on $host:

`cat $errorlog`
EOF33
fi
/bin/rm -f $errordiff
#
# Check lost+found directories.
# -----------------------------
#
# Check for files lying around in the various lost+found directories.
# Complain to the system administrator if anything is found.
#
oldfilelist=/tmp/oldfilelist.$$
for d in `findfile '/lost\+found$'`
do
   if [ ! -d $d ]; then
      continue
   fi
   cd $d
   ls -l | grep -v '^total ' >$oldfilelist 
   if [ -s $oldfilelist ]; then
      mail -s " Extraneous files found in $d on $host" $admin <$oldfilelist
   fi
   cd /
done
/bin/rm -f $oldfilelist
#
# /$spool/mqueue checks.
# ----------------------
#
# Check that there aren't any old files lying around in /$spool/mqueue.
# The time specified on the 'find' command line should be longer than
# the time sendmail will store the message trying to deliver it.
#
#echo "*** Checking /$spool/mqueue on `date` ***"
if [ -d /$spool/mqueue ]; then
   cd /$spool/mqueue
   mqueuefilelist=/tmp/mqueuefilelist.$$
   find . -type f -mtime +14 -exec ls -ld {} \; >$mqueuefilelist
   if [ -s $mqueuefilelist ]; then
      echo "" >>$mqueuefilelist
      echo "Check these files and delete them if necessary." >>$mqueuefilelist
      echo "See page 11 of the Sendmail Installation Guide." >>$mqueuefilelist
      mail -s "Old /$spool/mqueue files found on $host" $admin <$mqueuefilelist
   fi
   /bin/rm -f $mqueuefilelist
   cd /
fi
#
# Sync the master registry with the NIS group map.
# ------------------------------------------------
#
# Do this only on the registry master node.
#
if [ "$rgymaster" = "$host" ]; then
   yplisting=/tmp/ypxfr.out.$$
   /bin/rm -f $yplisting
   if [ -f /usr/local/bin/ypxfr.group ]; then
      /usr/local/bin/ypxfr.group 2>&1 >>$yplisting
   else
      mail -s "Can't sync registry with NIS group map on $host" $admin <<EOF98
$0 can't find /usr/local/bin/ypxfr.group
to sync the registry with NIS group map.
EOF98
   fi
   let i="`grep -v '^#### ypxfr' $yplisting | wc -l`"
   if [ $i -ne 0 ]; then
      mail -s "Registry sync with NIS passwd/group maps on $host" $admin <<EOF99
$0 registry sync gave:

`cat $yplisting`
EOF99
   fi
fi
#
# Clean up.
# ---------
#
/bin/rm -f $yplisting
#
exit 0
#
# End of Dailystuff.
#
