[Nagiosplug-checkins] Nagios-Plugin/lib/Nagios Plugin.pm, 1.14, 1.15

Nathan Vonnahme n8v at users.sourceforge.net
Fri Nov 10 02:26:18 CET 2006


Update of /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv13114/lib/Nagios

Modified Files:
	Plugin.pm 
Log Message:
* exposed Getopt and Threshold functionality from top level Nagios::Plugin
* exchanged Class::Struct for Class::Accessor
* POD is not updated yet.



Index: Plugin.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin.pm,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- Plugin.pm	18 Oct 2006 11:51:19 -0000	1.14
+++ Plugin.pm	10 Nov 2006 01:26:16 -0000	1.15
@@ -1,30 +1,67 @@
-# This is only because Class::Struct doesn't allow subclasses
-# Trick stolen from Class::DBI
-###package Nagios::__::Plugin;
-
-use Class::Struct;
-struct "Nagios::__::Plugin" => {
-    perfdata => '@',
-    shortname => '$',
-    messages => '%',
-    };
 
 package Nagios::Plugin;
 
 use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
+use Nagios::Plugin::Getopt;
+use Nagios::Plugin::Threshold; 
+use Params::Validate qw(:all);
 
 use strict;
 use warnings;
 
 use Carp;
+use base qw(Class::Accessor::Fast);
+
+# do we need all of these to be accessible?
+Nagios::Plugin->mk_accessors(qw(
+								perfdata 
+								messages 
+								opts
+								threshold
+								));
 
 use Exporter;
-our @ISA = qw(Exporter Nagios::__::Plugin);
+our @ISA = qw(Exporter);
 our @EXPORT = (@STATUS_CODES);
 our @EXPORT_OK = qw(%ERRORS);
 
 # Remember to update Nagios::Plugin::Functions as well!
-our $VERSION = "0.14";
+our $VERSION = "0.15";
+
+sub new {
+	my $class = shift;
+#	my %args = @_;
+
+	my %args = validate( @_, {
+		shortname => 0,
+		usage => 1,
+		version => 0,
+		url => 0,
+		plugin => 0,
+		blurb => 0,
+		extra => 0,
+		license => 0,
+		timeout => 0 },
+	);
+	my $shortname = undef;
+	if (exists $args{shortname}) {
+		$shortname = $args{shortname};
+		delete $args{shortname};
+	}
+	my $self = {
+	    shortname => $shortname,
+	    perfdata => [],        # to be added later
+	    messages => {
+			warning => [],
+			critical => [],
+			ok => []
+			},
+		opts => new Nagios::Plugin::Getopt(%args),
+		threshold => undef,  # defined later
+	};		
+	bless $self, $class;
+	return $self;
+}
 
 sub add_perfdata {
     my ($self, %args) = @_;
@@ -38,9 +75,9 @@
 }
 
 sub set_thresholds { 
-    shift; 
+    my $self = shift; 
     require Nagios::Plugin::Threshold;
-    Nagios::Plugin::Threshold->set_thresholds(@_); 
+    return $self->threshold( Nagios::Plugin::Threshold->set_thresholds(@_)); 
 }
 
 # NP::Functions wrappers
@@ -56,14 +93,54 @@
     my $self = shift;
     Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self });
 }
+
 # Override default shortname accessor to add default
 sub shortname {
     my $self = shift;
-    $self->{'Nagios::__::Plugin::shortname'} = shift if @_;
-    return $self->{'Nagios::__::Plugin::shortname'} || 
+    $self->{shortname} = shift if @_;
+    return $self->{shortname} || 
            Nagios::Plugin::Functions::get_shortname();
 }
 
+# top level interface to Nagios::Plugin::Threshold
+sub check_threshold {
+	my $self = shift;
+
+	my %args;
+
+	if ( $#_ == 0 && ! ref $_[0]) {  # one positional param
+		%args = (check => shift);
+	}
+	else {
+		%args = validate ( @_, {  # named params
+			check => 1,
+			warning => 0,
+			critical => 0,
+		} );
+	}
+
+	if (! $self->threshold || exists $args{warning} || exists $args{critical}) {
+		$self->set_thresholds( 
+							   warning  => $args{warning} || $self->opts->warning , 
+							   critical => $args{critical} || $self->opts->critical ,
+							   );
+	}
+
+
+	return $self->threshold->get_status($args{check});
+}
+
+# top level interface to my Nagios::Plugin::Getopt object
+sub arg {
+    my $self = shift;
+	$self->opts->arg(@_);
+}
+sub getopts {
+    my $self = shift;
+	$self->opts->getopts(@_);
+}
+
+
 # -------------------------------------------------------------------------
 # NP::Functions::check_messages helpers and wrappers
 
@@ -80,8 +157,8 @@
     croak "Error code '$code' not supported by add_message"
         if $code eq 'unknown' || $code eq 'dependent';
 
-    $self->messages($code, []) unless $self->messages($code);
-    push @{$self->messages($code)}, @messages;
+    $self->messages($code, []) unless $self->messages->{$code};
+    push @{$self->messages->{$code}}, @messages;
 }
 
 sub check_messages {
@@ -90,7 +167,7 @@
 
     # Add object messages to any passed in as args
     for my $code (qw(critical warning ok)) {
-        my $messages = $self->messages($code) || [];
+        my $messages = $self->messages->{$code} || [];
         if ($args{$code}) {
             unless (ref $args{$code} eq 'ARRAY') {
                 if ($code eq 'ok') {
@@ -123,9 +200,10 @@
 Nagios::Plugin - a family of perl modules to streamline writing Nagios 
 plugins
 
-
 =head1 SYNOPSIS
 
+    # TODO NJV -- make this more like check_stuff.
+
     # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default
     # See also Nagios::Plugin::Functions for a functional interface
     use Nagios::Plugin;
@@ -188,9 +266,7 @@
     #   | size=36kB;10:25;25: time=...
 
     # Option handling methods (NOT YET IMPLEMENTED - use 
-    #   Nagios::Plugin::Getopt for now)
-    
-
+    #   Nagios::Plugin::Getopt for 
 
 =head1 DESCRIPTION
 
@@ -280,6 +356,7 @@
 
 =over 4
 
+=item check_threshold( $value )
 =item check_threshold( check => $value, warning => $warn, critical => $crit )
 
 =back
@@ -382,6 +459,8 @@
 
 =head2 OPTION HANDLING METHODS
 
+TODO
+
 NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now.
 
 
@@ -426,9 +505,6 @@
 
 Originally by Ton Voon, E<lt>ton.voon at altinity.comE<gt>.
 
-Nathan Vonnahme added extra tests and subsequent fixes.
-
-
 =head1 COPYRIGHT AND LICENSE
 
 Copyright (C) 2006 by Nagios Plugin Development Team





More information about the Commits mailing list