#!/usr/bin/ksh
#**************************************************************************
#
#   Licensed Materials - Property of IBM
#   5697-FM3 5655-A96
#   (C) Copyright IBM Corp. 1998, 2000
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* #
#                                                                           #
# Collecting IBM MQSeries Workflow service related information.             #
#                                                                           #
# This shell script should run after a problem with FlowMark occurs.        #
# All related information will be put together and compressed in one file.  #
#                                                                           #
# ------------------------------------------------------------------------- #
#                                                                           #
# You can custimze the general behavior of the shell script by modifying    #
# the switches which control the information to be collected.               #
#                                                                           #
# The default values for the above specified options can be modified in the #
# following section. Allowed values are yes and no and ff for future use.   #
# Using an option will change the default.                                  #
#                                                                           #
# *-*-*-*-*-*-*-*-*-*-* #                                                   #
ord='/tmp/'`hostname`   #                                                   #
                        #  h  Display help information (no optarray enrty)  #
    optarray[1]=$ord    #  d  changing directory for results                #
    optarray[2]='yes'   #  W  MQWorkflow information                        #
    optarray[3]='yes'   #  M  MQSeries information                          #
    optarray[4]='yes'   #  D  DB2 infomation                                #
    optarray[5]='no'    #  S  Collecting system information (snap)          #
    optarray[6]='yes'   #  s  Collecting basic system information           #
    optarray[7]='no'    #  m  Display menu                                  #
    optarray[8]='fu'    #  a  Attaching to processes                        #
    optarray[9]='fu'    #  c  Analyse core files                            #
   optarray[10]='yes'   #  z  Compressing results                           #
   optarray[11]='no'    #  H  HACMP information                             #
   optarray[12]='fu'    #                                                   #
   optarray[13]='fu'    #                                                   #
   optarray[14]='fu'    #     for future use                                #
                        #                                                   #
# *-*-*-*-*-*-*-*-*-*-* #       Defaults:  no  = actual set to no           #
#                                          yes = actual set to yes          #
#                                      'value' = defined value              #
#                                          ans = actual not supported       #
# End of customization area                fu  = reserved for future use    #
# *-*-*-*-*-*-*-*-*-*-* #                                                   #
  arraymax='14'         #                                                   #
# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* #
# ------------------------------------------------------------------------  #
# ------------------------------------ do not touch the following source -  #
# ------------------------------------------------------------------------  #
# Author ..........:  E. Voesch  - Member of the Workflow team
# Created at ......:  February 2000
# Last modification:
#      date .......:
#      reason .....:
# ------------------------------------------------------------------------  #
# Save the command line options for logging
# ---------------------------------------------------------------------------
InvocationArgs=$*

dsp_help () {
if [[ $1 != 'noclear' ]]; then
#   clear
   echo ' ----------------------------------------------------------------------
   echo ' Defined options and their meaning:
fi
echo '                                                                        '
echo ' opt. parameter   description                                  default  '
echo ' ---------------------------------------------------------------------- '
echo '  -h              Display help information                       -      '
echo '                                                                        '
echo '  -d              Directory for results                         '${optarray[1]}
echo '  -W              Collecting Workflow information               '${optarray[2]}
echo '  -M              Collecting queue manager information          '${optarray[3]}
echo '  -D              Collecting database information               '${optarray[4]}
echo '  -H              Collecting high availability information      '${optarray[11]}
echo '  -S              Collecting system information (snap)          '${optarray[5]}
echo '  -s              Collecting basic system information           '${optarray[6]}
echo '  -m              Display menu                                  '${optarray[7]}
echo '  -a              Attaching to processes                        '${optarray[8]}
echo '  -c              Analyse core files                            '${optarray[9]}
echo '  -z              Compressing results                           '${optarray[10]}
echo ' ---------------------------------------------------------------------  '
echo ' If you specify an option and the default will be changed               '
echo ' ---------------------------------------------------------------------  '
}

# ===========================================================================
# ---------------------------------------------------------------------------
# Option handling incl the needed function
# ---------------------------------------------------------------------------

chdefault () {
   case ${optarray[$1]} in
     no)  optarray[$1]='yes'
          return             ;;
     yes) optarray[$1]='no'
          return             ;;
     fu)  optarray[$1]='fu'
          return             ;;
     ans) clear
          echo ' This option is actual not supported. Please try another one.'
          dsp_help "noclear"
          exit               ;;
   esac
   echo 'Internal error. Invalid value for option customized. Please check the shell script.'
   exit
}

while getopts :d:WMDSHsmaczh optname
do
    case $optname in
      d)   optarray[1]=$OPTARG ;;
                              #  d  changing directory for results
      W)   chdefault 2  ;;    #  W  MQWorkflow information
      M)   chdefault 3  ;;    #  M  MQSeries information
      D)   chdefault 4  ;;    #  D  DB2 infomation
      S)   chdefault 5  ;;    #  S  Collecting system information (snap)
      s)   chdefault 6  ;;    #  s  Collecting basic system information
      m)   chdefault 7  ;;    #  m  Display menu
      a)   chdefault 8  ;;    #  a  Attaching to processes
      c)   chdefault 9  ;;    #  c  Analyse core files
      z)   chdefault 10 ;;    #  z  Compressing results
      H)   chdefault 11 ;;    #  H  High availability
      h)   dsp_help           # Display help information
           exit           ;;
      :)   echo 'missing value for '$optname
           dsp_help "noclear"
           exit  ;;
      ?)   echo 'unknown option specified'
           dsp_help "noclear"
           exit  ;;
    esac
done
shift $(($OPTIND -1))

# ---------------------------------------------------------------------------
# Menus (incl initial value specifcation)
# ---------------------------------------------------------------------------

clear
echo "+-------------------------------------------------------------------+"
echo "  Initialization - this may take some seconds - please be patient    "
echo "+-------------------------------------------------------------------+"

os_level=`oslevel | sed -e "s/\.//"g`
sysvalue[1]=' -A'
sysvalue[2]=' -D'
sysvalue[3]=' -f'
sysvalue[4]=' -g'
sysvalue[5]=' -G'
if [[ $os_level > 4209 ]]; then
  sysvalue[6]=' -i'
else
  sysvalue[6]=' '
fi
sysvalue[7]=' -k'
sysvalue[8]=' -l'
sysvalue[9]=' -n'
sysvalue[10]=' -p'
sysvalue[11]=' -s'
sysvalue[12]=' -S'
sysvalue[13]=' -t'
if [[ $os_level > 4319 ]] ; then
  sysvalue[14]=' -b'
  sysvalue[15]=' -L'
else
  sysvalue[14]=' '
  sysvalue[15]=' '
fi
snapvalue=''

i=1
until (( $i > 15 ));
do
  sysarray[$i]=${optarray[5]}
  (( i=i+1 ))
done

sysimenu () {
clear
echo "+-------------------------------------------------------------------+"
echo "  Specify System Information Collection Options                      "
echo "+-------------------------------------------------------------------+"
echo "   1)  Asynchronous TTY information                 "${sysarray[1]}
echo "   2)  Dump and /unix information                   "${sysarray[2]}
echo "   3)  File system information                      "${sysarray[3]}
echo "   4)  Exact operating system environment           "${sysarray[4]}
echo "   5)  Predefined Object Data Manager files         "${sysarray[5]}
echo "   6)  Kernel information                           "${sysarray[7]}
echo "   7)  Programming language information             "${sysarray[8]}
echo "   8)  Network File System information              "${sysarray[9]}
echo "   9)  Printer information                          "${sysarray[10]}
echo "  10)  System Network Architecture information      "${sysarray[11]}
echo "  11)  Security files information                   "${sysarray[12]}
echo "  12)  TCP/IP information                           "${sysarray[13]}
if [[ $os_level > 4209 ]] ; then
  echo "  13)  Installation debug vital product data        "${sysarray[6]}
fi
if [[ $os_level > 4319 ]] ; then
  echo "  14)  SSA Adapter/Disk information                 "${sysarray[14]}
  echo "  15)  Logical volume manager (LVM) information     "${sysarray[15]}
fi
echo "                                                    "
echo "  90)  Previous menu                                "
echo "   0)  Accept specified values and start collection "
echo "+-------------------------------------------------------------------+"
if [[ $invsel != '' ]];then
   echo "  "$invsel
   echo "+-------------------------------------------------------------------+"
fi
read sel01
}

chsysdefault () {
   case ${sysarray[$1]} in
     no)  sysarray[$1]='yes'
          return             ;;
     yes) sysarray[$1]='no'
          return             ;;
   esac
}

msel01 () {
sel01='x'
until [[ $sel01 = '0' ]]
do
   sysimenu
   invsel=''
   case $sel01 in
      0)  sel='0'         ;;
      1)  chsysdefault 1  ;;
      2)  chsysdefault 2  ;;
      3)  chsysdefault 3  ;;
      4)  chsysdefault 4  ;;
      5)  chsysdefault 5  ;;
      6)  chsysdefault 6  ;;
      7)  chsysdefault 7  ;;
      8)  chsysdefault 8  ;;
      9)  chsysdefault 9  ;;
     10)  chsysdefault 10 ;;
     11)  chsysdefault 11 ;;
     12)  chsysdefault 12 ;;
     13)  chsysdefault 13 ;;
     14)  chsysdefault 14 ;;
     15)  chsysdefault 15 ;;
     90)  sel01='0'       ;;
      *)  invsel='Invalid selection specified - try again' ;;
   esac
done
}

mainmenu () {
clear
echo "+-------------------------------------------------------------------+"
echo "  Analysis and Collecting Workflow Related Data  -  Main Menu        "
echo "+-------------------------------------------------------------------+"
echo "   1)  Working directory              "${optarray[1]}
echo "   2)  MQWorkflow information                       "${optarray[2]}
echo "   3)  MQSeries information                         "${optarray[3]}
echo "   4)  DB2 infomation                               "${optarray[4]}
echo "   5)  High availability (HACMP)                    "${optarray[11]}
echo "   6)  Collecting system information (snap)         "${optarray[5]}
echo "   7)  Collecting basic system information          "${optarray[6]}
echo "   8)  Attaching to processes                       "${optarray[8]}
echo "   9)  Analyse core files                           "${optarray[9]}
echo "  10)  Compressing results                          "${optarray[10]}
echo "                                                    "
echo "  20)  Specify System Information Collection Options"
##echo "  30)  Define granularity of analysis with dbx      "
echo "                                                    "
echo "  99)  Exit program                                 "
echo "                                                    "
echo "   0)  Accept defined values and start collection   "
echo "                                                    "
echo "+-------------------------------------------------------------------+"
if [[ $invsel != '' ]];then
   echo "  "$invsel
   echo "+-------------------------------------------------------------------+"
fi
read sel selwdir
}

if [[ ${optarray[7]} = 'yes' ]]; then
   sel='x'
   until [[ $sel = '0' ]]
   do
      mainmenu
      invsel=''
      case $sel in
         0)  ;;
         1)  if [[ $selwdir != '' ]] ; then
                optarray[1]=$selwdir
             fi           ;;
         2)  chdefault 2  ;;
         3)  chdefault 3  ;;
         4)  chdefault 4  ;;
         5)  chdefault 11 ;;
         6)  chdefault 5  ;;
         7)  chdefault 6  ;;
         8)  chdefault 8  ;;
         9)  chdefault 9  ;;
        10)  chdefault 10 ;;
        20)  msel01       ;;
##     30)  msel02       ;;
        99)  exit         ;;
         *)  invsel='Invalid selection specified - try again' ;;
      esac
   done
fi

i=1
until (( $i > 15 ))
do
  if [[ ${sysarray[$i]} = 'yes' ]]; then
    snapvalue=$snapvalue${sysvalue[$i]}
  fi
  (( i=i+1 ))
done
if [[ $snapvalue = '' ]]; then
   optarray[5]='no'
else
   optarray[5]='yes'
fi

# ---------------------------------------------------------------------------
# Set collecting dbx output
# ---------------------------------------------------------------------------
if [[ ${optarray[8]} = 'yes' || ${optarray[9]} = 'yes' ]]; then
   dbx_collection='yes'
fi

# ===========================================================================
# ---------------------------------------------------------------------------
# Internal customization area
# ---------------------------------------------------------------------------
           shname=`basename $0`            # The name of this shell
            dispm='yes'                    # Message display mode (yes or no)
           pwddir=`pwd`
           resdir=${optarray[1]}'/FMCresults'        # Set directory for results
          snapdir=${optarray[1]}'/FMCresults/snap'
          workdir=${optarray[1]}'/t'$$     # Set working directory
           maxbak=10                       # Maximum of managed "bak" versions
          logname='FMCcollect.log'
        colfmclog=$resdir/$logname         # File containing the log information
          tarfile=$resdir'/FMCcollect.tar' # tar file name
# ---------------------------------------------------------------------------
# Some temporarly used names and definitions
# ---------------------------------------------------------------------------
     fmcmonitor=$workdir'/fmonitor.sh'     # Monitor shell name
    fmczchk_out='fmczchk.out'              # Checker output file
       fmclinks='fmclinks.found'           # Additional specified links
     fmcinstfix='fmcinstfix.out'           # Query installed fixes
  fmc_proc_list='fmcprocess.list'          # Running Workflow process list
      fmclocale='fmclocale.info'           # NLS - System locale information
         fmc_ha='fmchacmp.info'            # High availability information
# ===========================================================================
# ---------------------------------------------------------------------------
# Check space of working directory
# ---------------------------------------------------------------------------
if [[ ! -d ${optarray[1]} ]] ; then
   mkdir ${optarray[1]}
   if [[ $? > 0 ]];then
      echo 'Invalid directory for results specified'
      exit
   fi
fi

if (( `df -k ${optarray[1]} | grep / | awk '{print $3}'` < 8000 )); then
   echo 'There may be not enough space in the '${optarray[1]}' directory'
   echo 'For this tool you need at least 8MB free space'
   exit
fi

# ---------------------------------------------------------------------------
# Displaying and recording messages
# ---------------------------------------------------------------------------
#   function call: col_msg
#   options......: -l            message is only logged in the log file
#                  -d            message is only displayed
#   parameter....: $1            message id used as array index
#                  $2            message variable 1
#                  $3            message variable 2
col_msg () {
      show_msg='both'
      while getopts :ld optname
      do
         case $optname in
           l)   show_msg='nodisplay'     ;; # log message only
           d)   show_msg='nolog'         ;; # display message only
           :)   echo 'missing value for '$optname 'internal error'
                exit  ;;
           ?)   echo 'unknown option specified - internal error'
                exit  ;;
         esac
      done
      shift $(($OPTIND -1))

      if [[ $1 != '' ]]; then
         case $1 in

100) msgtxt='+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'      ;;
101) msgtxt='---------------------------------------------------------------------'      ;;

111) msgtxt=' '                                                                          ;;
112) msgtxt='You have '$2' saved versions'                                               ;;
113) msgtxt='   Creation date ..: '$m_date                                               ;;
114) msgtxt='   Version ........: '$2                                                    ;;
115) msgtxt='   Host ...........: '$m_uname                                              ;;
116) msgtxt='   OS Level .......: '$m_oslevel                                            ;;
117) msgtxt='   Invocation args : '$InvocationArgs                                       ;;
118) msgtxt=$m_uptime                                                                    ;;
119) msgtxt='+++ Starting with collecting Workflow related service information +++'      ;;
120) msgtxt='Compressing collected information'                                          ;;
121) msgtxt='Collecting ................: '$2                                            ;;
122) msgtxt='There is not enough space in the working directory: '$2                     ;;
123) msgtxt='+++ The results are collected in '$2                                        ;;
124) msgtxt='+++ The messages are logged in '$2                                          ;;
125) msgtxt='+++ Collecting of Workflow related service information data ended +++'      ;;
126) msgtxt='The settings for the "hostname" do not match with "uname -n"'               ;;
127) msgtxt='No option specified for running dbx'                                        ;;
128) msgtxt='Nothing collected during this step'                                         ;;
129) msgtxt='Collecting system locale information'                                       ;;
130) msgtxt='End of analyzing and collecting Workflow related service data.'             ;;
131) msgtxt='Not all information is correct collected. For details refer to '$2          ;;
132) msgtxt='Due to space limitation a result file was compressed:'                      ;;
133) msgtxt='Due to missing space the result file was NOT compressed:'                   ;;
134) msgtxt='dbx is not installed - no analysis performed'                               ;;
135) msgtxt='The tar command returns: '$2                                                ;;
136) msgtxt='The results are stored in: '$2                                              ;;
137) msgtxt='There is not enough space in: '$2                                           ;;
138) msgtxt='............ from directory: '$2                                            ;;
139) msgtxt='Searching for additionl specified links'                                    ;;
140) msgtxt='.............. changed name: '$2                                            ;;
141) msgtxt='Starting with creating the HACMP cluster snapshot'                          ;;
142) msgtxt='HACMP is not installed'                                                     ;;
143) msgtxt='Unable to create snapshot. RC = '$2                                         ;;
144) msgtxt='Unable to locate snapshot information '$2                                   ;;
145) msgtxt='DB2 admin server '$2' found. Number: '$3                                    ;;
146) msgtxt='DB2 instance '$2' found. Number: '$3                                        ;;
147) msgtxt='DB2 admin server for DB2 instance '$2' is '$3                               ;;
148) msgtxt='Unsupported DB2 Version located'                                            ;;

152) msgtxt='+ ==> The located core file is invalid or is corrupted: '                   ;;
153) msgtxt='Specified snap options: '$2                                                 ;;
154) msgtxt='No results generated with instfix'                                          ;;
155) msgtxt='No results generated with locale'                                           ;;
156) msgtxt='System collection not successful '                                          ;;
157) msgtxt='Not enough space in '                                                       ;;
158) msgtxt='+ ==> No core file found in the current directory: '                        ;;
159) msgtxt='+ ==> Please start from the directory where the core file is located.'      ;;
160) msgtxt='Start collecting information for MQWorkflow configuration '$2               ;;
161) msgtxt='No machine profile for MQWorkflow located'                                  ;;

170) msgtxt='MQSeries Machine-wide Configuration File notfound.'                         ;;
171) msgtxt='Queue manager directory '$2' does not exist'                                ;;


# ---------------------------------------------------------------------------
# End of messages used in this shell
# ---------------------------------------------------------------------------
            *) echo $shname': Internal error. Unknown message index specified.'
               exit ;;
         esac
         msgid='FMC00'$1
         mtime=`LC_ALL=C date | awk '{print $4}'`
         if [[ $show_msg != 'nolog' ]] ; then
            echo $msgid $mtime $msgtxt >> $colfmclog
         fi
         if [[ $show_msg != 'nodisplay' ]] ; then
            echo $msgid $msgtxt
         fi
      else
         echo $shname': Internal error. Invalid function call (dsp_msg). No index specified.'
         exit 999
      fi
}

# ===========================================================================
# ---------------------------------------------------------------------------
# General used functions
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# Backup of old output files e.g. the collection log file
# ---------------------------------------------------------------------------
#   function call:  col_bak
#   parameter....:  $1            contains the name of the file to backup

col_bak () {
      if [[ -f $1.bak$maxbak ]]
      then
         col_msg '112' $maxbak
         exit
      fi
      (( i=$maxbak-1 ))
      while (( i > 0 ))
      do
         (( j=i+1 ))
         if [[ -f $1.bak$i ]]
         then
            mv $1.bak$i $1.bak$j
         fi
         (( i=i-1 ))

      done
      mv $1 $1.bak1
}

# ---------------------------------------------------------------------------
# Check for free space
# ---------------------------------------------------------------------------
#   function call: check_space
#   parameter....: $1            file name to be checked for enough space
#                  $2            location (filesystem)
check_space ()
{
      free_space=`df -k $2 | grep / | awk '{print $3}'`
      need_space=`ls -s $1 | awk '{print $1}'`
      if (( $free_space > $need_space )) ; then
         return 0
      else
         compress $1
         need_space=`ls -s $1.Z | awk '{print $1}'`
         if (( $free_space > $need_space )) ; then
            return 1
         else
            compress -d $1
            return 9
         fi
      fi
}

# ---------------------------------------------------------------------------
# Taring subroutine
# ---------------------------------------------------------------------------
#   function call: taring
#   parameter....: $1            tar options
#                  $2            tarfile
#                  $3            object to be tared
#                  $4            subdirectory
#                  $5            duplicate switch/directory
taring ()
{
      if [[ $4 = '' ]]; then
         tarsubdir=''
      else
         tarsubdir='-C '$4
         if [[ $5 = '' ]]; then
             col_msg '138' $4
         else
             col_msg -l '140' $3
             col_msg '138' $5
         fi
      fi
      tar $1 $2 $tarsubdir $3
      tar_ret=$?
      if [[ $tar_ret = '0' ]] ; then
         return 0
      else
         col_msg '135' $tar_ret
         (( tar_return_sum=tar_return_sum+1 ))
         return 1
      fi
}

# ---------------------------------------------------------------------------
# Collecting the detailed information found
# ---------------------------------------------------------------------------
#   function call: collect
#   parameter....: $1            file name to be collected
#                  $2            subdirectory
collect ()
{
      if [[ $2 = '' ]]; then
          c_dir=`pwd`
      else
          c_dir=$2
      fi
      if [[ $1 = '' ]]; then
          col_msg '128'
          return
      else
         duplicate=''
         typeset -i dnum=`cat $colfmclog | grep "FMC00121" | grep $1 | wc -l `
         if [[ $dnum > 0 ]]
         then
            cp $c_dir/$1 $workdir/$1.duplicate.$dnum
            duplicate=$c_dir
            c_name=$1.duplicate.$dnum
            c_dir=$workdir
         else
            c_name=$1
         fi
         col_msg '121' $1
      fi
      check_space $c_dir/$c_name ${optarray[1]}
      check_space_return=$?
      case $check_space_return in
          0 )  taring -rf $tarfile $c_name $c_dir $duplicate   ;;
          1 )  col_msg '132' $c_dir/$c_name
               taring -rf $tarfile $c_name $c_dir $duplicate
               compress -d $c_dir/$c_name                      ;;
          9 )  col_msg '133' $c_dir/$c_name          ;;
      esac
      if [[ $duplicate != '' ]] ; then
         rm $workdir/$1.duplicate.$dnum
      fi
}

# ===========================================================================
# ---------------------------------------------------------------------------
# Set variables and check some FlowMark information
# Initialize the collection log file and the tar file
# ---------------------------------------------------------------------------
if [[ ! -d $resdir ]]            # Checking the results directory
then                             # No backup of this directory is done
   mkdir $resdir
fi

if [[ -d $workdir ]]             # Check and create a working directory
then
   col_bak $workdir
fi
mkdir $workdir

if [[ -f $colfmclog ]]           # Check the collection log file
then
   col_bak $colfmclog
fi

banner Workflow > $colfmclog     # Create the log file header (init)
          m_date=`date`          # msg 113
         m_uname=`uname -n`      # msg 115
       m_oslevel=`oslevel`       # msg 116
        m_uptime=`uptime`        # msg 118
col_msg -l '101'
col_msg -l '113'
col_msg -l '114' $shname
col_msg -l '115'
col_msg -l '116'
col_msg -l '117'                 # msg InvocationArgs
col_msg -l '111'
col_msg -l '118'
col_msg    '100'
col_msg    '119'
col_msg    '100'

# ---------------------------------------------------------------------------
# collecting this file (init)
# ---------------------------------------------------------------------------
if [[ -f $tarfile ]]             # Check the tar file
then
   col_bak $tarfile
fi
tar_return_sum=0
check_space $0 $workdir
check_space_return=$?

case $check_space_return in
     0 )  cp -p `which $0` $workdir/fmczcol.sav
          if [[ $? = 0 ]]
          then
             col_msg '121' fmczcol.sav
             taring -cf $tarfile fmczcol.sav $workdir
          else
             col_msg -d '122' $workdir
             exit 1
          fi                                    ;;
     1 )  col_msg '137' $workdir
          exit                                  ;;
     9 )  col_msg '137' $workdir
          exit                                  ;;
esac

# ===========================================================================
# ---------------------------------------------------------------------------
#  The "hostname" command should return the same result as "uname -n"
#  otherwise Workflow may produce unpredictable results.
# ---------------------------------------------------------------------------
if [[ `uname -n` = `hostname` ]]; then
     fmc_uname=`uname -n`
   else
     col_msg '126'
fi

# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Functions main dbx analysis area
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

# ---------------------------------------------------------------------------
# Initialize the files needed for the dbx analysis
# ---------------------------------------------------------------------------
#   function call: fmc_dbx_init
#   parameter....: none

fmc_dbx_init () {
   dbxheader=$workdir/fmcdbx\.header
   if [[ -f $dbxheader ]]; then
      col_bak $dbxheader
   fi
                                  # -----------------------------------------
                                  #     Header preparation of dbx output file
                                  # -----------------------------------------
   banner 'debug output' > $dbxheader
   echo '------------------------------------------------' >> $dbxheader
   echo ' Creation date: ' `date`    >> $dbxheader
   echo ' Version .....: '$0         >> $dbxheader
   echo ' Host ........: '$fmc_uname >> $dbxheader
   echo '------------------------------------------------' >> $dbxheader

          dbxouthead=$workdir/fmcdbx\.outhead
   echo '------------------------------------------------'  > $dbxouthead
   echo ' Redirected messages from dbx run               ' >> $dbxouthead
   echo '------------------------------------------------' >> $dbxouthead

   dbxoutput[0]=$workdir/fmcdbx\.output
   dbxoutputname[0]=fmcdbx\.output
   if [[ -f ${dbxoutput[0]} ]]; then
      col_bak ${dbxoutput[0]}
   fi
   cat $dbxheader > ${dbxoutput[0]}

         dbxdetach=$workdir/dbxdetach
           dbxquit=$workdir/dbxquit
   echo 'detach'  > $dbxdetach
   echo 'quit'    > $dbxquit
}
# ---------------------------------------------------------------------------
# Get the process ids of actual running proccesses
# ---------------------------------------------------------------------------
#   function call: fmc_core_init
#   parameter....: none
fmc_core_init () {

            dbxinput[1]=$workdir/tmp1in$$
          dbxthreads[1]=$workdir/tmp2in$$
        dbxthreadnum[1]=$workdir/tmp3in$$
   dbxthreads_detail[1]=$workdir/tmp4in$$
          fmcthreads[1]=$workdir/etcore.sh
           dbxoutput[1]=$workdir/fmcdbx\.output
       dbxoutputname[1]=fmcdbx\.output
           dbxoutmsg[1]=$workdir/fmcdbx\.outmsg
       dbxoutmsgname[1]=fmcdbx\.outmsg
      banner 'core file' > ${dbxoutput[1]}
      echo '------------------------------------------------' >> ${dbxoutput[1]}
      echo ' Creation date: '`date`                           >> ${dbxoutput[1]}
##      echo ' Core file....: '     ????               ${P_pid[$i]}                     >> ${dbxoutput[1]}
##      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[1]}
      echo '------------------------------------------------' >> ${dbxoutput[1]}

            max_dbx_run=1
}

# ---------------------------------------------------------------------------
# Get the process ids of actual running proccesses
# ---------------------------------------------------------------------------
#   function call: fmc_pids
#   parameter....: none
fmc_dbx_pids () {
   i=0
   ps -eo user,pid,comm | grep fmc | grep -v "^$procname" |
   while read INPUT; do
      (( i=i+1 ))
              P_user[$i]=`echo $INPUT | cut -d" " -f1`
               P_pid[$i]=`echo $INPUT | cut -d" " -f2`
              P_name[$i]=`echo $INPUT | cut -d" " -f3`
           dbxoutput[$i]=$workdir/fmc_dbx\.output\.${P_pid[$i]}
       dbxoutputname[$i]=fmc_dbx\.output\.${P_pid[$i]}
           dbxoutmsg[$i]=$workdir/fmc_dbx\.outmsg\.${P_pid[$i]}
       dbxoutmsgname[$i]=fmc_dbx\.outmsg\.${P_pid[$i]}
            dbxinput[$i]=$workdir/tmp1in$$\.${P_pid[$i]}
          dbxthreads[$i]=$workdir/tmp2in$$\.${P_pid[$i]}
        dbxthreadnum[$i]=$workdir/tmp3in$$\.${P_pid[$i]}
   dbxthreads_detail[$i]=$workdir/tmp4in$$\.${P_pid[$i]}
          fmcthreads[$i]=$workdir/et${P_pid[$i]}.sh

      banner 'process' > ${dbxoutput[$i]}
      echo '------------------------------------------------' >> ${dbxoutput[$i]}
      echo ' Creation date: '`date`                           >> ${dbxoutput[$i]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutput[$i]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[$i]}
      echo '------------------------------------------------' >> ${dbxoutput[$i]}

      echo '------------------------------------------------'  > ${dbxoutmsg[$i]}
      echo ' Redirected messages from dbx run               ' >> ${dbxoutmsg[$i]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutmsg[$i]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutmsg[$i]}
      echo '------------------------------------------------' >> ${dbxoutmsg[$i]}

      echo '------------------------------------------------' >> ${dbxoutput[0]}
      echo ' Detailed output created for  '                   >> ${dbxoutput[0]}
      echo ' Process-ID...: '${P_pid[$i]}                     >> ${dbxoutput[0]}
      echo ' Process-Name.: '${P_name[$i]}                    >> ${dbxoutput[0]}
      echo '------------------------------------------------' >> ${dbxoutput[0]}
   done

   max_dbx_run=$i
}

# ---------------------------------------------------------------------------
# Create built-in shells and command files
# ---------------------------------------------------------------------------
#   function call: fmc_dbx_shells
#   parameter....: none
fmc_dbx_shells () {
   tgp='\$t'
   i=0
   while (( $i < $max_dbx_run )); do
     (( i=i+1 ))
       # -------------------------------------------------------------------
       # Building shell scripts for preparing the threadids as input for dbx
       # -------------------------------------------------------------------
cat > ${fmcthreads[$i]} <<FMC_LABEL1a
#!/usr/bin/ksh
# The first positional parameter contains the output of the dbx thread subcommand
# The second contains the output file to be used
if [[ -s ${dbxthreadnum[$i]} ]]; then
   cat > ${dbxthreads_detail[$i]} <<FMC_LABEL1b
      sh echo '========== thread summary information =============' >> ${dbxoutput[$i]}
      sh echo ' '                                                >> ${dbxoutput[$i]}
      thread                                                     >> ${dbxoutput[$i]}
      sh echo ' '                                                >> ${dbxoutput[$i]}
FMC_LABEL1b
      while read threadid
      do
         cat >> ${dbxthreads_detail[$i]} <<FMC_LABEL1c
         sh echo ' '                                                >> ${dbxoutput[$i]}
         sh echo '---------- starting information for thread: '\$threadid' ----------' >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         thread current \$threadid; thread info \$threadid          >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         where                                                      >> ${dbxoutput[$i]}
         sh echo ' '                                                >> ${dbxoutput[$i]}
         registers                                                  >> ${dbxoutput[$i]}
#
         sh echo ' '
         sh echo '---- listi for '\$threadid' ----'
         sh echo ' '
         listi
         sh echo '------------------------------------------------'
#
         sh echo ' '                                                >> ${dbxoutput[$i]}
         sh echo '---------- ending information for thread: '\$threadid' ------------' >> ${dbxoutput[$i]}
FMC_LABEL1c
     done < ${dbxthreadnum[$i]}
else
   cat > ${dbxthreads_detail[$i]} <<FMC_LABEL1d
      sh echo ' ' >> ${dbxoutput[$i]}
      sh echo '---------- attention: no thread information located ----------' >> ${dbxoutput[$i]}
      sh echo ' ' >> ${dbxoutput[$i]}
FMC_LABEL1d
fi

exit
FMC_LABEL1a
chmod 755 ${fmcthreads[$i]}

# ---------------------------------------------------------------------------
# Define command file for dbx
# ---------------------------------------------------------------------------
cat > ${dbxinput[$i]} <<FMC_LABEL2
   sh echo '========== dbx summary information =============' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- where information -------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   where                                                      >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- dump information --------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   dump                                                       >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- register information ----------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   registers                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
#
   sh echo ' '                                                ;
   sh echo '---------- listi information -------------------' ;
   sh echo ' '                                                ;
   listi                                                      ;
   sh echo '------------------------------------------------' ;
#
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   thread > ${dbxthreads[$i]};
   sh awk '{print \$1}' ${dbxthreads[$i]} | grep '$tgp' | sed -e "s/>//g" | sed -e "s/$tgp//g" > ${dbxthreadnum[$i]};
   sh ${fmcthreads[$i]};
   source ${dbxthreads_detail[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- attribute information ---------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   attribute                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- mutex information -------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   mutex                                                      >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- condition information ---------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   condition                                                  >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '---------- map information ---------------------' >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   map                                                        >> ${dbxoutput[$i]};
   sh echo ' '                                                >> ${dbxoutput[$i]};
   sh echo '========== end of dbx summary information ======' >> ${dbxoutput[$i]};
FMC_LABEL2
   done
}

# ---------------------------------------------------------------------------
# Create the dbx monitoring shell
# ---------------------------------------------------------------------------
#   function call: fmc_dbx_monitor
#   parameter....: none
fmc_dbx_monitor () {
cat > $fmcmonitor <<FMC_LABEL3
#!/usr/bin/ksh
#  \$1  dbx output file to be used for verification
#  \$2  dbx process id to be monitored
#  \$3  Mode: (a)ttach process or analyse (c)ore file
#  \$4  core file name
#  \$5  core program

while true
do
    ps c \$2 > /dev/null
    if [[ \$? = '1' ]]; then
       break
    fi
    outputsize=\`ls -l \$1 | awk '{print \$5}' \`
    case \$3 in
      a)   echo 'Analyzing. Please wait. Process .........: '\$2
           echo '                   ...  Result file size : '\$outputsize

           ;;
      c)   echo 'Analyzing. Please wait. Core file .......: '\$4
           echo '                   ...  Core program ....: '\$5
           echo '                   ...  Result file size : '\$outputsize
           ;;
    esac
    sleep 20
    if [[ \$outputsize = \`ls -l \$1 | awk '{print \$5}'\` ]]; then
        if [[ \`tail -1 \$1\` = '========== end of dbx summary information ======' ]]; then
           break
        fi
        kill -2 \$2
        echo 'Sending signal to dbx process ' >> $colfmclog
        echo 'Sending signal to dbx process '
## function not exported        col_msg "plu_line" "19" \$2
    fi
done
exit
FMC_LABEL3
chmod 755 $fmcmonitor
}

# ---------------------------------------------------------------------------
# Verify that dbx is installed
# ---------------------------------------------------------------------------
#   function call: dbx_check
#   parameter....: none
dbx_check () {

if [[ `lslpp -L | grep bos.adt.debug` = '' ]]; then
   echo 'bos.adt.debug is not installed'
   return 1
fi
}

# ---------------------------------------------------------------------------
# Calling dbx and its related monitor
# ---------------------------------------------------------------------------
#   function call: dbx_run
#   parameter....: $1            mode of dbx call ( a=attach c=core )
#                  $2            core program
#                  $3            core file
###                  $4            path of program form core
dbx_run () {

i=0
while (( $i < $max_dbx_run ))
do
  (( i=i+1 ))
  case $1 in
    a)    dbx -d100 -a ${P_pid[$i]} -c ${dbxinput[$i]} -I /usr/lpp/fmc/bin < $dbxdetach > ${dbxoutmsg[$i]} 2>&1 &
          dbxpid[$i]=$!        # dbx processid
          $fmcmonitor ${dbxoutput[$i]} ${dbxpid[$i]} $1 &
          monpid[$i]=$!
            ;;
    c)    dbx -d100 -c ${dbxinput[$i]} $2 $3 < $dbxquit > ${dbxoutmsg[$i]} 2>&1 &
          dbxpid[$i]=$!        # dbx processid
          $fmcmonitor ${dbxoutput[$i]} ${dbxpid[$i]} $1 $3 $2 &
          monpid[$i]=$!
            ;;
  esac
done

## to be tested in detail (timing)
## wait for  ${dbxpid[$i]}
   i=0
   while (( $i < $max_dbx_run ))
   do
      (( i=i+1 ))
      while true
      do
         ps c ${dbxpid[$i]} > /dev/null
         if [[ $? = '1' ]]; then
            break
         fi
         sleep 10
         if [[ `tail -1 ${dbxoutput[$i]}` = ${col_fmc_msg[5]} ]]; then
            break
         fi
      done
# kill the monitor shell if actual in sleep mode
      ps c ${monpid[$i]} > /dev/null
      if [[ $? = '0' ]]; then
         kill ${monpid[$i]}
      fi
  done
}

# ---------------------------------------------------------------------------
# Finishing dbx work
# ---------------------------------------------------------------------------
#   function call: dbx_finish
#   parameter....: $1                  contains the outputfile
#                  $2                  contains the mode (core or process)

dbx_finish () {
i=0
while (( $i < $max_dbx_run )); do
  (( i=i+1 ))
  cat ${dbxoutput[$i]} >> $1
  cat $dbxouthead      >> $1
  cat ${dbxoutmsg[$i]} >> $1
done

echo '================================================' >> $1
echo ' Analyzing of '$2' information is finished. '     >> $1
echo '================================================' >> $1
echo ' '
echo 'Analyzing of '$2' information is finished.'
}

# ---------------------------------------------------------------------------
# Find and analyse the core files in the system
# ---------------------------------------------------------------------------
#   function call: dbx_core_files
#   parameter....: none

dbx_core_files () {
##   echo 'tbd function locate core files'
##   echo 'actual used core file is' $pwddir'/core'

#          dbxoutput[$i]=$workdir/fmc_dbx\.output\.${P_pid[$i]}
#          dbxoutmsg[$i]=$workdir/fmc_dbx\.outmsg\.${P_pid[$i]}

   corename[1]=$pwddir/core
   if [[ -f ${corename[1]} ]]           # Check the core file
   then
      dbxouttmp=$workdir/dbxout.temp
      dbx -d100 /usr/bin/ls ${corename[1]} < $dbxquit > $dbxouttmp 2>&1
      corepgmname=`cat $dbxouttmp | grep Core | awk '{print $4}' | sed -e "s/(//" | sed -e "s/)//"`
      if [[ xx$corepgmname != 'xx' ]]
      then
         corepgmtemp=`which $corepgmname`
         corepgmtemp=`ls -Fl $corepgmtemp`
         set $corepgmtemp
         shift $(($# -1))
         eval corepgmtemp=$1
         corepgm[1]=$corepgmtemp
         coreoutname[1]=dbxcore.output
         coreout[1]=$workdir/${coreoutname[1]}
         cat $dbxheader > ${coreout[1]}
         echo ' ' >> ${coreout[1]}
         ls -l ${corename[1]} >> ${coreout[1]}
         echo ' ' >> ${coreout[1]}
         core_file_count=1
         return 0
      else
         col_msg '103'
         col_msg '152' $pwddir
         col_msg '103'
         return 1
      fi
   else
      col_msg '103'
      col_msg '158' $pwddir
      col_msg '159'
      col_msg '103'
      return 1
   fi
}

# ========================================================================
# Verify that dbx is installed                       (start) optarray[8+9]
# ========================================================================
dbx_check
# -------------------------- run dbx only if dbx is availbale ------ (8) -
if [[ $? = 0 ]]; then
# -------------------------- check options set for dbx run --------- (9) -
  if [[ ${optarray[8]} = "yes" || ${optarray[9]} = "yes" ]]; then
    fmc_dbx_init
# ========================================================================
# Attaching to processes                               (start) optarray[8]
# ========================================================================
    if [[ ${optarray[8]} = "yes" ]]; then
       fmc_dbx_pids
       fmc_dbx_shells
       fmc_dbx_monitor
       dbx_run "a"
       dbx_finish ${dbxoutput[0]} process
       collect ${dbxoutputname[0]} $workdir
    fi
# ========================================================================
#                                                        (end) optarray[8]
# ========================================================================
# ========================================================================
# Analyse core files                                   (start) optarray[9]
# ========================================================================
    if [[ ${optarray[9]} = "yes" ]]; then
       max_dbx_run=1
       fmc_core_init
       dbx_core_files
       if [[ $? = 0 ]]; then
         fmc_dbx_shells
         fmc_dbx_monitor
         i=0
         while (( $i < $core_file_count )) ; do
            (( i=i+1 ))
            dbx_run "c" ${corepgm[$i]} ${corename[$i]} ${corepath[$i]}
            dbx_finish  ${coreout[$i]} core
            collect  ${coreoutname[$i]} $workdir
         done
       fi
    fi
# ========================================================================
#                                                        (end) optarray[9]
# ========================================================================
# ------------------------------------------------------------- else (9) -
  else
     col_msg -l '127'
  fi
# ------------------------------------------------------------- else (8) -
else
   col_msg '134'
fi
# ========================================================================
#                                                      (end) optarray[8+9]
# ========================================================================


# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Starting main collection area (dbx already done)
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

# ---------------------------------------------------------------------------
# Searching for links
# ---------------------------------------------------------------------------
#   function call: link_search
#   parameter....: $1            directory to be searched
#                  $2            output file
#                  $3            grep for string
link_search ()
{
   if [[ $3 = '' ]] ; then
       ls -Fl $1 > /dev/null | grep @                         >> $2
       linkrc=$?
   else
       ls -Fl $1 > /dev/null | grep $3 | grep @               >> $2
       linkrc=$?
   fi
   echo ' Searching for links in '$1                          >> $2
   if [[ $linkrc = '0' ]]
   then
      echo '------------------------------------------------' >> $2
   else
      echo ' No additional links found      '                 >> $2
      echo '------------------------------------------------' >> $2
   fi
}

# ---------------------------------------------------------------------------
# Searching for DB2 instances
# ---------------------------------------------------------------------------
#   function call: get_instance_array
#   parameter....:               none


get_instance_array () {
(( inst_count=0 ))
(( adm_count=0 ))
if [[ -x /usr/lpp/db2_05_00/instance/db2ilist ]] ; then
   db2_vers='db2_05_00'
fi
if [[ -x /usr/lpp/db2_06_00/instance/db2ilist ]] ; then
   db2_vers='db2_06_00'
fi
if [[ $db2_vers = '' ]] ; then
   col_msg '148'
   return 1
fi
/usr/lpp/db2_05_00/instance/db2ilist | while read instance
do
   db_adm=`su - $instance -c 'db2set -g | grep DB2ADMINSERVER | cut -f2 -d= '`
   if [[ .$db_adm = .$instance ]] ; then
      (( count=0 ))
      adm_dup='no'
      while (( $count < $adm_count ))
      do
        # check for duplicate DB2ADMINSERVER
        (( count=count+1 ))
        if [[ .$db_adm = .${DB2_adm_array[$count]} ]] ; then
           adm_dup='yes'
        fi
      done
      if [[ $adm_dup = 'no' ]] ; then
         (( adm_count=adm_count+1 ))
         DB2_adm_array[$adm_count]=$db_adm
         col_msg '145' $db_adm $adm_count
      fi
   else
      (( inst_count=inst_count+1 ))
      DB2_inst_array[$inst_count]=$instance
      col_msg '146' $instance $inst_count
      col_msg '147' $instance $db_adm
   fi
done
}

# ========================================================================
# Workflow information is included                     (start) optarray[2]
# ========================================================================
if [[ ${optarray[2]} = "yes" ]]; then

   echo '================================================' >  $workdir/$fmclinks
   echo ' Searching for additional specified links       ' >> $workdir/$fmclinks
   echo '================================================' >> $workdir/$fmclinks
   col_msg '139'
   link_search /usr/lpp/fmc/bin           $workdir/$fmclinks
   link_search /usr/lpp/fmc/lib           $workdir/$fmclinks
   link_search /usr/lpp/fmc               $workdir/$fmclinks
   link_search /usr/bin                   $workdir/$fmclinks  fmc
   link_search /usr/lib                   $workdir/$fmclinks  fmc
   collect $fmclinks $workdir

   WFinfo=$workdir/'workflow.info'
   if [[ -f /usr/lpp/fmc/fmcrc ]] ; then
      collect fmcrc /usr/lpp/fmc
      fmcRootDir=`cat /usr/lpp/fmc/fmcrc | grep RootDirectory | cut -f2 -d= `
      ls -l ${fmcRootDir} > $WFinfo
      if [[ -f ${fmcRootDir}/fmcrc ]] ; then
         collect fmcrc ${fmcRootDir}
      fi
      ls ${fmcRootDir}/cfgs | while read ConfigID
      do
         col_msg '160' $ConfigID
         ls -lR ${fmcRootDir}/cfgs/${ConfigID} >> $WFinfo
         ls ${fmcRootDir}/cfgs/${ConfigID}/log | while read fmclog
         do
            collect $fmclog ${fmcRootDir}/cfgs/${ConfigID}/log
         done
         ls ${fmcRootDir}/cfgs/${ConfigID}/fdl | while read fmcfdl
         do
            collect $fmcfdl ${fmcRootDir}/cfgs/${ConfigID}/fdl
         done
         collect fmcrc          ${fmcRootDir}/cfgs/${ConfigID}
         collect fmcrc.previous ${fmcRootDir}/cfgs/${ConfigID}
      done
   else
      col_msg '161'
   fi
   if [[ -f $workdir/workflow.info ]] ; then
      collect workflow.info $workdir
   fi
   if [[ -f $pwddir/fmczchk.log ]] ; then
      collect fmczchk.log $pwddir
   fi
fi

##162) msgtxt='                                                                     '      ;;
##173) msgtxt='                                                                     '      ;;
# ========================================================================
#                                                        (end) optarray[2]
# ========================================================================
# ========================================================================
# Identifying and collecting MQSeries information      (start) optarray[3]
# ========================================================================
if [[ ${optarray[3]} = "yes" ]]; then
   if [[ -f /var/mqm/mqs.ini ]] ; then
      collect mqs.ini /var/mqm
      MQDefaultPrefix=`cat /var/mqm/mqs.ini | grep DefaultPrefix | cut -f2 -d= `
      ls $MQDefaultPrefix/errors | while read mq_err
      do
         collect $mq_err $MQDefaultPrefix/errors
      done
      ls $MQDefaultPrefix/qmgrs/@SYSTEM/errors | while read mq_err
      do
         collect $mq_err $MQDefaultPrefix/qmgrs/@SYSTEM/errors
      done
      # get queue managers
      (( qmgr_count=0 ))
      cat /var/mqm/mqs.ini |
      while read templine
      do
         if [[ `echo $templine | awk '{print substr($1,length($1))}'` = ':' ]] ; then
            # Begin stanza
            if [[ $templine = 'QueueManager:' ]] ; then
               # get infos
               (( qmgr_count=qmgr_count+1 ))
               read templine
               readrc=$?
               while [[ `echo $templine | awk '{print substr($1,length($1))}'` != ':' && $readrc = 0 ]]
               do
                  # No stanza
                  tempname=`echo $templine | cut -f1 -d= `
                  case $tempname in
                     Name)      qmgr_name[$qmgr_count]=`echo $templine | cut -f2 -d= `  ;;
                     Prefix)    qmgr_pref[$qmgr_count]=`echo $templine | cut -f2 -d= `  ;;
                     Directory) qmgr_dire[$qmgr_count]=`echo $templine | cut -f2 -d= `  ;;
                  esac
                  read templine
                  readrc=$?
               done
            fi
         fi
      done
      (( i=0 ))
      while (( $i < $qmgr_count ))
      do
         (( i=i+1 ))
         TOP=${qmgr_pref[$i]}/qmgrs/${qmgr_name[$i]}
         if [[ ! -d $TOP ]] ; then
            col_msg '171' $TOP
         else
            collect qm.ini $TOP
            ls $TOP/errors | while read mq_err
            do
               collect $mq_err $TOP/errors
            done
         fi
      done
   else
      col_msg '170'
   fi
fi

# ========================================================================
#                                                        (end) optarray[3]
# ========================================================================
# ========================================================================
# Identifying and collecting DB2 information           (start) optarray[4]
# ========================================================================
if [[ ${optarray[4]} = "yes" ]]; then
   get_instance_array
   if [[ $? = 0 ]] ; then
      (( i=0 ))
      while (( $i < $inst_count ))
      do
         (( i=i+1 ))
         (( db_count=0 ))
         db_info=$workdir/${DB2_inst_array[$i]}.info
         echo '================================================' >  $db_info
         echo ' DB2 instance informations                      ' >> $db_info
         echo '================================================' >> $db_info
         echo '       output of db2 list node directory        ' >> $db_info
         echo '------------------------------------------------' >> $db_info
         su - ${DB2_inst_array[$i]} -c "db2 list node directory" >> $db_info
         echo '------------------------------------------------' >> $db_info
         echo '       output of db2 list db directory          ' >> $db_info
         echo '------------------------------------------------' >> $db_info
         su - ${DB2_inst_array[$i]} -c "db2 list db directory"   >> $db_info
         echo '------------------------------------------------' >> $db_info
         echo '       output of db2 get dbm cfg                ' >> $db_info
         echo '------------------------------------------------' >> $db_info
         su - ${DB2_inst_array[$i]} -c "db2 get dbm cfg"         >> $db_info
         echo '------------------------------------------------' >> $db_info
         echo '       output of db2set                         ' >> $db_info
         echo '------------------------------------------------' >> $db_info
         su - ${DB2_inst_array[$i]} -c "db2set"                  >> $db_info
         echo '------------------------------------------------' >> $db_info
         su - ${DB2_inst_array[$i]} -c "db2 list db directory | grep 'Database alias' " | awk '{print $4}' | while read db
         do
             echo '------------------------------------------------' >> $db_info
             echo '       output of db2 get db cfg for '$db          >> $db_info
             echo '------------------------------------------------' >> $db_info
             su - ${DB2_inst_array[$i]} -c 'db2start'
             dbstartrc=$?
             eval su - ${DB2_inst_array[$i]} -c 'db2 get db cfg for $db' >> $db_info
             if [[ $dbstartrc = 0 ]] ; then
                su - ${DB2_inst_array[$i]} -c 'db2stop'
             fi
         done
         collect ${DB2_inst_array[$i]}.info $workdir
         rm $db_info
         inst_home=`lsuser -a home ${DB2_inst_array[$i]} | cut -d= -f2 `
         if [[ -f $inst_home/sqllib/db2dump/db2diag.log ]] ; then
            collect db2diag.log $inst_home/sqllib/db2dump
         fi
      done
   fi
fi

# ========================================================================
#                                                        (end) optarray[4]
# ========================================================================
# ========================================================================
# Identifying and collecting system information (snap) (start) optarray[5]
# ========================================================================
if [[ ${optarray[5]} = "yes" ]]; then
   if [[ -d $snapdir ]]; then
      rm -R $snapdir
   fi
   mkdir $snapdir
   dispm='no'
   col_msg '153' $snapvalue
   dispm='yes'
   snap $snapvalue -d $snapdir -c
   snap_rc=$?
   if [[ $snap_rc > 0 ]]; then
      col_msg '156' $snap_rc
      col_msg '157' $snapdir
   else
      collect snap.tar.Z $snapdir
   fi
fi

# ========================================================================
#                                                        (end) optarray[5]
# ========================================================================
# ========================================================================
# Identifying and collecting basic system information  (start) optarray[6]
# ========================================================================
if [[ ${optarray[6]} = "yes" ]]; then
# iostat      in conjunction with -a
# ipcs -a
# lsps -s a

   fmclocout=$workdir/$fmclocale
                #---------------------------------------------------------
                # Search for installed fixes and record details
                #---------------------------------------------------------
   echo '================================================' >  $workdir/$fmcinstfix
   echo ' Query for installed fixes                      ' >> $workdir/$fmcinstfix
   echo '================================================' >> $workdir/$fmcinstfix
   echo '       output of instfix -ia                    ' >> $workdir/$fmcinstfix
   echo '------------------------------------------------' >> $workdir/$fmcinstfix
   instfix -ia                                             >> $workdir/$fmcinstfix
   echo '================================================' >> $workdir/$fmcinstfix
   echo '       output of instfix -ic                    ' >> $workdir/$fmcinstfix
   echo '------------------------------------------------' >> $workdir/$fmcinstfix
   instfix -ic                                             >> $workdir/$fmcinstfix
   collect $fmcinstfix $workdir

   col_msg '129'
   echo '================================================' >  $fmclocout
   echo '   NLS: System Locale Information               ' >> $fmclocout
   echo '================================================' >> $fmclocout
   echo '   Command out from : locale                    ' >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   locale                                                  >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   echo '   Command out from : locale -a                 ' >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   locale -a                                               >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   echo '   Command out from : locale -m                 ' >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   locale -m                                               >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   echo '   Command out from : locale charmap            ' >> $fmclocout
   echo '------------------------------------------------' >> $fmclocout
   locale charmap                                          >> $fmclocout
   echo '================================================' >> $fmclocout
   echo '   End of system locale information collection  ' >> $fmclocout
   echo '================================================' >> $fmclocout

   collect $fmclocale $workdir

   collect services /etc
   collect inetd.conf /etc

fi
# ========================================================================
#                                                        (end) optarray[6]
# ========================================================================

# ========================================================================
# Collecting high availability (HACMP)                (start) optarray[11]
# ========================================================================
if [[ ${optarray[11]} = "yes" ]]; then
   fmchaout=$workdir/$fmc_ha
   lslpp -L | grep cluster > $fmchaout
   typeset -i ClusterNum=`wc -l < $fmchaout`
   if [[ $ClusterNum > 0 ]] ; then
      # some HACMP components are installed
      col_msg '141'
      /usr/sbin/cluster/utilities/clsnapshot -c -i -n WF$$.snapshot -d 'Workflow information collection'
      snapshotrc=$?
      if [[ $snapshotrc = 0 ]] ; then
         if [[ -f WWSS.snapshot.info ]] ; then
            collect WW$$.snapshot.info /usr/sbin/cluster/snapshots
            rm /usr/sbin/cluster/snapshots/WW$$.snapshot.info
         else
            col_msg '144' WW$$.snapshot.info
         fi
         if [[ -f WWSS.snapshot.odm ]] ; then
            collect WW$$.snapshot.odm /usr/sbin/cluster/snapshots
            rm /usr/sbin/cluster/snapshots/WW$$.snapshot.odm
         else
            col_msg '144' WW$$.snapshot.odm
         fi
      else
         col_msg '143' $snapshotrc
      fi
      collect $fmc_ha $workdir
   else
      col_msg '142'
   fi
fi

# ========================================================================
#                                                       (end) optarray[11]
# ========================================================================

# ========================================================================
# End: Collecting log file and compress tarfile if required
# ========================================================================
collect $logname $resdir
col_msg '130'

if [[ $tar_return_sum > 0 ]]; then
   col_msg '131' $colfmclog
   col_msg '136' $workdir
else
   if [[ ${optarray[10]} = 'yes' ]]; then
      col_msg '120'
      if [[ -f $tarfile\.Z ]]             # Check the tar file
      then
         col_bak $tarfile.Z
      fi
      compress $tarfile
      tarfile=$tarfile.Z
   fi
   rm -R $workdir
   col_msg '111'
   col_msg '100'
   col_msg -l '125'
   col_msg -d '123' $tarfile
fi
col_msg -d '124' $colfmclog
col_msg '100'
exit
