#!/bin/csh -f
#
# Copyright (c) 1998, 2005, Oracle. All rights reserved.  
#
#   NAME
#     probedev
#
#   DESCRIPTION
#     Produce diagnostic information for device failure analysis
#
#   MODIFIED   (MM/DD/YY) 
#   rvullika   07/12/05 - Remove references to reliaty support email/phone 
#                         number (bug 4486205)
#   tdziedzi   05/18/05 - Product name change 
#   jwadleig   04/29/05 - add solaris64.. missing from arch list
#   rvullika   10/18/04 - Cleanup unsupported architectures
#   rvullika   10/12/04 - replace linux86-lfs with linux32
#   tdziedzi   05/18/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#     05/31/02 jrw  remove rbl ref.  Add rbtool.
#     12/03/01 gms  v3 mods.
#     06/27/01 jrw  support linux86-lfs.
#     10/11/00 gms  use prtconf -v for solaris.
#     08/24/00 gms  support linux, solv9.
#     09/21/99 gms  pick up database files of form <host>:<dev>
#     12/14/98 gms  removed superfluous 'set echo'.
#     09/17/98 gms  fixed grep for local db dir; show rbconfig contents.
#     09/08/98 gms  look for /tmp/make* (scripts from makedev).
#     09/02/98 gms  original coding.

    set me   = "probedev"

#   Usage:
#
#       $me [-h | ?] [-x] [-y] [<devicefile>]
#
#   <devicefile>      pathname of devicefile (if it exists)
#   -h, ?             print help
#   -x                turns on echo
#   -y                trace install routines

#####################################################
#
#   help requested?...

set tmp = /tmp/foo$$
echo "$argv" >$tmp
grep  "\-h" $tmp >&/dev/null
if ($status == 0) goto usage
grep  "\?" $tmp >&/dev/null
if ($status == 0) then
usage:
    echo "" 
    echo "$me is a diagnostic script that collects device- and"
    echo "driver-related information to aid in diagnosing problems"
    echo "encountered during Oracle Secure Backup device configuration."
    echo "$me takes a few seconds to run and must be run as root."
    echo "When it completes, you will be asked to forward the output"
    echo "to the support staff at Oracle."
    echo ""
    echo "Usage:"
    echo "      $me [<option>...] [<device-pathname>]"
    echo ""
    echo "      <option>:"
    echo "          -h, ?             print usage."
    echo "          -x                turn on echo."
    echo "          -y                trace calls through subscripts."
    echo "      <device-pathname>     Optional device file to test."
    echo "" 
    rm -f $tmp
    exit (1)
    endif
rm -f $tmp

############################################################
#
#   perform common initialization...

    setenv OBTRACE 0
    if (! $?installobinit) then
        if (-f initinstall.sh) then
            source initinstall.sh
            set st = $status
        else if (-f install/initinstall.sh) then
            source install/initinstall.sh
            set st = $status
        else
            echo "ERROR: No initinstall.sh program exists in either '.' or './install'.  Can't continue."
            goto failed
            endif

        if ($st) exit ($st)
        endif

    set path = (. install /usr/ucb /usr/sbin /usr/bin /sbin /bin /usr/etc /etc /usr/bsd)
    set tmp  = ${OB_TMP}/obt$$
    set out  = ${OB_TMP}/${me}.out
    set tmpargs = ($argv)
    unset devarg

#####################################################
#
#   process command line arguments...
#

    while ($#argv)
        if ("$1" == "") goto next_arg
        if ("$1" !~ -*) then
            if ($?devarg) then
                echo "only one device file may be specified."
                goto failed
                endif
            set devarg = "$1"
            goto next_arg
            endif
        switch ($1)
            case -x
                set echo
                breaksw
            case -y
                setenv OBTRACE 1
                breaksw
            default :
                echo "unrecognized option: $1"
                goto failed
            endsw
next_arg:
        shift
        end

############################################################
#
#   do call tracing if requested...

    if ($OBTRACE) then
        if (-f trenter) then
            trenter $me $argv
        else if (-f install/trenter) then
            install/trenter $me $argv
            endif
        endif

############################################################
#
#   look for -x (echo) option right away...

    echo $argv | grep "\-x" >&/dev/null
    if ($status == 0) set echo

############################################################
#
#   allow execution from either ob or ob/install...

    if (-f install/$me) then
        set idir = $cwd/install
    else if (-f $me) then
        set idir = $cwd
    else
        unset echo
        echo "ERROR: Your current directory must be either"
        echo "       ob or ob/install to run this script."
        goto failed
        endif

onintr userquit

############################################################
#
#   user must be root...

whoami |& grep root >&/dev/null
if ($status) then
    id |& grep "uid=0" >&/dev/null
    if ($status) then
        echo "You must be logged in as 'root' to run this script.  Please"
        echo "change your user identity to 'root' and re-run this program."
        goto failed
        endif
    endif

############################################################
#
#   dig up info on this machine...

    source $idir/machinfo.sh
    if ($?error) then
        echo "ERROR: can't determine machine information for this host: $error"
        goto failed
        endif

############################################################
#
#   collect info on machine type and os...

    echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >$out
    echo "" >>$out
    echo -n "$me run on " >>$out
    date >>$out
    echo "args: $tmpargs" >>$out
    echo "" >>$out
    echo -n "uname says: " >>$out
    uname -a >>&$out
    echo "" >>$out
    echo "os_name:   $os_name"   >>$out
    echo "os_ver:    $os_ver"    >>$out
    echo "mach_type: $mach_type" >>$out
    echo "mach_arch: $mach_arch" >>$out
    echo "mach_name: $mach_name" >>$out
    echo "" >>$out

    if ($?kernel_leaf) then
        echo "kernel_leaf:    $kernel_leaf" >>$out
        echo "kernel_path:    ${kernel_path}:" >>$out
        ls -l $kernel_path >>&$out
        echo "ob_kernel_path: ${ob_kernel_path}:" >>$out
        ls -l $ob_kernel_path >>&$out
        echo "" >>$out
        endif

############################################################
#
#   hunt down obconfig file and database dir...

    set obconfig = "/etc/obconfig"
    if ($?OBCONFIG) then
        set obconfig = "$OBCONFIG"
    else if (! -r $obconfig && -r ./obconfig) then
        set obconfig = "./obconfig"
        endif

    if (! -r $obconfig) then
        echo "Oracle Secure Backup configuration file $obconfig not found.  To use an alternative"
        echo "    file, set the environment variable OBCONFIG to the pathname of the"
        echo "    Oracle Secure Backup configuration file, then re-run ${me}."
        unset obconfig
    else
        echo "Using Oracle Secure Backup configuration file ${obconfig}..." >>$out
        echo "" >>$out
        cat $obconfig >>$out
        endif
    echo "" >>$out

    set local_db_dir = "/usr/etc/ob"
    if ($?obconfig) then
        set local_db_dir = `grep -i <$obconfig "^[	 ]*local[	 ][	 ]*db[	 ][	 ]*dir:[	 ]"`
        if ($#local_db_dir == 4) then
            set local_db_dir = "$local_db_dir[4]"
            endif
        endif

    if (-d "$local_db_dir") then
        echo "Using local device database directory ${local_db_dir}." >>$out
    else
        echo "No local database directory found." >>$out
        unset local_db_dir
        endif
    echo "" >>$out

############################################################
#
#   show what's installed...

    echo "Installed Oracle Secure Backup components..." >>$out
    obtool -V           >>&$out
    obtar -V            >>&$out
    obt -V              >>&$out
    obi -V              >>&$out
    /etc/observiced -V  >>&$out
    /etc/obscheduled -V >>&$out
    /etc/obrobotd -V    >>&$out
    /etc/obixd -V       >>&$out
    /etc/obndmpd -V     >>&$out
    echo "" >>$out

############################################################
#
#   dump platform-dependent info...

    unset logname
    switch ($mach_arch)

        case hpvxfs :
            echo "Configuration (from ioscan)..." >>$out
            ioscan -fk >>$out
            echo "" >>$out
            echo "Installed device drivers..." >>$out
            echo "" >>$out
            lsdev >>&$out
            echo "" >>$out
            set logname = "/var/adm/syslog/syslog.log"
            breaksw

        case linux32 :
lincom:
            echo "/proc/scsi/scsi..." >>$out
            cat /proc/scsi/scsi >>$out
            echo "" >>$out
            set logname = "dmesg"
            breaksw

        case rs6000 :
            goto rscom
        case rs6000x :
rscom:
            echo "Available SCSI adapters..." >>$out
            lscfg -l "scsi*" >>$out
            echo "" >>$out
            echo "Available SCSI devices..." >>$out
            echo "" >>$out
            lsdev -C -s scsi -H >>$out
            echo "" >>$out
            echo "Methods..." >>$out
            ls -l /etc/methods/*ob >>$out
            echo "" >>$out
            breaksw

        case solaris :
            goto solcom
        case solaris64 :
            goto solcom
        case solv9 :
solcom:
            echo "Configuration (from prtconf)..." >>$out
            echo "" >>$out
            prtconf -v >>$out
            echo "" >>$out

            echo "Devices of interest..." >>$out
            du -a /devices | grep ob >>$out
            echo "" >>$out

            echo "Driver objects..." >>$out
            ls -l /usr/kernel/drv/ob* >>$out
            echo "" >>$out

            echo "Status of ob driver..." >>$out
            modinfo | grep Loadaddr >>$out
            modinfo | grep ob >>$out
            if ($status) echo " (ob not loaded)" >>$out
            echo "" >>$out
            set logname = "/var/adm/messages"
            breaksw

        case sparc :
            set logname = "dmesg"
            breaksw

        default :
            echo "WARNING: ${me}: $mach_arch -- unsupported machine type."
            breaksw
        endsw

############################################################
#
#   show any existing ob devices...

    echo "Existing ob devices..." >>$out
    ls -l /dev/*ob* >>&$out
    echo "" >>$out

############################################################
#
#   if there are any device database files, dump them...

    if ($?local_db_dir) then
        ls $local_db_dir/rws[ht]? >&$tmp
        grep match <$tmp >&/dev/null
        if ($status) then
            set dbs = (`cat $tmp`)
        else
            set dbs = ()
            endif

        ls $local_db_dir/*:* >&$tmp
        grep match <$tmp >&/dev/null
        if ($status) then
            set dbs = ($dbs `cat $tmp`)
            endif

        if ($#dbs > 0) then
            echo "Device database files..." >>$out
            echo "" >>$out
            foreach db ($dbs)
                echo "${db}:" >>$out
                cat $db >>$out
                echo "" >>$out
                end
        else
            echo "No device database files found." >>$out
            echo "" >>$out
            endif
        endif

############################################################
#
#   dump any makedev scripts...

    ls ${OB_TMP}/makedev* >&$tmp
    grep match <$tmp >&/dev/null
    if ($status) then
        set dbs = (`cat $tmp`)
    else
        set dbs = ()
        endif
    if ($#dbs > 0) then
        echo "Makedev runs..." >>$out
        echo "" >>$out
        foreach db ($dbs)
            echo "${db}:" >>$out
            cat $db >>$out
            echo "" >>$out
            end
    else
        echo "No makedev leftovers found." >>$out
        echo "" >>$out
        endif
    endif

############################################################
#
#   if a device was specified, see what obi reports...

    if ($?devarg) then
        setenv WST_DDB 80f
        echo "obi $devarg -l" >>$out
        obi $devarg -l >>&$out
        echo "" >>$out
        echo "Error log for ${devarg}..." >>$out
        obi $devarg -du >>&$out
        echo "" >>$out
        unsetenv WST_DDB
        endif

############################################################
#
#   finally, if this system has a log, dump a chunk of it...
                                                            
    if ($?logname) then
        echo "${logname}..." >>$out
        if ("$logname" == "dmesg") then
            dmesg >>$out
        else
            tail -1000 $logname >>$out
            endif
        echo "" >>$out
        endif

############################################################
#
#   announce our gratification at getting here...

    echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >>$out
    echo "" >>$out

    echo ""
    echo "The output of this procedure is in ${out}."
    echo "Please email this to Oracle Support'"
    echo "(After sending the file, you may delete it.)"
    echo ""

done:
    if ($?tmp) then
        rm -f $tmp >&/dev/null
        endif
    if ($OBTRACE) then
        $idir/trexit $me $st
        endif

# cat $out

    exit ($st)

failed:
    set st = 1
    goto done

userquit:
    unset echo
    setenv OBTRACE 0
    echo "ERROR: $me interrupted from console."
    set st = 9
    goto done

############################################################

