#! /bin/bash
# Copyright 2017, Cumulus Networks, Inc.  All rights reserved.
#
# Record routing state; used by cl-support
#
#TIMEOUT=90
#DEFAULT
#ONCORE=bgpd,eigrpd,isisd,ldpd,nhrpd,ospf6d,ospfclient,ospfd,pimd,ripd,ripngd,ssd,watchfrr,zebra,pimd

module=${0##*/}
funcs=(frr)
nondef_funcs=(ospftable ospf6table evpntable)
json=
jexec=

func_frr()
{
    exec_cmd frr.show_ver vtysh -c "show version"
    exec_cmd frr.show_debug vtysh -c "show debugging"
    exec_cmd frr.show_log vtysh -c "show logging"

    exec_cmd $jexec ospf vtysh -c "show ip ospf vrf all $json"
    exec_cmd $jexec ospf.neighbor vtysh -c "show ip ospf vrf all neighbor $json"
    exec_cmd $jexec ospf.interface vtysh -c "show ip ospf vrf all interface $json"
    exec_cmd ospf.database vtysh -c "show ip ospf vrf all database"
    exec_cmd ospf.borderrouters vtysh -c "show ip ospf vrf all border-routers"
    exec_cmd ospf.database.router vtysh -c "show ip ospf vrf all database router"
    exec_cmd ospf.vrfs vtysh -c "show ip ospf vrfs"
    exec_cmd ospf.show_running vtysh -c "show running ospfd"

    exec_cmd ospf6 vtysh -c "show ipv6 ospf6"
    exec_cmd ospf6.neighbor vtysh -c "show ipv6 ospf6 neighbor"
    exec_cmd ospf6.interface vtysh -c "show ipv6 ospf6 interface"
    exec_cmd ospf6.database vtysh -c "show ipv6 ospf6 database"
    exec_cmd ospf6.redistribute vtysh -c "show ipv6 ospf6 redistribute"
    exec_cmd ospf6.spf.tree vtysh -c "show ipv6 ospf6 spf tree"
    exec_cmd ospf6.database.router vtysh -c "show ipv6 ospf6 database router"
    exec_cmd ospf6.route_summary vtysh -c "show ipv6 ospf6 route summary"
    exec_cmd ospf6.show_running vtysh -c "show running ospf6d"

    exec_cmd bgp.show_running vtysh -c "show running bgpd"
    exec_cmd $jexec bgp.summary vtysh -c "show ip bgp vrf all summary $json"
    exec_cmd $jexec bgp.neighbors vtysh -c "show ip bgp vrf all neighbors $json"
    exec_cmd bgp.nexthop vtysh -c "show ip bgp vrf all nexthop"
    exec_cmd bgp.update_group vtysh -c "show ip bgp vrf all update-group"
    exec_cmd bgp.route vtysh -c "show ip bgp vrf all"
    exec_cmd $jexec bgp.vrfs vtysh -c "show bgp vrfs $json"

    exec_cmd $jexec bgp.ipv6_summary vtysh -c "show bgp vrf all ipv6 uni summary $json"
    exec_cmd bgp.ipv6_update_group vtysh -c "show bgp vrf all ipv6 uni update-group"
    exec_cmd $jexec bgp.ipv6_route vtysh -c "show bgp vrf all ipv6 uni $json"

    exec_cmd $jexec bgp.ipv4_vpn_route_leak vtysh -c "show bgp vrf all ipv4 unicast route-leak $json"
    exec_cmd $jexec bgp.ipv6_vpn_route_leak vtysh -c "show bgp vrf all ipv6 unicast route-leak $json"
    exec_cmd bgp.ipv4_vpn_all vtysh -c "show bgp ipv4 vpn all"
    exec_cmd bgp.ipv6_vpn_all vtysh -c "show bgp ipv6 vpn all"

    exec_cmd frr.show_running vtysh -c "show running-config"

    exec_cmd zebra.vrf vtysh -c "show vrf"
    exec_cmd zebra.vrf.vni vtysh -c "show vrf vni"
    exec_cmd zebra.intf vtysh -c "show interface vrf all"
    exec_cmd zebra.nht vtysh -c "show ip nht vrf all"
    exec_cmd zebra.ipv6_nht vtysh -c "show ipv6 nht vrf all"
    exec_cmd zebra.route vtysh -c "show ip route vrf all"
    exec_cmd zebra.ipv6_route vtysh -c "show ipv6 route vrf all"
    exec_cmd zebra.client vtysh -c "show zebra client"
    exec_cmd zebra.table_summary vtysh -c "show zebra router table summary"

    exec_cmd $jexec pim.upstream vtysh -c "show ip pim vrf all upstream $json"
    exec_cmd $jexec pim.join vtysh -c "show ip pim vrf all join $json"
    exec_cmd $jexec pim.mroutes vtysh -c "show ip mroute vrf all $json"
    exec_cmd $jexec pim.interfaces vtysh -c "show ip pim vrf all interface detail $json"
    exec_cmd $jexec pim.state vtysh -c "show ip pim vrf all state $json"
    exec_cmd $jexec pim.neighbors vtysh -c "show ip pim vrf all neighbor $json"
    exec_cmd $jexec pim.mlag.summary vtysh -c "show ip pim mlag summary $json"
    exec_cmd $jexec pim.mlag.upstream vtysh -c "show ip pim vrf all mlag upstream $json"
    exec_cmd $jexec pim.mlag.interface vtysh -c "show ip pim mlag interface detail $json"
    exec_cmd $jexec pim.vxlan.sg vtysh -c "show ip pim vxlan-groups $json"
    exec_cmd $jexec pim.igmp.group vtysh -c "show ip igmp vrf all group $json"
    exec_cmd pim.igmp.join vtysh -c "show ip igmp vrf all join"
    exec_cmd $jexec pim.igmp.int vtysh -c "show ip igmp vrf all int detail $json"
    exec_cmd $jexec pim.rp.info vtysh -c "show ip pim vrf all rp-info $json"
    exec_cmd $jexec pim.rpf vtysh -c "show ip pim vrf all rpf $json"
    exec_cmd pim.multicast vtysh -c "show ip multicast vrf all"
    exec_cmd $jexec pim.msdp.peer vtysh -c "show ip msdp vrf all peer $json"
    exec_cmd $jexec pim.msdp.sa vtysh -c "show ip msdp vrf all sa detail $json"
    exec_cmd $jexec pim.msdp.mesh vtysh -c "show ip msdp vrf all mesh-group $json"

    # NOTE - Don't enable json version of these cmds without scale testing.
    # json version was removed because these cmds were leading to memory pileup on scale
    exec_cmd $jexec evpn.vni.detail vtysh -c "show evpn vni detail $json"
    exec_cmd $jexec evpn vtysh -c "show evpn $json"
    exec_cmd $jexec evpn.arp_nd_redirect vtysh -c "show evpn arp-nd-redirect $json"
    exec_cmd $jexec evpn.es.detail vtysh -c "show evpn es detail $json"
    exec_cmd $jexec evpn.es_evi.detail vtysh -c "show evpn es-evi detail $json"
    exec_cmd $jexec evpn.l2_nh vtysh -c "show evpn l2-nh $json"
    exec_cmd evpn.mac.vni vtysh -c "show evpn mac vni all"
    exec_cmd evpn.arp_cache.vni vtysh -c "show evpn arp-cache vni all"
    exec_cmd $jexec evpn.rmac.vni vtysh -c "show evpn rmac vni all $json"
    exec_cmd $jexec evpn.nexthop.vni vtysh -c "show evpn next-hops vni all $json"
    exec_cmd $jexec bgp.evpn.summary vtysh -c "show bgp l2vpn evpn summary $json"
    exec_cmd $jexec bgp.evpn.es.detail vtysh -c "show bgp l2vpn evpn es detail $json"
    exec_cmd $jexec bgp.evpn.es_evi.detail vtysh -c "show bgp l2vpn evpn es-evi detail $json"
    exec_cmd $jexec bgp.evpn.es_vrf vtysh -c "show bgp l2vpn evpn es-vrf $json"
    exec_cmd $jexec bgp.evpn.vni vtysh -c "show bgp l2vpn evpn vni $json"
    exec_cmd bgp.evpn.route vtysh -c "show bgp l2vpn evpn route"
    exec_cmd bgp.evpn.route.vni.all vtysh -c "show bgp vni all"
    exec_cmd bgp.evpn.route.mac_ip_evi_es vtysh -c "show bgp l2vpn evpn route mac-ip-evi-es"
    exec_cmd bgp.evpn.route.mac_ip_global_es vtysh -c "show bgp l2vpn evpn route mac-ip-global-es"
    exec_cmd $jexec bgp.evpn.vrfimportrt vtysh -c "show bgp l2vpn evpn vrf-import-rt $json"
    exec_cmd $jexec bgp.evpn.importrt vtysh -c "show bgp l2vpn evpn import-rt $json"
    exec_cmd bgp.evpn.update_group vtysh -c "show bgp l2vpn evpn update-groups"
    exec_cmd bgp.mac_hash vtysh -c "show bgp mac hash"

    exec_cmd $jexec pbr.map vtysh -c "show pbr map"
    exec_cmd $jexec pbr.intf vtysh -c "show pbr interface"
    exec_cmd $jexec pbr.nhop.group vtysh -c "show pbr nexthop-groups"

    exec_cmd vrrp.summary vtysh -c "show vrrp"

    exec_cmd frr.thread_cpu vtysh -c "show thread cpu"
    exec_cmd frr.thread_poll vtysh -c "show thread poll"
    exec_cmd frr.debug_hashtable vtysh -c "show debug hashtable"
    exec_cmd frr.work_queues vtysh -c "show work-queues"
    exec_cmd frr.memory vtysh -c "show memory"
    exec_cmd frr.modules vtysh -c "show modules"
    exec_cmd evpn.local_mac.all  vtysh -c "show evpn local-mac"

    sed -i -e 's/authentication-key [^ ]*/ authentication-key SSSS/' -e '/password/s/ [^ ]*$/ SSSS/' -e '/message-digest/s/md5 [^ ]*/md5 SSSS/' -e '/authentication string/s/ [^ ]*$/ SSSS/' -e '/key-string/s/ [^ ]*$/ SSSS/' *.show_running

    [ $SUP_SECURE -eq 0 ] && {
        [ $SUP_VERBOSE -eq 1 ] &&
            date +"%F_%T.%N: ${FUNCNAME[0]}: copy.vtysh.history files to archive" 1>&2
        for f in /home/* /root; do
            [ -e ${f}/.history_frr ] &&
                cp -a ${f}/.history_frr ${f##*/}.vtysh.history
        done
     }
}

func_ospftable()
{
    exec_cmd $jexec ospf.route vtysh -c "show ip ospf vrf all route $json"
    exec_cmd $jexec ospf.neighbor_detail vtysh -c "show ip ospf vrf all neighbor detail $json"
    exec_cmd ospf.database.network vtysh -c "show ip ospf vrf all database network"
    exec_cmd ospf.database.external vtysh -c "show ip ospf vrf all database external"
    exec_cmd ospf.database.max_age vtysh -c "show ip ospf vrf all database max-age"
    exec_cmd ospf.interface.traffic vtysh -c "show ip ospf vrf all interface traffic"
}

func_ospf6table()
{
    exec_cmd ospf6.database_detail vtysh -c "show ipv6 ospf6 database detail"
    exec_cmd ospf6.route_detail vtysh -c "show ipv6 ospf6 route detail"
    exec_cmd ospf6.neigbor_detail vtysh -c "show ipv6 ospf6 neighbor detail"
    exec_cmd ospf6.borderrouters_detail  vtysh -c "show ipv6 ospf6 border-routers detail"
    exec_cmd ospf6.linkstate_detail vtysh -c "show ipv6 ospf6 linkstate detail"
    exec_cmd ospf6.interface.traffic vtysh -c "show ipv6 ospf6 vrf all interface traffic"
}

func_evpntable()
{
    # NOTE - Don't enable json version of these cmds without scale testing.
    # json version was removed because these cmds were leading to memory pileup on scale
    exec_cmd bgp.evpn.route.detail vtysh -c "show bgp l2vpn evpn route detail"
    exec_cmd bgp.evpn.route.vni.all.detail vtysh -c "show bgp vni all detail"
}

# main
while getopts "c:jl" Option; do
    case $Option in
    c) [ $SUP_VERBOSE -eq 1 ] && # default submods
        echo ${module}: Invoked for "$OPTARG" core dumps 1>&2 ;;
    j) jexec=-j json=json ;;
    l) echo ${funcs[@]} ';' ${nondef_funcs[@]}
       exit 0 ;;
    *) ;;
    esac
done
shift $((OPTIND - 1))

main()
{
    local -r TIMEFORMAT='%2R seconds' tfile=/run/${module}_funcstime$$
    local secs

    if [ $# -ne 0 ]; then # only run specified sub-modules
        [ $SUP_VERBOSE -eq 1 ] && echo ${module}: run only submodules: $@ 1>&$stderr
        funcs=($@)
    fi

    for func in ${funcs[@]}; do
        [ $SUP_VERBOSE -eq 1 ] && echo "$module.$func" 1>&2
        date +"### $module.$func Started at %F-%T.%N"
        { time func_$func 2>&$stderr  ; } 2>$tfile
        read secs < $tfile
        echo "### $module.$func Completed in $secs"
    done
    rm $tfile
}

TIMEFORMAT="Module $module Completed in %2R seconds"
exec 42>&2
stderr=42

{ time main $@ 2>&$stderr ; } 2>&1

exit 0
