From c6cbf050974c8f6642fa1d7bde309710b66cbfa0 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Fri, 16 Mar 2007 11:25:15 +0000 Subject: Cleanups, mostly to N::P::Range/Threshold/Performance. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1641 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index 384538b..b207c21 100644 --- a/Changes +++ b/Changes @@ -1,8 +1,12 @@ Revision history for Perl module Nagios::Plugin. -??? ?? - - fixed warnings when no uom specified for add_perfdata - - added max_state function in N::P::Functions +0.16 ?? + - perldoc updates to Performance, Threshold, and Range (Gavin) + - remove default use of Threshold from N::P::Performance (Gavin) + - remove remaining Class::Struct usages from Threshold + Range (Gavin) + - remove remaining Class::Struct usages from Performance (Gavin) + - fixed warnings when no uom specified for add_perfdata (Ton) + - added max_state function in N::P::Functions (Ton) 0.15 19th December 2006 - exposed Getopt and Threshold functionality from top level Nagios::Plugin diff --git a/Makefile.PL b/Makefile.PL index 63b36da..c043edb 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -10,11 +10,11 @@ WriteMakefile( Class::Accessor => 0, Test::More => 0.62, Carp => 0, - Test::Exception => 0, Config::Tiny => 0, File::Spec => 0, File::Basename => 0, IO::File => 0, + Math::Calc::Units => 0, # used in N::P::Performance }, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 576322b..b063e4c 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -22,8 +22,7 @@ our @ISA = qw(Exporter); our @EXPORT = (@STATUS_CODES); our @EXPORT_OK = qw(%ERRORS); -# Remember to update Nagios::Plugin::Functions as well! -our $VERSION = "0.15"; +our $VERSION = $Nagios::Plugin::Functions::VERSION; sub new { my $class = shift; diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index 513501b..526dbed 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm @@ -9,20 +9,21 @@ use strict; 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 @@ sub die { nagios_die(@_); } # ------------------------------------------------------------------------ +# 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. diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 1f036f2..63727c0 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -7,7 +7,7 @@ use warnings; 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) ); diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index dbb637c..3828d1a 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm @@ -4,7 +4,12 @@ use 5.006; 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 @@ our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 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 @@ sub check_range { } } +# Constructor - map args to hashref for SUPER +sub new +{ + shift->SUPER::new({ @_ }); +} + 1; + __END__ =head1 NAME @@ -135,7 +139,7 @@ Nagios::Plugin::Range - class for handling Nagios::Plugin range data. $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; diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 0c4805a..ad58134 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -5,55 +5,75 @@ use 5.006; 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 diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t index a110b4c..6f31b56 100644 --- a/t/Nagios-Plugin-04.t +++ b/t/Nagios-Plugin-04.t @@ -5,18 +5,20 @@ use strict; #use Test::More 'no_plan'; use Test::More tests=>26; -use Test::Exception; BEGIN { use_ok('Nagios::Plugin') }; use Nagios::Plugin::Functions; Nagios::Plugin::Functions::_fake_exit(1); -lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; +eval { Nagios::Plugin->new(); }; +ok(! $@, "constructor DOESN'T die without usage"); my $p = Nagios::Plugin->new(); -dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; -dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage"; +eval { $p->add_arg('warning', 'warning') }; +ok($@, "add_arg() dies if you haven't instantiated with usage"); +eval { $p->getopts }; +ok($@, "getopts() dies if you haven't instantiated with usage"); $p = Nagios::Plugin->new( usage => "dummy usage statement" ); @@ -41,7 +43,8 @@ can_ok $p, 'threshold'; #isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; -dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; +eval { $p->check_threshold() }; +ok($@, "check_threshold dies if called with no args"); # thresholds set implicitly diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index ccb53eb..d3711bb 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t @@ -1,7 +1,6 @@ use strict; -use Test::More tests => 71; -#use Test::Exception; # broken for now so we don't need this. +use Test::More tests => 87; BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Functions', ':all' ); @@ -37,19 +36,31 @@ sub test_expected_statuses { my $debug = shift; foreach (sort {$a<=>$b} keys %$expected) { - is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; - if ($debug) { - diag "val = $_; critical check = ".$t->critical->check_range($_). - "; warning check = ".$t->warning->check_range($_); - } + is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; + if ($debug) { + diag "val = $_; critical check = ".$t->critical->check_range($_). + "; warning check = ".$t->warning->check_range($_); + } } use Data::Dumper; diag "thresh dump: ". Dumper $t if $debug; } test_expected_statuses( $t, $expected ); +# GMC: this test seems bogus to me - either we've died, in which case internal +# state is undefined (and untestable!), or we should be returning a non-fatal error +if (0) { + diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; + eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; + ok( defined $t, "Threshold ('5:33', '') set"); + cmp_ok( $t->warning->start, '==', 5, "Warning start set"); + cmp_ok( $t->warning->end, '==', 33, "Warning end set"); + ok( ! $t->critical->is_set, "Critical not set"); +} + +# GC: same as previous test, except critical is undef instead of '' diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; -eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); ok( defined $t, "Threshold ('5:33', '') set"); cmp_ok( $t->warning->start, '==', 5, "Warning start set"); cmp_ok( $t->warning->end, '==', 33, "Warning end set"); @@ -88,7 +99,6 @@ $expected = { qw( ) }; test_expected_statuses( $t, $expected ); - # "I'm going to die homeless, penniless, and 30 pounds overweight." # "...and that's...okay." @@ -163,4 +173,37 @@ $expected = { qw( ) }; test_expected_statuses( $t, $expected ); + +# GMC: as of 0.16, set_thresholds can also be called as a mutator +diag "threshold mutator: warn if more than 30; critical if > 60" + if $ENV{TEST_VERBOSE}; +my $t1 = $t; +$t->set_thresholds(warning => "0:45", critical => "0:90"); +is($t1, $t, "same threshold object after \$t->set_thresholds"); +ok( defined $t, "Threshold ('0:45', '0:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 45, "Warning end ok"); +is( $t->critical->start, 0, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + + +# Also as of 0.16, accepts N::P::Range objects as arguments +my $warning = Nagios::Plugin::Range->parse_range_string("50"); +my $critical = Nagios::Plugin::Range->parse_range_string("70:90"); +$t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); +ok( defined $t, "Threshold from ranges ('50', '70:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 50, "Warning end ok"); +is( $t->critical->start, 70, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + +$critical = Nagios::Plugin::Range->parse_range_string("90:"); +$t->set_thresholds(warning => "~:20", critical => $critical); +ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); +ok( $t->warning->start_infinity, "Warning start ok (infinity)"); +is( $t->warning->end, 20, "Warning end ok"); +is( $t->critical->start, 90, "Critical start ok"); +ok( $t->critical->end_infinity, "Critical end ok (infinity)"); + + ok 1, "sweet, made it to the end."; -- cgit v0.10-9-g596f