From e736a3c2b0a62707f12cf66fbb65ef23eeb01dd6 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 15 Jun 2006 08:38:18 +0000 Subject: Added rrdlabel method. Fixed parse_perfstring if value=0 git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1428 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index ae1498b..a86a117 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl module Nagios::Plugin. +0.12 ?? + - rrdlabel method available to get a performance label, + converted to something rrd can use + - fixes to parse_perfstring routine if values are 0 + 0.11 14th June 2006 - Interface changed for parse_perfstring, returning empty array if not parseable diff --git a/MANIFEST b/MANIFEST index 336daab..f9c8b03 100644 --- a/MANIFEST +++ b/MANIFEST @@ -11,3 +11,4 @@ lib/Nagios/Plugin/Performance.pm lib/Nagios/Plugin/Range.pm lib/Nagios/Plugin/Threshold.pm lib/Nagios/Plugin/Base.pm +META.yml Module meta-data (added by MakeMaker) diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index ed2e6f5..2acc6ea 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -23,7 +23,7 @@ use Exporter; our @ISA = qw(Exporter Nagios::__::Plugin); our @EXPORT_OK = qw(%ERRORS); -our $VERSION = '0.11'; +our $VERSION = '0.12'; sub add_perfdata { my ($self, %args) = @_; diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 82c1a3b..45109ae 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -28,7 +28,7 @@ sub _parse { my $string = shift; my $p = $class->new; $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?\s*//; - return undef unless ($1 && $2); + return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); $p->label($1); $p->value($2+0); $p->uom($3); @@ -50,6 +50,21 @@ sub parse_perfstring { return @perfs; } +sub rrdlabel { + my $self = shift; + my $name = $self->label; + if ($name eq "/") { + $name = "root"; + # If filesystem name, remove initial / and convert subsequent "/" to "_" + } elsif ($name =~ s/^\///) { + $name =~ s/\//_/g; + } + # Convert bad chars + $name =~ s/\W/_/g; + # Shorten + return substr( $name, 0, 19 ); +} + 1; __END__ @@ -94,6 +109,13 @@ If there is an error parsing the string, an empty array is returned. These all return scalars. min and max are not well supported yet. +=item rrdlabel + +Returns a label that can be used for the dataset name of an RRD, ie, between 1-19 +characters long with characters [a-zA-Z0-9_]. + +There is no guarantee that multiple N:P:Performance objects will have unique rrdlabels. + =item threshold This returns a Nagios::Plugin::Threshold object. diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index a00b2db..aa0ab64 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -8,6 +8,7 @@ Nagios::Plugin::Base->exit_on_die(0); my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); cmp_ok( $p[0]->label, 'eq', "/", "label okay"); +cmp_ok( $p[0]->rrdlabel, 'eq', "root", "rrd label okay"); cmp_ok( $p[0]->value, '==', 382, "value okay"); cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay"); cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay"); @@ -16,6 +17,7 @@ ok( ! defined $p[0]->min, "min okay"); ok( ! defined $p[0]->max, "max okay"); cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); +cmp_ok( $p[1]->rrdlabel, 'eq', "var", "rrd label okay"); cmp_ok( $p[1]->value, '==', 218, "value okay"); cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); @@ -65,3 +67,10 @@ cmp_ok( $p[1]->value, "==", 426, "value okay"); cmp_ok( $p[1]->uom, "eq", "B", "uom okay"); ok( ! defined $p[1]->threshold->warning, "warn okay"); ok( ! defined $p[1]->threshold->critical, "crit okay"); + +# RRDlabel testing +@p = Nagios::Plugin::Performance->parse_perfstring("/home/a-m=0 shared-folder:big=20 12345678901234567890=20"); +cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _"); +cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); +cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); + -- cgit v0.10-9-g596f