Skip to content

Instantly share code, notes, and snippets.

@jandahl
Last active December 18, 2015 17:10
Show Gist options
  • Save jandahl/5817009 to your computer and use it in GitHub Desktop.
Save jandahl/5817009 to your computer and use it in GitHub Desktop.
clogin script to crawl the routers in the array given on line 3. Assumes working .cloginrc. Has progress bar.
#!/usr/bin/env bash
function aboutMe {
echo -e "\n\tTCAMcheck.sh v. 2013-06-20"
echo -e "\tPull out L3 TCAM load from 760x routers and show more than $warningThreshold% load"
echo -e "\n\tExample:\n\n\t\t`basename $0` \033[1;31mdr1.hors dr6.hors router3 router4\033[1;0m\n"
echo -e "\n\tWhen run from the command line, it will update your screen/byobu title line and show you the results in less"
echo -e "\n\tIt even has a progress bar!"
echo -e "\n\tIt *should* play nice with pipes and crontab, but YMMV"
echo e- "\n\tAlso, it doesn't tell you when it can't reach a router."
echo -e "\n\n"
exit 0
}
function showBar {
percDone=$(echo 'scale=2;'$1/$2*100 | bc) # calculate percentages based on the two command line arguments supplied
barLen=$(echo ${percDone%'.00'}) # set total bar length - remove redundant zeroes
bar='' # zero out done bar
fills='' # zero out undone bar
for (( b=0; b<$barLen; b++ ))
do
bar=$bar"=" # fill out the done bar
done
blankSpaces=$(echo $((100-$barLen))) # define how many undone indicators to use
for (( f=0; f<$blankSpaces; f++ ))
do
fills=$fills"-" # fill out the undone bar
done
clear # clear screen
echo '['$bar'>'$fills'] - '$barLen'% - '$currentRouter # show all at once - done, current progress, undone, percentage, router name
}
function doStuff() {
# Housekeeping
touch $file
echo "" > $file
counter=0
if [ ${#routers} -eq 1 ]; then
echo -e "Trying to poll ${#routers[*]} routers:\n$@" >> $file
else
echo "Trying to poll ${#routers[*]} router: $@" >> $file
fi
echo -e "+------------------------------------------------------------------------------------+"
echo -e "| Empty lines are good! This script only outputs if the result is more than $warningThreshold% load |"
echo -e "+------------------------------------------------------------------------------------+"
for currentRouter in ${routers[*]}
do
if [ -z "$PS1" ] && [ -t 1 ]; then # If this is an interactive shell and not in a pipe
showBar $counter ${#routers[*]} # then show a progress bar
if [ -n $STY ] ; then # if it is in screen/byobu, also update the title
echo -n -e "\033kChecking TCAM for $currentRouter\033\\"
fi
fi
counter=$[$counter+1]
echo "----- $currentRouter -----" >> $file
clogin -c "show platform hardware capacity" $currentRouter | grep --after-context=3 "L3 Forwarding Resources" | awk -v threshold=$warningThreshold '{
if(int($2)==72 &&
int($9)>=threshold ||
int($1)==144 &&
int($8)>=threshold)
print;}' >> $file # fetch "show platform hardware capacity", only show interesting lines, then abuse awk to only show troubling lines
done
if [ -z "$PS1" ] && [ -t 1 ]; then # If this is an interactive shell and not in a pipe
if [ -n $STY ] ; then # if it is in screen/byobu, also update the title
echo -n -e "\033kTCAM sweep results\033\\"
fi
less $file # show file contents
if [ -n $STY ] ; then # if it is in screen/byobu, also update the title
echo -n -e "\033k$(hostname)\033\\"
fi
clear
else
cat $file # You just want to watch the world burn, don't you?
fi
}
routers=( "$@" )
file=~/temp.txt
warningThreshold=80
case $# in
0)
aboutMe
;;
*)
doStuff "$*"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment