I really wanted to actively monitor my bandwidth with iperf and get information into Nagios; this script does exactly that. I really wanted to use iperf3 originally but it still doesn't have all the functionality I wanted to output the reports I needed so I created this check.

Installation

1. Copy the script below into a file called check_bandwidthv2.sh located with your other Nagios plug-ins. I use /usr/local/nagios/libexec but yours may differ.

 

#!/bin/bash
#Nagios iPerf check by Richard Harvey C2017
#Requires
#Commands
IPERF=/usr/bin/iperf

#Server list
#speedtest.serverius.net
#bouygues.iperf.fr

#Default Variables
SERVER=bouygues.iperf.fr
WARNING=18
CRITICAL=8

usage() {
    echo "check_bandwidthv2"
    echo "Requires iperf not compatible with iperf3 due to missing functionality"
    echo "For the EULA for this script visit https://harveyit.co.uk/contact/eula"
    echo ""
    echo "Usage: check_bandwidthv2 -h <server> -w <warnlevel> -c <critlevel>"
    echo ""
    echo "Warning/Critical levels are in Mb (Megabit)"
    echo ""
    echo "  e.g. check_bandwidthv2 -h example.com -w 20 -c 10"
    echo ""
 }

while getopts ":h:w:c:" o; do
    case "${o}" in
        h)
            SERVER="$OPTARG"
            ;;
        c)
            CRITICAL=$(( ${OPTARG} ))
            ;;
        w)
            WARNING=$(( ${OPTARG} ))
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

#SCRIPT BEGINS
#Runs test
iperfcmd=$($IPERF -c $SERVER -y C | sed 's/.*,*,//')

#Calculates output
x=1000000
calc=$(($iperfcmd / $x))

#Nagios outputs
if [ $calc -ge $WARNING ];
then
        echo "OK - bandwidth is $calc Mb"
        exit 0
fi

if [ $calc -lt $WARNING ] && [ $calc -gt $CRITICAL ];
then
        echo "Warning - Bandwidth is $calc Mb"
        exit 1
fi

if [ $calc -lt $CRITICAL ];
then
        echo "Critical - Bandwidth is only $calc Mb"
        exit 2
fi

 

2. Save the file and make it executable

chmox +x check_bandwidthv2.sh

3. Add check_bandwidthv2 to your commands config, mine is located in /usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name check_officebandwidthv2
        command_line $USER1$/check_bandwidthv2.sh -h $ARG1$ -w $ARG2$ -c $ARG3$
        }

4. Finally, define your service check

define service{
        use                             local-service
        host_name                       officecyberoam
        service_description             Office bandwidth check
        check_command                   check_officebandwidthv2!bouygues.iperf.fr!18!8
        check_interval                  15
        }

 


Loading Conversation