check_dhcp_pool

This is a Nagios plugin for checking the number of free ip addresses in the given DHCP pool on a Windows DHCP server.

check_dhcp_pool

Copy the file to your nagios/libexec directory, fix owner and make it executable.

#! /bin/bash
# ##############################################################################
# check_dhcp_pool.sh - Nagios plugin
#
# This script querys a MS Windows DHCP-Server via SNMP v2 to fetch informations about the given DHCP-Pool.
#
# Copyright (C) 2006, 2007 Lars Michelsen <lars@vertical-visions.de>,
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#
# GNU General Public License: http://www.gnu.org/licenses/gpl-2.0.txt
#
# Report bugs to: lars@vertical-visions.de
#
# 2006-07-05 Version 0.2
# 2007-10-27 Version 0.3
# ##############################################################################
 
if [ $# -lt 3 ]; then
	echo "check_dhcp_pool.sh - Lars Michelsen (Version 0.3 - 2007-10-27)"
	echo "Usage: $0 <host> <community> <pool-ip> [<warn=10>] [<crit=5>]"
	exit 3
fi
 
IP="$1"
COMMUNITY="$2"
POOL="$3"
WARN="$4"
CRIT="$5"
 
if [ ${#WARN} -lt 1 ]
then
	WARN=10
fi
 
if [ ${#CRIT} -lt 1 ]
then
	CRIT=5
fi
 
FREEOID=".1.3.6.1.4.1.311.1.3.2.1.1.3.$POOL"
USEDOID=".1.3.6.1.4.1.311.1.3.2.1.1.2.$POOL"
 
SNMP_RESULT=`snmpget -v 2c -c $COMMUNITY $IP $FREEOID`
FREE=`echo $SNMP_RESULT|cut -d " " -f4`
 
SNMP_RESULT=`snmpget -v 2c -c $COMMUNITY $IP $USEDOID`
USED=`echo $SNMP_RESULT|cut -d " " -f4`
 
MAX=`echo "$FREE+$USED" |bc`
PERCFREE=`echo "$FREE*100/$MAX" |bc`
PERCUSED=`echo "$USED*100/$MAX" |bc`
 
#DEBUG: echo "FREE: $FREE USED: $USED MAX: $MAX PERC: $PERCFREE,$PERCUSED"
 
if [ "$FREE" -le "$WARN" -a "$FREE" -gt "$CRIT" ]; then
	echo -n "Warning: $FREE Addresses in pool $POOL free"
	RET=1
elif [ "$FREE" -le "$CRIT" ]; then
	echo -n "Critical: $FREE Addresses in pool $POOL free"
	RET=2
else
	echo -n "OK: $FREE Addresses of $MAX in pool $POOL free"
	RET=0
fi
 
# Performance-Data
echo " | ipAddressesFree=$FREE;$WARN;$CRIT;0;$MAX"
exit $RET

Sample configuration

Here some sample command and service definition for Nagios configuration.

define command {
  command_name   check_dhcp_pool
  command_line   $USER1$/check_dhcp_pool.sh $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$ $ARG4$
}
 
define service {
  host_name            dhcp-server
  service_description  ip-addresses-free
  check_command        check_dhcp_pool!readOnlyCommunity!192.168.0.0
  use                  service-standard
}
Comments (2) Trackbacks (0)
  1. leonNo Gravatar
    15:25 on June 10th, 2010

    Hi all,
    I wrote a simple function for error handling.
    Enjoy it !
    ————-
    function errorhandling {

    if [ ${1} -gt "0" ];then
    echo “Error:” $1
    exit 2
    fi
    }

    if [ $# -lt 3 ]; then
    echo “check_dhcp_pool.sh – Lars Michelsen (Version 0.3 – 2007-10-27)”
    echo “Usage: $0 [] []”
    exit 3
    fi

    IP=”$1″
    COMMUNITY=”$2″
    POOL=”$3″
    WARN=”$4″
    CRIT=”$5″

    if [ ${#WARN} -lt 1 ]
    then
    WARN=10
    fi

    if [ ${#CRIT} -lt 1 ]
    then
    CRIT=5
    fi

    FREEOID=”.1.3.6.1.4.1.311.1.3.2.1.1.3.$POOL”
    USEDOID=”.1.3.6.1.4.1.311.1.3.2.1.1.2.$POOL”

    SNMP_RESULT=`snmpget -v 2c -c $COMMUNITY $IP $FREEOID`
    errorhandling $?

    FREE=`echo $SNMP_RESULT|cut -d ” ” -f4`
    errorhandling $?

  2. leonNo Gravatar
    15:26 on June 10th, 2010

    Hi all,
    I wrote a simple function for error handling.
    Enjoy it !
    ———–
    function errorhandling {

    if [ ${1} -gt "0" ];then
    echo “Error:” $1
    exit 2
    fi
    }

    if [ $# -lt 3 ]; then
    echo “check_dhcp_pool.sh – Lars Michelsen (Version 0.3 – 2007-10-27)”
    echo “Usage: $0 [] []”
    exit 3
    fi

    IP=”$1″
    COMMUNITY=”$2″
    POOL=”$3″
    WARN=”$4″
    CRIT=”$5″

    if [ ${#WARN} -lt 1 ]
    then
    WARN=10
    fi

    if [ ${#CRIT} -lt 1 ]
    then
    CRIT=5
    fi

    FREEOID=”.1.3.6.1.4.1.311.1.3.2.1.1.3.$POOL”
    USEDOID=”.1.3.6.1.4.1.311.1.3.2.1.1.2.$POOL”

    SNMP_RESULT=`snmpget -v 2c -c $COMMUNITY $IP $FREEOID`
    errorhandling $?

    FREE=`echo $SNMP_RESULT|cut -d ” ” -f4`
    errorhandling $?

No trackbacks yet.