Nagios plugin to monitor bandwidth statistics

Below is a little plugin I wrote today for Nagios to monitor bandwidth usage on an interface.  It’s pretty basic and there are probably much better ways to do this, but this doesn’t require installing any extra packages on most systems.

#!/bin/bash
IFACE=eth0
SEC=5
while getopts ":i:s:" opt; do
 case $opt in
 i)
 IFACE=$OPTARG
 if [ ! -f /sys/class/net/$IFACE/statistics/rx_bytes ]; then
 echo "Interface $IFACE does not exist!"
 exit 1
 fi
 ;;
 s)
 SEC=$OPTARG
 ;;
 \?)
 echo "Invalid option: -$OPTARG"
 echo "Valid Options are -i to choose an interface and -s to specify number of seconds to average bandwidth over."
 echo "Interface will default to eth0 if not specified and seconds will default to 5 if not specified" >&2
 exit 1
 ;;
 :)
 echo "Option -$OPTARG requires an argument." >&2
 exit 1
 ;;
 esac
done
RXBytes1=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
TXBytes1=`cat /sys/class/net/$IFACE/statistics/tx_bytes`
sleep $SEC
RXBytes2=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
TXBytes2=`cat /sys/class/net/$IFACE/statistics/tx_bytes`
RXBytes3="$(((RXBytes2-RXBytes1)/SEC))"
TXBytes3="$(((TXBytes2-TXBytes1)/SEC))"
echo 'OK - Bandwidth Statistics only|RX='$RXBytes3'Bytes;;;;|TX='$TXBytes3'Bytes;;;;'
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s