#!/pkg/bin/ksh
# Created by Noritake Izaiku Jan 2008
# Copyright (c) 2008-2009 by cisco Systems, Inc.
# All rights reserved.
#
#----------------------------------------------------------------------

# This script fragment contains the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment

# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands; showtech infra expects the
# command exactly the way it was entered.
# Use "\" ahead of any special character (eg. |) used in the command line.

# Maximum number of netflow processes
#   nfmgr, nfma, nfea, nfsvr, nf_producer
#
NF_MAX_PROCESS_NUM=5

#
#
#
nf_init() {
    echo "Preparing show tech-support netflow"
    # echo "Getting process and trace names."
    trace_cnt=0
    if [ "$in_proc_flag" != "set" ]; then
        proc_flag="all"
        proc_name[0]="nfmgr"
        proc_name[1]="nfma"
        proc_name[2]="nfea"
        proc_name[3]="nfsvr"
        if [ "$platform_type" == "HFR" ]; then
            proc_name[4]="pse_fam"
        else
            proc_name[4]="nf_producer"
        fi
        proc_name[5]=""
        trc_names="mgr ma ea server worker platform producer "
        trace_cnt=1
    else
        proc_flag="set"
        if [ "${in_p[0]}" != "no" ]; then
            proc_name[$trace_cnt]="nfmgr"
            trc_name[$trace_cnt]="mgr"
            trace_cnt=$(($trace_cnt + 1));
        fi
        if [ "${in_p[1]}" != "no" ]; then
            proc_name[$trace_cnt]="nfma"
            trc_name[$trace_cnt]="ma"
            trace_cnt=$(($trace_cnt + 1));
        fi
        if [ "${in_p[2]}" != "no" ]; then
            proc_name[$trace_cnt]="nfea"
            trc_name[$trace_cnt]="ea"
            trace_cnt=$(($trace_cnt + 1));
        fi
        if [ "${in_p[3]}" != "no" ]; then
            proc_name[$trace_cnt]="nfsvr"
            trc_name[$trace_cnt]="server worker"
            trace_cnt=$(($trace_cnt + 1));
        fi
        if [ "${in_p[4]}" != "no" ]; then
            if [ "$platform_type" == "HFR" ]; then
                proc_name[$trace_cnt]="pse_fam"
            else
                proc_name[$trace_cnt]="nf_producer"
            fi
            trc_name[$trace_cnt]="platform producer"
            trace_cnt=$(($trace_cnt + 1));
        fi

        proc_name[5]=""
        trc_name[5]=""

        trc_names="${trc_name[0]} ${trc_name[1]} ${trc_name[2]} ${trc_name[3]} ${trc_name[4]}"
    fi

    # echo "Getting platform information."
    platform_cmd='ls /pkg/bin 2> /dev/null | grep hfr'

    if [ "$platform_cmd" = "" ]; then
        #
        # If commands doesn't exist, then we load this command as C12K.
        #
        platform_cmd="show_platform"
        platform_cmd_op=""
        platform_type="C12K"
    else
        platform_cmd="shelfmgr_show_hfr"
        platform_cmd_op="-e"
        platform_type="HFR"
    fi


    # echo "Getting location names and node ids."
    # "location" is set by "default_parser_function"
    if [ "$location" = "all" ]; then
        loc_flag="all"
        platforms=`$platform_cmd $platform_cmd_op | grep -e "CPU" | grep -v "UNKNOWN" | cut -c -16`
        i=0; for location_var in $platforms; do
            locations[i]="$location_var"
            nodename_var=`node_conversion -N ${locations[i]}`
            nodeids[i]=`node_conversion -i $nodename_var`
            i=$(($i + 1))
        done
        locations[i]=""
        nodeids[i]=""
    else
        loc_flag="set"
        locations[0]="$location"
        locations[1]=""
        nodename_var=`node_conversion -N ${locations[0]}`
        nodeids[0]=`node_conversion -i $nodename_var`
        nodeids[1]=""
    fi

    #echo "Getting process ids."
    i=0;k=0; while [ "${nodeids[i]}" ]; do
        j=0;while [ "${proc_name[j]}" ]; do
            pids[k]=`sysmgr_show -o -p ${proc_name[j]} -n ${nodeids[i]} | grep -e PID | cut -c 27-`
            k=$(($k + 1))
            j=$(($j + 1))
        done
        i=$(($i + 1))
    done

    #echo "Getting flow exporter-map configuration."
    femall=`nvgen -c -l 1 -t 1 -o 1 2> /dev/null | grep -E "flow exporter-map" | cut -c 19-`
    #echo "Getting flow monitor-map configuration."
    fmmall=`nvgen -c -l 1 -t 1 -o 1 2> /dev/null | grep -E "flow monitor-map" | cut -c 18-`
    #echo "Getting sampler-map configuration."
    fsmall=`nvgen -c -l 1 -t 1 -o 1 2> /dev/null | grep -E "sampler-map" | cut -c 13-`
    #echo "Preparing done"

    #echo "Confirm variables"
    #i=0; while [ "${locations[i]}" ]; do
    #echo "LOCATION :${locations[i]}/NODEID :${nodeids[i]}"
    #    i=$(($i + 1))
    #done
    #echo "PRC NAME : ${proc_name[0]} ${proc_name[1]} ${proc_name[2]} ${proc_name[3]} ${proc_name[4]} "
    #echo "PID      : ${pids[0]} ${pids[1]} ${pids[2]} ${pids[3]} ${pids[4]} "
    #echo "TRC NAMES: $trc_names (type=$trace_cnt)"
    #echo "$femall"
    #echo "$fmmall"
    #echo "$fsmall"

}

#
# Common commands
#
nf_command_common() {
    commandCommon[0]='show version'
    commandCommon[1]='show install active'
    commandCommon[2]='show running-config'
    commandCommon[3]=''

    run_commands Common
}

#
# show process <process name> location <location name>
#
nf_show_process() {
    i=0;
    if [ "$loc_flag" = "set" ]; then
        commandProcess[i]='show processes blocked location '${locations[0]}
        i=$(($i + 1))
        j=0; while [ "${proc_name[j]}" ]; do
            commandProcess[i]='show processes '${proc_name[j]}' location '${locations[0]}
            i=$(($i + 1))
            j=$(($j + 1))
        done
    else
        commandProcess[i]='show processes blocked location all'
        i=$(($i + 1))
        j=0; while [ "${proc_name[j]}" ]; do
            commandProcess[i]='show processes '${proc_name[j]}' location all'
            i=$(($i + 1))
            j=$(($j + 1))
        done
    fi
    commandProcess[i]=''

    run_commands Process
}

#
# run attach_process -p <process id> -n <node id> -v -i 1
#
nf_attach_process() {
    echo "------------------------------- attach_process --------------------------------"

    i=0; k=0; while [ "${locations[i]}" ]; do

        echo "------------------------------------------------------------"
        echo "Location : ${locations[i]}"
        echo "------------------------------------------------------------"
        
        j=0; while [ "${proc_name[j]}" ]; do
            if [ "${pids[k]}" ]; then
                echo "----------------------------------------"
                echo "Process : ${proc_name[j]}"
                echo "----------------------------------------"
                attach_process -p ${pids[k]} -n ${nodeids[i]} -v -i 1
            fi
            k=$(($k + 1))
            j=$(($j + 1))
        done
        i=$(($i + 1))
    done
}

#
# show flow exporter-map <exporter-map name>
# show flow monitor-map <monitor-map name>
# show samplerr-map <smapler-map name>
#
nf_show_flow_map() {
    i=0;

    for femname in $femall; do
        commandFlowmap[i]='show flow exporter-map '$femname
        i=$((i + 1))
    done
    for fmmname in $fmmall; do
        commandFlowmap[i]='show flow monitor-map '$fmmname
        i=$((i + 1))
    done
    for fsmname in $fsmall; do
        commandFlowmap[i]='show sampler-map '$fsmname
        i=$((i + 1))
    done
    commandFlowmap[i]=''

    run_commands Flowmap
}


#
# show flow exporter <exporter-map name> location <location name>
#
nf_show_flow_fem() {
    i=0; j=0; while [ "${locations[i]}" ]; do
        for femname in $femall; do
            commandFlowexp[j]='show flow exporter '$femname' location '${locations[i]}
            j=$(($j + 1))
        done
        i=$(($i + 1))
    done
    commandFlowexp[i]=''

    run_commands Flowexp
}


#
# show flow monitor <monitor-map name> location <location name>
#
nf_show_flow_fmm() {
    i=0;j=0; while [ "${locations[i]}" ]; do
        for fmmname in $fmmall; do
            commandFlowmon[j]='show flow monitor '$fmmname' location '${locations[i]}
            j=$(($j + 1))
        done
        i=$(($i + 1))
    done
    commandFlowmon[j]=''

    run_commands Flowmon
}


#
# show flow monitor <monitor-map name> cache location <location name>
#
nf_show_flow_fmm_cache() {
    i=0;j=0; while [ "${locations[i]}" ]; do
        for fmmname in $fmmall; do
            commandFlowmoncache[j]='show flow monitor '$fmmname' cache location '${locations[i]}
            j=$(($j + 1))
        done
        i=$(($i + 1))
    done
    commandFlowmoncache[j]=''

    run_commands Flowmoncache
}


#
# show flow internal mgr location <location name>
#
nf_show_flow_internal() {
    commandFlowinternal[0]='show flow internal mgr'
    commandFlowinternal[1]=''

    run_commands Flowinternal
}


#
# show flow platform producer statistics location <location name>
# show flow platform pse policer-rate location <location name>
#
nf_show_flow_platform() {
    j=0; 
    i=0;while [ "${locations[i]}" ]; do
        commandFlowplatform[j]='show flow platform producer statistics location '${locations[i]}
        j=$(($j + 1))
        i=$(($i + 1))
    done
    i=0; while [ "${locations[i]}" ]; do
        commandFlowplatform[j]='show flow platform pse policer-rate location '${locations[i]}
        j=$(($j + 1))
        i=$(($i + 1))
    done
    commandFlowplatform[j]=''

    run_commands Flowplatform
}


#
# show flow trace <process names> location <location name>
#
nf_show_flow_trace() {
    i=0;
    if [ "$loc_flag" = "all" ]; then
        if [ "$trace_cnt" = "$NF_MAX_PROCESS_NUM" ]; then
            while [ "${trc_name[i]}" ]; do
                commandTrace[i]='show flow trace '${trc_name[i]}' location all'
                i=$(($i + 1))
            done
        else
            if [ "$proc_flag" = "all" ]; then
                commandTrace[i]='show flow trace all location all'
                i=$(($i + 1))
            else
                commandTrace[i]='show flow trace '$trc_names' location all'
                i=$(($i + 1))
            fi
        fi
    else
        if [ "$trace_cnt" = "$NF_MAX_PROCESS_NUM" ]; then
            while [ "${trc_name[i]}" ]; do
                commandTrace[i]='show flow trace '${trc_name[i]}' location '${locations[0]}
                i=$(($i + 1))
            done
        else
            if [ "$proc_flag" = "all" ]; then
                commandTrace[i]='show flow trace all location '${locations[0]}
                i=$(($i + 1))
            else
                commandTrace[i]='show flow trace '$trc_names' location '${locations[0]}
                i=$(($i + 1))
            fi
        fi
    fi

    commandTrace[i]=''

    run_commands Trace

}


#
# show im trace operatoin all
#
nf_show_im_trace() {
    echo "-------------------- show im trace operation all location <> ------------------"

    i=0; while [ "${locations[i]}" ]; do

        echo "------------------------------------------------------------"
        echo "Location : ${locations[i]}"
        echo "------------------------------------------------------------"
        im_show_trace -C -P -A -D -R -G -i ${nodeids[i]} | grep netflow

        i=$(($i + 1))
    done

}

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
  case "$1" in
     -P) in_p[0]="$2"; in_p[1]="$3"; in_p[2]="$4"; in_p[3]="$5"; in_p[4]="$6"; in_p[5]=""; in_proc_flag="set"; shift 6;;
     *)  default_parser_function "$@"; shift $#;;
  esac
#  echo "mgr=${setp[0]} ma=${setp[1]} ea=${setp[2]} pd=${setp[3]} sv=${setp[4]} location=$location."
done

# A function called display() must be provided that calls the functions to 
# run the required show commands. The display() function is called in 
# /pkg/bin/show_tech_comp_file_frag

display() {

    # Print the output heading 
    print_main_heading "show tech-support netflow"


    # Initialize input parameters
    nf_init

    # Print the specific commands
    nf_command_common
    nf_show_process
    nf_attach_process
    nf_show_flow_map
    nf_show_flow_fem
    nf_show_flow_fmm
    nf_show_flow_fmm_cache
    nf_show_flow_internal
    if [ "$platform_type" = "HFR" ]; then
        nf_show_flow_platform
    fi
    nf_show_flow_trace   
    nf_show_im_trace

    # Print the closing heading. 
    print_main_heading "show tech-support netflow complete"
}


# This function calls the display() function and sends the output to file if
# the file option has been set. 
. /pkg/bin/show_tech_file_fragment
