summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes3
-rw-r--r--lib/Nagios/Plugin.pm2
-rw-r--r--lib/Nagios/Plugin/Functions.pm2
-rw-r--r--lib/Nagios/Plugin/Performance.pm25
-rw-r--r--t/Nagios-Plugin-Performance.t16
5 files changed, 33 insertions, 15 deletions
diff --git a/Changes b/Changes
index 78382e1..90a1e0c 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.29 2nd December 2008
4 - clean_label, for cleaning up a label for RRD, but without truncation
5
30.28 21st November 2008 60.28 21st November 2008
4 - Fixed test problems when run against Test::More 0.86 7 - Fixed test problems when run against Test::More 0.86
5 - Added max_state_* wrappers 8 - Added max_state_* wrappers
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 7299929..eb3ccdc 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -25,7 +25,7 @@ our @EXPORT_OK = qw(%ERRORS);
25# CPAN stupidly won't index this module without a literal $VERSION here, 25# CPAN stupidly won't index this module without a literal $VERSION here,
26# so we're forced to duplicate it explicitly 26# so we're forced to duplicate it explicitly
27# Make sure you update $Nagios::Plugin::Functions::VERSION too 27# Make sure you update $Nagios::Plugin::Functions::VERSION too
28our $VERSION = "0.28"; 28our $VERSION = "0.29";
29 29
30sub new { 30sub new {
31 my $class = shift; 31 my $class = shift;
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
index 74a7c6b..ffa23f0 100644
--- a/lib/Nagios/Plugin/Functions.pm
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -12,7 +12,7 @@ use Params::Validate qw(:types validate);
12use Math::Calc::Units; 12use Math::Calc::Units;
13 13
14# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
15our $VERSION = "0.28"; 15our $VERSION = "0.29";
16 16
17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
18 18
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm
index a7655fc..a9f9198 100644
--- a/lib/Nagios/Plugin/Performance.pm
+++ b/lib/Nagios/Plugin/Performance.pm
@@ -70,18 +70,22 @@ sub parse_perfstring {
70 70
71sub rrdlabel { 71sub rrdlabel {
72 my $self = shift; 72 my $self = shift;
73 my $name = $self->clean_label;
74 # Shorten
75 return substr( $name, 0, 19 );
76}
77
78sub clean_label {
79 my $self = shift;
73 my $name = $self->label; 80 my $name = $self->label;
74 if ($name eq "/") { 81 if ($name eq "/") {
75 $name = "root"; 82 $name = "root";
76 } 83 } elsif ( $name =~ s/^\/// ) {
77 # If filesystem name, remove initial / and convert subsequent "/" to "_"
78 elsif ($name =~ s/^\///) {
79 $name =~ s/\//_/g; 84 $name =~ s/\//_/g;
80 } 85 }
81 # Convert bad chars 86 # Convert all other characters
82 $name =~ s/\W/_/g; 87 $name =~ s/\W/_/g;
83 # Shorten 88 return $name;
84 return substr( $name, 0, 19 );
85} 89}
86 90
87# Backward compatibility: create a threshold object on the fly as requested 91# Backward compatibility: create a threshold object on the fly as requested
@@ -212,9 +216,18 @@ Returns a string based on 'label' that is suitable for use as dataset name of
212an RRD i.e. munges label to be 1-19 characters long with only characters 216an RRD i.e. munges label to be 1-19 characters long with only characters
213[a-zA-Z0-9_]. 217[a-zA-Z0-9_].
214 218
219This calls $self->clean_label and then truncates to 19 characters.
220
215There is no guarantee that multiple N:P:Performance objects will have unique 221There is no guarantee that multiple N:P:Performance objects will have unique
216rrdlabels. 222rrdlabels.
217 223
224=item clean_label
225
226Returns a "clean" label for use as a dataset name in RRD, ie, it converts
227characters that are not [a-zA-Z0-9_] to _.
228
229It also converts "/" to "root" and "/{name}" to "{name}".
230
218=item perfoutput 231=item perfoutput
219 232
220Outputs the data in Nagios::Plugin perfdata format i.e. 233Outputs the data in Nagios::Plugin perfdata format i.e.
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t
index fa36ce0..ceb82c5 100644
--- a/t/Nagios-Plugin-Performance.t
+++ b/t/Nagios-Plugin-Performance.t
@@ -1,6 +1,6 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 123; 3use Test::More;
4BEGIN { use_ok('Nagios::Plugin::Performance') }; 4BEGIN { use_ok('Nagios::Plugin::Performance') };
5 5
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; 6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
@@ -11,20 +11,20 @@ Nagios::Plugin::Functions::_fake_exit(1);
11my (@p, $p); 11my (@p, $p);
12my @test = ( 12my @test = (
13 { 13 {
14 perfoutput => "/=382MB;15264;15269;0;32768", label => '/', rrdlabel => 'root', value => 382, uom => 'MB', warning => 15264, critical => 15269, min => 0, max => 32768, 14 perfoutput => "/=382MB;15264;15269;0;32768", label => '/', rrdlabel => 'root', value => 382, uom => 'MB', warning => 15264, critical => 15269, min => 0, max => 32768, clean_label => "root",
15 }, { 15 }, {
16 perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, 16 perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var",
17 }, {
18 perfoutput => '/var/long@:-/filesystem/name/and/bad/chars=218MB;9443;9448', label => '/var/long@:-/filesystem/name/and/bad/chars', rrdlabel => 'var_long____filesys', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => 'var_long____filesystem_name_and_bad_chars',
17 }, 19 },
18); 20);
19 21
22plan tests => (8 * scalar @test) + 94;
23
20# Round-trip tests 24# Round-trip tests
21for my $t (@test) { 25for my $t (@test) {
22 # Parse to components 26 # Parse to components
23 ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput}); 27 ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput});
24 for (sort keys %$t) {
25 next if m/^perfoutput$/;
26 is($p->$_(), $t->{$_}, "$_ okay (" . (defined $t->{$_} ? $t->{$_} : 'undef') . ")");
27 }
28 28
29 # Construct from components 29 # Construct from components
30 my @construct = qw(label value uom warning critical min max); 30 my @construct = qw(label value uom warning critical min max);
@@ -33,6 +33,8 @@ for my $t (@test) {
33 # Check threshold accessor 33 # Check threshold accessor
34 is($p->threshold->warning->end, $t->{warning}, "threshold warning okay ($t->{warning})"); 34 is($p->threshold->warning->end, $t->{warning}, "threshold warning okay ($t->{warning})");
35 is($p->threshold->critical->end, $t->{critical}, "threshold critical okay ($t->{critical})"); 35 is($p->threshold->critical->end, $t->{critical}, "threshold critical okay ($t->{critical})");
36 is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay");
37 is($p->clean_label, $t->{clean_label}, "clean_label okay" );
36 38
37 # Construct using threshold 39 # Construct using threshold
38 @construct = qw(label value uom min max); 40 @construct = qw(label value uom min max);