Title: [opsview] [11533] Updated check_snmp_tcpcurrestab_cssfix to use SNMPv3 library
Revision
11533
Author
hstevenson
Date
2013-02-19 16:54:12 +0000 (Tue, 19 Feb 2013)

Log Message

Updated check_snmp_tcpcurrestab_cssfix to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_tcpcurrestab_cssfix
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_tcpcurrestab_cssfix	2013-02-19 16:28:13 UTC (rev 11532)
+++ trunk/opsview-core/nagios-plugins/check_snmp_tcpcurrestab_cssfix	2013-02-19 16:54:12 UTC (rev 11533)
@@ -29,31 +29,64 @@
 # perhaps obviously, the number of currently established connections
 # This is partly handy, though, as we can use it to generate an
 # average number of connections per minute
-
 use strict;
-use lib qw ( /usr/local/nagios/perl/perl );
+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;
 
-my $script         = "check_tcpcurrestab_cssfix";
-my $script_version = "1.0";
-my $script_description =
-  "Returns the average number of TCP connections established every minute, for Cisco CSS";
+my $script = "check_tcpcurrestab_cssfix";
 
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => $script,
+    version   => "1.1",
+    blurb =>
+      "Returns the average number of TCP connections established every minute, for Cisco CSS",
+    snmp => {
+        "snmp-version" => 1,
+        "snmp-timeout" => 2,
+    },
+);
+
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold (conns per minute)},
+    default  => 60,
+    required => 1,
+);
+
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold (conns per minute)},
+    default  => 200,
+    required => 1,
+);
+
+$np->add_arg(
+    spec => "debug|D",
+    help => qq{-D, --debug=INTEGER
+    Output some debug information},
+);
+
+$np->getopts;
+
+my $warning  = $np->opts->warning;
+my $critical = $np->opts->critical;
+my $hostname = $np->opts->hostname;
+
+my $s = $np->snmp;
+
 my $oid_tcpCurrEstab = ".1.3.6.1.2.1.6.9.0";
 my $oid_sysDescr     = ".1.3.6.1.2.1.1.1.0";
 
-my $hostname     = "192.168.10.30";
-my $version      = "1";
-my $timeout      = 2;
-my $warning      = 60;
-my $critical     = 200;
-my $status       = 0;              # Default to OK
 my $returnstring = "";
-my $community    = "public";       # Default community string
-my $prevdata;
-my $port = 161;
+my $status       = 0;
 
 sub my_die {
     my (@text) = @_;
@@ -61,9 +94,6 @@
     exit 3;
 }
 
-# SNMP variables we use
-our ( $s, $e );
-
 # Database settings
 # Where to put the .db file
 my $db_home = "/usr/local/nagios/var/";
@@ -77,47 +107,6 @@
 # Statement handle
 my $st_h;
 
-our ( $opt_D, $opt_h, $opt_H, $opt_C, $opt_w, $opt_c, $opt_p );
-getopts( "Dh:H:C:w:c:p:" );
-if ($opt_h) {
-    usage();
-    exit 3;
-}
-if ($opt_H) {
-    $hostname = $opt_H;
-}
-else {
-    print "No hostname specified\n";
-    usage();
-}
-if ($opt_C) {
-    $community = $opt_C;
-}
-if ($opt_w) {
-    $warning = $opt_w;
-}
-else {
-    print "Must specify warning value\n";
-    usage();
-    exit 3;
-}
-if ($opt_c) {
-    if ( $opt_c < $opt_w ) {
-        print "Critical value must be greater or equal to warning value\n";
-        exit 3;
-    }
-
-    $critical = $opt_c;
-}
-else {
-    print "Must specify critical value\n";
-    usage();
-    exit 3;
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
-
 ### SQL Queries
 # Create table SQL query
 my $sql_create_hosts = "CREATE TABLE hosts (hostname UNIQUE,conns,modified_on)";
@@ -135,7 +124,7 @@
 		AND name='hosts'"
     );
 
-    unless ( $res->[0]->['name'] eq "hosts" ) {
+    unless ( $res->[0]->[0] eq "hosts" ) {
 
         # Create tables
         $st_h = $db_h->prepare($sql_create_hosts);
@@ -186,7 +175,7 @@
           . " WHERE hostname='$hostname'"
     );
 
-    warn 'Attempting to write to database', $/ if ($opt_D);
+    warn 'Attempting to write to database', $/ if ( $np->opts->debug );
 
     my $retries = 5;
     ATTEMPT: {
@@ -200,41 +189,19 @@
         # NOTE: the execute has a sleep time in it so we don't need to
         # sleep ourselves
         $retries--;
-        warn "Database is locked - $retries retries left.", $/ if ($opt_D);
+        warn "Database is locked - $retries retries left.", $/
+          if ( $$np->opts->debug );
         redo ATTEMPT;
     }
     if ( $retries < 1 ) {
         my_die( 'Unable to obtain write lock on sqlite database' );
     }
     else {
-        warn 'Database record written', $/ if ($opt_D);
+        warn 'Database record written', $/ if ( $np->opts->debug );
     }
 }
 
-# Create the SNMP session
-$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 we can't connect using SNMPv1 lets try as SNMPv2
-    $s->close();
-    sleep 0.5;
-    $version = "2c";
-    ( $s, $e ) = Net::SNMP->session(
-        -community => $community,
-        -hostname  => $hostname,
-        -version   => $version,
-        -timeout   => $timeout,
-        -port      => $port,
-    );
-
     if ( !defined( $s->get_request($oid_sysDescr) ) ) {
         print "Agent not responding, tried SNMP v1 and v2\n";
         exit 3;
@@ -345,21 +312,3 @@
     return $tcpcurrestab;
 }
 
-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)
-         -w 		Warning threshold (conns per minute)
-         -c 		Critical threshold (conns per minute)
-         -D 		Output some debug information
-
-USAGE
-    exit 3;
-}

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

Reply via email to