1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package Monitoring::Plugin::Performance;
use 5.006;
use strict;
use warnings;
use Carp;
use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_ro_accessors(
qw(label value uom warning critical min max)
);
use Monitoring::Plugin::Functions;
use Monitoring::Plugin::Threshold;
use Monitoring::Plugin::Range;
our ($VERSION) = $Monitoring::Plugin::Functions::VERSION;
sub import {
my ($class, %attr) = @_;
$_ = $attr{use_die} || 0;
Monitoring::Plugin::Functions::_use_die($_);
}
# This is NOT the same as N::P::Functions::value_re. We leave that to be the strict
# version. This one allows commas to be part of the numeric value.
my $value = qr/[-+]?[\d\.,]+/;
my $value_re = qr/$value(?:e$value)?/;
my $value_with_negative_infinity = qr/$value_re|~/;
sub _parse {
my $class = shift;
my $string = shift;
$string =~ /^'?([^'=]+)'?=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o;
return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne ""));
my @info = ($1, $2, $3, $4, $5, $6, $7);
# We convert any commas to periods, in the value fields
map { defined $info[$_] && $info[$_] =~ s/,/./go } (1, 3, 4, 5, 6);
# Check that $info[1] is an actual value
# We do this by returning undef if a warning appears
my $performance_value;
{
my $not_value;
local $SIG{__WARN__} = sub { $not_value++ };
$performance_value = $info[1]+0;
return undef if $not_value;
}
my $p = $class->new(
label => $info[0], value => $performance_value, uom => $info[2], warning => $info[3], critical => $info[4],
min => $info[5], max => $info[6]
);
return $p;
}
# Map undef to ''
sub _nvl {
my ($self, $value) = @_;
defined $value ? $value : ''
}
sub perfoutput {
my $self = shift;
# Add quotes if label contains a space character
my $label = $self->label;
if ($label =~ / /) {
$label = "'$label'";
}
my $value = $self->value;
# To prevent invalid output, we change empty value to value "U"
if ($value eq '') {
$value = 'U';
}
my $out = sprintf "%s=%s%s;%s;%s;%s;%s",
$label,
$value,
$self->_nvl($self->uom),
$self->_nvl($self->warning),
$self->_nvl($self->critical),
$self->_nvl($self->min),
$self->_nvl($self->max);
# Previous implementation omitted trailing ;; - do we need this?
$out =~ s/;;$//;
return $out;
}
sub parse_perfstring {
my ($class, $perfstring) = @_;
my @perfs = ();
my $obj;
while ($perfstring) {
$perfstring =~ s/^\s*//;
# If there is more than 1 equals sign, split it out and parse individually
if (@{[$perfstring =~ /=/g]} > 1) {
$perfstring =~ s/^(.*?=.*?)\s//;
if (defined $1) {
$obj = $class->_parse($1);
} else {
# This could occur if perfdata was soemthing=value=
# Since this is invalid, we reset the string and continue
$perfstring = "";
$obj = $class->_parse($perfstring);
}
} else {
$obj = $class->_parse($perfstring);
$perfstring = "";
}
push @perfs, $obj if $obj;
}
return @perfs;
}
sub rrdlabel {
my $self = shift;
my $name = $self->clean_label;
# Shorten
return substr( $name, 0, 19 );
}
sub clean_label {
my $self = shift;
my $name = $self->label;
if ($name eq "/") {
$name = "root";
} elsif ( $name =~ s/^\/// ) {
$name =~ s/\//_/g;
}
# Convert all other characters
$name =~ s/\W/_/g;
return $name;
}
# Backward compatibility: create a threshold object on the fly as requested
sub threshold
{
my $self = shift;
return Monitoring::Plugin::Threshold->set_thresholds(
warning => $self->warning, critical => $self->critical
);
}
# Constructor - unpack thresholds, map args to hashref
sub new
{
my $class = shift;
my %arg = @_;
# Convert thresholds
if (my $threshold = delete $arg{threshold}) {
$arg{warning} ||= $threshold->warning . "";
$arg{critical} ||= $threshold->critical . "";
}
$class->SUPER::new(\%arg);
}
1;
__END__
=head1 NAME
Monitoring::Plugin::Performance - class for handling Monitoring::Plugin
performance data.
=head1 SYNOPSIS
use Monitoring::Plugin::Performance use_die => 1;
# Constructor (also accepts a 'threshold' obj instead of warning/critical)
$p = Monitoring::Plugin::Performance->new(
label => 'size',
value => $value,
uom => "kB",
warning => $warning,
critical => $critical,
min => $min,
max => $max,
);
# Parser
@perf = Monitoring::Plugin::Performance->parse_perfstring(
"/=382MB;15264;15269;; /var=218MB;9443;9448"
)
or warn("Failed to parse perfstring");
# Accessors
for $p (@perf) {
printf "label: %s\n", $p->label;
printf "value: %s\n", $p->value;
printf "uom: %s\n", $p->uom;
printf "warning: %s\n", $p->warning;
printf "critical: %s\n", $p->critical;
printf "min: %s\n", $p->min;
printf "max: %s\n", $p->max;
# Special accessor returning a threshold obj containing warning/critical
$threshold = $p->threshold;
}
# Perfdata output format i.e. label=value[uom];[warn];[crit];[min];[max]
print $p->perfoutput;
=head1 DESCRIPTION
Monitoring::Plugin class for handling performance data. This is a public
interface because it could be used by performance graphing routines,
such as nagiostat (http://nagiostat.sourceforge.net), perfparse
(http://perfparse.sourceforge.net), nagiosgraph
(http://nagiosgraph.sourceforge.net) or NagiosGrapher
(http://www.nagiosexchange.org/NagiosGrapher.84.0.html).
Monitoring::Plugin::Performance offers both a parsing interface (via
parse_perfstring), for turning nagios performance output strings into
their components, and a composition interface (via new), for turning
components into perfdata strings.
=head1 USE'ING THE MODULE
If you are using this module for the purposes of parsing perf data, you
will probably want to set use_die => 1 at use time. This forces
&Monitoring::Plugin::Functions::plugin_exit to call die() - rather than exit() -
when an error occurs. This is then trappable by an eval. If you don't set use_die,
then an error in these modules will cause your script to exit
=head1 CLASS METHODS
=over 4
=item Monitoring::Plugin::Performance->new(%attributes)
Instantiates a new Monitoring::Plugin::Performance object with the given
attributes.
=item Monitoring::Plugin::Performance->parse_perfstring($string)
Returns an array of Monitoring::Plugin::Performance objects based on the string
entered. If there is an error parsing the string - which may consists of several
sets of data - will return an array with all the successfully parsed sets.
If values are input with commas instead of periods, due to different locale settings,
then it will still be parsed, but the commas will be converted to periods.
=back
=head1 OBJECT METHODS (ACCESSORS)
=over 4
=item label, value, uom, warning, critical, min, max
These all return scalars. min and max are not well supported yet.
=item threshold
Returns a Monitoring::Plugin::Threshold object holding the warning and critical
ranges for this performance data (if any).
=item rrdlabel
Returns a string based on 'label' that is suitable for use as dataset name of
an RRD i.e. munges label to be 1-19 characters long with only characters
[a-zA-Z0-9_].
This calls $self->clean_label and then truncates to 19 characters.
There is no guarantee that multiple N:P:Performance objects will have unique
rrdlabels.
=item clean_label
Returns a "clean" label for use as a dataset name in RRD, ie, it converts
characters that are not [a-zA-Z0-9_] to _.
It also converts "/" to "root" and "/{name}" to "{name}".
=item perfoutput
Outputs the data in Monitoring::Plugin perfdata format i.e.
label=value[uom];[warn];[crit];[min];[max].
=back
=head1 SEE ALSO
Monitoring::Plugin, Monitoring::Plugin::Threshold, https://www.monitoring-plugins.org/doc/guidelines.html
=head1 AUTHOR
This code is maintained by the Monitoring Plugin Development Team: see
https://monitoring-plugins.org
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014 by Monitoring Plugin Team
Copyright (C) 2006-2014 by Nagios Plugin Development Team
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|