snmpinfo.pl een nieuwe nagios plugin
October 12th, 2010 § Leave a Comment
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;
snmpinfo, a new nagios plugin
September 21st, 2010 § Leave a Comment
Had some fun (see the picture) creating a new snmp plugin. I always wondered if it would not be possible to get all of the ethernet devices in a device and get the complete info about the mtu size, the oper/admin status, etc.. Currently the base works in Nagios, it shows the complete information and return’s an error when an snmp session could not be established or shows an OK when it is possible to get the status.
Next thing to do is toy around with the Getopt::…. options to make sure you can be warned once a named device is down.
It currently works on debian pc’s. Will give it a try at windows pc/servers and other stuff like HP-UX, Cisco, etc…
Update: 22 September
Tested it on Cisco, MSA storage, Vmware ESX, etc… It seems they are all using a good snmp implementation. Will start cleaning up the code and release it as GPL soon.
perl, snmp en Nagios
September 15th, 2010 § Leave a Comment
De afgelopen maanden ben ik voor Nagios veel in de weer geweest met de check_snmp plugin. Een leuke plugin waar je veel informatie mee kunt ophalen. Punt is wel dat je nogal beperkt bent in de opties die je eigenlijk zou willen hebben. Ben daarom ook maar eens zelf gaan knutselen en heb mijn basis in 2 avondjes wel af.
./snmpinfo.pl
Mtu – 16436 Device – lo OperStatus – 1 Speed – 10000000 AdminStatus – 1 Type – 24
Mtu – 1500 Device – eth0 OperStatus – 1 Speed – 10000000 AdminStatus – 1 Type – 6
Mtu – 1500 Device – wlan0 OperStatus – 2 Speed – 10000000 AdminStatus – 1 Type – 6
Mtu – 1500 Device – vboxnet0 OperStatus – 2 Speed – 10000000 AdminStatus – 2 Type – 6
Wat ik o.a. wil bereiken is dat ik naar een device (pc, server, cisco geval) wil wijzen en in 1 plugin aanvraag wil kunnen zien wat er wel en niet op draait. Daarbij wil ik ook informatie over het ifType, de mtu, de snelheid, de errors, etc… zien, ongeacht of een netwerk verbinding nu werkt of niet. Ook wil ik in de argumenten kunnen meegeven welke verbinding open/dicht _moet_ staan.
Moet nog even wat argument zaken doornemen maar dan heb ik weer een nieuwe plugin. Een aanvulling heb ik ook al bedacht, wil eens kijken of ik een routering kan checken.
