summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2007-03-16 11:25:15 (GMT)
committerGavin Carr <gonzai@users.sourceforge.net>2007-03-16 11:25:15 (GMT)
commitc6cbf050974c8f6642fa1d7bde309710b66cbfa0 (patch)
tree0eb0ea62814b0623fb619c2af3287a585de48c2c
parente5109f99c9657a1a8e9fb32b19254a417e2ccd65 (diff)
downloadmonitoring-plugin-perl-c6cbf050974c8f6642fa1d7bde309710b66cbfa0.tar.gz
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
-rw-r--r--Changes10
-rw-r--r--Makefile.PL2
-rw-r--r--lib/Nagios/Plugin.pm3
-rw-r--r--lib/Nagios/Plugin/Functions.pm18
-rw-r--r--lib/Nagios/Plugin/Performance.pm2
-rw-r--r--lib/Nagios/Plugin/Range.pm24
-rw-r--r--lib/Nagios/Plugin/Threshold.pm90
-rw-r--r--t/Nagios-Plugin-04.t13
-rw-r--r--t/Nagios-Plugin-Threshold.t61
9 files changed, 154 insertions, 69 deletions
diff --git a/Changes b/Changes
index 384538b..b207c21 100644
--- a/Changes
+++ b/Changes
@@ -1,8 +1,12 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
3??? ?? 30.16 ??
4 - fixed warnings when no uom specified for add_perfdata 4 - perldoc updates to Performance, Threshold, and Range (Gavin)
5 - added max_state function in N::P::Functions 5 - remove default use of Threshold from N::P::Performance (Gavin)
6 - remove remaining Class::Struct usages from Threshold + Range (Gavin)
7 - remove remaining Class::Struct usages from Performance (Gavin)
8 - fixed warnings when no uom specified for add_perfdata (Ton)
9 - added max_state function in N::P::Functions (Ton)
6 10
70.15 19th December 2006 110.15 19th December 2006
8 - exposed Getopt and Threshold functionality from top level Nagios::Plugin 12 - 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(
10 Class::Accessor => 0, 10 Class::Accessor => 0,
11 Test::More => 0.62, 11 Test::More => 0.62,
12 Carp => 0, 12 Carp => 0,
13 Test::Exception => 0,
14 Config::Tiny => 0, 13 Config::Tiny => 0,
15 File::Spec => 0, 14 File::Spec => 0,
16 File::Basename => 0, 15 File::Basename => 0,
17 IO::File => 0, 16 IO::File => 0,
17 Math::Calc::Units => 0, # used in N::P::Performance
18 }, # e.g., Module::Name => 1.1 18 }, # e.g., Module::Name => 1.1
19 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 19 ($] >= 5.005 ? ## Add these new keywords supported since 5.005
20 (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module 20 (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);
22our @EXPORT = (@STATUS_CODES); 22our @EXPORT = (@STATUS_CODES);
23our @EXPORT_OK = qw(%ERRORS); 23our @EXPORT_OK = qw(%ERRORS);
24 24
25# Remember to update Nagios::Plugin::Functions as well! 25our $VERSION = $Nagios::Plugin::Functions::VERSION;
26our $VERSION = "0.15";
27 26
28sub new { 27sub new {
29 my $class = shift; 28 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;
9use warnings; 9use warnings;
10use File::Basename; 10use File::Basename;
11use Params::Validate qw(validate :types); 11use Params::Validate qw(validate :types);
12use Math::Calc::Units;
12 13
13# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
14our $VERSION = "0.15"; 15our $VERSION = "0.16";
15 16
16our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
17 18
18require Exporter; 19require Exporter;
19our @ISA = qw(Exporter); 20our @ISA = qw(Exporter);
20our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
21our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); 22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert);
22our %EXPORT_TAGS = ( 23our %EXPORT_TAGS = (
23 all => [ @EXPORT, @EXPORT_OK ], 24 all => [ @EXPORT, @EXPORT_OK ],
24 codes => [ @STATUS_CODES ], 25 codes => [ @STATUS_CODES ],
25 functions => [ qw(nagios_exit nagios_die check_messages max_state) ], 26 functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ],
26); 27);
27 28
28use constant OK => 0; 29use constant OK => 0;
@@ -150,6 +151,17 @@ sub die { nagios_die(@_); }
150 151
151 152
152# ------------------------------------------------------------------------ 153# ------------------------------------------------------------------------
154# Utility functions
155
156# Simple wrapper around Math::Calc::Units::convert
157sub convert
158{
159 my ($value, $from, $to) = @_;
160 my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact');
161 return $newval;
162}
163
164# ------------------------------------------------------------------------
153# check_messages - return a status and/or message based on a set of 165# check_messages - return a status and/or message based on a set of
154# message arrays. 166# message arrays.
155# Returns a nagios status code in scalar context. 167# 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;
7 7
8use Carp; 8use Carp;
9use base qw(Class::Accessor::Fast); 9use base qw(Class::Accessor::Fast);
10Nagios::Plugin::Performance->mk_ro_accessors( 10__PACKAGE__->mk_ro_accessors(
11 qw(label value uom warning critical min max) 11 qw(label value uom warning critical min max)
12); 12);
13 13
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;
4 4
5use strict; 5use strict;
6use warnings; 6use warnings;
7
7use Carp; 8use Carp;
9use base qw(Class::Accessor::Fast);
10__PACKAGE__->mk_accessors(
11 qw(start end start_infinity end_infinity alert_on)
12);
8 13
9use Nagios::Plugin::Functions; 14use Nagios::Plugin::Functions;
10our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 15our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -12,15 +17,7 @@ our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
12use overload 17use overload
13 '""' => sub { shift->_stringify }; 18 '""' => sub { shift->_stringify };
14 19
15use Class::Struct; 20# alert_on constants (undef == range not set)
16struct "Nagios::Plugin::Range" => {
17 start => '$',
18 end => '$',
19 start_infinity => '$', # TRUE / FALSE
20 end_infinity => '$', # TRUE / FALSE
21 alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set
22 };
23
24use constant OUTSIDE => 0; 21use constant OUTSIDE => 0;
25use constant INSIDE => 1; 22use constant INSIDE => 1;
26 23
@@ -119,7 +116,14 @@ sub check_range {
119 } 116 }
120} 117}
121 118
119# Constructor - map args to hashref for SUPER
120sub new
121{
122 shift->SUPER::new({ @_ });
123}
124
1221; 1251;
126
123__END__ 127__END__
124 128
125=head1 NAME 129=head1 NAME
@@ -135,7 +139,7 @@ Nagios::Plugin::Range - class for handling Nagios::Plugin range data.
135 $r = Nagios::Plugin::Range->new; 139 $r = Nagios::Plugin::Range->new;
136 140
137 # Instantiate by parsing a standard nagios range string 141 # Instantiate by parsing a standard nagios range string
138 $r = Nagios::Plugin::Range->parse_range_string; 142 $r = Nagios::Plugin::Range->parse_range_string( $range_str );
139 143
140 # Returns true if the range is defined/non-empty 144 # Returns true if the range is defined/non-empty
141 $r->is_set; 145 $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;
5use strict; 5use strict;
6use warnings; 6use warnings;
7 7
8use base qw(Class::Accessor::Fast);
9__PACKAGE__->mk_accessors(qw(warning critical));
10
8use Nagios::Plugin::Range; 11use Nagios::Plugin::Range;
9use Nagios::Plugin::Functions qw(:codes nagios_die); 12use Nagios::Plugin::Functions qw(:codes nagios_die);
10our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 13our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
11 14
12use Class::Struct; 15sub get_status
13struct "Nagios::Plugin::Threshold" => { 16{
14 warning => 'Nagios::Plugin::Range',
15 critical => 'Nagios::Plugin::Range',
16 };
17
18sub set_thresholds {
19 my ($class, %args) = @_;
20 my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new );
21 if (defined $args{warning}) {
22 my $r = Nagios::Plugin::Range->parse_range_string($args{warning});
23 if (defined $r) {
24 $t->warning($r);
25 } else {
26 nagios_die( "Warning range incorrect: '$args{warning}'" );
27 }
28 }
29 if (defined $args{critical}) {
30 my $r = Nagios::Plugin::Range->parse_range_string($args{critical});
31 if (defined $r) {
32 $t->critical($r);
33 } else {
34 nagios_die( "Critical range incorrect: '$args{critical}'" );
35 }
36 }
37 return $t;
38}
39
40sub get_status {
41 my ($self, $value) = @_; 17 my ($self, $value) = @_;
42 18
43 if ($self->critical->is_set) { 19 if ($self->critical->is_set) {
44 if ($self->critical->check_range($value) == 1) { 20 return CRITICAL if $self->critical->check_range($value);
45 return CRITICAL;
46 }
47 } 21 }
48 if ($self->warning->is_set) { 22 if ($self->warning->is_set) {
49 if ($self->warning->check_range($value) == 1) { 23 return WARNING if $self->warning->check_range($value);
50 return WARNING;
51 }
52 } 24 }
53 return OK; 25 return OK;
54} 26}
27
28sub _inflate
29{
30 my ($self, $value, $key) = @_;
31
32 # Return an undefined range if $value is undef
33 return Nagios::Plugin::Range->new if ! defined $value;
34
35 # For refs, check isa N::P::Range
36 if (ref $value) {
37 nagios_die("Invalid $key object: type " . ref $value)
38 unless $value->isa("Nagios::Plugin::Range");
39 return $value;
40 }
41
42 # Otherwise parse $value
43 my $range = Nagios::Plugin::Range->parse_range_string($value)
44 or nagios_die("Cannot parse $key range: '$value'");
45 return $range;
46}
47
48sub set_thresholds
49{
50 my ($self, %arg) = @_;
51
52 # Equals new() as a class method
53 return $self->new(%arg) unless ref $self;
54
55 # On an object, just acts as special mutator
56 $self->set($_, $arg{$_}) foreach qw(warning critical);
57}
58
59sub set
60{
61 my $self = shift;
62 my ($key, $value) = @_;
63 $self->SUPER::set($key, $self->_inflate($value, $key));
64}
55 65
66# Constructor - inflate scalars to N::P::Range objects
67sub new
68{
69 my ($self, %arg) = @_;
70 $self->SUPER::new({
71 map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical)
72 });
73}
74
561; 751;
76
57__END__ 77__END__
58 78
59=head1 NAME 79=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 @@
5use strict; 5use strict;
6#use Test::More 'no_plan'; 6#use Test::More 'no_plan';
7use Test::More tests=>26; 7use Test::More tests=>26;
8use Test::Exception;
9 8
10BEGIN { use_ok('Nagios::Plugin') }; 9BEGIN { use_ok('Nagios::Plugin') };
11use Nagios::Plugin::Functions; 10use Nagios::Plugin::Functions;
12Nagios::Plugin::Functions::_fake_exit(1); 11Nagios::Plugin::Functions::_fake_exit(1);
13 12
14 13
15lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; 14eval { Nagios::Plugin->new(); };
15ok(! $@, "constructor DOESN'T die without usage");
16 16
17my $p = Nagios::Plugin->new(); 17my $p = Nagios::Plugin->new();
18dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; 18eval { $p->add_arg('warning', 'warning') };
19dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage"; 19ok($@, "add_arg() dies if you haven't instantiated with usage");
20eval { $p->getopts };
21ok($@, "getopts() dies if you haven't instantiated with usage");
20 22
21$p = Nagios::Plugin->new( usage => "dummy usage statement" ); 23$p = Nagios::Plugin->new( usage => "dummy usage statement" );
22 24
@@ -41,7 +43,8 @@ can_ok $p, 'threshold';
41#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; 43#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined";
42 44
43 45
44dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; 46eval { $p->check_threshold() };
47ok($@, "check_threshold dies if called with no args");
45 48
46 49
47# thresholds set implicitly 50# 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 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 71; 3use Test::More tests => 87;
4#use Test::Exception; # broken for now so we don't need this.
5BEGIN { 4BEGIN {
6 use_ok('Nagios::Plugin::Threshold'); 5 use_ok('Nagios::Plugin::Threshold');
7 use_ok('Nagios::Plugin::Functions', ':all' ); 6 use_ok('Nagios::Plugin::Functions', ':all' );
@@ -37,19 +36,31 @@ sub test_expected_statuses {
37 my $debug = shift; 36 my $debug = shift;
38 37
39 foreach (sort {$a<=>$b} keys %$expected) { 38 foreach (sort {$a<=>$b} keys %$expected) {
40 is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; 39 is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}";
41 if ($debug) { 40 if ($debug) {
42 diag "val = $_; critical check = ".$t->critical->check_range($_). 41 diag "val = $_; critical check = ".$t->critical->check_range($_).
43 "; warning check = ".$t->warning->check_range($_); 42 "; warning check = ".$t->warning->check_range($_);
44 } 43 }
45 } 44 }
46 use Data::Dumper; 45 use Data::Dumper;
47 diag "thresh dump: ". Dumper $t if $debug; 46 diag "thresh dump: ". Dumper $t if $debug;
48} 47}
49test_expected_statuses( $t, $expected ); 48test_expected_statuses( $t, $expected );
50 49
50# GMC: this test seems bogus to me - either we've died, in which case internal
51# state is undefined (and untestable!), or we should be returning a non-fatal error
52if (0) {
53 diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
54 eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") };
55 ok( defined $t, "Threshold ('5:33', '') set");
56 cmp_ok( $t->warning->start, '==', 5, "Warning start set");
57 cmp_ok( $t->warning->end, '==', 33, "Warning end set");
58 ok( ! $t->critical->is_set, "Critical not set");
59}
60
61# GC: same as previous test, except critical is undef instead of ''
51diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; 62diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
52eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; 63$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef);
53ok( defined $t, "Threshold ('5:33', '') set"); 64ok( defined $t, "Threshold ('5:33', '') set");
54cmp_ok( $t->warning->start, '==', 5, "Warning start set"); 65cmp_ok( $t->warning->start, '==', 5, "Warning start set");
55cmp_ok( $t->warning->end, '==', 33, "Warning end set"); 66cmp_ok( $t->warning->end, '==', 33, "Warning end set");
@@ -88,7 +99,6 @@ $expected = { qw(
88) }; 99) };
89test_expected_statuses( $t, $expected ); 100test_expected_statuses( $t, $expected );
90 101
91
92# "I'm going to die homeless, penniless, and 30 pounds overweight." 102# "I'm going to die homeless, penniless, and 30 pounds overweight."
93# "...and that's...okay." 103# "...and that's...okay."
94 104
@@ -163,4 +173,37 @@ $expected = { qw(
163) }; 173) };
164test_expected_statuses( $t, $expected ); 174test_expected_statuses( $t, $expected );
165 175
176
177# GMC: as of 0.16, set_thresholds can also be called as a mutator
178diag "threshold mutator: warn if more than 30; critical if > 60"
179 if $ENV{TEST_VERBOSE};
180my $t1 = $t;
181$t->set_thresholds(warning => "0:45", critical => "0:90");
182is($t1, $t, "same threshold object after \$t->set_thresholds");
183ok( defined $t, "Threshold ('0:45', '0:90') set");
184is( $t->warning->start, 0, "Warning start ok");
185is( $t->warning->end, 45, "Warning end ok");
186is( $t->critical->start, 0, "Critical start ok");
187is( $t->critical->end, 90, "Critical end ok");
188
189
190# Also as of 0.16, accepts N::P::Range objects as arguments
191my $warning = Nagios::Plugin::Range->parse_range_string("50");
192my $critical = Nagios::Plugin::Range->parse_range_string("70:90");
193$t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical);
194ok( defined $t, "Threshold from ranges ('50', '70:90') set");
195is( $t->warning->start, 0, "Warning start ok");
196is( $t->warning->end, 50, "Warning end ok");
197is( $t->critical->start, 70, "Critical start ok");
198is( $t->critical->end, 90, "Critical end ok");
199
200$critical = Nagios::Plugin::Range->parse_range_string("90:");
201$t->set_thresholds(warning => "~:20", critical => $critical);
202ok( defined $t, "Threshold from string + range ('~:20', '90:') set");
203ok( $t->warning->start_infinity, "Warning start ok (infinity)");
204is( $t->warning->end, 20, "Warning end ok");
205is( $t->critical->start, 90, "Critical start ok");
206ok( $t->critical->end_infinity, "Critical end ok (infinity)");
207
208
166ok 1, "sweet, made it to the end."; 209ok 1, "sweet, made it to the end.";