I have a DHCP server that sometime has no free IP addresses to assign. What can I do to monitor how many IP are still available?
Well, perl and NSClient++ installed on my Micro$oft DHCP server, I wrote a simple program to execute and parse the command
netsh dhcp server show mibinfo
that gives some output containing the data I need. The source is quite simple: it assume the DHCP server is on a 10.20.x.y network and prints a list of comma separated numbers: the network (30 means 10.20.30.0/24), the assigned address number, the free address number.
#!/bin/perl
# the output will be a list like this:
# (30,74,95) (31,139,7) (32,110,1)$mystr=””;
@myoutput=`netsh dhcp server show mibinfo`;
foreach $myrow (@myoutput) {
if ( $myrow =~ /Subnet = 10\.20\.(.*)\.0\./ ) {
$mystr = $mystr . “($1,”;
}
if ( $myrow =~ /(.*) Addresses in use = (.*)\./ ) {
$mystr = $mystr . “$2,”;
}
if ( $myrow =~ /(.*) free Addresses = (.*)\./ ) {
$mystr = $mystr . “$2) “;
}
}
print “$mystr”;
Then I needed to add this line to the nsc.ini file, in order to run my perl script remotely
check_dhcp=C:\NSClient++\scripts\check_dhcp.pl
Once restarted the NSClient service, from my nagios server it’s possible to get the result from command line:
$ /usr/lib/nagios/plugins/check_nrpe -H myserverdhcp -c check_dhcp
(31,75,94) (32,144,2) (33,111,0)
A simple bash script plugin can be written to parse the result and getting nagios able to monitor the DHCP free address number value. E.g. this
#!/bin/bash
MYHOST=$1
MYNET=$2
ThresholdWARN=$3
ThresholdCRIT=$4
# Return values
RET_OK=”0″
RET_WARN=”1″
RET_CRIT=”2″
RET_UNKN=”3″checkdata () {
VAL=`echo $2 | wc | awk ‘{print $2}’`
if [ $VAL -eq 0 ]; then
echo $1 is not set
exit $RET_UNKN
fi
}# MAIN
checkdata “Remote IP” $MYHOST
checkdata “Network number” $MYNET
checkdata “Threshold WARN” $ThresholdWARN
checkdata “Threshold CRIT” $ThresholdCRITMYRETSTRING=`/usr/lib/nagios/plugins/check_nrpe -H $MYHOST -c check_dhcp`
MYFREEADDR=`echo $MYRETSTRING | sed -e “s/.*($MYNET,//” | sed -e “s/).*//” | sed -e “s/.*,//”`
checkdata “IP number” $MYFREEADDREXTRAMESSG=”|’DHCPfreeAddr’=$MYFREEADDR$MYNET””;$ThresholdWARN;$ThresholdCRIT”
if [ $MYFREEADDR -lt $ThresholdCRIT ]; then
echo “CRITICAL – Only $MYFREEADDR IP Available for $MYNET network$EXTRAMESSG”
exit $RET_CRIT
fi
if [ $MYFREEADDR -lt $ThresholdWARN ]; then
echo “WARNING – Only $MYFREEADDR IP Available for $MYNET network$EXTRAMESSG”
exit $RET_WARN
fi
echo “OK – $MYFREEADDR IP Available for $MYNET network$EXTRAMESSG”
exit $RET_OK
Then nagios has to be set up with the usual lines in the command configuration file
define command{
command_name check_nt_dhcp
command_line /usr/lib/nagios/plugins/check_nt_dhcp.sh $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
And in the services file:
define service{
use generic-service ; Name of service template to usehost_name dhcpserver
service_description Free IP on 10.20.33.0 network
is_volatile 0
check_period 24×7
max_check_attempts 10
normal_check_interval 30
retry_check_interval 30
contact_groups admins
notification_interval 240
notification_period 24×7
notification_options c,r
check_command check_nt_dhcp!33!10!5
}