Remote DHCP

Martijn Lievaart m at rtij.nl
Wed Dec 5 14:48:16 CET 2007


<citaat van="Jose Manuel García">
> Hi,
>
>
>
> I’m writting from Spain and my english is not very good, thought, I’ll try
> to do my best.
>
>
>
> I have a Nagios 2.9 under Ubuntu 6.10. Everything works fine but I need to
> monitor a Remote Windows DHCP Server and I don’t know how. Evidently
> check_dhcp is not useful in remote hosts.

Just wrote this yesterday (mostly ripped from check_dhcp.pl), you need to
install dhcping.

/usr/local/nagios/plugins/check-dhcping:
#!/usr/bin/perl -w
#
# dhcping suid wrapper
#

use strict;
use warnings;

use Getopt::Long qw(:config no_ignore_case);
use IO::Socket;
use IO::Interface;

my ($status, $host, $mac, $ip, $verbose, $timeout);
my $interface = 'eth0';
my $VERSION     = 0.1;
my $HELP        = 0;
my $DOTTED_QUAD = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

%ENV=();

# Default values
$timeout = 3;

my %STATUSCODE = (  'OK'       => '0',
                    'WARNING'  => '1',
                    'CRITICAL' => '2',
		    'UNKNOWN'  => '3');

my $usage = <<EOF;

This plugin tests the availability of a given DHCP server using unicast
delivery.
This is necessary if the monitoring host is on a different subnet. Using
broadcast
and a DHCP relay would also prevent the check from probing individual
servers.

Usage: check_dhcp -H|--host [-i|--interface] [-t|--timeout]
 -H, --host=ADDRESS
   IP address of DHCP server that we must hear from
 -i, --interface=STRING
   Interface to use for obtaining ip/mac address from (default eth0)
 -t, --timeout=INTEGER
   Seconds to wait for DHCPOFFER before timeout occurs (default: $timeout)
 -h, --help
   Print this help screen

Note: This plugin will need to run with root permissions in order to
access priviledge
      ports 67 & 68.  Since Nagios by default runs its plugins under
unpriviledged
      user 'nagios', you will need to setuid root like:

           % chown root:root check_dhcping.pl
           % chmod 4755 check_dhcping.pl


EOF

my $debug;

# handle cmdline args
my $result = GetOptions( "H|host=s"        => \$host,
			 "i|interface=s"   => \$interface,
			 "t|timeout:i"     => \$timeout,
			 "h|help"          => \$HELP,
			 "d|debug"         => \$debug,
			 );

if( !$result ) {
    print "ERROR: Problem with cmdline args\n";
    print $usage;
    exit($STATUSCODE{'UNKNOWN'});
}
if( $HELP ) {
    print $usage;
    exit($STATUSCODE{'UNKNOWN'});
}
if ( !$host ){
    print "ERROR: Missing required arguments\n";
    print $usage;
    exit($STATUSCODE{'UNKNOWN'});
}
unless ( $host =~ /($DOTTED_QUAD)/ ){
    print "Invalid IP Address: $host\n";
    exit($STATUSCODE{'UNKNOWN'});
}
$host = $1;

# Get the mac and IP address for the given interface
my $s = IO::Socket::INET->new(Proto => 'udp');
if ( !($ip  = $s->if_addr($interface)) ){
    print "Error: $!\n";
    exit($STATUSCODE{'CRITICAL'});
}
if ( !($mac  = $s->if_hwaddr($interface)) ){
    print "Error: $!\n";
    exit($STATUSCODE{'CRITICAL'});
}
# This is needed in order to avoid sperl complaining
# about "Insecure dependencies"
unless ( $ip =~ /($DOTTED_QUAD)/ ){
    print "Invalid IP Address: $ip\n";
    exit($STATUSCODE{'UNKNOWN'});
}
$ip = $1;

unless ($mac =~ /((?:[[:xdigit:]]{2}:){5}[[:xdigit:]]{2})/) {
    print "Invalid hw Address: $mac\n";
    exit($STATUSCODE{'UNKNOWN'});
}
$mac = $1;

my $args = "-i -t $timeout -c $ip -s $host -g $ip -h $mac";
$debug and print "ip=$ip, args=$args\n";
system "/usr/bin/dhcping $args";

if ($? == -1) {
    print "failed to execute dhcping: $!\n";
    exit($STATUSCODE{'UNKNOWN'});
}

if ($? & 127) {
    printf "child died with signal %d\n", ($? & 127);
    exit($STATUSCODE{'UNKNOWN'});
}

# Note that dhcping has already provided some text on stdout,
# which is reasonably suitable for nagios.
# All that is left is to return an appropriate return code.
my $rc = $? >> 8;
exit ($rc ? 2 : 0);


-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list