bandwidth monitoring script for nagios (CentOS) – bw_watch.sh

by

in

Bash script for nagios, that monitors bandwidth usage on remote host.
# usage ./bw_watch bw_warning bw_critical pkt_warning pkt_critical
It takes momentary 5 secs average bandwidth from “vnstat”, and returns value for nagios.

bc and vnstat packages at host side are necessary.
bc package should be in default centos repository, vnstat and nrpe You can find in EPEL repo.

Script itself, that You have to put to nagios plugins directory. In my case it was: /usr/lib64/nagios/plugins/

Script bw_watch.sh has to be in server that has to be checked.
Download link

#!/bin/bash
# script created by nsc
# usage ./bw_watch bw_warning bw_critical pkt_warning pkt_critical
# bw usage is in kbits/s

if [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]] || [[ -z $4 ]]
then
	echo "VARIABLES ARE NOT SET!!!"
	echo "usage $0 bw_warning bw_critical pkt_warning pkt_critical"
	echo "bw usage is in kbits/s"
	exit 2
fi

bw_warn=$1
bw_crit=$2
packets_warn=$3
packets_crit=$4

bw_output=$(vnstat -tr 5 -s )
rx_value=$(echo $bw_output | grep -o "rx [[:digit:]]*\.*[[:digit:]]* .bit/s" | cut -f2 -d' ' )
rx_unit=$(echo $bw_output | grep -o "rx [[:digit:]]*\.*[[:digit:]]* .bit/s" | cut -f3 -d' ' )
rx_packets=$(echo $bw_output | grep -o "rx [[:digit:]]*\.*[[:digit:]]* .bit/s [[:digit:]]* packets/s" | cut -f4 -d' ' )
tx_value=$(echo $bw_output | grep -o "tx [[:digit:]]*\.*[[:digit:]]* .bit/s" | cut -f2 -d' ' )
tx_unit=$(echo $bw_output | grep -o "tx [[:digit:]]*\.*[[:digit:]]* .bit/s" | cut -f3 -d' ' )
tx_packets=$(echo $bw_output | grep -o "tx [[:digit:]]*\.*[[:digit:]]* .bit/s [[:digit:]]* packets/s" | cut -f4 -d' ' )

#convert rx to kbits/s
if [ $rx_unit == "Mbit/s" ]
then rx_value=`echo "$rx_value * 1024" | bc`
fi

#convert tx to kbits/s
if [ $tx_unit == "Mbit/s" ]
then tx_value=`echo "$tx_value * 1024" | bc`
fi

#convert to integer
rx_value=${rx_value/.*}
tx_value=${tx_value/.*}


if [ $bw_crit -lt $rx_value ] || [ $bw_crit -lt $tx_value ] || [ $packets_crit -lt $rx_packets ] || [ $packets_crit -lt $tx_packets ]
then
	echo "CRITICAL: RX/TX: $rx_value/$tx_value kbits/s. PKT: RX/TX: $rx_packets/$tx_packets"
	exit 2
elif [ $bw_warn -lt $rx_value ] || [ $bw_warn -lt $tx_value ] || [ $packets_warn -lt $rx_packets ] || [ $packets_warn -lt $tx_packets ]
then
	echo "WARNING: RX/TX: $rx_value/$tx_value kbits/s. PKT: RX/TX: $rx_packets/$tx_packets"
	exit 1
else
	echo "OK: RX/TX: $rx_value/$tx_value kbits/s. PKT: RX/TX: $rx_packets/$tx_packets"
	exit 0
fi

Download link

On host, that has to be checked, you have to add to nrpe.conf (usually /etc/nagios/nrpe.cfg)

command[check_bandwidth]=/usr/lib64/nagios/plugins/bw_watch.sh 100 500 100 500

restart nrpe

service nrpe restart

nagios command definition at monitoring server (usually commands.cfg):

define command
        command_name    check_bandwidth
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_bandwidth -t60 
}

define service at monitoring server (where monitored host configuration is located):
Parameters are: bandwidth_warning, bandwidth_critical, packets_warning, packets_critical
Bandwidth is metered in kbits/s.

define service{
        use                             generic-service
        host_name                       server.hostname.tld
        service_description             Bandwidth
        check_command                   check_bandwidth
        }

And reload nagios configuration

service nagios reload

Insipration source: nagios bandwidth plugin


Comments

8 responses to “bandwidth monitoring script for nagios (CentOS) – bw_watch.sh”

  1. Hey, had a go at implementing this, but I’m getting NRPE: Unable to read output all the time.

    Anyway to debug it ?

    1. Have You tried to execute the script itself?
      Do You have vnstat installed on Your system? This nagios script depends on it.

      1. Yeh, it seems to work fine.

        But … should the check cmd in command.cfg actually be something more like:

        check_command check_bandwidth!100!200!300!400

        ?

        1. it is possible to do that, but in my example – no.
          The way it works in this tutorial is:
          check_command check_bandwidth

          Nagios server uses only “check_bandwidth” command (without arguments) to check the bandwidth.

          This command is defined like that (also no arguments):
          command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_bandwidth -t60

          It basically just executes ‘check_bandwidth’ command that is described on the monitored host.

          And Your monitored host should have that ‘check_bandwidth’, but this time with the parameters:
          in nrpe.cfg:
          command[check_bandwidth]=/usr/lib64/nagios/plugins/bw_watch.sh 2048 4096 2048 4096

          To be able to “pass” arguments to nrpe, need to redo the logic of nagios configs. It won’t work on the current described setup.

          1. Got it, thanks for your help, its a useful post !

  2. HI ,

    I have installed vnstat tool on server.I can able to view the traffic usage,But Executed your bw_watch.sh script i am getting some error .can you please help me on this issue.

    Error :-

    ./bw_watch.sh 2048 4096 2048 4096
    ./bw_watch.sh: line 28: [: ==: unary operator expected
    ./bw_watch.sh: line 33: [: ==: unary operator expected
    ./bw_watch.sh: line 42: [: 4096: unary operator expected
    ./bw_watch.sh: line 42: [: 4096: unary operator expected
    ./bw_watch.sh: line 42: [: 4096: unary operator expected
    ./bw_watch.sh: line 42: [: 4096: unary operator expected
    ./bw_watch.sh: line 46: [: 2048: unary operator expected
    ./bw_watch.sh: line 46: [: 2048: unary operator expected
    ./bw_watch.sh: line 46: [: 2048: unary operator expected
    ./bw_watch.sh: line 46: [: 2048: unary operator expected

  3. Hi thanks for the script! I was just wondering if we want to alert when the bandwidth is below a certain number how can we do that?

  4. Ignore me I have changed the if statements to ‘gt’ rather than ‘lt’

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.