[Nagiosplug-checkins] Nagios-Plugin/lib/Nagios/Plugin Functions.pm, 1.10, 1.11 Performance.pm, 1.11, 1.12 Range.pm, 1.8, 1.9 Threshold.pm, 1.10, 1.11

Gavin Carr gonzai at users.sourceforge.net
Fri Mar 16 12:25:17 CET 2007


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

Modified Files:
	Functions.pm Performance.pm Range.pm Threshold.pm 
Log Message:
Cleanups, mostly to N::P::Range/Threshold/Performance.

Index: Performance.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Performance.pm,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- Performance.pm	14 Mar 2007 23:47:19 -0000	1.11
+++ Performance.pm	16 Mar 2007 11:25:15 -0000	1.12
@@ -7,7 +7,7 @@
 
 use Carp;
 use base qw(Class::Accessor::Fast);
-Nagios::Plugin::Performance->mk_ro_accessors(
+__PACKAGE__->mk_ro_accessors(
     qw(label value uom warning critical min max)
 );
 

Index: Threshold.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Threshold.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- Threshold.pm	14 Mar 2007 23:47:22 -0000	1.10
+++ Threshold.pm	16 Mar 2007 11:25:15 -0000	1.11
@@ -5,55 +5,75 @@
 use strict;
 use warnings;
 
+use base qw(Class::Accessor::Fast);
+__PACKAGE__->mk_accessors(qw(warning critical));
+
 use Nagios::Plugin::Range;
 use Nagios::Plugin::Functions qw(:codes nagios_die);
 our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
 
-use Class::Struct;
-struct "Nagios::Plugin::Threshold" => {
-	warning => 'Nagios::Plugin::Range',
-	critical => 'Nagios::Plugin::Range',
-	};
-
-sub set_thresholds {
-	my ($class, %args) = @_;
-	my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new );
-	if (defined $args{warning}) {
-		my $r = Nagios::Plugin::Range->parse_range_string($args{warning});
-		if (defined $r) {
-			$t->warning($r);
-		} else {
-			nagios_die( "Warning range incorrect: '$args{warning}'" );
-		}
-	}
-	if (defined $args{critical}) {
-		my $r = Nagios::Plugin::Range->parse_range_string($args{critical});
-		if (defined $r) {
-			$t->critical($r);
-		} else {
-			nagios_die( "Critical range incorrect: '$args{critical}'" );
-		}
-	}
-	return $t;
-}
-
-sub get_status {
+sub get_status 
+{
 	my ($self, $value) = @_;
 
 	if ($self->critical->is_set) {
-		if ($self->critical->check_range($value) == 1) {
-			return CRITICAL;
-		}
+		return CRITICAL if $self->critical->check_range($value);
 	}
 	if ($self->warning->is_set) {
-		if ($self->warning->check_range($value) == 1) {
-			return WARNING;
-		}
+		return WARNING if $self->warning->check_range($value);
 	}
 	return OK;
 }
+
+sub _inflate
+{
+    my ($self, $value, $key) = @_;
+
+    # Return an undefined range if $value is undef
+    return Nagios::Plugin::Range->new if ! defined $value;
+
+    # For refs, check isa N::P::Range
+    if (ref $value) {
+        nagios_die("Invalid $key object: type " . ref $value)
+            unless $value->isa("Nagios::Plugin::Range");
+        return $value;
+    }
+
+    # Otherwise parse $value
+    my $range = Nagios::Plugin::Range->parse_range_string($value) 
+        or nagios_die("Cannot parse $key range: '$value'");
+    return $range;
+}
+
+sub set_thresholds
+{
+	my ($self, %arg) = @_;
+
+    # Equals new() as a class method
+    return $self->new(%arg) unless ref $self;
+
+    # On an object, just acts as special mutator
+    $self->set($_, $arg{$_}) foreach qw(warning critical);
+}
+
+sub set
+{
+    my $self = shift;
+    my ($key, $value) = @_;
+    $self->SUPER::set($key, $self->_inflate($value, $key));
+}
 		
+# Constructor - inflate scalars to N::P::Range objects
+sub new 
+{
+    my ($self, %arg) = @_;
+    $self->SUPER::new({
+        map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical)
+    });
+}
+
 1;
+
 __END__
 
 =head1 NAME

Index: Functions.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Functions.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- Functions.pm	14 Mar 2007 23:47:18 -0000	1.10
+++ Functions.pm	16 Mar 2007 11:25:15 -0000	1.11
@@ -9,20 +9,21 @@
 use warnings;
 use File::Basename;
 use Params::Validate qw(validate :types);
+use Math::Calc::Units;
 
 # Remember to update Nagios::Plugins as well
-our $VERSION = "0.15";
+our $VERSION = "0.16";
 
 our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
 
 require Exporter;
 our @ISA = qw(Exporter);
 our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
-our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state);
+our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert);
 our %EXPORT_TAGS = (
     all => [ @EXPORT, @EXPORT_OK ],
     codes => [ @STATUS_CODES ],
-    functions => [ qw(nagios_exit nagios_die check_messages max_state) ],
+    functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ],
 );
 
 use constant OK         => 0;
@@ -150,6 +151,17 @@
 
 
 # ------------------------------------------------------------------------
+# Utility functions
+
+# Simple wrapper around Math::Calc::Units::convert
+sub convert
+{
+    my ($value, $from, $to) = @_;
+    my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact');
+    return $newval;
+}
+
+# ------------------------------------------------------------------------
 # check_messages - return a status and/or message based on a set of 
 #   message arrays.
 #   Returns a nagios status code in scalar context. 

Index: Range.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Range.pm,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- Range.pm	14 Mar 2007 23:47:22 -0000	1.8
+++ Range.pm	16 Mar 2007 11:25:15 -0000	1.9
@@ -4,7 +4,12 @@
 
 use strict;
 use warnings;
+
 use Carp;
+use base qw(Class::Accessor::Fast);
+__PACKAGE__->mk_accessors(
+    qw(start end start_infinity end_infinity alert_on)
+);
 
 use Nagios::Plugin::Functions;
 our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -12,15 +17,7 @@
 use overload
         '""' => sub { shift->_stringify };
 
-use Class::Struct;
-struct "Nagios::Plugin::Range" => {
-	start => '$',
-	end => '$',
-	start_infinity => '$',	# TRUE / FALSE
-	end_infinity => '$',	# TRUE / FALSE
-	alert_on => '$',	# OUTSIDE 0, INSIDE 1, not defined == range not set
-	};
-
+# alert_on constants (undef == range not set)
 use constant OUTSIDE => 0;
 use constant INSIDE => 1;
 
@@ -119,7 +116,14 @@
 	}
 }
 
+# Constructor - map args to hashref for SUPER
+sub new 
+{
+    shift->SUPER::new({ @_ });
+}
+
 1;
+
 __END__
 
 =head1 NAME
@@ -135,7 +139,7 @@
     $r = Nagios::Plugin::Range->new; 
 
     # Instantiate by parsing a standard nagios range string
-    $r = Nagios::Plugin::Range->parse_range_string;
+    $r = Nagios::Plugin::Range->parse_range_string( $range_str );
 
     # Returns true if the range is defined/non-empty
     $r->is_set;





More information about the Commits mailing list