#!/bin/bash
#shell script to zip up  log files, as well as any cores 
#and javacores found on system.
# This script can rum prompted or in quiet mode.
#  copyLogs <MODE>
# Mode = collection mode
# MODE=0 prompted collection
# MODE=1 collect network info only
# MODE=2 collect network info + base logs
# MODE=3 collect network info + base logs + extended
# MODE=4 collect all info 
# MODE=5 collect files in /home/hscpe/ibmsupt only
# MODE=6 collect files for 3rd party extensions 
# MODE=9 run prompt to copy files 

#set -x
cleanup()
{
  if [ -d /tmp/pesupport ]; then
     rm -rf /tmp/pesupport
  fi
  if [ -f /tmp/rpm.list ]; then
     rm -f /tmp/rpm.list
  fi

  if [ "$1" = "1" ]; then
    rm -f /$ZIPDIR/HMClogs* 2>/dev/null
  fi
  rm -rf /tmp/sysinfo.log 2>/dev/null
  rm -rf /tmp/_ctsupt_ 2>/dev/null
  rm -rf /tmp/rpm.list 2>/dev/null
  rm -rf /dump/RVTrace.tar 2>/dev/null
  rm -rf /tmp/cod 2>/dev/null
  exit 0

}

collectIBMSUPT()
{
  echo "     Collecting.."
  zip -grq /$ZIPDIR/$LOGFILENAME.zip /home/hscpe/ibmsupt/* 2>/dev/null
  echo "     Cleaning /home/hscpe/ibmsupt"
  #Need to clean to avoid problems with saveupgrade data , later
  rm -rf /home/hscpe/ibmsupt/*
}

copytodvd()
{
  #clear
  echo ""
  echo " Would you like to move zip file to a DVD or other device? "
  echo " The answer no will write to the HMC hardfile"
  echo " Please type yes or no."
  read -e answer
  if [[ $answer = yes || $answer = y ]]; then
     FILESIZE=`ls -al $COPYFILE | awk {'print $5'}`
     echo ""
     echo "The file is $FILESIZE bytes. Ensure you have enough room on the device."
     echo "Use formatted Read/Write media only"
     echo "Enter a device name from the list below. Example /media/cdrom "
     echo ""
     /usr/hmcrbin/lsmediadev -F mount_point=description | grep -v null
     echo "network=move to another system via the scp command"
     echo ""
     
     read -e answer1

     INPUT=`/usr/hmcrbin/lsmediadev -F mount_point | grep "$answer1"`
     echo 
     if [ "$answer1" == "$INPUT" ] 
     then
         umount $answer1  2>/dev/null
        
         mount $answer1  2>/dev/null
         if [[ $? -ne 0 ]];then
            echo " Mount of $answer1 failed "
            exit 1
         fi
         echo "Copy to $answer1 in progress";echo
         cp $COPYFILE $answer1
         if [[ $? -eq 0 ]];then
            echo $COPYFILE placed on $answer1. 
            umount $answer1
         else
            echo "write error to $answer1"
            echo "$COPYFILE is in the /$ZIPDIR directory"
         fi
     elif [ "$answer1" == "network" ] ;then
         echo "     Enter the user name on the destination host."
         read -e username   
         echo "     Enter the destination host. example myhost.mycompany.com"
         read -e filehost   
         echo "     Enter the destination filesystem on the host. example /tmp/hmcdir"
         echo "     The directory must exist and have correct permissions."
         echo ""                                                    
         read -e filedir   
         echo "     scp $COPYFILE $username@$filehost:$filedir"
         scp $COPYFILE $username@$filehost:$filedir
         if [[ $? -eq 1 ]];then
            echo ""
            echo "     Command failed $COPYFILE is in the /$ZIPDIR directory."
         else
            echo "The send was successful"
         fi
    
     else
        echo "     \"$answer1\" was not a valid device command or could not be mounted"
        echo "    $COPYFILE is in the /$ZIPDIR directory."
     fi
  else
   echo "     $COPYFILE is in the /$ZIPDIR directory."
  fi
}

collectnetworkinfo()
{
   clear
   echo ""
   echo "     Collecting the network information."
   mkdir -p /tmp/pesupport/network_cfg/
   NETWORKCFGDIR=/tmp/pesupport/network_cfg

   /opt/hsc/bin/lssysconn -r all > $NETWORKCFGDIR/lssysconn
   /opt/hsc/bin/lssyscfg -r sys > $NETWORKCFGDIR/lssyscfgsys
   /opt/hsc/bin/lssyscfg -r frame > $NETWORKCFGDIR/lssyscfgframe
   /opt/hsc/bin/lshmc -v > $NETWORKCFGDIR/lshmc_v
   /opt/hsc/bin/hsc version > $NETWORKCFGDIR/lshmcV
   /opt/hsc/bin/lshmc -n > $NETWORKCFGDIR/lshmcn
   cp /etc/HmcBase  $NETWORKCFGDIR/hmcbase
   arp -a > $NETWORKCFGDIR/arpa
   ps auxfh > $NETWORKCFGDIR/psauxfh
   cat /var/log/messages > $NETWORKCFGDIR/messages
   if [ -f /var/lib/dhcp/db/dhcpd.leases ]; then
      cat /var/lib/dhcp/db/dhcpd.leases > $NETWORKCFGDIR/dhcpdleases 
   fi
   cat /root/.bash_history > $NETWORKCFGDIR/rootbashhistory
   cat /opt/hsc/data/HmcNetConfig > $NETWORKCFGDIR/hmcnetconfig
   if [ -f /opt/hsc/data/bpcHosts ]; then
      cp /opt/hsc/data/bpcHosts  $NETWORKCFGDIR/bpchosts
   fi
   if [ -f /var/log/detectNetCards.log ]; then
      cp /var/log/detectNetCards.log  $NETWORKCFGDIR/netcardslog
   fi
   cp /etc/sysconfig/dhcpd  $NETWORKCFGDIR/sysconfigdhcpd
   if [ -f /var/lib/dhcp/etc/hosts ]; then
      cp /var/lib/dhcp/etc/hosts  $NETWORKCFGDIR/hosts
   fi

   for i in 0 1 2 3 4 5 6 ; do
      if [ -f /etc/sysconfig/network/ifcfg-eth$i ]; then
           cat /etc/sysconfig/network/ifcfg-eth$i > $NETWORKCFGDIR/ifcfgeth$i
      fi
   done

   ls -latr /opt/hsc/data/.hmc > $NETWORKCFGDIR/.hmc
   if [ -f /opt/hsc/data/.hmc/.removed ]; then
     cat /opt/hsc/data/.hmc/.removed > $NETWORKCFGDIR/.removed
   fi

   zip -gqr /$ZIPDIR/$LOGFILENAME.zip $NETWORKCFGDIR
}

collectHMClogs()
{


   echo ""
   echo "     Collecting base HMC logs"   


   #make a new /tmp/sysinfo.log file
   file=/tmp/sysinfo.log
   echo *************SYSTEM INFORMATION************** >> $file
   date +"%a %b %e %r %Z %Y" >> $file
   echo >> $file
   echo Disk Usage: >> $file
   echo ----------- >> $file
   df >> $file
   echo >> $file
   echo Memory and Swap Usage: >> $file
   echo ---------------------- >> $file
   free -b >> $file
   echo >> $file
   echo Top 30 Processes Using the Most Memory >> $file
   echo -------------------------------------- >> $file
   ps auwwwx | sort +4 -5 +9 -10 -r -n | head -30 >> $file
   echo >> $file
   echo Top 30 Processes Using the Most CPU Time >> $file
   echo ---------------------------------------- >> $file
   ps auwwwx | sort +2 -3 +9 -10 -r -n | head -30 >> $file
   echo >> $file

   echo ""
   echo "     Dumping the object repository"
   /opt/hsc/bin/dumpRep -t >/dev/null 2>&1

   /opt/hsc/bin/hscSignal 202


   zip -gq /$ZIPDIR/$LOGFILENAME.zip \
      /opt/hsc/data/version \
      /var/hsc/log/cimserver.log \
      /var/hsc/log/cmd_controller.log* \
      /var/hsc/log/evt_processor.log* \
      /var/hsc/log/gui_server.log* \
      /var/hsc/log/hdwr_svr.log  \
      /var/hsc/log/hmcdump*  \
      /var/hsc/log/hmclogger.log  \
      /var/hsc/log/hmclogger_root.log  \
      /var/hsc/log/iqyycom* \
      /tmp/sysinfo.log  \
      /var/hsc/log/uri*  \
      /var/log/messages*  \
      /var/log/sendSignal.log  \
      /var/hsc/log/vsm_inst_root.log* \
      /root/.version  2> /dev/null

   for i in 1 2 3 4 
   do
     zip -gq /$ZIPDIR/$LOGFILENAME.zip \
              /var/hsc/log/cimLogs/cimserver.log.$i.gz  2>/dev/null
   done
   for i in 1 2 3 4 5 6 7 8 
   do
     zip -gq /$ZIPDIR/$LOGFILENAME.zip \
              /var/hsc/log/hdwr_svr.log.old.$i 2>/dev/null
   done


   echo ""
   echo "     Gather ccfw info"
   
   /opt/ccfw/hmcdebuginfo > /dev/null 2>&1
   zip -gq /$ZIPDIR/$LOGFILENAME.zip /tmp/hmcdebuginfo* 
   zip -gq /$ZIPDIR/$LOGFILENAME.zip   /opt/ccfw/ccfw.out   > /dev/null 2>&1
   rm -rf /tmp/hmcdebuginfo*
   cd /

   export HWMCAHOME=/opt/ccfw
   /opt/ccfw/native/runAsRoot/acuppd.sh --clean copyLogs
   /opt/ccfw/native/runAsRoot/acuppd.sh --lock copyLogs
   zip -gqD /$ZIPDIR/$LOGFILENAME.zip /opt/ccfw/data/acuppd.copyLogs.tgz  > /dev/null 2>&1
   /opt/ccfw/native/runAsRoot/acuppd.sh --clean copyLogs

   RMCDIR=/dump/rmc 
   mkdir -p $RMCDIR

   echo;echo "     Gather RMC logs"
   /opt/hsc/bin/lspartition -dlpar  > $RMCDIR/lspartition.out 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.ServiceRM/trace > $RMCDIR/RMC_ServiceRM.trace 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.ServiceRM/trace.0 > $RMCDIR/RMC_ServiceRM.trace.0 2>&1  
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.ServiceRM/trace_s > $RMCDIR/RMC_ServiceRM.trace_s 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.DMSRM/trace > $RMCDIR/RMC_DMSRM.trace 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.DMSRM/trace.0 > $RMCDIR/RMC_DMSRM.trace.0 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/trace > $RMCDIR/RMC.trace 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.LparCmdRM/trace > $RMCDIR/RMC_LparCmdRM.trace 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.LparCmdRM/trace.0 > $RMCDIR/RMC_LparCmdRM.trace.0 2>&1
   /usr/bin/rpttr /var/ct/IW/log/mc/IBM.LparCmdRM/trace.1 > $RMCDIR/RMC_LparCmdRM.trace.1 2>&1

   # now call ctsnap
   echo;echo "     Gather ctsnap"
   /usr/sbin/rsct/bin/ctsnap -d $RMCDIR/_ctsupt_    > /dev/null 2>&1

   zip -grq /$ZIPDIR/$LOGFILENAME.zip $RMCDIR/*
   rm -rf $RMCDIR

   zip -gq /$ZIPDIR/$LOGFILENAME.zip \
              /var/ct/IW/run/mc/IBM.LparCmdRM/core* \
              /var/ct/IW/run/mc/IBM.ServiceRM/core* \
              /var/ct/IW/run/mc/IBM.DMSRM/core* 2>/dev/null
}

collectExtendedCIMlogs()
{

	echo ""
	echo "     Collecting extended CIM log information."
	echo "       This operation can take a few minutes"

	cd /
	if [ "$ARCHLOGCOUNT" == "0" ] ; then
    
   	     zip -grq /$ZIPDIR/$LOGFILENAME.zip \
                  /var/hsc/log/cimLogs/* 2> /dev/null
	else
   	     let i=1
   	     let "ARCHLOGCOUNT += 1"
   	     while [[ $i -ne $ARCHLOGCOUNT ]] ; do
                  zip -gq /$ZIPDIR/$LOGFILENAME.zip /var/hsc/log/cimLogs/cimserver.log.$i.gz 
                  let i=i+1
  	     done
	fi 
}

collectExtendedhdwrlogs()
{

	echo ""
	echo "     Collecting extended hardware server log information."
	echo "       This operation can take a few minutes"

	cd /
	if [ "$ARCHLOGCOUNT" == "0" ] ; then
    
    	     zip -gq /$ZIPDIR/$LOGFILENAME.zip \
                 /var/hsc/log/hdwr_svr.log.old* 2> /dev/null
	else
   	    let i=1
   	    let "ARCHLOGCOUNT += 1"
   	    while [[ $i -ne $ARCHLOGCOUNT ]] ; do
                zip -gq /$ZIPDIR/$LOGFILENAME.zip /var/hsc/log/hdwr_svr.log.old.$i
                let i=i+1
  	    done
	fi 
}

collectExtendedHMClogs()
{

	echo ""
	echo "     Collecting extended HMC log information."
	echo "        This operation can take a few minutes"
	rpm -qa > /tmp/rpm.list

   	#collect COD info
   	HOSTLIST=`lssyscfg -r sys -F name`
   	mkdir -p /tmp/cod
   	cd /tmp/cod
   	for i in $HOSTLIST
   	do
       		/usr/hmcrbin/lscod -m $i -t hist > $i.cod.hist 2>&1
   	done
   	tar -cf COD.history.tar * 2> /dev/null 

   	cd /

      	tar -cvf /dump/RVTrace.tar /opt/ccfw/data/service/RVTrace_*.trm 2> /dev/null

	cd /
	zip -gq /$ZIPDIR/$LOGFILENAME.zip \
   	   /var/hsc/log/aca.log* \
   	   /var/hsc/log/acm.log* \
   	   /var/hsc/log/zGlue* \
   	   /var/hsc/log/credmgr.log \
   	   /var/hsc/log/ftp.log \
   	   /var/hsc/log/SyslogSystemEvents.log \
   	   /var/hsc/log/uri*  \
   	   /var/hsc/log/vterms* \
   	   /var/hsc/log/5250* \
   	   /var/hsc/log/mksysplan.log \
   	   /var/hsc/log/*.sysplan.debug \
   	   /tmp/rpm.list \
   	   /tmp/sysinfo.log  \
   	   /var/log/boot.msg \
   	   /var/log/XFree86* \
   	   /var/log/dmesg \
   	   /var/log/detectNetCards.log \
   	   /etc/jlbd.manifest \
   	   /dump/HMCMonitor/* \
           /dump/RVTrace.tar \
           /tmp/cod/COD.history.tar \
           /tmp/HmcBaseInstall.log* \
           /tmp/HmcInstall.log \
           /opt/ccfw/data/rcs/rcsControl.log 2> dev/null


	rm -f /tmp/rpm.list

	echo;echo "     Collect screen captures"
	cd /home
	for i in *
	do
   	  if [ -d /home/$i/.screen_capture ]; then
     		zip -gq /$ZIPDIR/$LOGFILENAME.zip /home/$i/.screen_capture/*
          fi
        done
        cd /

	echo;echo "     Get the profiles data"
	tar -cvf profiles.tar /var/hsc/profiles/*  >/dev/null 2>&1
	zip -qg /$ZIPDIR/$LOGFILENAME.zip profiles.tar 2> /dev/null
	rm profiles.tar

	echo;echo "     Generating hardware server dump file"
	killall -s USR1 hdwr_svr  > /dev/null 2>&1

        echo;echo "     Look for javacores"
  	for mntpoint in `/bin/df -l -m | /usr/bin/tail +2 | /bin/awk '{print $6}'`
  	do
   	   #echo $mntpoint
   	   for n in `/usr/bin/find $mntpoint  -name "javacore*" -type f 2> /dev/null`
   	   do
      	      zip -qg /$ZIPDIR/$LOGFILENAME.zip $n
   	   done
  	done
  	#get other core files
  	LOC=`pwd`
  	for i in / /tmp  /opt/ccfw /usr/websm/codebase /opt/hsc/bin
  	do
    	  for n in `/usr/bin/find  $i -name "core*" -maxdepth 1 -type f 2> /dev/null`  
    	  do
      	    if [[ -f $n ]]; then
               zip -qg /$ZIPDIR/$LOGFILENAME.zip $n
      	    fi
    	  done 
  	done
}

runthirdparty()
{
if [ -d /opt/hsc/data/pedbg ]; then
   if [ "$1" == "on" ]; then
      for i in /opt/hsc/data/pedbg/*.pdrt
      do
        if [ -f $i ]; then
           while read line
           do
              if [ "$line" != "" ]; then
                eval "$line"
              fi
           done < $i
        fi
      done
      cleanup
   fi
   if [ "$1" == "off" ]; then
      for i in /opt/hsc/data/pedbg/*.pdst
      do
        if [ -f $i ]; then
           while read line
           do
              if [ "$line" != "" ]; then
                eval "$line"
              fi
           done < $i
        fi
      done
      cleanup 
   fi
fi

if [[ "$1" == "off" || "$1" == "on" ]]; then
   cleanup
fi


# Now proceed to collect any additional log files specified
# by other subsystems.

if [ -d /opt/hsc/data/pedbg ]; then
   echo;echo "     Collecting other subsystem's log files"
   for i in /opt/hsc/data/pedbg/*.pdcfg
   do
       if [ -f $i ]; then
	  
	  sname=`basename $i`
          echo ""
	  echo " Would you like to collect "${sname%%.*}" files. Please type yes or no."
	  echo " No files will be collected if you answer no."
	  read -e answer
	  if [[ $answer = yes || $answer = y ]]
	  then
	    for f in `cat $i`
	    do
	      zip -g /$ZIPDIR/$LOGFILENAME.zip $f 2>/dev/null
    	    done	
	  fi
       fi
   done
fi
}

runquiet()
{
   case $MODE in
     1 ) collectnetworkinfo ;;
     2 ) collectnetworkinfo
         collectHMClogs ;;
     3 ) collectnetworkinfo
         collectHMClogs
         collectExtendedHMClogs ;;
     4 ) collectnetworkinfo 
         collectHMClogs
         collectExtendedHMClogs
         collectExtendedCIMlogs 
         collectExtendedhdwrlogs ;;
     5 ) collectIBMSUPT ;;
     6 ) runthirdparty ;;
     9 ) runcopyprompt ;;
     * ) #Mode not supported. Set to 0 and continue prompted operation
         echo;echo " \"$MODE\" is not a valid option, switching to prompted mode."
         echo 
         MODE=0 ;;
   esac
   if [ $MODE != "0" ] ; then
         echo;echo " $LOGFILENAME.zip was created in /$ZIPDIR directory"
         exit 0
   fi

} 
     
runcopyprompt()
{
   LOGFILESFOUND=`ls /dump/HMClogs* 2> /dev/null`
   if [ "$?" == "0" ]; then
      for i in $LOGFILESFOUND
      do
         COPYFILE=$i
         echo;echo " Found $COPYFILE"
         echo 
         copytodvd 
      done
   else
      echo;echo " No files that begin with \"HMClogs\" found"
   fi
  
   exit 0
}


#MAIN =============================================================


ZIPDIR="dump"
JAVACORE=0
ARCHLOGCOUNT=0
uid=`id -u`
if [ $uid -ne 0 ]
then 
  echo;echo " You need to be root to run the copyLogs script"
  exit 0
fi

trap "cleanup 1" 2 3 5 9 11 15
stty -crterase
datelabel=`date +%m%d%Y%H%M`
hmcname=`hostname`
basename="HMClogs."
LOGFILENAME=$basename$hmcname$datelabel

if [[ $# -ne 1 ]]; then
  MODE=0
else
  MODE=$1
fi

#remove any existing /$ZIPDIR/HMClogs.zip file and /tmp/sysinfo.log file

if [ "$MODE" != "9" ]; then
    rm -f /$ZIPDIR/HMClogs* 2>/dev/null
    rm -f /$ZIPDIR/HSClogs* 2>/dev/null
    rm -f /tmp/sysinfo.log 2>/dev/null
fi


if [[ "$MODE" != "0" || "$MODE" != "on" ]]; then
    runquiet
fi 

collectnetworkinfo

#Limit log collection by only collecting logs in /home/hscpe/ibmsupt
if [ -d /home/hscpe/ibmsupt ] ; then
   echo;echo " Collect files in /home/hscpe/ibmsupt ?"
	  read -e answer
	  if [[ $answer = yes || $answer = y ]]; then
               collectIBMSUPT
          fi
fi
clear
echo;echo " Collect base HMC debug logs? Please type yes or no"
read -e answer
if [[ $answer = no || $answer = n ]]; then
     COPYFILE=/$ZIPDIR/$LOGFILENAME.zip
     echo;echo "Created $COPYFILE"
     copytodvd
     exit 0
else
     collectHMClogs
fi

clear
echo;echo 
echo " Would you like to collect extended log data? Please type yes or no."
echo " If you answer yes, the size of the data collected may be very large."
echo 
echo "  These additional logs would be collected on request of PE support."
read -e answer
if [[ $answer = no || $answer = n ]]; then
   cd /tmp/pesupport
   zip -gqr /$ZIPDIR/$LOGFILENAME.zip $NETWORKCFGDIR
   cd /
   COPYFILE=/$ZIPDIR/$LOGFILENAME.zip
   echo;echo "Created $COPYFILE"
   copytodvd
   cleanup
fi


echo;echo
echo "     Continuing data collection ...."

collectExtendedHMClogs

#Debug extension for 3rd parties
runthirdparty


cd /
ARCHLOGCOUNT=0
clear
echo 
echo " Would you like to collect archived log data? Please type yes or no."
echo " If you answer yes, the size of the data collected may be very large."
echo 
echo "  These additional logs would be collected on request of PE support."
read -e answer
if [[ $answer = yes || $answer = y ]]; then
     COUNT=`ls /var/hsc/log/cimLogs/cimserver* | wc -l`
     echo " $COUNT CIM logs were found"
     echo " Enter the number of logs to collect" 
     read -e ARCHLOGCOUNT
     if [[ -f /var/hsc/log/cimLogs/cimserver.log.$ARCHLOGCOUNT.gz ]]; then
                 collectExtendedCIMlogs 
     else
            echo " You entered: $ARCHLOGCOUNT. This input is not valid"
            echo " Collecting all logs. " 
            ARCHLOGCOUNT=0
            collectExtendedCIMlogs 
     fi

     ARCHLOGCOUNT=0
     COUNT=`ls /var/hsc/log/hdwr_svr.log.old* | wc -l`
     echo " $COUNT hardwareserverlogs logs were found"
     echo " Enter the number of logs to collect" 
     read -e ARCHLOGCOUNT
     if [[ -f /var/hsc/log/hdwr_svr.log.old.$ARCHLOGCOUNT ]]; then
                 collectExtendedhdwrlogs 
     else
            echo " You entered: $ARCHLOGCOUNT. This input is not valid"
            echo " Collecting all logs. " 
            ARCHLOGCOUNT=0
            collectExtendedhdwrlogs 
     fi
fi

COPYFILE=/$ZIPDIR/$LOGFILENAME.zip
echo;echo " Created $COPYFILE"
copytodvd 
cleanup
