Vanstechelman.eu
   

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
        }

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.