snmpinfo.pl een nieuwe nagios plugin

Poos geleden ben ik eens begonnen om zelf een nagios plugin te schrijven. Had het er al eerder over maar heb nu een initiele versie. Let op, gebruik dit niet zomaar op een productie machine. Voorlopig loopt het alleen door alle netwerkkaarten/switchpoorten en geeft een x-aantal waardes weer.

Binnenkort maak ik wel een nette versie die ergens te downloaden is, voor nu maar even een past in een blog.

Patches en opmerkingen welkom.

!/usr/bin/perl

########################### snmpinfo.pl #################
# Version : 0.1
# Date : Oct 12 2010
# Author : Hans Wolters ( h-wolters at nl.linux.org )
# License : GPL http://www.gnu.org/licenses/gpl.txt
# Changelog : Inititial version, will only gather info
#             about all of the devices.
#             Tested on single machines, vmware, cisco
# Usage
# snmpinfo.pl -h hostname -C community -p portnumber
#########################################################

use strict;
#use warnings;

use Net::SNMP;
use Getopt::Long;

my $OID_ifTotal         = ‘.1.3.6.1.2.1.2.1.0’;
my $OID_ifDescr         = ‘.1.3.6.1.2.1.2.2.1.2’;
my $OID_ifType          = ‘.1.3.6.1.2.1.2.2.1.3’;
my $OID_ifMtu           = ‘.1.3.6.1.2.1.2.2.1.4’;
my $OID_ifSpeed         = ‘.1.3.6.1.2.1.2.2.1.5’;
my $OID_ifAdminStatus   = ‘.1.3.6.1.2.1.2.2.1.7’;
my $OID_ifOperStatus    = ‘.1.3.6.1.2.1.2.2.1.8’;
my $OID_ifHighSpeed     = ‘.1.3.6.1.2.1.31.1.1.1.15’;

my $ifTotal = 0;
my @ifList;
my @ifType;
my @ifMtu;
my @ifSpeed;

my @ifHighSpeed;
my @ifAdminStatus;
my @ifOperStatus;

my $hostname;
my $snmpcommunity;
my $snmpport;

my $OKresultstring = undef;

my
%ERRORS=(‘OK’=>0,’WARNING’=>1,’CRITICAL’=>2,’UNKNOWN’=>3,’DEPENDENT’=>4)
;

GetOptions(
“hostname=s”        => \$hostname,
“community=s”      => \$snmpcommunity,
“h=s”                         => \$hostname,
“C=s”                         => \$snmpcommunity,
“p:i”                           => \$snmpport,
“port:i”                      => \$snmpport,

);

my ($session, $error) = Net::SNMP->session(
-hostname        => $hostname || ‘localhost’,
-community      => $snmpcommunity || ‘public’,
-port                    => $snmpport || 161,
);

if (!defined($session)) {
printf(“ERROR: %s.\n”, $error);
exit $ERRORS{“UNKNOWN”};
}

sub get_ifTotal{
my $result = $session->get_request(-varbindlist => [
$OID_ifTotal ],);

if (!defined $result) {
printf “ERROR: %s.\n”, $session->error();
$session->close();
exit $ERRORS{“UNKNOWN”};
} else {
$ifTotal = $result->{$OID_ifTotal};

for (my $i=1; $i < $ifTotal+1; $i++) {
push(@ifList,           $OID_ifDescr.”.$i”);
push(@ifType,           $OID_ifType.”.$i”);
push(@ifMtu,            $OID_ifMtu.”.$i”);
push(@ifSpeed,          $OID_ifSpeed.”.$i”);
push(@ifAdminStatus,    $OID_ifAdminStatus.”.$i”);
push(@ifOperStatus,     $OID_ifOperStatus.”.$i”);
push(@ifHighSpeed,      $OID_ifHighSpeed.”.$i”);

}
}
}

sub get_snmpcardinfo{

$OKresultstring = “”;

for (my $i=0; $i < $ifTotal; $i++) {

my $result = $session->get_request(-varbindlist => [
$ifList[$i] ],);
$OKresultstring = “\n$OKresultstring Device – “.
$result->{$ifList[$i]};

$result = $session->get_request(-varbindlist => [
$ifType[$i] ],);
$OKresultstring = “\n$OKresultstring Type – “.
$result->{$ifType[$i]};

$result = $session->get_request(-varbindlist => [
$ifMtu[$i] ],);
$OKresultstring = “\n$OKresultstring Mtu – “.
$result->{$ifMtu[$i]};

$result = $session->get_request(-varbindlist => [
$ifOperStatus[$i] ],);
$OKresultstring = “\n$OKresultstring OperStatus – “.
$result->{$ifOperStatus[$i]};

my $result = $session->get_request(-varbindlist => [
$ifSpeed[$i] ],);
$OKresultstring = “\n$OKresultstring Speed – “.
$result->{$ifSpeed[$i]};

$result = $session->get_request(-varbindlist => [
$ifAdminStatus[$i] ],);
$OKresultstring = “\n$OKresultstring AdminStatus – “.
$result->{$ifAdminStatus[$i]};

}
$OKresultstring =~ s/^\s+//;
printf (“OK: %s”,$OKresultstring );
exit $ERRORS{“OK”};
}
get_ifTotal();
get_snmpcardinfo();
$session->close();
exit 0;

 

Tagged: , ,

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: