How I can check for exact amount of space (MB or GB) in Nagios using check_nt command instead of percentage used value?

How I can check for exact amount of space left (MB or GB) in Nagios using check_nt command instead of percentage value?

Currently I'm using this command:

 check_command           check_nt!USEDDISKSPACE!-l c -w 90 -c 95

but this is checking for percentage value of used disk space and I want to receive notification when I will have exact amount of GB left on some drive (for example warning when there will be 10GB left and critical when there will be only 5GB left).

Thank you


Solution 1:

check_nt!USEDDISKSPACE returns the both of size and percentage of disk usage. But thresholds are percentage.

If you want to receive alert based on size, you can write a wrapper shell script for check_nt command, e.g check_disk_by_size:

#!/bin/bash

FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s pa$$word \
-v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [ $UNIT == "Gb" ]; then
    SIZE=`echo $SIZE \* 1024 | bc`
fi

if [ `echo "$SIZE >= $6" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space OK - $FREESPACE"
    exit 0
elif [ `echo "$SIZE < $6" | bc` -eq 1 -a `echo "$SIZE > $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space WARNING - $FREESPACE"
    exit 1
elif [ `echo "$SIZE <= $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space CRITICAL - $FREESPACE"
    exit 2
fi

Testing:

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 10240 -c 5120
c:\_Drive_Space OK - free 13.01 Gb (36%)

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 14240 -c 5120
c:\_Drive_Space WARNING - free 13.01 Gb (36%)

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 16240 -c 15120
c:\_Drive_Space CRITICAL - free 13.01 Gb (36%)

You can add it to Nagios like this:

"check_disk_by_size.sh".

# 'check_disk_by_size' command definition
define command{
       command_name    check_disk_by_size
       command_line    $USER1$/check_disk_by_size.sh -H $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
       }

define service{
        use                     generic-service
        host_name               fileserver1
        service_description     Drive Space fileserver1: L:
        check_command           check_disk_by_size!-l L -w 4096 -c 2048
        notifications_enabled   1
        }

Solution 2:

I have modified the code to adapt new BASH syntax and more robust check.

#!/bin/bash

usage() { echo "$0 check_nt -H host [-p port] [-w warning] [-c critical] [-l params] [-t timeout]" 1>&2; exit 1; }

while getopts ":H:p:l:w:c:" opt; do
    case "${opt}" in
        H ) HOST_NAME=$OPTARG;;
        p ) PORT=$OPTARG;;
        l ) DISC=$OPTARG;;
        w ) WARN_THRESHOLD=$OPTARG;;
        c ) CRITICAL_THRESHOLD=$OPTARG;;
#        t ) TIME_OUT=$OPTARG;;
        \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
        : ) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
    esac
done
shift $((OPTIND-1))

if [[ -z "$HOST_NAME" ]] || [[ -z "$PORT" ]] || [[ -z "$DISC" ]] || [[ -z "$WARN_THRESHOLD" ]] || [[ -z "$CRITICAL_THRESHOLD" ]] ; then
    usage
fi

FREESPACE=`/usr/local/nagios/libexec/check_nt -H $HOST_NAME -p $PORT \
-v USEDDISKSPACE -l $DISC | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [[ $UNIT == "Gb" ]];then
    SIZE=`echo $SIZE \* 1024 | bc`
fi

if [[ `echo "$SIZE >= $WARN_THRESHOLD" | bc` -eq 1 ]];then
    echo "$DISC:\_Drive_Space OK - $FREESPACE"
    exit 0
elif [[ `echo "$SIZE  $CRITICAL_THRESHOLD" | bc` -eq 1 ]];then
    echo "$DISC:\_Drive_Space WARNING - $FREESPACE"
    exit 1
elif [[ `echo "$SIZE 

Solution 3:

I have modified Gnought's script which is a modification of quanta's script even further to remove some code errors and modified the results to more-closely resemble the original output of check_nt which gives better data when analyzing trends over time as well as performance data being added back in.

#!/bin/bash
# Date: 2015-06-30
# Purpose: A wrapper script for check_nt to set threshold for exact space
#          free rather than just percentage.  Useful on VERY large drives.
# Example: check_disk_by_size.sh -H 192.168.0.1 -l c -w 10240 -c 5120

usage() { echo "$0 -H host [-s password] [-p port] [-w warning] [-c critical] [-l params]" 1>&2; exit 1; }

while getopts ":H:s:p:l:w:c:" opt; do
    case "${opt}" in
        H ) HOST_NAME=$OPTARG;;
        s ) PASSW0RD=$OPTARG;;
        p ) PORT=$OPTARG;;
        l ) DISC=$OPTARG;;
        w ) WARN_THRESHOLD=$OPTARG;;
        c ) CRITICAL_THRESHOLD=$OPTARG;;
        \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
        : ) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
    esac
done
shift $((OPTIND-1))

if [[ -z "${HOST_NAME}" ]] || [[ -z "${PASSW0RD}" ]] || [[ -z "${PORT}" ]] || [[ -z "${DISC}" ]] || [[ -z "${WARN_THRESHOLD}" ]] || [[ -z "${CRITICAL_THRESHOLD}" ]] ; then
    usage
fi

CHECKRESULT=`/usr/local/nagios/libexec/check_nt -H ${HOST_NAME} -p ${PORT} -s ${PASSW0RD} -v USEDDISKSPACE -l ${DISC}`
PERFDATA=`echo ${CHECKRESULT} | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $2 }'`
FREESPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`
USEDSPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $3 }'`
TOTALSPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $2 }'`

if [[ -z ${FREESPACE} ]]; then
    ## Command failed or server offline
    echo "ERROR ERROR: Command failed"
    exit 1
fi

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [[ ${UNIT} == "Gb" ]]; then
    SIZE=`echo ${SIZE} \* 1024 | bc`
fi

if [[ `echo "${SIZE} >= ${WARN_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ OK - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 0
elif [[ `echo "${SIZE} < ${WARN_THRESHOLD}" | bc` -eq 1 && `echo "${SIZE} > ${CRITICAL_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ WARNING - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 1
elif [[ `echo "${SIZE} <= ${CRITICAL_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ CRITICAL - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 2
fi