Title: [opsview] [10989] Migrated check_snmp_weblogic_jmsqueuelength to new SNMP tools
Revision
10989
Author
tvoon
Date
2012-12-13 15:11:39 +0000 (Thu, 13 Dec 2012)

Log Message

Migrated check_snmp_weblogic_jmsqueuelength to new SNMP tools

Modified Paths


Modified: trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm	2012-12-13 14:22:13 UTC (rev 10988)
+++ trunk/opsview-core/lib/Opsview/NagiosPlugin/SNMP.pm	2012-12-13 15:11:39 UTC (rev 10989)
@@ -175,7 +175,7 @@
 
 # Makes connection and returns the Net::SNMP object
 sub snmp {
-    my $self = shift;
+    my ( $self, $overrides ) = @_;
 
     my $opts = $self->opts;
 
@@ -199,7 +199,9 @@
         );
     }
     else {
-        push( @args, '-community' => $opts->get('rocommunity') );
+        push( @args,
+            '-community' => $overrides->{rocommunity}
+              || $opts->get('rocommunity') );
     }
 
     my ( $session, $error ) = Net::SNMP->session( "-version", $version, @args );

Modified: trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jmsqueuelength
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jmsqueuelength	2012-12-13 14:22:13 UTC (rev 10988)
+++ trunk/opsview-core/nagios-plugins/check_snmp_weblogic_jmsqueuelength	2012-12-13 15:11:39 UTC (rev 10989)
@@ -20,20 +20,63 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #
 
+use warnings;
 use strict;
-use FindBin qw($Bin $Script);
-use lib qw ( /usr/local/nagios/perl/lib );
+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;
 use Storable qw(lock_store lock_retrieve);
 use File::Path;
 
-# About us
-my $script         = "check_snmp_weblogic_jmsqueuelength";
-my $script_version = "1.0";
-my $script_description =
-  "Checks the length (current messages) of a particular JMS queue by name";
+my $oid_testoid =
+  ".1.3.6.1.4.1.140.625.10.1.0"; # Used to check whether SNMP is responding
+
+my $Script = "check_snmp_weblogic_jmsqueuelength";
+my $np     = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "WEBLOGIC",
+    version   => "2.0",
+    blurb =>
+      "Checks the length (current messages) of a particular JMS queue by name",
+    snmp => {
+        "snmp-timeout"   => 2,
+        "snmp-check-oid" => $oid_testoid,
+        "v1-fallback"    => 1,
+    },
+);
+
+$np->add_arg(
+    spec => "server|s=s",
+    help => qq{-s, --server=SERVERNAME
+   Server name. Will append to end of community string},
+);
+
+$np->add_arg(
+    spec => "queue|q=s",
+    help => qq{-q, --queue=QUEUENAME
+   Queue name to check number of items in},
+    default => "not_specified",
+);
+
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+   Minimum and maximum number of allowable result, outside of which a
+   warning will be generated (default: 0)},
+    default => 0,
+);
+
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+   Minimum and maximum number of the generated result, outside of
+   which a critical will be generated (default: 0) },
+    default => 0,
+);
+
+$np->getopts;
+
 my $store_dir = "$Bin/../var/plugins/${Script}";
 my $store;
 my $max_use = 10 + int( rand(10) ); # random number between 10 and 20
@@ -42,109 +85,28 @@
     mkpath($store_dir) || die "Unable to create $store_dir directory: $!", $/;
 }
 
-my $hostname;
-
 # Performance data
 my $perfdata = "";
 
 # SNMP variables
-my $oid_testoid =
-  ".1.3.6.1.4.1.140.625.10.1.0"; # Used to check whether SNMP is responding
 my $queuename_base_oid = ".1.3.6.1.4.1.140.625.220.1.15";
 my $queue_length_oid   = ".1.3.6.1.4.1.140.625.220.1.40";
-my $queue_name         = "not_specified";
 my $queue_oid          = 0;
-my $community          = "public";                       # Default community
-my $timeout            = 2;                              # SNMP timeout
-my $retval       = 0;   # Innocent until proven guilty
-my $retmsg       = "";  # Text to return from plugin
-my $uptime       = 0;
-my $version      = "2c";
-my $list_servers = 0;
-my $queue_oid;
-my $warning  = 0;
-my $critical = 0;
-my $port     = 161;
-our ( $s, $e );
+my $s;
 
 # Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_s, $opt_q, $opt_w, $opt_c, $opt_p );
-getopts( "hH:C:s:q:w:c:p:" );
-if ($opt_h) {
-    usage();
-    exit 0;
+my $hostname  = $np->opts->hostname;
+my $community = $np->opts->rocommunity;
+if ( defined $np->opts->server ) {
+    $community .= '@' . $np->opts->server;
 }
-if ($opt_H) {
-    $hostname = $opt_H;
-}
-else {
-    print "No hostname specified\n";
-    usage();
-    exit 3;
-}
-if ($opt_C) {
-    $community = $opt_C;
-}
-if ($opt_s) {
-    $community = "$community\@$opt_s";
-}
-else {
-    print "Must specify server name with -s\n";
-    usage();
-    exit 3;
-}
-if ($opt_q) {
-    $queue_name = $opt_q;
-}
-else {
-    print "Must specify queue name with -q\n";
-    usage();
-    exit 3;
-}
-if ( defined($opt_w) ) {
+my $queue_name = $np->opts->queue;
 
-    # Should check this is a number really.
-    $warning = $opt_w;
-}
-if ( defined($opt_c) ) {
+my $warning  = $np->opts->warning;
+my $critical = $np->opts->critical;
 
-    # Should check this is a number really.
-    $critical = $opt_c;
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
+my $store_file = $store_dir . "/${hostname}_${queue_name}.dat";
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
-
-$script $script_version
-
-$script_description
-
-Usage: $script -H <hostname> [ -C <community> ] -s <server> -q <queue>
-		-w <warning> -c <critical>
-
-Options: -H     Hostname or IP address
-         -p     Port (default: 161)
-         -C     Community (default is public)
-         -s     Server name
-         -w     Warning threshold
-         -c     Critical threshold
-	 -q	Queue name
-
---------------------------------------------------------------------
-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
-}
-
-my $store_file = $store_dir . "/${opt_H}_${opt_q}.dat";
-
 if ( -f $store_file ) {
 
     # if lock_retrieve fails, file is corrupt - just carry on with empty store
@@ -178,34 +140,8 @@
 }
 
 # Create the SNMP session
-$version = "1";
-( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-    -port      => $port,
-);
+$s = $np->snmp( { rocommunity => $community } );
 
-if ( !defined( $s->get_request($oid_testoid) ) ) {
-
-    # 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_testoid) ) ) {
-        print "Agent not responding, tried SNMP v1 and v2\n";
-        exit 1;
-    }
-}
-
 # Increase size of SNMP max message buffer
 if ( !defined $s->max_msg_size(65535) ) {
     print "SNMP Error: ", $s->error, $/;

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

Reply via email to