#!/bin/bash
#shell script to zip up a couple of log files, as well as any cores 
#and javacores found on system.
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/HSClogs* 2>/dev/null
  fi
  rm -f /tmp/sysinfo.log 2>/dev/null
  rm -rf /tmp/_ctsupt_ 2>/dev/null
  exit 0

}
copytodvd()
{
  #either copy HSClogs.zip to a DVD, or just leave it in /tmp dir 
  clear
  echo Would you like to move zip file to a DVD?  Please type yes or no.
  read -e answer
  if [[ $answer = yes || $answer = y ]]
  then
     umount /media/cdrom
     mount /media/cdrom
     cp /$ZIPDIR/HSClogs$datelabel.zip /media/cdrom
     if [ $? -eq 0 ]
     then
      echo HSClogs$datelabel.zip placed on DVD. 
      umount /media/cdrom
      rm /$ZIPDIR/HSClogs$datelabel.zip
     fi
  else
    echo HSClogs$datelabel.zip created in /$ZIPDIR directory.
  fi
}

ZIPDIR="dump"
JAVACORE=0
uid=`id -u`
if [ $uid -ne 0 ]
then 
  echo "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%S)

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
clear
echo
echo
echo
echo "Collecting network files and basic configuration 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
cat /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
	cat /etc/sysconfig/dhcpd  > $NETWORKCFGDIR/sysconfigdhcpd
	cat /var/lib/dhcp/etc/hosts > $NETWORKCFGDIR/hosts
fi
cat /root/.bash_history > $NETWORKCFGDIR/rootbashhistory
cat /opt/hsc/data/HmcNetConfig > $NETWORKCFGDIR/hmcnetconfig
if [ -f /opt/hsc/data/bpcHosts ];then
cat /opt/hsc/data/bpcHosts > $NETWORKCFGDIR/bpchosts
fi
cat /var/log/detectNetCards.log > $NETWORKCFGDIR/netcardslog
cat /etc/sysconfig/network/ifcfg-eth0 > $NETWORKCFGDIR/ifcfgeth0
if [ -f /etc/sysconfig/network/ifcfg-eth1 ]; then
cat /etc/sysconfig/network/ifcfg-eth1 > $NETWORKCFGDIR/ifcfgeth1
fi
if [ -f /etc/sysconfig/network/ifcfg-eth2 ]; then
  cat /etc/sysconfig/network/ifcfg-eth2 > $NETWORKCFGDIR/ifcfgeth2
fi
if [ -f /etc/sysconfig/network/ifcfg-eth3 ]; then
  cat /etc/sysconfig/network/ifcfg-eth3 > $NETWORKCFGDIR/ifcfgeth3
fi
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

clear
echo
echo
echo
echo "Would you like to collect additional data? "
echo "   This data should be collected for most problems."
echo
echo "If you answer yes, you will be asked a series of questions. If in doubt,"
echo "answer yes to the prompts. The size of the data collected will be larger."
echo
echo " Please type yes or no to continue. "
read -e answer
if [[ $answer = no || $answer = n ]]; then
   cd /tmp/pesupport
   zip -gqr /$ZIPDIR/HSClogs$datelabel.zip network_cfg
   cd /
   copytodvd
   cleanup
fi
echo "Continuing data collection ...."

#generate javacore files
clear
echo
echo
echo
echo "Would you like to generate java core files. Please type yes or no."
echo "No core files will be collected if you answer no."
read -e answer
if [[ $answer = yes || $answer = y ]]
then
 #create 2 cores for each proc
 JAVACORE=1
 for z in 1 2 
 do

   for i in WConsole CIMServer CommandC CIME
   do

    STOP_PID=`ps -efww | grep $i | grep -v grep | cut -c10-16`
    if [ "$STOP_PID" != "" ]
    then
        echo "Sending SIGQUIT to "$i
        #echo $STOP_PID
	    kill -3 $STOP_PID > /dev/null 2>&1
        # wait (with timeout) 
        for j in 1 2 3 4 5 6
        do
                if ps -efww | grep $i > /dev/null 2>&1 
                then
                        sleep 1
                else
                        break
                fi
        done
    fi

   done
   sleep 5
 done
 sleep 5  
fi
clear
echo 
echo
echo
echo
#generate hardware server dump file
echo "Would you like to generate a hardware server dump file. Please type yes or no."
read -e answer
if [[ $answer = yes || $answer = y ]]
then
    echo "generating hardware server dump file"
    killall -s USR1 hdwr_svr  > /dev/null 2>&1
fi

echo ""
echo "Dumping the object repository"

NAMESPACE=`ls /var/sniacimom/persistence/classes/root | grep ibmhsc | grep -v info`
for i in $NAMESPACE; do
   echo "dumpRep $i"
   /opt/hsc/bin/dumpRep root/$i
done


#remove any existing /$ZIPDIR/HSClogs.zip file and /tmp/sysinfo.log file
rm -f /$ZIPDIR/HSClogs* 2>/dev/null
rm -f /tmp/sysinfo.log 2>/dev/null

#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

rpm -qa > /tmp/rpm.list

#zip everything up into /dump/HSClogs.zip.
cd /tmp/pesupport
zip -gqr /$ZIPDIR/HSClogs$datelabel.zip network_cfg
cd /

if [ -f /opt/ccfw/data/service/RVTrac* ]; then 
     tar -cf /tmp/RVTrace.tar /opt/ccfw/data/service/RVTrace_*.trm
fi



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 *

cd /
 		

zip -gqj /$ZIPDIR/HSClogs$datelabel.zip \
   /opt/hsc/data/version \
   /var/hsc/log/aca.log* \
   /var/hsc/log/acm.log* \
   /var/hsc/log/cimserver.log* \
   /var/hsc/log/credmgr.log* \
   /var/hsc/log/slp.log* \
   /var/hsc/log/controller.log*  \
   /var/hsc/log/hdwr_svr.log*  \
   /var/hsc/log/hmcdump*  \
   /var/hsc/log/iqyylog.log \
   /var/hsc/log/iqzdtrac.trm  \
   /var/hsc/log/backuphdr.log \
   /var/hsc/log/hmcRestore.log \
   /var/hsc/log/saveUpgradeFiles.log \
   /var/hsc/log/restoreUpgradeFiles.log \
   /var/hsc/log/restore.log* \
   /var/hsc/log/vterms.log \
   /var/hsc/log/5250proxy.log \
   /var/hsc/log/5250proxy_trace.log \
   /tmp/bridge.WServer.log  \
   /tmp/console.WConsole.log \
   /tmp/rpm.list \
   /tmp/sysinfo.log  \
   /var/log/boot.msg \
   /var/log/messages*  \
   /var/log/XFree86* \
   /etc/jlbd.manifest \
   /root/.version \
   /dump/HMCMonitor/* \
   /tmp/RVTrace.tar \
   /opt/ccfw/data/rcs/rcsControl.log \
   /tmp/cod/COD.history.tar

rm -f /tmp/rpm.list
rm -f /tmp/RVTrace.tar
rm -rf /tmp/cod

# Gather any screen capture

cd /home
for i in *
do
  if [ -d /home/$i/.screen_capture ]
  then
     zip -gD /$ZIPDIR/HSClogs$datelabel.zip /home/$i/.screen_capture/*
  fi
done
cd /
    
clear
echo 
echo
echo
echo "Would you like to collect the extended HMC logs ?"
echo "  This operation may create a very large dump file."
echo
echo " Please type yes or no."
read -e answer
if [[ $answer = yes || $answer = y ]]
then
  echo ""
  echo "Gathering extended CIM logs"

  if [ -f /var/hsc/log/cimLogs/cim* ]; then
    zip -gD /$ZIPDIR/HSClogs$datelabel.zip /var/hsc/log/cimLogs/cimserver.log* 
  fi

fi


clear
echo 
echo
echo
echo Would you like to collect RSCT trace files.
echo  Generally collected for DLPAR and Service Focal Point problems.
echo
echo Please type yes or no.
read -e answer
if [[ $answer = yes || $answer = y ]]
then
  echo ""
  echo "Gather RMC logs"

  /usr/bin/rpttr /var/ct/IW/log/mc/IBM.ServiceRM/trace > /$ZIPDIR/RMC_ServiceRM.trace 2>&1
  /usr/bin/rpttr /var/ct/IW/log/mc/IBM.ServiceRM/trace.0 > /$ZIPDIR/RMC_ServiceRM.trace.0 2>&1
  /usr/bin/rpttr /var/ct/IW/log/mc/IBM.DMSRM/trace > /$ZIPDIR/RMC_DMSRM.trace 2>&1
  /usr/bin/rpttr /var/ct/IW/log/mc/IBM.DMSRM/trace.0 > /$ZIPDIR/RMC_DMSRM.trace.0 2>&1
  /usr/bin/rpttr /var/ct/IW/log/mc/trace > /$ZIPDIR/RMC.trace 2>&1
  zip -gD /$ZIPDIR/HSClogs$datelabel.zip /$ZIPDIR/RMC*
  rm -f /$ZIPDIR/RMC*

# now call ctsnap
   rm -rf /tmp/_ctsupt_
   /usr/sbin/rsct/bin/ctsnap -d /tmp/_ctsupt_
   zip -gD /$ZIPDIR/HSClogs$datelabel.zip /tmp/_ctsupt_/*
   rm -rf /tmp/_ctsupt_
fi

clear
echo 
echo
echo
echo "Would you like to collect CCFW info files. Please type yes or no."
read -e answer
if [[ $answer = yes || $answer = y ]]
then
  echo ""
  echo "Gather ccfw info"
  /opt/ccfw/hmcdebuginfo > /dev/null 2>&1
  zip -gD /$ZIPDIR/HSClogs$datelabel.zip  /tmp/hmcdebuginfo* /opt/ccfw/ccfw.out
  rm /tmp/hmcdebuginfo*
  export HWMCAHOME=/opt/ccfw
  /opt/ccfw/native/runAsRoot/acuppd.sh --clean copyLogs
  /opt/ccfw/native/runAsRoot/acuppd.sh --lock copyLogs
  if [ -f /opt/ccfw/data/acuppd.copyLogs.* ]; then
	  zip -gD /$ZIPDIR/HSClogs$datelabel.zip /opt/ccfw/data/acuppd.copyLogs.tgz
  fi
  /opt/ccfw/native/runAsRoot/acuppd.sh --clean copyLogs
fi

echo ""
echo "Get the profiles data"

tar -cvf profiles.tar /var/hsc/profiles/*  >/dev/null 2>&1
zip -g /$ZIPDIR/HSClogs$datelabel.zip profiles.tar
rm profiles.tar

if [ "$JAVACORE" == "1" ]; then
  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`
   do
      zip -g /$ZIPDIR/HSClogs$datelabel.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`  
    do
      if [[ -f $n ]]; then
               zip -g /$ZIPDIR/HSClogs$datelabel.zip $n
      fi
    done 
  done
fi

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

if [ -d /opt/hsc/data/pedbg ]; then
   echo "Collecting other subsystem's log files"
   for i in /opt/hsc/data/pedbg/*.pdcfg
   do
       if [ -f $i ]; then
	  
	  sname=`basename $i`
	  clear
	  echo 
	  echo
	  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/HSClogs$datelabel.zip $f 2>/dev/null
    	    done	
	  fi
       fi
   done
fi
copytodvd 
cleanup
