Skip to main content

Nagios plugin to check the currently used bandwidth

This page shows you how you can create a Nagios plugin to check how much bandwidth the server is currently using. In order to do this, we will use the tool vnstat. This tools offers the functionality to check how much bandwidth you use over the last 5 seconds.

The Nagios plugin you can find here does not follow the Nagios requirements. The requirements state that there should be a help build in. The plugin I wrote, only expects 2 parameters: a warning and critical level.

The actual plugin

You should store this plugin in the following file: /usr/lib/nagios/plugins/check_bandwidth

#!/bin/sh

warn=$1
crit=$2

i=$(vnstat -tr)

rx=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* kB/s")
tx=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* kB/s")

status="$rx $tx"

rx1=$(echo $rx | awk '{ print $2 }' | awk -F\. '{ print $1 }')
tx1=$(echo $tx | awk '{ print $2 }' | awk -F\. '{ print $1 }')

if (( $warn <= $rx1 )) || (( $warn <= $tx1 ))
then
  if (( $crit <= $rx1 )) || (( $crit <= $tx1 ))
  then
    echo "CRITICAL - $status"
    exit 2
  else
    echo "WARNING - $status"
    exit 1
  fi
else
  echo "OK - $status"
  exit 0
fi

The plugin configuration definition

You should store the following text in this file: /etc/nagios-plugins/config/bandwidth.cfg

# 'check_bandwidth' command definition
define command{
        command_name    check_bandwidth
        command_line    /usr/lib/nagios/plugins/check_bandwidth $ARG1$ $ARG2$
        }

Using this plugin in Nagios

The following example demonstrates how to use this plugin. The example checks the local machine and will give a warning alert when the bandwidth is higher than 10KB/sec and a critical alert when the bandwidth is higher than 20KB/sec.

define service{
        use                     generic-service
        host_name               LNX-DEBIANSRV
        service_description     CHECK-BANDWIDTH
        check_command           check_bandwidth!10!20
        }

script with modifications

Currently I needed similar script.
I liked Your method, so I make similar script that fit my need.
It monitors bandwidth and packets/s.

Left "Insipration source" at bottom of my post, I hope You don't mind for backlink to my upgraded bw_watch.sh script:
http://loginroot.com/bandwidth-monitoring-script-for-nagios-centos-bw_wa...

creating check_bandwidth file

Is this a sh file extension? Or is it something else? I followed your directions just as you have them and am getting the following error when I check for errors:

Checking services...
Error: Service check command 'check_bandwidth' specified in service 'CHECK-BANDWIDTH' for host 'remotehost' not defined anywhere!
Error: Service check command 'check_bandwidth' specified in service 'CHECK-BANDWIDTH' for host 'remotehost2' not defined anywhere!

I took a look at the check_bandwidth file and it's not bold like all the other plugins so I thought it had something to do with a file extension. I'm new to working with Nagios so it may be something completely unrelated.

Re: creating check_bandwidth file

Nevermind I just ran :

chmod 755 check_bandwidth

to make it executable. Great plugin thanks. Currently I have it set to check if the bandwidth on my network goes below a certain level (indicating a possible attack or major issue): Here's my revised version of your code:

#!/bin/sh

warn=$1
crit=$2

i=$(vnstat -tr)

rx=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* kB/s")
tx=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* kB/s")

status="$rx $tx"

rx1=$(echo $rx | awk '{ print $2 }' | awk -F\. '{ print $1 }')
tx1=$(echo $tx | awk '{ print $2 }' | awk -F\. '{ print $1 }')

if (( $warn >= $rx1 )) || (( $warn >= $tx1 ))
then
if (( $crit >= $rx1 )) || (( $crit >= $tx1 ))
then
echo "CRITICAL - $status"
exit 2
else
echo "WARNING - $status"
exit 1
fi
else
echo "OK - $status"
exit 0
fi

So really all I did was flip the signs around between the variables but it works for what I need. Thanks again.

Issue with Mbits

I like the looks of your plugin, but just by looking at it, it seems like you have an issue with the output from vnstat.

It looks like you do a regex for kB/s, but vnstat can output MB/s and I think GB/s (not sure if I got the syntax exact there).

Re: Issue with Mbits

Thanks for your comment. You are right, according to the vnstat manual, the tool can output in KB, MB, GB and even TB.

When I wrote this script, I needed it to monitor a low-traffic network interface, as such I only got replies in KB/sec.

If you can have a mix of KB, MB, or more in the output of vnstat, then not only the regular expression must be changed, but the entire script needs more logic. The script would need to know that 1MB is more that 100KB. The current version of the script only compares the displayed numbers against a benchmark.

I would recommend to add some lines which check the value of the used units, and to convert all values to the same unit. So if vnstat reports an average bandwith usage of 10MB/sec, then multiply 10 by 1024 to have the bandwidth usage in KB/sec.

something like this

something like this then?


rx_value=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $2 }')
tx_value=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $2 }')
rx_unit=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $3 }')
tx_unit=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $3 }')

#recalculate rx_value and tx_value, depending on the unit in rx_unit and tx_unit
#first for rx
if [ $rx_unit == "Mbit/s" ]
then rx_value_recal=`echo "$rx_value * 1024" | bc`
else rx_value_recal=`echo "$rx_value" | bc`
fi

#...then also for tx
if [ $tx_unit == "Mbit/s" ]
then tx_value_recal=`echo "$tx_value * 1024" | bc`
else tx_value_recal=$tx_value
fi

Bandwidth plugin on CentOS5

Thanks for the plugin for nagios bandwidth checking. Here is a version on a CentOS5 host:

#!/bin/sh

warn=$1
crit=$2

i=$(vnstat -tr)

rx_value=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $2 }' | cut -d. -f1)
tx_value=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $2 }' | cut -d. -f1)
rx_unit=$(echo $i | grep -o "rx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $3 }' | cut -d. -f1)
tx_unit=$(echo $i | grep -o "tx [[:digit:]]*\.[[:digit:]]* .bit/s" | awk '{ print $3 }' | cut -d. -f1)

#recalculate rx_value and tx_value, depending on the unit in rx_unit and tx_unit
#first for rx
if [ $rx_unit == "Mbit/s" ]
then rx_value_recal=`echo "$rx_value * 1024" | bc`
else rx_value_recal=`echo "$rx_value" | bc`
fi

#...then also for tx
if [ $tx_unit == "Mbit/s" ]
then tx_value_recal=`echo "$tx_value * 1024" | bc`
else tx_value_recal=$tx_value
fi

status="$rx_value_recal $tx_value_recal"

if [ $warn -lt $rx_value_recal -o $warn -lt $tx_value_recal ]
then
if [ $crit -lt $rx_value_recal -o $crit -lt $tx_value_recal ]
then
echo "CRITICAL: rx $rx_value_recal tx $tx_value_recal kbps. Limit of $crit exceeded"
exit 2
else
echo "WARNING: rx $rx_value_recal tx $tx_value_recal kbps. Limit of $warn exceeded"
exit 1
fi
else
echo "OK: rx $rx_value_recal tx $tx_value_recal kbps"
exit 0
fi

issue

I am facing error while running command ..please check it once..

[root@Centos-SQL libexec]# ./check_bandwith 10 20
./check_bandwith: line 15: [: ==: unary operator expected
./check_bandwith: line 21: [: ==: unary operator expected
./check_bandwith: line 28: [: -o: integer expression expected
OK: rx tx kbps

change #!/bin/sh

change

#!/bin/sh

to

#!/bin/bash

in script, most likely your /bin/sh link points to bash

syntax error line 16

bonjour,
Si tu peux m'aider voici l'erreur lors de l'execution de script
line 16: ((: 10<=: syntax error: operand expected (error token is "<=")

Try to replace '/bin/sh' by

Try to replace '/bin/sh' by '/bin/bash' (or otherwise).
You can also try to replace the parentheses '((' by square brackets '[['
Have you already tried the script for CentOs that was suggested above?