Title: [opsview] [10979] Added SNMP options framework, subclassing Nagios::Plugin.
Revision
10979
Author
tvoon
Date
2012-12-13 10:28:16 +0000 (Thu, 13 Dec 2012)

Log Message

Added SNMP options framework, subclassing Nagios::Plugin. Updated check_snmp_apcups to use this new framework

Modified Paths

Added Paths

Modified: trunk/opsview-core/filelist
===================================================================
--- trunk/opsview-core/filelist	2012-12-13 06:53:16 UTC (rev 10978)
+++ trunk/opsview-core/filelist	2012-12-13 10:28:16 UTC (rev 10979)
@@ -321,6 +321,8 @@
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/Monitoringserver.pm lib/Opsview/Monitoringserver.pm
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/Monitoringclusternode.pm lib/Opsview/Monitoringclusternode.pm
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/Nagios.pm lib/Opsview/Nagios.pm
+d nagios:nagios 0755 /usr/local/nagios/lib/Opsview/NagiosPlugin
+f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/NagiosPlugin/SNMP.pm lib/Opsview/NagiosPlugin/SNMP.pm
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/NotificationMethods.pm lib/Opsview/NotificationMethods.pm
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/Timeperiod.pm lib/Opsview/Timeperiod.pm
 f nagios:nagios 0644 /usr/local/nagios/lib/Opsview/Hostgroupinfo.pm lib/Opsview/Hostgroupinfo.pm

Added: trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm	                        (rev 0)
+++ trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm	2012-12-13 10:28:16 UTC (rev 10979)
@@ -0,0 +1,220 @@
+# Inspired by Nagios::Plugin::SNMP, but with much reduced option set
+package Opsview::NagiosPlugin::SNMP;
+
+use warnings;
+use strict;
+
+use Exporter;
+use base qw(Exporter Nagios::Plugin);
+
+use Net::SNMP;
+
+#  Have to copy, inheritence doesn't work for these
+use constant OK       => 0;
+use constant WARNING  => 1;
+use constant CRITICAL => 2;
+use constant UNKNOWN  => 3;
+
+our @EXPORT = qw(OK WARNING CRITICAL UNKNOWN);
+
+our $VERSION = '1.0';
+
+our $SNMP_USAGE = <<EOF;
+
+-H,--hostname=HOST -p,--port=INT -v,--snmp-version=1|2c|3 
+-C,--rocommunity=S 
+-u,--auth-username=S -P,--auth-password=S -a,--auth-protocol=S -e,--priv-protocol=S -x,--priv-password=S
+EOF
+
+sub new {
+    my $class = shift;
+    my %args  = @_;
+
+    # Not sure this should be set as it confuses
+    #$args{'usage'} .= $SNMP_USAGE;
+
+    $args{license}
+      ||= "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
+";
+
+    my $snmp_args = {
+        "snmp-version" => "2c",
+        %{ delete $args{snmp} || {} },
+    };
+
+    my $self = $class->SUPER::new(%args);
+
+    #  Add standard SNMP options to the plugin
+    $self->_snmp_add_options($snmp_args);
+
+    return $self;
+}
+
+#  Add Nagios::Plugin options related to SNMP to the plugin
+
+sub _snmp_add_options {
+    my ( $self, $defaults ) = @_;
+
+    $self->add_arg(
+        'spec'     => 'hostname|H=s',
+        'help'     => "-H, --hostname HOSTNAME\n   Host to check HOSTNAME",
+        'required' => 1
+    );
+
+    # We have to support -v for snmp_version, so this will override Nagios::Plugin's -v options
+    $self->add_arg(
+        'spec' => 'snmp-version|v=s',
+        'help' =>
+          "-v, --snmp-version 1|2c|3 [default 2c]\n   Select appropriate SNMP version. Disables --verbose",
+        'default' => $defaults->{"snmp-version"},
+    );
+
+    $self->add_arg(
+        'spec' => 'rocommunity|C=s',
+        'help' =>
+          "-C, --rocommunity NAME\n   Community string. SNMP v1|2c only. Defaults to 'public'",
+        'default' => 'public'
+    );
+
+    $self->add_arg(
+        'spec' => 'auth-username|U=s',
+        'help' =>
+          "-U, --auth-username USERNAME\n   Auth username. SNMP v3 only",
+        default => '',
+    );
+
+    $self->add_arg(
+        'spec' => 'auth-password|P=s',
+        'help' => "-P, --auth-password PASSWORD\n   Auth password. SNMPv3 only",
+        default => '',
+    );
+
+    $self->add_arg(
+        'spec' => 'auth-protocol|a=s',
+        'help' => "-a, --auth-protocol PROTO\n"
+          . "   Auth protocol. SNMPv3 only. Default md5",
+        'default' => 'md5'
+    );
+
+    $self->add_arg(
+        'spec' => 'priv-password|x=s',
+        'help' => "-x, --priv-password PASSWORD\n"
+          . "   Priv password. SNMPv3 only",
+        'default' => ''
+    );
+
+    $self->add_arg(
+        'spec' => 'priv-protocol|e=s',
+        'help' => "-e, --priv-protocol PROTO\n"
+          . "   Priv protocol. SNMPv3 only. Default des",
+        'default' => 'des'
+    );
+
+    $self->add_arg(
+        'spec'    => 'port|p=s',
+        'help'    => "-p, --port INT\n   SNMP agent port. Default 161",
+        'default' => '161'
+    );
+
+    my $snmp_timeout = $defaults->{"snmp-timeout"}
+      || ( ( $self->opts->{timeout} || 10 ) - 1 );
+    $self->add_arg(
+        'spec' => 'snmp-timeout|T=s',
+        'help' =>
+          "-T, --snmp-timeout INT\n   SNMP timeout. Defaults to plugin timeout - 1 second",
+        'default' => $snmp_timeout,
+    );
+}
+
+=head3 _snmp_validate_opts() - Validate passed in SNMP options
+
+This method validates that any options passed to the plugin using
+this library make sense.  Rules:
+
+=over 4
+
+ * If SNMP is version 1 or 2c, rocommunity must be set
+ * If SNMP is version 3, auth-username and auth-password must be set
+
+=back
+
+=cut
+
+sub _snmp_validate_opts {
+
+    my $self = shift;
+
+    my $opts = $self->opts;
+
+    if ( $opts->get('snmp-version') eq '3' ) {
+
+        my @errors;
+
+        for my $p (qw(auth-username auth-password auth-protocol)) {
+            push( @errors, $p ) unless $opts->get($p);
+        }
+
+        $self->die( "SNMP parameter validation failed.  Missing: "
+              . join( ', ', @errors ) )
+          if scalar(@errors) > 0;
+
+    }
+    else {
+
+        $self->die("SNMP parameter validation failed. Missing rocommunity!")
+          if $opts->get('rocommunity') eq '';
+
+    }
+
+    return 1;
+
+}
+
+# Makes connection and returns the Net::SNMP object
+sub snmp {
+    my $self = shift;
+
+    my $opts = $self->opts;
+
+    my $version = $opts->get( 'snmp-version' );
+
+    my $hostname = $opts->get( 'hostname' );
+
+    my @args = (
+        '-hostname' => $hostname,
+        '-version'  => $opts->get('snmp-version'),
+        '-port'     => $opts->get('port'),
+        '-timeout'  => $opts->get('snmp-timeout'),
+    );
+    if ( $version eq '3' ) {
+        push(
+            @args,
+            '-username'     => $opts->get('auth-username'),
+            '-authpassword' => $opts->get('auth-password'),
+            '-authprotocol' => $opts->get('auth-protocol'),
+            '-privpassword' => $opts->get('priv-password'),
+            '-privprotocol' => $opts->get('priv-protocol')
+        );
+    }
+    else {
+        push( @args, '-community' => $opts->get('rocommunity') );
+    }
+
+    my ( $session, $error ) = Net::SNMP->session(@args);
+
+    if ( $error ne '' ) {
+        $self->die( "Net-SNMP session creation failed: $error" );
+    }
+
+    return $session;
+}
+
+sub getopts {
+    my $self = shift;
+    $self->SUPER::getopts();
+    $self->_snmp_validate_opts();
+}
+
+1;

Modified: trunk/opsview-core/nagios-plugins/check_snmp_apcups
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_apcups	2012-12-13 06:53:16 UTC (rev 10978)
+++ trunk/opsview-core/nagios-plugins/check_snmp_apcups	2012-12-13 10:28:16 UTC (rev 10979)
@@ -17,26 +17,37 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA
 
+use warnings;
+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_snmp_apcups";
-$script_version = "2.1.1";
+# This plugin used to default to snmp-version as 1
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "APCUPS",
+    version   => "3.0",
+    blurb     => "Monitors APC SmartUPS via AP9617 SNMP management card",
+    snmp      => {
+        "snmp-version" => 1,
+        "snmp-timeout" => 2,
+    },
+);
 
-$metric = 1;
+$np->add_arg(
+    'spec' => 'convert-to-fahrenheit|F',
+    'help' =>
+      "-F, --convert-to-fahrenheit\n   Converts Celsius temperature to Fahrenheit. Default Celsius",
+    'default' => 0,
+);
 
-$ipaddress = "192.168.1.1"; # default IP address, if none supplied
-$version   = "1";           # SNMP version
-$timeout   = 2;             # SNMP query timeout
-my $port = 161;
+$np->getopts;
 
-# $warning = 100;
-# $critical = 150;
-$status       = 0;
-$returnstring = "";
+my $status       = 0;
+my $returnstring = "";
 
-$community = "public";      # Default community string
-
 # .1.3.6.1.4.1.
 # enterprises.318.1.1.1.1.1.1.0 = STRING: "SMART-UPS 1000"		upsIdent
 
@@ -59,81 +70,44 @@
 # enterprises.318.1.1.1.4.2.1.0			Configured voltage
 # enterprises.318.1.1.1.8.1.0			Whether agent is communicating with UPS (1)
 
-$oid_upstype                     = ".1.3.6.1.4.1.318.1.1.1.1.1.1.0";
-$oid_battery_capacity            = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
-$oid_battery_temperature         = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
-$oid_battery_runtimeremain       = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
-$oid_battery_replace             = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
-$oid_input_voltage               = ".1.3.6.1.4.1.318.1.1.1.3.2.1.0";
-$oid_input_frequency             = ".1.3.6.1.4.1.318.1.1.1.3.2.4.0";
-$oid_input_reasonforlasttransfer = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
-$oid_output_voltage              = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
-$oid_output_frequency            = ".1.3.6.1.4.1.318.1.1.1.4.2.2.0";
-$oid_output_load                 = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
-$oid_output_current              = ".1.3.6.1.4.1.318.1.1.1.4.2.4.0";
-$oid_output_configuredvoltage    = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
-$oid_comms                       = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
-$oid_test_result                 = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";
-$oid_test_date                   = ".1.3.6.1.4.1.318.1.1.1.7.2.4.0";
-$oid_sysDescr                    = ".1.3.6.1.2.1.1.1.0";
+my $oid_upstype                     = ".1.3.6.1.4.1.318.1.1.1.1.1.1.0";
+my $oid_battery_capacity            = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
+my $oid_battery_temperature         = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
+my $oid_battery_runtimeremain       = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
+my $oid_battery_replace             = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
+my $oid_input_voltage               = ".1.3.6.1.4.1.318.1.1.1.3.2.1.0";
+my $oid_input_frequency             = ".1.3.6.1.4.1.318.1.1.1.3.2.4.0";
+my $oid_input_reasonforlasttransfer = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
+my $oid_output_voltage              = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
+my $oid_output_frequency            = ".1.3.6.1.4.1.318.1.1.1.4.2.2.0";
+my $oid_output_load                 = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
+my $oid_output_current              = ".1.3.6.1.4.1.318.1.1.1.4.2.4.0";
+my $oid_output_configuredvoltage    = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
+my $oid_comms                       = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
+my $oid_test_result                 = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";
+my $oid_test_date                   = ".1.3.6.1.4.1.318.1.1.1.7.2.4.0";
+my $oid_sysDescr                    = ".1.3.6.1.2.1.1.1.0";
 
-$upstype                     = "";
-$battery_capacity            = 0;
-$battery_temperature         = 0;
-$battery_runtimeremain       = 0;
-$battery_replace             = "";
-$convert_temp                = 0;
-$input_voltage               = 0;
-$input_frequency             = 0;
-$input_reasonforlasttransfer = "";
-$output_voltage              = 0;
-$output_frequency            = 0;
-$output_load                 = 0;
-$output_current              = 0;
-$output_configuredvoltage    = 0;
-$outagecause                 = "";
-$test_result                 = "";
-$test_date                   = "";
+my $upstype                     = "";
+my $battery_capacity            = 0;
+my $battery_temperature         = 0;
+my $battery_runtimeremain       = 0;
+my $battery_replace             = "";
+my $convert_temp                = 0;
+my $input_voltage               = 0;
+my $input_frequency             = 0;
+my $input_reasonforlasttransfer = "";
+my $output_voltage              = 0;
+my $output_frequency            = 0;
+my $output_load                 = 0;
+my $output_current              = 0;
+my $output_configuredvoltage    = 0;
+my $outagecause                 = "";
+my $test_result                 = "";
+my $test_date                   = "";
 
-# Do we have enough information?
-if ( @ARGV < 1 ) {
-    print "Too few arguments\n";
-    usage();
-}
+my $s = $np->snmp;
 
-getopts( "hH:C:w:c:Fp:" );
-if ($opt_h) {
-    usage();
-    exit(0);
-}
-if ($opt_H) {
-    $hostname = $opt_H;
-}
-else {
-    print "No hostname specified\n";
-    usage();
-}
-if ($opt_F) {
-    $convert_temp = 1;
-}
-if ($opt_C) {
-    $community = $opt_C;
-}
-else {
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
-
-# Create the SNMP session
-my ( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-    -port      => $port,
-);
-
 main();
 
 # Close the session
@@ -180,6 +154,7 @@
             return 1;
         }
     }
+    my $temp;
     foreach ( $s->var_bind_names() ) {
         $temp = $s->var_bind_list()->{$_};
     }
@@ -426,7 +401,7 @@
     }
     #######################################################
 
-    $issue = "";
+    my $issue = "";
 
     if ( $input_reasonforlasttransfer eq "1" ) {
         $outagecause = "No events";
@@ -462,6 +437,7 @@
         $outagecause = "Cannot establish reason";
     }
 
+    my $test_result_string;
     if ( $test_result eq "1" ) {
         $test_result_string = "Passed";
     }
@@ -513,50 +489,21 @@
           "$issue - $upstype - BATTERY:(capacity $battery_capacity%%, temperature $battery_temperature, runtime $battery_runtimeremain minutes) INPUT:(voltage $input_voltage V, frequency $input_frequency Hz) OUTPUT:(voltage $output_voltage V, frequency $output_frequency Hz, load $output_load%%)    LAST EVENT:$outagecause";
     }
     append($temp);
-    $minutes = $battery_runtimeremain;
-    ( $minutes, $null ) = split( /minutes/, $battery_runtimeremain );
+    my $minutes = $battery_runtimeremain;
+    ( $minutes, undef ) = split( /minutes/, $battery_runtimeremain );
     $minutes =~ s/ //g;
-    ( $celsius, $null ) = split( / /, $battery_temperature );
-    $perfinfo = sprintf
+    my ( $celsius, undef ) = split( / /, $battery_temperature );
+    my $perfinfo = sprintf
       "|runtime=$minutes temp_celsius=$celsius in_volts=$input_voltage out_volts=$output_voltage load=$output_load%%";
     append($perfinfo);
 
 }
 
 ####################################################################
-# help and usage information                                       #
-####################################################################
-
-sub usage {
-    print << "USAGE";
------------------------------------------------------------------	 
-$script v$script_version
-
-Monitors APC SmartUPS via AP9617 SNMP management card.
-
-Usage: $script -H <hostname> -c <community> [...]
-
-Options: -H 	Hostname or IP address
-         -p 	Port (default: 161)
-         -C 	Community (default is public)
-         -F 	Display temperature in Fahrenheit
-	 
------------------------------------------------------------------	 
-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;
-}
-
-####################################################################
 # Appends string to existing $returnstring                         #
 ####################################################################
 
 sub append {
-    my $appendstring = @_[0];
+    my $appendstring = $_[0];
     $returnstring = "$returnstring$appendstring";
 }

_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to