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



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 ))
  if (( $crit <= $rx1 )) || (( $crit <= $tx1 ))
    echo "CRITICAL - $status"
    exit 2
    echo "WARNING - $status"
    exit 1
  echo "OK - $status"
  exit 0

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


You might also be interested in...