#!/bin/bash

if [[ -z $1 ]]
then
    # echo -n "0"
    exit 1
fi

HOST="$1"
USER="username"
TIMEOUT=10s
export SSHPASS="password"

# For IPv4 we have to grab global routing table and VPNv4 routes from two seperate places and add them together...
CMD="show controllers np struct IPV4-LEAF-FAST-P np0 | in Entries"

RAW_OUTPUT=$(timeout $TIMEOUT sshpass -e ssh -T -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null $USER@$HOST "$CMD" </dev/zero 2>/dev/null)

GRT_UCASTV4_USED=$(echo -n "$RAW_OUTPUT" | grep "Entries" | awk '{print $6}' | tr -d "," | tr -d " ")
GRT_UCASTV4_TOTAL=$(echo -n "$RAW_OUTPUT" | grep "Entries" | awk '{print $9}' | tr -d " ")

CMD="show controllers np struct IPV4-LEAF-P np0 | in Entries"
RAW_OUTPUT=$(timeout $TIMEOUT sshpass -e ssh -T -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null $USER@$HOST "$CMD" </dev/zero 2>/dev/null)

VPNV4_UCASTV4_USED=$(echo -n "$RAW_OUTPUT" | grep "Entries" | awk '{print $6}' | tr -d "," | tr -d " " | tr -d "\r\n")
# No need to grab total for VPNv4, they used the same memory as the GRT unicast v4 routes but the counters are split for GRT and VPNv4 "used"

UCASTV4_USED=$(($GRT_UCASTV4_USED+$VPNV4_UCASTV4_USED))
UCASTV4_TOTAL="$GRT_UCASTV4_TOTAL"

CMD="show controllers np struct IPV6-LEAF-P np0 | in Entries"
RAW_OUTPUT=$(timeout $TIMEOUT sshpass -e ssh -T -q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null $USER@$HOST "$CMD" </dev/zero 2>/dev/null)

UCASTV6_USED=$(echo -n "$RAW_OUTPUT" | grep "Entries" | awk '{print $6}' | tr -d "," | tr -d " ")
UCASTV6_TOTAL=$(echo -n "$RAW_OUTPUT" | grep "Entries" | awk '{print $9}' | tr -d " " | tr -d "\r\n")

# Must remove new line from output for Cacti
echo -n "ucastv4_used:$UCASTV4_USED ucastv4_total:$UCASTV4_TOTAL ucastv6_used:$UCASTV6_USED ucastv6_total:$UCASTV6_TOTAL"

