Title: [opsview] [11525] Updated check_snmp_cisco_linkstatus to use SNMPv3 library
Revision
11525
Author
hstevenson
Date
2013-02-19 12:49:04 +0000 (Tue, 19 Feb 2013)

Log Message

Updated check_snmp_cisco_linkstatus to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_cisco_linkstatus
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_cisco_linkstatus	2013-02-19 11:03:50 UTC (rev 11524)
+++ trunk/opsview-core/nagios-plugins/check_snmp_cisco_linkstatus	2013-02-19 12:49:04 UTC (rev 11525)
@@ -20,46 +20,106 @@
 #    along with Opsview; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #
-
 use strict;
-use lib qw ( /usr/local/nagios/perl/lib );
+use warnings;
+
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
 use Net::SNMP;
+use Opsview::NagiosPlugin::SNMP;
 use DBI;
-use Getopt::Std;
 
-# About us
-my $script             = "check_snmp_cisco_linkstatus";
-my $script_version     = "0.6";
-my $script_description = "Monitors specified interface for link state changes";
+my $script = "check_snmp_cisco_linkstatus";
 
-my $hostname = '192.168.10.20';
-my $port     = 161;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => $script,
+    version   => "0.7",
+    blurb     => "Monitors specified interface for link state changes",
+    snmp      => {
+        "snmp-version" => 2,
+        "snmp-timeout" => 5,
+        "v1-fallback"  => 1,
+    },
+);
 
-# Cache hold time in seconds
-my $cache_hold_time = 1800;
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold (bits per second or n%)},
+    default => 0,
+);
 
-# Where to put the .db file
-my $db_home = "/usr/local/nagios/var/";
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold (bits per second or n%)},
+    default => 0,
+);
 
-# Name of the database file
-my $db_name = "$script.db";
+$np->add_arg(
+    spec => "expirytime|t=s",
+    help => qq{-t, --expirytime=INTEGER
+    Cache expiry time - the time in seconds before the
+    monitored device's interface table is rescanned for
+    new or changed interfaces},
+    default => 60,
+);
 
-# Has the cache expired?
-my $cache_expired = 0;
+$np->add_arg(
+    spec => "interface|i=s",
+    help => qq{-i, --interface=INTEGER
+    Interface name to monitor},
+    required => 1,
+);
 
-# Have we rescanned the device yet?
-my $rescanned = 0;
+$np->add_arg(
+    spec => "outbound|o",
+    help => qq{-o, --outbound=INTEGER
+    Monitor outbound traffic instead of inbound},
+);
 
-# Any interfaces that are no longer on the device?
-my $interface_vanished = 0;
+$np->getopts;
 
-# speed of the interface
-my $link_speed = 0;
+my $warning               = $np->opts->warning;
+my $critical              = $np->opts->critical;
+my $user_specified_ifname = $np->opts->interface;
+my $direction             = "In";
+my $cache_hold_time       = 1800;
 
-# Performance data
-my $perfdata = "";
+if ( $np->opts->outbound ) {
+    $direction = "Out";
+}
+if ( $np->opts->expirytime ) {
 
-# SNMP variables
+    # Validity test - must be numeric
+    unless ( $np->opts->expirytime =~ /^[0-9]+$/ ) {
+        print
+          "Specify time in seconds - $np->opts->expirytime is not a valid integer\n";
+        exit 3;
+    }
+    $cache_hold_time = $np->opts->expirytime;
+}
+
+my $s = $np->snmp;
+
+my $db_home = "/usr/local/nagios/var/";
+my $db_name = "$script.db";
+
+my $interface_vanished = 0;
+my $rescanned          = 0;
+my $link_speed         = 0;
+my $perfdata           = "";
+my $retval             = 0;
+my $retmsg             = "";
+my $uptime             = 0;
+my @snmp_interface_ids;
+my $throughput     = 0;
+my $throughput_pct = 0;
+my $critical_pct   = 0;
+my $warning_pct    = 0;
+my $hostname       = $np->opts->hostname;
+
 my $oid_sysDescr =
   ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is actually responding
 my $oid_sysUptime = ".1.3.6.1.2.1.1.3.0"; # SNMP agent uptime
@@ -73,98 +133,9 @@
     "$oid_interfaces_base.5",             # Friendly interface name
     "$oid_interfaces_base.8"
 );                                        # Friendly interface name
-my $community = "public";                 # Default community
-my $timeout   = 5;                        # SNMP timeout
-my $retval    = 0;                        # Innocent until proven guilty
-my $retmsg    = "";                       # Text to return from plugin
-my $uptime    = 0;
-my @snmp_interface_ids;
-my $version        = "2c";
-my $warning        = 0;
-my $critical       = 0;
-my $direction      = 'In';
-my $throughput     = 0;
-my $throughput_pct = 0;
-my $linkstate;
-my $user_specified_ifname;
-my $critical_pct = 0;
-my $warning_pct  = 0;
 
-our ( $s, $e, $st_h, $db_h );
+our ( $e, $st_h, $db_h );
 
-# Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_t, $opt_w, $opt_c, $opt_o, $opt_i, $opt_p );
-getopts( "hH:C:t:w:c:oi:p:" );
-if ($opt_h) {
-    usage();
-    exit 0;
-}
-if ($opt_H) {
-    $hostname = $opt_H;
-}
-else {
-    print "No hostname specified\n";
-    usage();
-    exit 3;
-}
-if ($opt_C) {
-    $community = $opt_C;
-}
-if ($opt_t) {
-
-    # Validity test - must be numeric
-    unless ( $opt_t =~ /^[0-9]+$/ ) {
-        print "Specify time in seconds - $opt_t is not a valid integer\n";
-        exit 3;
-    }
-    $cache_hold_time = $opt_t;
-}
-if ($opt_w) {
-    unless ( $opt_w =~ /^[0-9\.]+%?$/ ) {
-        print "Warning must be an integer indicating bits per second\n";
-        exit 3;
-    }
-
-    if ( $opt_w =~ /%$/ ) {
-        $warning_pct = 1;
-        $opt_w =~ s/%//;
-    }
-
-    $warning = $opt_w;
-}
-if ($opt_c) {
-    unless ( $opt_c =~ /^[0-9\.]+%?$/ ) {
-        print "Warning must be an integer indicating bits per second\n";
-        exit 3;
-    }
-
-    if ( $opt_c =~ /%$/ ) {
-        $critical_pct = 1;
-        $opt_c =~ s/%//;
-    }
-
-    $critical = $opt_c;
-}
-if ($opt_o) {
-    $direction = "Out";
-}
-if ($opt_i) {
-    $user_specified_ifname = $opt_i;
-}
-else {
-    print "Must specify interface name!\n";
-    exit 3;
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
-
-# Can't have warning and critical in different types
-if ( $warning_pct ne $critical_pct ) {
-    print "Warning and critical values must either both be in bps, or in %\n";
-    exit 3;
-}
-
 # Some of our SQL statements (We do this after getopts because we use
 # the provided hostname variable to create the SQL statements
 my $sql_create_hosts =
@@ -172,36 +143,6 @@
 my $sql_create_interfaces = "CREATE TABLE interfaces (hostname,name,ifindex)";
 my $sql_host_info = "SELECT modified_on FROM hosts WHERE hostname='$hostname'";
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
-$script $script_version
-
-Monitors interface status and throughput
-
-Usage: $script -H <hostname> -C <community> -i <interface> [...]
-
-Options: 
-    -H     Hostname or IP address
-    -C     Community (default is public)
-    -t     Cache expiry time - the time in seconds before the
-           monitored device's interface table is rescanned for
-           new or changed interfaces.
-    -i     Interface name to monitor
-    -o     Monitor outbound traffic instead of inbound
-    -w     Warning threshold (bits per second or n%)
-    -c     Critical threshold (bits per second or n%)
-
---------------------------------------------------------------------     
-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
-------------------------------------------------------------------
-EOF
-
-}
-
 sub clean_up {
     if ($st_h) {
         $st_h->finish();
@@ -274,29 +215,9 @@
     return $result;
 }
 
-# Create the SNMP session
-$version = "2c";
-( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-);
-
 if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-
-    # If we can't connect using SNMPv1 lets try as SNMPv2
-    $s->close();
-    sleep 0.5;
-    $version = "1";
-    ( $s, $e ) = Net::SNMP->session(
-        -community => $community,
-        -hostname  => $hostname,
-        -version   => $version,
-        -timeout   => $timeout,
-    );
     if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-        print "Agent not responding, tried SNMP v1 and v2\n";
+        print "Agent not responding";
         exit 1;
     }
 }
@@ -311,7 +232,11 @@
 		 AND name='hosts'"
 );
 
-unless ( $res->[0]->['name'] eq "hosts" ) {
+#If not defined, define with no value - removes Uninitialized Value warning
+if ( !defined( $res->[0]->[0] ) ) {
+    $res->[0]->[0] = "";
+}
+unless ( $res->[0]->[0] eq "hosts" ) {
 
     # Create tables
     $st_h = $db_h->prepare($sql_create_hosts);
@@ -324,7 +249,7 @@
 }
 
 # See if we have a last-checked value for this host
-my $modified_on;
+my $modified_on = "";
 $st_h = $db_h->prepare($sql_host_info);
 $st_h->execute();
 $st_h->bind_columns( \$modified_on );
@@ -353,12 +278,12 @@
 ## Check our last values against what we have now
 # Get interface information from database
 our ( $ifname, $ifindex, $ifstate );
+$ifindex = "";
 my $dbsafe_ifname = $db_h->quote($user_specified_ifname);
 $st_h = $db_h->prepare(
         "SELECT ifindex FROM interfaces WHERE hostname='$hostname'"
       . " AND name=$dbsafe_ifname"
 );
-
 $st_h->execute();
 $st_h->bind_columns( \$ifindex );
 
@@ -397,8 +322,8 @@
 }
 
 # Compare states
-my $name = $s->var_bind_list()->{"$oid_interfaces_base.2.$ifindex"};
-$linkstate  = $s->var_bind_list()->{"$oid_interfaces_base.8.$ifindex"};
+my $name      = $s->var_bind_list()->{"$oid_interfaces_base.2.$ifindex"};
+my $linkstate = $s->var_bind_list()->{"$oid_interfaces_base.8.$ifindex"};
 $link_speed = $s->var_bind_list()->{"$oid_interfaces_base.5.$ifindex"};
 
 # Check whether or not this is still the same interface

_______________________________________________
Opsview-checkins mailing list
Opsview-checkins@lists.opsview.org
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to