#!/usr/bin/ksh
#***********************************************************************
#
# NAME: cmvccfg.ksh (familyName)
#
# PURPOSE:
#
#    The purpose of this script is to provide a method of collecting
#    useful information of any CMVC environment.  This is
#    including (but not limited to) information relating to the
#    operating system and database installation.
#    The information gathered by this script can
#    be passed onto technical support to aid in problem determination.
#
# EXAMPLE USAGE:
#
#       cmvccfg.ksh $LOGNAME  2>&1  |  tee cmvccfg.out
#
# ENVIRONMENT VARIABLES:
#       CMVC_FAMILY
#
# NOTE:
# * It must be run from the CMVC family user id.
#
# **********************************************************************
# *
# *           NOTICE TO USERS OF THE SOURCE CODE EXAMPLES
# *
# * INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE
# * CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS"
# * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
# * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# * FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
# * AND PERFORMANCE OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS
# * ONE OR MORE GROUPS, IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE
# * EXAMPLES PROVE DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED DEALER)
# * ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR
# * CORRECTION.
# *
# **********************************************************************
#***********************************************************************

#-------------------------------------------------------+
# Return a string identifying the operating system.
# Output:  OPSYS = (AIX, HP-UX, SunOS, Solaris)
#-------------------------------------------------------+
get_opsys()
{
raw=`uname -a`
OPSYS=`echo $raw | cut -f1 -d' '`

# Test for Solaris (5... or higher) or SunOS (4...)
# The problem is that the Solaris op-sys is identified as SunOS, and
# there is no string that indicates that is "Solaris". The distinction
# is done only by the version number.

if [ "$OPSYS" = "SunOS" ]
then
   var=`echo $raw | cut -f3 -d' ' | cut -c1-1`
   if [ $var -ne 4 ]
     then
       OPSYS="Solaris"
   fi
fi
export OPSYS
print "get_opsys: " $OPSYS
}

#---------------------------------------------------------------------+
#  Returns a string identifying the database that is used by the
#  CMVC cmvcd executable which is found first in the PATH.
#  OUTPUT:   CMVCDB = (DB2, DB2V2, Oracle, Sybase, Informix)
#---------------------------------------------------------------------+
get_database()
{
# First, determine which is the actual cmvcd that is executed.
# This is important because the family might be using a cmvcd executable
# that does not reside in /usr/lpp/cmvc/bin.

if [ "$OPSYS" = "HP-UX" ]
then
   which_cmvcd=`whence cmvcd`
else
   which_cmvcd=`which cmvcd`
fi
# Then get the line that mentions the database name. The format should
# be "cmvc Database: name".
# If the string is not found, then this means that the CMVC version is
# earlier than 2.3 (that is, 2.1 or 2.2, which did not have the necessary
# what strings).

database_row=`what $which_cmvcd | grep "Database: "`
if [ "$database_row" = "" ]
then
   print "The cmvcd version is not 2.3, and it does not contain \n"
   print "the explicit information about which database is used.\n"
   print "Therefore, there will not be a special handling for Oracle,\n"
   print "which means that some warning messages may be added to the\n"
   print "syslog when killing the parent daemons.\n"
#  Assume DB2, which will NOT do the special processing for Oracle
   export CMVC_DB=`DB2`
else
#  Finally, get the actual database name.
   export CMVC_DB=`echo $database_row | cut -d" " -f3`
fi

print "get_database: " $CMVC_DB
}

#-------------------------------------------------------+
# Main
#-------------------------------------------------------+

# display command line
print -u2 "# ${0} ${*}"

## check for valid arguments
## if family name is passed, use it
## if no family name is passed, check for CMVC_FAMILY and use it
## if no CMVC_FAMILY and no family name passed, put out usage message

if [[ ${#} < 1 ]]
then
  unset cmvcfamily
  if [[ -n ${CMVC_FAMILY} ]] ; then
    cmvcfamily=$(print ${CMVC_FAMILY} | sed 's/@.*$//')
    cmvcfamily=${CMVC_FAMILY}
  else
    print -u2 ' '
    print -u2 '  Usage: cmvccfg cmvcFamily'
    print -u2 ' '
    print -u2 '    Where cmvcFamily is the family DB2 database name.'
    print -u2 ' '
    print -u2 '  This command displays system information and database'
    print -u2 '  information for a CMVC family.'
    print -u2 ' '
    print -u2 '  Note:  This command must be run using the CMVC family user ID.' print -u2 ' '
    return 1
  fi
else
cmvcFamily=$1
fi

  print '+-------------------------------------------------------------+'
  print '|                                                             |'
  print '| CMVC Configuration information                              |'
  print '|                                                             |'
  print '+-------------------------------------------------------------+'
  print ' '
  print 'Configuration date and time'
    date
  print ' '

  print 'cmvccfg -------------------------- Operating system and version'
  print ' '
    get_opsys    # get operating system in variable OPSYS
  print $OPSYS
  print 'uname -a '
    uname -a
  print ' '

  print 'cmvccfg ---------------------------- Database Management System'
  print ' '
    get_database # get name of database used by the cmvcd executable
                 # in variable CMVC_DB
  print "Database: $CMVC_DB"
  print ' '

  print 'cmvccfg ------------------------------------------ CMVC version'
  print 'what cmvcd | grep cmvc '
  case "$OPSYS" in
     AIX|Solaris)
       what `which cmvcd` | grep cmvc
       ;;
     HP-UX)
       what `whence cmvcd` | grep cmvc
       ;;
   esac
  print ' '

  print 'cmvccfg ----------------------------- All environment variables'
  print 'set '
    set
  print ' '

  print 'cmvccfg ---------------------- Total space including swap space'
  print 'df -k '
    df -k
  print ' '

  print 'cmvccfg --------------------------------------------- User info'
  print 'id '
    id
  print ' '

if [ "$CMVC_DB" = "DB2" ]
then
  print " "

  print 'cmvccfg ------------------------------------------- DB2 version'
  print 'db2level '
    db2level
  print ' '

  print 'cmvccfg -------------------------------- DB2 registry variables'
  print 'db2set -all '
    db2set -all
  print ' '

  print 'cmvccfg ----------------------------- DB2 environment variables'
  print 'set | grep -i db2 '
    set | grep -i db2
  print ' '

  print 'cmvccfg ------------------------------------------- DB2 connect'
  print "db2 connect to $LOGNAME"
    db2 connect to $LOGNAME
  print ' '

  print 'cmvccfg ------------------------------- DB2 admin configuration'
  print 'db2 get admin cfg '
    db2 get admin cfg
  print ' '

  print 'cmvccfg -------------------- DB2 database manager configuration'
  print 'db2 get dbm cfg '
    db2 get dbm cfg
  print ' '

  print 'cmvccfg --------------------- DB2 family database configuration'
  print "db2 get db cfg for ${cmvcFamily}"
    db2 get db cfg for ${cmvcFamily}
  print ' '

  print 'cmvccfg ------------------- Disconnecting from the DB2 database'
  print 'db2 terminate'
    db2 terminate
  print ' '

fi

if [ "$CMVC_DB" = "Oracle" ]
then
  print " "

  print 'cmvccfg ---------------------------------------- ORACLE version'
  print 'grep -i server $ORACLE_HOME/unix.prd '
    grep -i server $ORACLE_HOME/unix.prd
  print ' '
fi

if [ "$CMVC_DB" = "Informix" ]
then
  print " "
  print 'cmvccfg -------------------------------------- INFORMIX version'
  print 'oninit -V '
    oninit -V
  print ' '
fi

if [ "$CMVC_DB" = "Sybase" ]
then
  print " "
  print 'cmvccfg ---------------------------------------- SYBASE version'
  print ' '
isql -Usa -P$SYBASE_SA_PASS >> /tmp/$$.tmp 2>&1 <<!!!
select @@version
go
!!!
  cat /tmp/$$.tmp
  print ' '
fi

  print 'cmvccfg ----------------------------------- CMVC audit.log tail'
  print 'ls -al $HOME/audit/log '
    ls -al $HOME/audit/log
  print ' '
  print 'tail $HOME/audit/log'
    tail $HOME/audit/log
  print ' '

# This stuff needs to be last, to avoid problems with the piping and tee

  print 'cmvccfg ----------------------------------- CMVC user exit info'
  print 'showing the non-comments in $HOME/config/userExits'
    grep -v "^#" $HOME/config/userExits
  print ' '
  print 'cmvccfg ---------------------------------------- End of cmvccfg'

exit 0

# end of file
