Modified: trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm 2012-12-13 12:13:20 UTC (rev 10986)
+++ trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm 2012-12-13 14:08:54 UTC (rev 10987)
@@ -40,14 +40,15 @@
";
my $snmp_args = {
- "snmp-version" => "2c",
+ "snmp-version" => "2c",
+ "snmp-check-oid" => ".1.3.6.1.2.1.1.1.0", # sysDescr
%{ delete $args{snmp} || {} },
};
my $self = $class->SUPER::new(%args);
# Add standard SNMP options to the plugin
- $self->_snmp_add_options($snmp_args);
+ $self->_snmp_add_options( $self->{snmp_args} = $snmp_args );
return $self;
}
@@ -184,7 +185,6 @@
my @args = (
'-hostname' => $hostname,
- '-version' => $opts->get('snmp-version'),
'-port' => $opts->get('port'),
'-timeout' => $opts->get('snmp-timeout'),
);
@@ -202,12 +202,34 @@
push( @args, '-community' => $opts->get('rocommunity') );
}
- my ( $session, $error ) = Net::SNMP->session(@args);
+ my ( $session, $error ) = Net::SNMP->session( "-version", $version, @args );
if ( $error ne '' ) {
$self->die( "Net-SNMP session creation failed: $error" );
}
+ if ( $version eq "2c" && $self->{snmp_args}->{"v1-fallback"} ) {
+ if (
+ !defined $session->get_request(
+ $self->{snmp_args}->{"snmp-check-oid"} ) )
+ {
+
+ ( $session, $error ) = Net::SNMP->session( "-version", "1", @args );
+ if ( $error ne '' ) {
+ $self->die( "Net-SNMP session creation failed: $error" );
+ }
+
+ if (
+ !defined $session->get_request(
+ $self->{snmp_args}->{"snmp-check-oid"} ) )
+ {
+ $self->nagios_die( UNKNOWN,
+ "Agent not responding, tried SNMP v1 and v2c"
+ );
+ }
+ }
+ }
+
return $session;
}
Modified: trunk/opsview-core/nagios-plugins/check_snmp_uptime
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_uptime 2012-12-13 12:13:20 UTC (rev 10986)
+++ trunk/opsview-core/nagios-plugins/check_snmp_uptime 2012-12-13 14:08:54 UTC (rev 10987)
@@ -21,154 +21,43 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-use lib qw ( /usr/local/nagios/perl/lib );
+use warnings;
+
+# TV: Cannot enable strict. There is some mad logic to calculate rollovers of uptime, but
+# I can't see how this is supposed to work. Safest to leave it as it is
+#use strict;
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
use Net::SNMP;
-use Getopt::Std;
+use Opsview::NagiosPlugin::SNMP;
-$script = "check_uptime";
-$script_version = "3.0";
-$script_description =
- "Returns uptime of SNMP agent (usually equal to system uptime). Warning state if less than 1 hour.";
+my $np = Opsview::NagiosPlugin::SNMP->new(
+ usage => "Usage: %s",
+ shortname => "UPTIME",
+ version => "4.0",
+ blurb =>
+ "Returns uptime of SNMP agent (usually equal to system uptime). Warning state if less than 1 hour",
+ snmp => {
+ "snmp-timeout" => 2,
+ "v1-fallback" => 1,
+ },
+);
-$metric = 1;
+$np->getopts;
-$oid_sysDescr =
+my $oid_sysDescr =
".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is actually responding
-$oid_sysUpTime = ".1.3.6.1.2.1.1.3.0"; #
+my $oid_sysUpTime = ".1.3.6.1.2.1.1.3.0"; #
-$ipaddress = "192.168.10.30";
+my $status = 0;
+my $returnstring = "";
-$version = "2c"; # Default SNMP version to use
-$timeout = 2;
-
-$warning = 730; # warning threshold
-$critical = 3650; # critical threshold
-$status = 0;
-$returnstring = "";
-my $port = 161;
-
-$snmpv3_username = "initial"; # SNMPv3 username
-$snmpv3_password = ""; # SNMPv3 password
-$snmpv3_authprotocol = "md5"; # SNMPv3 hash algorithm (md5 / sha)
-$snmpv3_privpassword = ""; # SNMPv3 hash algorithm (md5 / sha)
-$snmpv3_privprotocol = "des"; # SNMPv3 encryption protocol (des / aes / aes128)
-$community = "public"; # Default community string (for SNMP v1 / v2c)
-
-# Do we have enough information?
-if ( @ARGV < 1 ) {
- print "Too few arguments\n";
- usage();
-}
-
-getopts( "hH:C:U:P:v:a:e:w:c:x:p:" );
-if ($opt_h) {
- usage();
- exit(0);
-}
-if ($opt_H) {
- $hostname = $opt_H;
-}
-else {
- print "No hostname specified\n";
- usage();
-}
-if ($opt_C) {
- $community = $opt_C;
-}
-if ($opt_U) {
- $snmpv3_username = $opt_U;
-}
-if ($opt_P) {
- $snmpv3_password = $opt_P;
-}
-if ($opt_a) {
- $snmpv3_authprotocol = $opt_a;
-}
-if ($opt_e) {
- $snmpv3_privprotocol = $opt_e;
-}
-if ($opt_v) {
- $version = $opt_v;
-}
-if ($opt_w) {
- $warning = $opt_w;
-}
-if ($opt_c) {
- $critical = $opt_c;
-}
-if ($opt_x) {
- $snmpv3_privpassword = $opt_x;
-}
-if ($opt_p) {
- $port = $opt_p;
-}
-
-# Checks whether requested SNMP version is supported
-if ( $version !~ /^[13]|[2c]$/ ) {
- print "SNMP v$version not supported by this plugin\n";
- exit(1);
-}
-
# Cache file name
-$statusfile = "/usr/local/nagios/var/uptime_$hostname.db";
+my $hostname = $np->opts->hostname;
+my $statusfile = "/usr/local/nagios/var/uptime_$hostname.db";
-# Create the SNMP session
-if ( $version == "3" ) {
- ( $s, $e ) = Net::SNMP->session(
- -username => $snmpv3_username,
- -authpassword => $snmpv3_password,
- -authprotocol => $snmpv3_authprotocol,
- -privpassword => $snmpv3_privpassword,
- -privprotocol => $snmpv3_privprotocol,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ($s) {
- }
- else {
- print "Agent not responding, tried SNMP v3 ($e)\n";
- exit(1);
- }
-}
+my $s = $np->snmp;
-my $triedv2c = 0; # Track whether we've attempted SNMPv2c connection
-if ( $version == "2c" ) {
- ( $s, $e ) = Net::SNMP->session(
- -community => $community,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-
- # try SNMP v1 if v2c doesn't work
- $triedv2c = 1;
- $version = 1;
- }
-}
-
-if ( $version == "1" ) {
- ( $s, $e ) = Net::SNMP->session(
- -community => $community,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ( !defined( $s->get_request($oid_sysDescr) ) ) {
- if ( $triedv2c == 1 ) {
- print "Agent not responding, tried SNMP v1 and v2c\n";
- }
- else {
- print "Agent not responding, tried SNMP v1\n";
- }
- exit(3);
- }
-}
-
main();
# Close the session
@@ -207,6 +96,7 @@
return 1;
}
}
+ my $uptime;
foreach ( $s->var_bind_names() ) {
$uptime = $s->var_bind_list()->{$_};
}
@@ -225,14 +115,15 @@
return 1;
}
}
+ my $timeticks;
foreach ( $s->var_bind_names() ) {
$timeticks = $s->var_bind_list()->{$_};
}
- $days = 0;
- ( $days, $null ) = split( /days/, $uptime );
- $days =~ s/ //g;
+ # Convert to days
+ my $days = sprintf( "%.1f", $timeticks / 100 / 60 / 60 / 24 );
+ my $temp;
$temp = "Uptime: $uptime|days=$days";
append($temp);
@@ -273,85 +164,11 @@
}
-sub usage {
- print << "USAGE";
-
-$script v$script_version
-
-$script_description
-
-Usage: $script -H <hostname> -c <community> [...]
-Options: -H Hostname or IP address
- -p Port (default: 161)
- -C Community (default is public)
- -U SNMPv3 auth username
- -P SNMPv3 auth password
- -x SNMPv3 priv passphrase
- -a SNMPv3 hashing algorithm (default is MD5)
- -e SNMPv3 encryption protocol (default is DES)
- -v SNMP version (1, 2c or 3 supported)
-
---------------------------------------------------------------------
-Copyright (C) 2003-2012 Opsview Limited. All rights reserved
-
-This program is free software; you can redistribute it or modify
-it under the terms of the GNU General Public License
---------------------------------------------------------------------
-
-USAGE
- exit 1;
-}
-
sub append {
- my $appendstring = @_[0];
+ my $appendstring = $_[0];
$returnstring = "$returnstring$appendstring";
}
-sub errorhandler {
- my $returnstring = @_[0];
- print "$returnstring\n";
-}
-
-####################################################################
-# Returns the SNMP community string for a given hostname / IP #
-####################################################################
-
-sub return_snmpcommunity {
- my $requiredhostname = @_[0];
- my $returncommunity = "public";
- my $tempcommunity = "";
- my $temphostname = "";
-
- my $row = 0;
- my $nullval = 0;
-
- if ( -e "$configfilepath/livehosttable.db" ) {
- open my $infile, "<$configfilepath/livehosttable.db"
- or die "Can't open file $configfilepath/livehosttable.db $1";
- foreach $line (<$infile>) {
- (
- $temphostname, $nullval, $nullval, $nullval, $nullval, $nullval,
- $nullval, $tempcommunity
- ) = split( /:/, $line );
- if ( $temphostname eq $requiredhostname ) {
- if ( $tempcommunity eq "" ) {
- $returncommunity = $defaultcommunity;
- }
- else {
- $returncommunity = $tempcommunity;
-
- # print "lookup for $temphostname successful: $tempcommunity\n";
- }
- last;
- }
- }
- }
- else {
- }
- close $infile;
- return ($returncommunity);
-}
-
sub readstatus {
$prev_uptime = 0;
$just_rolled_over = 0;