Using SNMP extend feature in Nagios

In Supportex we monitor a lot of web services and devices. One of the most convenient ways to monitor various parameters is SNMP extend feature.

Let’s consider how it works. Assume you already have SNMP daemon installed.

    1. Firstly you need to add configuration line in you SNMP daemon config and restart daemon:

extend raid-md0 /usr/local/bin/check_md_raid.pl

To use Nagios with extend feature you can use Michal Ludvig’s plugin, which could be found on his page.

    1. Next you need to create Nagios command:
      check_snmp_extend:USER1$/check_snmp_extend_em.pl -H $HOSTADDRESS$ -c $ARG1$
    2. And finally create Nagios service:
define service {
use check_snmp_extend_service
service_description RAID md0 	status
check_command 	check_snmp_extend!raid-md0
host_name server41
contact_groups 	Supportex
}

To meet our requirements we wrote our own script for Nagios with pure Perl, so it doesn’t fork external command  snmpget now and should work faster. Besides it could be used with  embedded Nagios Perl.
To use it you should install Net::SNMP library for Perl. So use

[root@playground ~]#yum install perl-Net-SNMP

for Centos or Fedora and

[root@playground ~]#aptitude install libsnmp-perl

for Ubuntu or Debian.

Download script for getting SNMP extend output.

Here it is:

#!/usr/bin/perl
# nagios: +epn
#
# Pure Perl script for getting SNMP extend output.
# Alex Tykhonov, Supportex.net, 2010.
# http://supportex.net/
# mail:
use POSIX;
use strict;
use Getopt::Long;
use lib "/usr/lib64/nagios/plugins" ;
use utils qw(%ERRORS &print_revision &support &usage );
use Net::SNMP v5.1.0 qw(snmp_type_ntop DEBUG_ALL);
sub print_usage {print '';};

# SNMP stuff:
my $USER = "yoursnmpuser";
my $PASS = "strongspassword";
my $AUTHPROT = "yourAuthProt";
my $PRIVPROT = "yourProvProt";

# Host related
my $host_address;
my $snmp_port;
my $command;

GetOptions("port=s", $snmp_port, "c=s", $command, "H=s", $host_address);
unless ( defined($host_address) && defined($command) )
{
print_usage();
}

if ( !defined($snmp_port) ) {$snmp_port = "161"; }

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

my ($s, $error) = Net::SNMP->session(
-hostname => $host_address,
-version => "3",
-username => $USER,
-authprotocol => $AUTHPROT,
-authpassword => $PASS,
-privpassword => $PASS,
-privprotocol => $PRIVPROT,
-port => $snmp_port );
if (!defined($s)) {
printf("ERROR: %s.n", $error);
exit 1;
}

my $oid = ".1.3.6.1.4.1.8072.1.3.2.3.1.2.";
$oid = $oid . length($command);

my @array = split(//, $command);
my $item;
my $ret = 3;
foreach $item (@array) { $oid = $oid . "." . ord($item); }

my $response = $s->get_request(
-varbindlist => [$oid]
);
$s->close;

my $output = $response->{$oid};
if ( $output =~ /OK/ ) { $ret = 0; }
if ( $output =~ /WARNING/ ) { $ret = 1; }
if ( $output =~ /CRITICAL/ ) { $ret = 2; }

print $output, "n";

exit($ret);

Didn’t find the answer to your question? Ask it our administrators to reply we will publish on website.

Leave a Reply

Your email address will not be published. Required fields are marked *