Title: [opsview] [11407] Updated check_snmp_weblogic_jms_dests to use SNMPv3 library
Revision
11407
Author
hstevenson
Date
2013-02-12 10:13:29 +0000 (Tue, 12 Feb 2013)

Log Message

Updated check_snmp_weblogic_jms_dests to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jms_dests
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jms_dests	2013-02-12 10:13:03 UTC (rev 11406)
+++ trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jms_dests	2013-02-12 10:13:29 UTC (rev 11407)
@@ -20,131 +20,95 @@
 #    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 DBI;
-use Getopt::Std;
+use Opsview::NagiosPlugin::SNMP;
 
-# About us
-my $script         = "check_snmp_weblogic_jms_dests";
-my $script_version = "0.1";
-my $script_description =
-  "Checks JMS consumers, messages, pending messages and pending bytes";
-my $hostname;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "check_snmp_weblogic_jms_dests",
+    version   => "0.2",
+    blurb =>
+      "Checks JMS consumers, messages, pending messages and pending bytes",
+    snmp => { "snmp-version" => 1, },
+);
 
-# Performance data
-my $perfdata = "";
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold},
+    required => 1,
+);
 
-# SNMP variables
-my $oid_testoid =
-  ".1.3.6.1.4.1.140.625.10.1.0"; # Used to check whether SNMP is responding
-my $oid_jd_consumers =
-  ".1.3.6.1.4.1.140.625.180.1.30"; # get_next here to find the value
-my $oid_jd_messages =
-  ".1.3.6.1.4.1.140.625.220.1.40"; # get_next here to find the value
-my $oid_jd_pending =
-  ".1.3.6.1.4.1.140.625.220.1.45"; # get_next here to find the value
-my $oid_jd_bytespending =
-  ".1.3.6.1.4.1.140.625.220.1.70"; # get_next here to find the value
-my $count            = 0;          # count of servers/connections
-my $community        = "public";   # Default community
-my $timeout          = 2;          # SNMP timeout
-my $retmsg           = "";
-my $version          = "1";
-my $warning          = 0;
-my $critical         = 0;
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold},
+    required => 1,
+);
+
+$np->add_arg(
+    spec => "consumers|O=s",
+    help => qq{-C, --consumers=INTEGER
+    Consumers},
+);
+
+$np->add_arg(
+    spec => "messages|M=s",
+    help => qq{-M, --messages=INTEGER
+    Messages},
+);
+
+$np->add_arg(
+    spec => "pendingmessages|P=s",
+    help => qq{-P, --pendingmessages=INTEGER
+    Pending messages},
+);
+
+$np->add_arg(
+    spec => "pendingbytes|B=s",
+    help => qq{-B, --pendingbytes=INTEGER
+    Pending bytes},
+);
+
+$np->getopts;
+
+my $warning          = $np->opts->warning;
+my $critical         = $np->opts->critical;
 my $check_consumers  = 0;
 my $check_messages   = 0;
 my $check_msgpending = 0;
-my $port             = 161;
-our ( $s, $e );
 
-# Command line arguments
-our (
-    $opt_h, $opt_H, $opt_C, $opt_w, $opt_c,
-    $opt_O, $opt_M, $opt_P, $opt_B, $opt_p
-);
-getopts( "hH:C:w:c:OMPBp:" );
-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 ( !defined($opt_w) or !defined($opt_c) ) {
-    print "Must specify both warning and critical values!\n\n";
-    usage();
-    exit 3;
-}
-elsif ( $opt_w > $opt_c ) {
-    print "Warning value must not be greater than critical value\n\n";
-    usage();
-    exit 3;
-}
-if ($opt_O) {
+my $count    = 0;
+my $retmsg   = "";
+my $perfdata = "";
+
+if ( $np->opts->consumers ) {
     $check_consumers = 1;
 }
-elsif ($opt_M) {
+elsif ( $np->opts->messages ) {
     $check_messages = 1;
 }
-elsif ($opt_P) {
+elsif ( $np->opts->pendingmessages ) {
     $check_msgpending = 1;
 }
-elsif ( !$opt_B ) {
-    print "Must specify one of -O, -M -P or -B\n\n";
-    usage();
+elsif ( !$np->opts->pendingbytes ) {
+    print "Must specify one of -O, -M, -P or -B\n";
     exit 3;
 }
-if ($opt_p) {
-    $port = $opt_p;
-}
 
-# Must have opt_w and opt_c
-$warning  = $opt_w;
-$critical = $opt_c;
+my $s = $np->snmp;
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
+my $oid_testoid         = ".1.3.6.1.4.1.140.625.10.1.0";
+my $oid_jd_consumers    = ".1.3.6.1.4.1.140.625.180.1.30";
+my $oid_jd_messages     = ".1.3.6.1.4.1.140.625.220.1.40";
+my $oid_jd_pending      = ".1.3.6.1.4.1.140.625.220.1.45";
+my $oid_jd_bytespending = ".1.3.6.1.4.1.140.625.220.1.70";
 
-$script $script_version
-
-$script_description
-
-Usage: $script -H <hostname> -C <community> -w <warning> ...
-		 ... -c <critical> [-O|-M|-P|-B]
-
-Options: -H     Hostname or IP address
-         -p     Port (default: 161)
-         -C     Community (default is public)
-         -w     Warning threshold
-         -c     Critical threshold
-         -O     Consumers
-         -M     Messages
-         -P     Pending messages
-         -B     Pending bytes
-
---------------------------------------------------------------------
-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
-
-}
-
 # Call this when you know you'll get a single value back
 sub get_oid_value {
     our ( $oid, $result, $status, $returnstring );
@@ -175,28 +139,7 @@
     return $result;
 }
 
-# Create the SNMP session
-( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-    -port      => $port,
-);
-
 if ( !defined( $s->get_request($oid_testoid) ) ) {
-
-    # If we can't connect using SNMPv1 lets try as SNMPv2c
-    $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_testoid) ) ) {
         print "Agent not responding, tried SNMP v1 and v2\n";
         exit 1;

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

Reply via email to