Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Thursday, 4 December 2014

Nagios XI: A Bash Plugin to Monitor Disk Space Utilization on Remote Linux Server

  • This plugin is designed using Bash script and intended to display the disk space used by the specified mount point or partition or volume.
  • This plugin is created so that 'Performance Gauges' in Nagios XI work properly, showing appropriate 'Warning' and 'Critical' regions.
See Also: 



For Performance Gauges to show details properly, performance data should be in following format:

'VarName'=CurrentValue Unit;WarningValue;CriticalValue;MininumValue;MaximumValue

Important:

1. It uses the output of 'df' command.
2. Mount point/Partition/Volume & Warning and Critical thresholds are to be mentioned by the user itself.

Script:


#!/bin/bash

help() {
cat << END
Usage :
        check_disk.sh -p [Path] -w [VALUE] -c [VALUE]

        OPTION          DESCRIPTION
        ----------------------------------
        -h              Help
        -w [VALUE]      Warning Threshold
        -c [VALUE]      Critical Threshold
        -p [Path]       Mount Point Name
        ----------------------------------
Note : [VALUE] must be an integer.
END
}

if [ $# -ne 6 ]
then
        help;
        exit 3;
fi

while getopts "p:w:c:" OPT
do
        case $OPT in
        p) MNT="$OPTARG" ;;
        w) WARN="$OPTARG" ;;
        c) CRIT="$OPTARG" ;;
        *) help ;;
        esac
done

if [ $WARN -ge $CRIT ]
then
        echo "ERROR : Warning threshold must be less than Critical threshold."
        exit 3
fi


# NAGIOS DISPLAY DATA

DF=`df -lh $MNT | grep -iv "filesystem"`
TOTAL=`echo $DF | awk '{print $2}'`
USED=`echo $DF | awk '{print $3}'`
AVAIL=`echo $DF | awk '{print $4}'`
PER=`echo $DF | awk '{print $5}' | tr -d "%"`

OUTPUT=`echo $MNT : Total Space= "$TOTAL", Used Space= "$USED", Available Space= "$AVAIL" i.e. ${PER}% Usage`

if [ $PER -le $WARN ]
then
        STATUS="OK";
        EXITSTAT=0;
elif [ $PER -ge $WARN ]
then
        if [ $PER -ge $CRIT ]
        then
                STATUS="CRITICAL";
                EXITSTAT=2;
        else
                STATUS="WARNING";
                EXITSTAT=1;
        fi
else
        STATUS="UNKNOWN";
        EXITSTAT=3;
fi

echo "$STATUS- $OUTPUT | 'Usage'=${PER};$WARN;$CRIT;0;100" && exit $EXITSTAT

How To Use:

To monitor remote Linux server:
1. Keep the plugin in /usr/local/nagios/libexec directory.
2. Add following line to the nrpe.cfg file:

command[check_disk.sh]=sudo /usr/local/nagios/libexec/check_disk.sh $ARG1$
3. Add the following line to /etc/sudoers file:

nagios ALL=(ALL) NOPASSWD:/usr/local/nagios/libexec/check_disk.sh

Usage:

On Monitoring Server:

/usr/local/nagios/libexec//check_nrpe -H 172.22.246.126 -c check_disk.sh -a '-p /volume1 -w 80 -c 90'
Output:

OK- /Volume1 : Total Space= 468G, Used Space= 80G, Available Space= 365G i.e. 18% Usage | 'Usage'=18;80;90;0;100
If you face an error like CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages, please read following article:


1 comment:

  1. Why does your df command specify -l? When I execute your script against my mount point, I get "df: no file systems processed", but if I mod the script and remove the -l (so, DF=`df -h $MNT | grep -iv "filesystem"`), the script works as expected.

    Honestly, it seems like -l is the exact opposite of what you want, but I could be misunderstanding

    ReplyDelete