#!/bin/bash

#------------------------------------------------------------------------
# September, 2014, Balaji Krishnamoorthy
# Copyright (c) 2014-2016, 2019 by Cisco Systems, Inc.
# All Rights Reserved
#------------------------------------------------------------------------

MYADDR=$(ifconfig eth-vf1.3073 | grep 'inet addr:' | awk '{print $2}' | awk -F: '{print $2}')

CONFDADDR=$(ds_test_client -l confd6 | tail -1 | awk -F, '{print $2}' | awk -F= '{print $2}')

if [[ "$MYADDR" != "$CONFDADDR" ]]; then
    echo -n "Confd not active on this node: Run this command from VRF 0 of " >& 2
    H=$(ssh -q $CONFDADDR hostname)
    echo "$H" >& 2
    exit 1
fi

FBLOCKS="CCS DCH DCMA DCMB DCl ECI FMAC RTP"

find /tmp -maxdepth 1 -cmin -10 | grep 'show\.platform' >& /dev/null
if [[ $? -ne 0 ]]; then
    show_cmd "show platform | save /tmp/show.platform" > /dev/null
fi

touch /tmp/show.platform

show_cmd "show controller fabric plane all statistics detail | save /root/plane_statistics.log" >& /dev/null
show_cmd "show controller fabric link port s3 rx statistics detail | save /root/s3rx_statistics.log" >& /dev/null
show_cmd "show controller fabric link port s2 rx statistics detail | save /root/s2rx_statistics.log" >& /dev/null
show_cmd "show controller fabric link port s1 rx statistics detail | save /root/s1rx_statistics.log" >& /dev/null

#
# Collect Falafel information 
#

find /tmp -maxdepth 1 -cmin -10 | grep 'show\.chassis' >& /dev/null
if [[ $? -ne 0 ]]; then
    show_cmd "show chassis | save /tmp/show.chassis" >& /dev/null
fi
 
touch /tmp/show.chassis

for rack in $(grep '\<[FL]CC\>' /tmp/show.chassis | awk '{print $2}'); do

    if [[ $rack =~ ^[0-9] ]]; then
        let I=4;
        let F=6;
    else
        let I=3;
        let F=3;
    fi

    find /tmp -maxdepth 1 -cmin -10 | grep 'sfe\.driver\.${rack}' >& /dev/null
    if [[ $? -ne 0 ]]; then
        show_cmd "show controller sfe driver rack $rack | save /tmp/sfe.driver.${rack}" >& /dev/null
    fi
 
    touch /tmp/sfe.driver.${rack}

    driver_rack_slot=$(grep '^Rack: ' /tmp/sfe.driver.${rack} | awk '{print $2}' | tr -d ',')
    driver_ip=$(grep '^Rack: ' /tmp/sfe.driver.${rack} | awk '{print $NF}')
    for fabrackslot in $(grep "^${rack}\/FC" /tmp/show.platform | awk '{print $1}'); do
        log=${fabrackslot#${rack}/FC}
        for (( inst = 0; inst < $I; inst++ )); do
            fabrs=$(echo $fabrackslot | tr '/' '_')
            unit=$((log * F + inst))
            show_cmd "show asic-error SFE $unit all location $driver_rack_slot | save /root/${fabrs}_${inst}_falafel_asic_err.log" >& /dev/null
            show_cmd "show controller sfe link-info rx 0 127 flap instance ${inst}  location $fabrackslot | save /root/${fabrs}_${inst}_falafel_link_flap.log" >& /dev/null
            show_cmd "show controller sfe diagshell ${inst} \"diag count\" location $fabrackslot | save /root/${fabrs}_${inst}_falafel_diag_count.log" >& /dev/null
            # show_cmd "show controller sfe diagshell ${inst} \"log file=/root/${fabrs}_${inst}_falafel_diag_queue.log on\" location $fabrackslot" >& /dev/null
            show_cmd "show controller sfe diagshell ${inst} \"diag queue\" location $fabrackslot | save /root/${fabrs}_${inst}_falafel_diag_queue.log" >& /dev/null
            # show_cmd "show controller sfe diagshell ${inst} \"log file=/root/${fabrs}_${inst}_falafel_diag_queue.log off\" location $fabrackslot" >& /dev/null
            # if [[ "$driver_ip" != "$MYADDR" ]]; then
            #     scp root@$driver_ip:/root/${fabrs}_${inst}_falafel_diag_queue.log /root >& /dev/null
            #     ssh -q $driver_ip rm -f /root/${fabrs}_${inst}_falafel_diag_queue.log >& /dev/null
            # fi

            for b in $FBLOCKS; do
                show_cmd "show controller sfe statistics block $b instance $inst location $fabrackslot | save /root/${fabrs}_${inst}_falafel_${b}_counters.log" >& /dev/null
            done
            echo -n "."
        done
    done


    for lcrackslot in $(grep "^${rack}\/[0-9][0-9]*"  /tmp/show.platform | awk '{print $1}'); do
        templink="${lcrackslot}/0/0"
        cardip=$(parselink -i $templink)
        slot=${lcrackslot#${rack}/}
        nodeid=$(printf "0x%02x%02x00" $rack $slot)
        lcrs=$(echo $lcrackslot | tr '/' '_')
        for (( inst = 0; inst < 5; inst++ )); do
            ssh $cardip /pkg/bin/asic_errors_show -n ${nodeid} -a 0x4 -i 0x${inst} -e 0x7 -s -t 0x0 -m 0x0 > /root/${lcrs}_${inst}_fia_asic_err.log
            ssh $cardip /pkg/bin/fia_driver_show -l r -e f -b 0x0 -f 0x23 -i 0x${inst} -n ${nodeid} -p 0xffffffff -a brief > /root/${lcrs}_${inst}_fia_link_flap.log
            ssh $cardip /pkg/bin/fia_driver_show -s x -b 0x24 -f 0x24 -i 0x${inst} -n ${nodeid} -t n -p 0xffffffff > /root/${lcrs}_${inst}_fia_link_flap.log
            echo -n "."
        done
    done
done

echo "."
tar cvfz /root/fabric_counters.tgz /root/*.log


