Skip to content

Instantly share code, notes, and snippets.

@fnordbar
Forked from radu-gheorghe/traffic.sh
Last active May 23, 2018 17:07
Show Gist options
  • Save fnordbar/f21ed55d4d7f83c2b885c12c98d42937 to your computer and use it in GitHub Desktop.
Save fnordbar/f21ed55d4d7f83c2b885c12c98d42937 to your computer and use it in GitHub Desktop.
bash script for checking out traffic via /proc/net/dev
#!/bin/bash
#shows traffic on the specified device
function human_readable {
VALUE=$1
BIGGIFIERS=( B K M G )
CURRENT_BIGGIFIER=0
while [ $VALUE -gt 10000 ] ;do
VALUE=$(($VALUE/1000))
CURRENT_BIGGIFIER=$((CURRENT_BIGGIFIER+1))
done
#echo "value: $VALUE"
#echo "biggifier: ${BIGGIFIERS[$CURRENT_BIGGIFIER]}"
echo "$VALUE${BIGGIFIERS[$CURRENT_BIGGIFIER]}"
}
function help {
echo -e "\nusage: $0 -I interface -w WarningBytesPerSecound -c CriticalBytesPerSecound\n\nThis plugin shows the bandwith currently used on a network interface."
exit -1
}
###CHECKS####
# Getting parameters:
while getopts "I:w:c:h" OPT; do
case $OPT in
"I") DEVICE=$OPTARG;;
"w") warning=$OPTARG;;
"c") critical=$OPTARG;;
"h") help;;
esac
done
( [ "$DEVICE" == "" ] ) && \
echo "ERROR: You must specify a interface" && help
#DEVICE=$1
IS_GOOD=0
for GOOD_DEVICE in `grep ":" /proc/net/dev | awk '{print $1}' | sed s/://`; do
if [ $DEVICE = $GOOD_DEVICE ]; then
IS_GOOD=1
break
fi
done
if [ $IS_GOOD -eq 0 ]; then
echo "UNKNOWN: Device $DEVICE not found."
exit 3
fi
#LIMITS
( [ "$warning" == "" ] || [ "$critical" == "" ] ) && \
echo "ERROR: You must specify all warning and critical levels" && help
( [[ "$warning" -ge "$critical" ]] ) && \
echo "ERROR: critical levels must be highter than warning levels" && help
###REAL STUFF
RECEIVED=`grep $DEVICE /proc/net/dev | awk '{print $2}'`
TRANSMITTED=`grep $DEVICE /proc/net/dev | awk '{print $10}'`
TOTAL=$(($RECEIVED+$TRANSMITTED))
#echo "Transmitted: `human_readable $TRANSMITTED`"
#echo "Received: `human_readable $RECEIVED`"
#echo "Total: `human_readable $TOTAL`"
SLP=1
#echo "Sleeping $SLP to calculate speed..."
sleep $SLP
RECEIVED=`grep $DEVICE /proc/net/dev | awk '{print $2}'`
TRANSMITTED=`grep $DEVICE /proc/net/dev | awk '{print $10}'`
SPEED=$((($RECEIVED+$TRANSMITTED-$TOTAL)/$SLP))
#echo "Current speed: `human_readable $SPEED`/s"
if ( [ $SPEED -gt $critical ] )
then
MSG="CRITICAL"
STATUS=2
else
if ( [ $SPEED -gt $warning ] )
then
MSG="WARNING"
STATUS=1
else
MSG="OK"
STATUS=0
fi
fi
#print Message and exit
echo "$MSG - iface $DEVICE `human_readable $SPEED`/s | 'spd'=$SPEED; 'wrn'=$warning; 'crt'=$critical; 'rev'=$RECEIVED; 'tra'=$TRANSMITTED;"
exit $STATUS
@fnordbar
Copy link
Author

fnordbar commented Apr 7, 2017

icinga / nagios plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment