#!/bin/bash
#==============================================================
# screenlogd_facet.bash
#
# ARGUMENTS:
#
# OPTION:     
#
# Facility:  SLAC/FACET
#
# Auth:  21-Jun-2011, jrock - port to FACET
# Rev:   dd-mmm-yyyy, Reviewer's Name (USERNAME)
#
# Rem:   Screen logging management
#        - save screenlog.0 to screenlog.1
#        - screenlog.0 will be recreated automatically by Screen program 
#           if removed.        
#        - remove screenlog.1 if older than 3 days.
#        - run as cron job
#        - Note: automatic restart of the screen logging daemon will be added 
#----------------------------------------------------------------------------
# Mod:
#        15-April-2105, jingchen
#        - removed screenlog.0 existing condition in cleanup
#==============================================================
EMAIL="jingchen@slac.stanford.edu"
#EMAIL="jrock@slac.stanford.edu"

emailcmd="/usr/bin/Mail -s $0 ${EMAIL}"

#
# Set up FACET_ROOT
#
if [ -d /afs/slac/g/facet ]; then
   source /afs/slac/g/facet/tools/script/ENVS_facet.bash
   export SCREEN_HOST=facet-dev2
   export FACET_ROOT=/afs/slac/g/facet
else 
   source /usr/local/facet/tools/script/ENVS_facet.bash
   export SCREEN_HOST=facet-daemon1
   export FACET_ROOT=/usr/local/facet 
fi
#
# Set up FACET_DATA
#
if [ -d /nfs/slac/g/facet ]; then
   export FACET_DATA=/nfs/slac/g/facet  
else
   export FACET_DATA=/u1/facet
fi

IOC_SCREEN=$FACET_ROOT/epics/iocCommon
IOC_DATA=$FACET_DATA/epics/ioc/data

# go through all the hard iocs (ioc-*, eioc-*)
# createDetachedScreen.bash checks for a screen process and socket file
# if these aren't found, creates a new screen session.
# the new screen process will write to the existing screenlog.0; if the file
# is not found, it will create one

echo STARTING SCREEN PROCESSES THAT NEED IT...
iocs=`grep ^[e]*ioc-.*$SCREEN_HOST $IOC_SCREEN/All/Prod/facet_screeniocs | awk '{print $1}'`
for i in $iocs
do
  echo checking screen process for $i
  $FACET_ROOT/tools/script/createDetachedScreen_facet.bash $i
done

/usr/local/facet/tools/script/check_screenlogdLog_facet.bash
cat /tmp/screenlogdLogErrList.log | Mail -s "FACET screenlogd createDetachedScreen errors" jingchen@mccdev.slac.stanford.edu
#cat /tmp/screenlogdLogErrList.log | Mail -s "FACET screenlogd createDetachedScreen errors" jrock@mccdev.slac.stanford.edu 

# now cleanup oversized screenlog.0, and old screenlog.1 files
cd $IOC_DATA
if [ $? -ne 0 ]; then                
    echo ERROR: $IOC_DATA not available   
    exit 1
fi

# first, check soft iocs for screenlog.0
# for each soft ioc with a socket file, make sure there's a screenlog.0
siocs=`grep ^sioc-.*$SCREEN_HOST $IOC_SCREEN/screeniocs | awk '{print $1 ":" $4}'`
echo LIST OF FACET PRODUCTION SOFT IOCS > /tmp/siocsMissingScreenlog.txt
echo - WITH SCREEN PROCESS AND >> /tmp/siocsMissingScreenlog.txt
echo - MISSING SCREENLOG.0 FILE >> /tmp/siocsMissingScreenlog.txt

for i in $siocs
do
   IOC=`echo $i | cut -d ":" -f 1`
   IHOST=`echo $i | cut -d ":" -f 2`

   echo HOST is $IHOST
   echo IOC is $IOC

   echo checking $IOC on $IHOST
   numPROCS=`ssh $IHOST  -l flaci ps -ef | grep " $IOC" | grep SCREEN | wc -l`

   if [ $numPROCS -gt 0 ]; then
      if [ -e $IOC_DATA/$IOC/screenlog.0 ]; then
         echo screenlog file exists for soft IOC $IOC
      else
         echo NO screenlog file exists for soft IOC $IOC
         echo $IOC >> /tmp/siocsMissingScreenlog.txt
      fi

   fi
done

echo END OF LIST >> /tmp/siocsMissingScreenlog.txt
cat /tmp/siocsMissingScreenlog.txt  | Mail -s "FACET SIOCs missing screenlog.0 files" jingchen@mccdev.slac.stanford.edu

# go through each IOC_DATA dir: backup and cleanup screenlog files
echo CLEANING UP screenlog FILES
export CURRTIMESTAMP=`date '+%Y%m%d%H%M%S'`

iocs=`grep ^[es]*ioc-.*$SCREEN_HOST $IOC_SCREEN/All/Prod/facet_screeniocs | awk '{print $1}'`

for i in $iocs
do
   echo cleanup for $i
   # first mv large screenlog.0 file over to backup
   loglist=`find $IOC_DATA/$i -name "screenlog.0" -size +1000k`
   if [ "$loglist" != "" ]; then
      echo copying screenlog.0
      cp -p $IOC_DATA/$i/screenlog.0 $IOC_DATA/$i/screenlog.$CURRTIMESTAMP
      rm $IOC_DATA/$i/screenlog.0
   fi

   # then remove backup files over 7 days old, but only if there is a screenlog.0 AND
   # if there are at least 2 backup copies
   # yes, if screenlog.0 was moved above and not re-created in time, this will fail,
   # but the next time around, i.e. the next day, the cleanup will occur

#   if [ -e $IOC_DATA/$i/screenlog.0 ]; then
      fcount=1
      loglist=`ls -1 -t $IOC_DATA/$i/screenlog.* | grep -v screenlog.0$`
      for f in $loglist
      do
         echo $fcount $f

         if [ $fcount -gt 2 ]; then
           #check to see if file is older than 7 days
           #if so, remove it
           if test `find $f -mtime +7`; then
              echo removing $f
              rm $f
           fi
         fi
         let fcount=fcount+1
      done
#   fi
done

echo DONE

exit 0

