Title: [opsview] [11326] Updated check_snmp_runningproc to use SNMPv3 library
- Revision
- 11326
- Author
- hstevenson
- Date
- 2013-02-01 17:45:24 +0000 (Fri, 01 Feb 2013)
Log Message
Updated check_snmp_runningproc to use SNMPv3 library
Modified Paths
Modified: trunk/opsview-core/nagios-plugins/check_snmp_runningproc
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_runningproc 2013-02-01 17:24:33 UTC (rev 11325)
+++ trunk/opsview-core/nagios-plugins/check_snmp_runningproc 2013-02-01 17:45:24 UTC (rev 11326)
@@ -21,63 +21,72 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
-use lib qw ( /usr/local/nagios/perl/lib );
-use Net::SNMP 4.1.0 qw(DEBUG_ALL);
-use Getopt::Std;
+use strict;
+use warnings;
-$script = "check_runningproc";
-$script_version = "2.1";
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
+use Net::SNMP;
+use Opsview::NagiosPlugin::SNMP;
-# $metric = 1;
-$firsttime = 0;
-$change = 0;
-$interfacesup = 0;
-$interfacesdown = 0;
-@matches = ();
-$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
-$hrswrunname = ".1.3.6.1.2.1.25.4.2.1.2.1";
-$basehrswrunname = ".1.3.6.1.2.1.25.4.2.1.2";
-$hostname = '';
-$version = "1";
-$retries = 2;
-$timeout = 4;
-$returnstring = "";
-my $port = 161;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+ usage => "Usage: %s",
+ shortname => "check_snmp_checkproc",
+ version => "2.2",
+ blurb => "Checks whether specified process is running",
+ snmp => { "snmp-version" => 1, },
+);
-$community = "public"; # Default community string
-$configfilepath = "/usr/local/nagios/etc";
+$np->add_arg(
+ spec => "processes|P=s",
+ help => qq{-P, --processes=STRING
+ Process name (eg: httpd, snmpd)},
-# Do we have enough information?
-if ( @ARGV < 1 ) {
- print "Too few arguments\n";
- usage();
-}
+ #default => 60,
+);
-getopts( "hH:C:P:p:r:t:" );
-if ($opt_h) {
- usage();
- exit(0);
-}
-if ($opt_H) {
- $hostname = $opt_H;
+$np->add_arg(
+ spec => "timeout|t=s",
+ help => qq{-t, --timeout=INTEGER
+ Timeout after <n> seconds},
+ default => 4,
+);
- # print "Hostname $opt_H\n";
-}
-else {
- print "No hostname specified\n";
- usage();
-}
-if ($opt_C) {
- $community = $opt_C;
+$np->add_arg(
+ spec => "retries|r=s",
+ help => qq{-r, --retries=INTEGER
+ Retry SNMP connection <n> time},
+ default => 2,
+);
- # print "Using community $opt_C\n";
-}
-else {
+$np->getopts;
- # print "Using community $community\n";
-}
-if ($opt_P) {
- $processes = lc($opt_P);
+my $retries = $np->opts->retries;
+my $timeout = $np->opts->timeout;
+my $processes = $np->opts->processes;
+
+my $oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
+my $hrswrunname = ".1.3.6.1.2.1.25.4.2.1.2.1";
+my $basehrswrunname = ".1.3.6.1.2.1.25.4.2.1.2";
+
+my $s = $np->snmp;
+
+my $status = 0;
+my $returnstring = "";
+my $counter = 0;
+my $line;
+my $critical = 0;
+my $oid;
+my $returnedprocess;
+my $sysdescr;
+my $tempstring;
+my $matches;
+my @processes;
+my @matches;
+
+#Make sure process is provided
+if ( defined($processes) ) {
+ $processes = lc($processes);
$processes =~ s/\.exe//g; # removes .exe file extentions
$processes =~ s/\.ex//g; # removes .ex file extentions
$processes =~ s/\.e//g; # removes .e file extentions
@@ -86,26 +95,7 @@
print "No processes specified\n";
exit(1);
}
-if ($opt_r) {
- $retries = $opt_r;
-}
-if ($opt_t) {
- $timeout = $opt_t;
-}
-if ($opt_p) {
- $port = $opt_p;
-}
-# Create the SNMP session
-my ( $s, $e ) = Net::SNMP->session(
- -community => $community,
- -hostname => $hostname,
- -version => $version,
- -retries => $retries,
- -timeout => $timeout,
- -port => $port,
-);
-
# Works out what type of device we are looking at
# and chances the OID if necessary
@@ -142,7 +132,7 @@
# Initialises array
foreach $line (@processes) {
- @matches[$counter] = 0;
+ $matches[$counter] = 0;
$counter++;
}
@@ -155,7 +145,6 @@
foreach ( $s->var_bind_names() ) {
$oid = $_;
$returnedprocess = $s->var_bind_list()->{$oid};
-
$returnedprocess = lc($returnedprocess);
$returnedprocess =~ s/\.exe//g; # removes .exe file extentions
$returnedprocess =~ s/\.ex//g; # removes .ex file extentions
@@ -164,7 +153,7 @@
foreach $line (@processes) {
if ( $returnedprocess =~ m/$line/ ) {
- @matches[$counter] = @matches[$counter] + 1;
+ $matches[$counter] = $matches[$counter] + 1;
}
$counter++;
}
@@ -192,8 +181,8 @@
$counter = 0;
$critical = 0;
foreach $line (@processes) {
- append( "$line(@matches[$counter]) " );
- if ( @matches[$counter] < 1 ) {
+ append( "$line($matches[$counter]) " );
+ if ( $matches[$counter] < 1 ) {
$critical = 1;
}
$counter++;
@@ -214,24 +203,7 @@
exit 0;
sub append {
- my $appendstring = @_[0];
+ my $appendstring = $_[0];
$returnstring = "$returnstring$appendstring";
}
-sub usage {
- print << "USAGE";
-
-$script v$script_version
-
-Checks whether specified process is running
-
-Usage: $script -H <hostname> -c <community> [...]
-Options: -H Hostname or IP address
- -p Port (default: 161)
- -C Community (default is public)
- -P Process name (eg: httpd, snmpd)
- -t <n> Timeout after <n> seconds
- -r <n> Retry SNMP connection <n> time
-USAGE
- exit 1;
-}
_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins