#! /pkg/bin/ksh

###
### March 2004, michael shiplett
###
### Copyright (c) 2004 by cisco Systems, Inc.
### All rights reserved.
###
### Gather failover data. Somewhat useful for general
### IS-IS data gathering as well.
###
### usage
###     fod [-i]
### options
###     i    gather only is-is failover information
###

# Ordered list of possible logging root directories with
# preferred directories first.
LOG_ROOTS="/harddisk: /disk0: /disk1:"

# Use first existing log root directory. Error conditions
# are checked later.
for LOG_ROOT in $LOG_ROOTS
do
    if [ -d $LOG_ROOT ]
    then
        break
    fi
done

# Log ouput directory. Will be created or emptied each run.
LOG_DIR="$LOG_ROOT/pm"


###
### Function
###     error -- print an error message and exit
###
### Arguments
###     1 -- error message
###
### Returns
###     nothing. Exits script.
###
error () {
    err_msg="$1"
    echo "$err_msg" 2>&1
    exit 1
}


###
### Function
###     isis_ltrace -- grab all the is-is ltrace logs for each instance
###
### Arguments
###     1 -- log directory in which to store the output
###
### Returns
###     nothing
###
isis_ltrace () {
    LOG_DIR="$1"

    LTRACE_LOG_DIR=/dev/shmem/ltrace/isis

    if [ -d $LTRACE_LOG_DIR ]
    then
        cd $LTRACE_LOG_DIR && for instance in *
        do
            isis_show_ltrace -I $instance -A -B -C \
                1>"$LOG_DIR"/isis-$instance-trace 2>&1
        done
    else
        echo "$LTRACE_LOG_DIR is not a directory" \
            1>"$LOG_DIR"/isis-trace 2>&1
    fi
}


# use normal process priority
setprio 10

# option processing
while getopts i c
do
    case $c in
    i)    ISIS_ONLY=1 ;;
    esac
done
shift $(($OPTIND - 1))


# prepare for logging
if [ x"$LOG_ROOT" == x ]
then
    error "LOG_ROOT variable is not set or is empty. Exiting."
elif [ ! -d "$LOG_ROOT" ]
then
    error "The root directory $LOG_ROOT does not exist. Exiting."
elif [ x"$LOG_DIR" == x ]
then
    error "LOG_DIR variable is not set or is empty. Exiting."
elif [ -f "$LOG_DIR" ]
then
    error "$LOG_DIR exists as a file. Expected a directory. Exiting."
elif [ -d "$LOG_DIR" ] 
then
    # empty existing directory
    rm -rf "$LOG_DIR"
fi

mkdir "$LOG_DIR"
cd "$LOG_DIR"

# show clock
iosclock -d 0x0                                       \
    1>"$LOG_DIR"/time-start 2>&1

# show isis instance INSTANCE ltrace
isis_ltrace "$LOG_DIR"

# show logging
show_logging                                          \
    1>"$LOG_DIR"/logging 2>&1

# show isis all
isis_show -a -d -v -B -J 0x0 -K 0x0                   \
    1>"$LOG_DIR"/isis-all 2>&1

if [ x"$ISIS_ONLY" != x1 ]
then
    # show rib trace
    show_ipv4_rib_ltrace -X 0x1 -Y 0x1 -Z ________    \
        1>"$LOG_DIR"/rib-trace 2>&1

    # show rib client redist hist
    show_ipv4_rib -X 0x1 -R                           \
        1>"$LOG_DIR"/rib-client-trace 2>&1

    # show bcdl trace ipv4_rib all-nodes
    show_ltrace_bcdl -B ipv4_rib -A                   \
        1>"$LOG_DIR"/bcdl-trace 2>&1

    # show cef ipv4 trace
    show_ltrace_ipv4_fib                              \
        1>"$LOG_DIR"/cef-trace 2>&1

    # show static trace
    ipv4_static_show -A 0x1 -L -- LTRACE_OPTIONS      \
        1>"$LOG_DIR"/static-trace 2>&1
fi

# show run
nvgen -c -l 1 -t 1 -o 1                               \
    1>"$LOG_DIR"/run-cf 2>&1

# show clock
iosclock -d 0x0                                   \
    1>"$LOG_DIR"/time-end 2>&1

exit 0
