Monitoring DRBD using Nagios and SNMP

9 Oct

Wrote a Naguis script to monitor DRBD using SNMP (I don’t really understand why Nagios made up its own plugin system when you can just use SNMP??), Anyway, to make this work you’ll need to:

1. Add a new ‘Check Command’ into Nagios’ checkcommands.cfg, something like: (the check_smpd_drbd.pl script is below)


define command{
command_name check_snmp_drbd
command_line $USER1$/check_snmp_drbd.pl -h $HOSTADDRESS$
}

2. Add a new ‘service’ definition to your services/**.cfg, something like:


define service{
use generic-service
host_name host_to_monitor
service_description DISK STATUS
is_volatile 0
check_period workhours
max_check_attempts 10
normal_check_interval 5
retry_check_interval 2
contact_groups infoservices-admins
notification_interval 120
notification_period 24x7
notification_options c,r
check_command check_snmp_drbd
}

3. On the target machine, you’ll need to make sure your snmpd daemon is sending you what you want, for Net-SNMP i just changed /etc/snmpd/snmpd.conf appending:


exec drbd_data /sbin/drbdadm state data
exec drbd_home /sbin/drbdadm state home
exec drbd_share /sbin/drbdadm state share

4. Make sure your firewall on the target machine allows snmpd through, you might want to put snmpd in your startup scripts to

5. The script to actually do the monitoring from the Nagios machine is:


#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my ($hostname, $snmp_resources, $snmp_values);
my (@resources, @values);
my (@tmp, @tmp2, $tmp3);
my ($key, $value);
my %status = ();
my $i;
my $x;
my $error;

# Get the command line options
# only "-h Hostname"
Getopt::Long::Configure ("bundling");
GetOptions(
'h=s' => \$hostname);

# Grab the snmp details - note this should
# probably use Net::SNMP
$snmp_resources=`/usr/bin/snmpwalk -v 1 -c snmponly $hostname 1.3.6.1.4.1.2021.8.1.2 2>/dev/null`;
$snmp_values=`/usr/bin/snmpwalk -v 1 -c snmponly $hostname 1.3.6.1.4.1.2021.8.1.101 2>/dev/null`;

# Didn't get any output - error
if ($snmp_resources eq "" )
{
print "Unknown host: $hostname\n";
exit $ERRORS{"CRITICAL"};
}

@resources = split(/\n/,$snmp_resources);
@values = split(/\n/,$snmp_values);
for ($i=0;$i< $#resources+1;$i++)
{
@tmp = split(/\s+/,$resources[$i]);
@tmp2 = split(/\s+/,$values[$i]);
$tmp3 = $values[$i];
$tmp3 =~ s/UCD-SNMP-MIB::extOutput..* = STRING: //g;
$status{$tmp[3]} = $tmp3;
}

# Check for "Primary/Secondary" or "Secondary/Primary"
while(($key, $value) = each(%status))
{
if (!($value eq "Primary/Secondary") && !($value eq "Secondary/Primary"))
{
print "ERROR: $key says: $value\n";
$error = 1;
}
}

# Send out status
if ($error)
{
exit $ERRORS{"CRITICAL"};
} else {
print "DRBD OK: ";
foreach $key (keys %status)
{
print $key . ",";
}
print "\n";
exit $ERRORS{"OK"};
}

Advertisements

One Response to “Monitoring DRBD using Nagios and SNMP”

  1. james September 7, 2011 at 11:49 am #

    I tried using this but either get Unknown host or Use of uninitialized value $hostname in concatenation when I run it at command line on the nagios server.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: