Created
August 25, 2015 02:08
-
-
Save microlinux/174723f466466dd165f4 to your computer and use it in GitHub Desktop.
A script to add commands to EdgeOS that show ethernet/vif traffic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# a script to add commands to EdgeOS that show ethernet/vif traffic | |
# | |
# default measurement period is 5 seconds | |
# probably extendable to any interface in /proc/net/dev | |
# see 'shif.sh -h' for standalone use | |
# | |
# install: sudo chmod 755 shif.sh | |
# sudo ./shif.sh install | |
# | |
# requires: bc (instructions during install) | |
# | |
# adds: | |
# show interfaces ethernet <name> traffic [seconds] | |
# show interfaces ethernet <name> vif <number> traffic [seconds] | |
# | |
# admin@router:~$ show interfaces ethernet eth6 traffic | |
# mbps pps bpp errors drops | |
# in .66 875 95 0 0 | |
# out 15.04 1314 1430 0 0 | |
### FUNCTIONS ### | |
function install_shif() | |
{ | |
TPL_DIR='/opt/vyatta/share/vyatta-op/templates/show/interfaces' | |
if [[ "$EUID" -ne 0 ]] | |
then | |
echo 'installation requires root privileges' | |
exit 1 | |
fi | |
if [ -z "$(which bc)" ] | |
then | |
echo 'bc not installed' | |
echo 'enable debian repos per https://goo.gl/YlKZ62' | |
echo 'run "sudo apt-get update && sudo apt-get install bc"' | |
echo 'shif.sh requires bc to function properly' | |
echo | |
fi | |
cp -pf ./shif.sh /config/scripts || exit 2 | |
chmod 755 /config/scripts/shif.sh || exit 3 | |
mkdir -p "$TPL_DIR/ethernet/node.tag/traffic/node.tag" || exit 4 | |
mkdir -p "$TPL_DIR/ethernet/node.tag/vif/node.tag/traffic/node.tag" || exit 5 | |
# ethernet traffic node | |
cat > "$TPL_DIR/ethernet/node.tag/traffic/node.def" << EOF | |
help: show interface traffic | |
run: /config/scripts/shif.sh -s5 "\$4" | |
EOF | |
# ethernet traffic measurement period node | |
cat > "$TPL_DIR/ethernet/node.tag/traffic/node.tag/node.def" << EOF | |
help: seconds to measure | |
run: /config/scripts/shif.sh -s"\$6" "\$4" | |
EOF | |
# ethernet vif traffic node | |
cat > "$TPL_DIR/ethernet/node.tag/vif/node.tag/traffic/node.def" << EOF | |
help: show interface traffic | |
run: /config/scripts/shif.sh -s5 "\$4.\$6" | |
EOF | |
# ethernet vif traffic measurement period node | |
cat > "$TPL_DIR/ethernet/node.tag/vif/node.tag/traffic/node.tag/node.def" << EOF | |
help: seconds to measure | |
run: /config/scripts/shif.sh -s"\$8" "\$4.\$6" | |
EOF | |
echo 'installation complete' | |
echo 'show interfaces ethernet <name> traffic [seconds]' | |
echo 'show interfaces ethernet <name> vif <number> traffic [seconds]' | |
} | |
function get_stats() | |
{ | |
stats=($(grep "^ *$1:" /proc/net/dev)) | |
if [ -z "$stats" ]; then exit 1; fi | |
echo "${stats[1]} ${stats[2]} ${stats[3]} ${stats[4]} ${stats[9]} ${stats[10]} ${stats[11]} ${stats[12]}" | |
} | |
function calc_mbps() | |
{ | |
mbps=$(bc 2> /dev/null <<< "scale=2; ($2 - $1) / 125000 / $SECS") | |
if [ -z "$mbps" ] | |
then | |
echo 0 | |
else | |
echo "$mbps" | |
fi | |
} | |
function calc_pps() | |
{ | |
pps=$(bc 2> /dev/null <<< "scale=0; ($2 - $1) / $SECS") | |
if [ -z "$pps" ] | |
then | |
echo 0 | |
else | |
echo "$pps" | |
fi | |
} | |
function calc_bpp() | |
{ | |
bpp=$(bc 2> /dev/null <<< "scale=0; ($2 - $1) / ($4 - $3)") | |
if [ -z "$bpp" ] | |
then | |
echo 0 | |
else | |
echo "$bpp" | |
fi | |
} | |
### OPTION DEFAULTS ### | |
CSV=0 | |
SECS=5 | |
### MAIN PROGRAM ### | |
while getopts 'hcs:' option | |
do | |
case "$option" in | |
h) | |
echo 'display interface mbps/pps/bpp and error/drop counts' | |
echo | |
echo 'usage: shif.sh [OPTIONS] INTERFACE' | |
echo | |
echo 'options:' | |
echo ' -s INT seconds to measure (5)' | |
echo ' -c display output in csv format' | |
echo ' mbps_in,pps_in,bpp_in,errs_in,drops_in,mbps_out,...' | |
exit | |
;; | |
s) | |
SECS=$(printf "%.0f" "$OPTARG") | |
if [ "$SECS" -lt 1 ]; then echo 'seconds must be >= 1'; exit 2; fi | |
;; | |
c) | |
CSV=1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ "$1" == 'install' ] | |
then | |
install_shif | |
exit | |
fi | |
stats_1=($(get_stats "$1")) | |
if [ $? -gt 0 ]; then echo 'interface not found'; exit 1; fi | |
sleep "$SECS" | |
stats_2=($(get_stats "$1")) | |
if [ $? -gt 0 ]; then echo 'interface not found'; exit 1; fi | |
mbps_in="$(calc_mbps ${stats_1[0]} ${stats_2[0]})" | |
pps_in="$(calc_pps ${stats_1[1]} ${stats_2[1]})" | |
bpp_in="$(calc_bpp ${stats_1[0]} ${stats_2[0]} ${stats_1[1]} ${stats_2[1]})" | |
errs_in="$((stats_2[2] - stats_1[2]))" | |
drops_in="$((stats_2[3] - stats_1[3]))" | |
mbps_out="$(calc_mbps ${stats_1[4]} ${stats_2[4]})" | |
pps_out="$(calc_pps ${stats_1[5]} ${stats_2[5]})" | |
bpp_out="$(calc_bpp ${stats_1[4]} ${stats_2[4]} ${stats_1[5]} ${stats_2[5]})" | |
errs_out="$((stats_2[6] - stats_1[6]))" | |
drops_out="$((stats_2[7] - stats_1[7]))" | |
if [ "$CSV" -eq 0 ] | |
then | |
echo ' mbps pps bpp errors drops' | |
printf "in %-8s %-9s %-5s %-7s %s\n" "$mbps_in" "$pps_in" "$bpp_in" "$errs_in" "$drops_in" | |
printf "out %-8s %-9s %-5s %-7s %s\n" "$mbps_out" "$pps_out" "$bpp_out" "$errs_out" "$drops_out" | |
else | |
echo "$mbps_in,$pps_in,$bpp_in,$errs_in,$drops_in,$mbps_out,$pps_out,$bpp_out,$errs_out,$drops_out" | |
fi | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment