| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Nagios::Plugin::Performance; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 6088 | use 5.006; | 
|  | 4 |  |  |  |  | 56 |  | 
|  | 4 |  |  |  |  | 194 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 4 |  |  | 4 |  | 23 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 154 |  | 
| 6 | 4 |  |  | 4 |  | 23 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 145 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 26 | use Carp; | 
|  | 4 |  |  |  |  | 83 |  | 
|  | 4 |  |  |  |  | 357 |  | 
| 9 | 4 |  |  | 4 |  | 26 | use base qw(Class::Accessor::Fast); | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 2360 |  | 
| 10 |  |  |  |  |  |  | __PACKAGE__->mk_ro_accessors( | 
| 11 |  |  |  |  |  |  | qw(label value uom warning critical min max) | 
| 12 |  |  |  |  |  |  | ); | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 4 |  |  | 4 |  | 9399 | use Nagios::Plugin::Functions; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 413 |  | 
| 15 | 4 |  |  | 4 |  | 1833 | use Nagios::Plugin::Threshold; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 43 |  | 
| 16 | 4 |  |  | 4 |  | 139 | use Nagios::Plugin::Range; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 22 |  | 
| 17 |  |  |  |  |  |  | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub import { | 
| 20 | 4 |  |  | 4 |  | 592 | my ($class, %attr) = @_; | 
| 21 | 4 |  | 100 |  |  | 31 | $_ = $attr{use_die} || 0; | 
| 22 | 4 |  |  |  |  | 19 | Nagios::Plugin::Functions::_use_die($_); | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # This is NOT the same as N::P::Functions::value_re. We leave that to be the strict | 
| 26 |  |  |  |  |  |  | # version. This one allows commas to be part of the numeric value. | 
| 27 |  |  |  |  |  |  | my $value = qr/[-+]?[\d\.,]+/; | 
| 28 |  |  |  |  |  |  | my $value_re = qr/$value(?:e$value)?/; | 
| 29 |  |  |  |  |  |  | my $value_with_negative_infinity = qr/$value_re|~/; | 
| 30 |  |  |  |  |  |  | sub _parse { | 
| 31 | 41 |  |  | 41 |  | 82 | my $class = shift; | 
| 32 | 41 |  |  |  |  | 66 | my $string = shift; | 
| 33 | 41 |  |  |  |  | 822 | $string =~ /^'?([^'=]+)'?=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o; | 
| 34 | 41 | 100 | 66 |  |  | 439 | return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 35 | 37 |  |  |  |  | 246 | my @info = ($1, $2, $3, $4, $5, $6, $7); | 
| 36 |  |  |  |  |  |  | # We convert any commas to periods, in the value fields | 
| 37 | 37 | 100 |  |  |  | 69 | map { defined $info[$_] && $info[$_] =~ s/,/./go } (1, 3, 4, 5, 6); | 
|  | 185 |  |  |  |  | 700 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # Check that $info[1] is an actual value | 
| 40 |  |  |  |  |  |  | # We do this by returning undef if a warning appears | 
| 41 | 37 |  |  |  |  | 145 | my $performance_value; | 
| 42 |  |  |  |  |  |  | { | 
| 43 | 37 |  |  |  |  | 50 | my $not_value; | 
|  | 37 |  |  |  |  | 40 |  | 
| 44 | 37 |  |  | 1 |  | 331 | local $SIG{__WARN__} = sub { $not_value++ }; | 
|  | 1 |  |  |  |  | 8 |  | 
| 45 | 37 |  |  |  |  | 116 | $performance_value = $info[1]+0; | 
| 46 | 37 | 100 |  |  |  | 217 | return undef if $not_value; | 
| 47 |  |  |  |  |  |  | } | 
| 48 | 36 |  |  |  |  | 154 | my $p = $class->new( | 
| 49 |  |  |  |  |  |  | label => $info[0], value => $performance_value, uom => $info[2], warning => $info[3], critical => $info[4], | 
| 50 |  |  |  |  |  |  | min => $info[5], max => $info[6] | 
| 51 |  |  |  |  |  |  | ); | 
| 52 | 36 |  |  |  |  | 664 | return $p; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # Map undef to '' | 
| 56 |  |  |  |  |  |  | sub _nvl { | 
| 57 | 185 |  |  | 185 |  | 958 | my ($self, $value) = @_; | 
| 58 | 185 | 100 |  |  |  | 757 | defined $value ? $value : '' | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub perfoutput { | 
| 62 | 37 |  |  | 37 | 1 | 406 | my $self = shift; | 
| 63 |  |  |  |  |  |  | # Add quotes if label contains a space character | 
| 64 | 37 |  |  |  |  | 94 | my $label = $self->label; | 
| 65 | 37 | 100 |  |  |  | 230 | if ($label =~ / /) { | 
| 66 | 2 |  |  |  |  | 7 | $label = "'$label'"; | 
| 67 |  |  |  |  |  |  | } | 
| 68 | 37 |  |  |  |  | 107 | my $out = sprintf "%s=%s%s;%s;%s;%s;%s", | 
| 69 |  |  |  |  |  |  | $label, | 
| 70 |  |  |  |  |  |  | $self->value, | 
| 71 |  |  |  |  |  |  | $self->_nvl($self->uom), | 
| 72 |  |  |  |  |  |  | $self->_nvl($self->warning), | 
| 73 |  |  |  |  |  |  | $self->_nvl($self->critical), | 
| 74 |  |  |  |  |  |  | $self->_nvl($self->min), | 
| 75 |  |  |  |  |  |  | $self->_nvl($self->max); | 
| 76 |  |  |  |  |  |  | # Previous implementation omitted trailing ;; - do we need this? | 
| 77 | 37 |  |  |  |  | 181 | $out =~ s/;;$//; | 
| 78 | 37 |  |  |  |  | 202 | return $out; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub parse_perfstring { | 
| 82 | 29 |  |  | 29 | 1 | 13963 | my ($class, $perfstring) = @_; | 
| 83 | 29 |  |  |  |  | 60 | my @perfs = (); | 
| 84 | 29 |  |  |  |  | 40 | my $obj; | 
| 85 | 29 |  |  |  |  | 94 | while ($perfstring) { | 
| 86 | 41 |  |  |  |  | 204 | $perfstring =~ s/^\s*//; | 
| 87 |  |  |  |  |  |  | # If there is more than 1 equals sign, split it out and parse individually | 
| 88 | 41 | 100 |  |  |  | 68 | if (@{[$perfstring =~ /=/g]} > 1) { | 
|  | 41 |  |  |  |  | 226 |  | 
| 89 | 14 |  |  |  |  | 108 | $perfstring =~ s/^(.*?=.*?)\s//; | 
| 90 | 14 | 100 |  |  |  | 58 | if (defined $1) { | 
| 91 | 13 |  |  |  |  | 44 | $obj = $class->_parse($1); | 
| 92 |  |  |  |  |  |  | } else { | 
| 93 |  |  |  |  |  |  | # This could occur if perfdata was soemthing=value= | 
| 94 |  |  |  |  |  |  | # Since this is invalid, we reset the string and continue | 
| 95 | 1 |  |  |  |  | 3 | $perfstring = ""; | 
| 96 | 1 |  |  |  |  | 6 | $obj = $class->_parse($perfstring); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | } else { | 
| 99 | 27 |  |  |  |  | 87 | $obj = $class->_parse($perfstring); | 
| 100 | 27 |  |  |  |  | 54 | $perfstring = ""; | 
| 101 |  |  |  |  |  |  | } | 
| 102 | 41 | 100 |  |  |  | 195 | push @perfs, $obj if $obj; | 
| 103 |  |  |  |  |  |  | } | 
| 104 | 29 |  |  |  |  | 164 | return @perfs; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub rrdlabel { | 
| 108 | 15 |  |  | 15 | 1 | 11137 | my $self = shift; | 
| 109 | 15 |  |  |  |  | 86 | my $name = $self->clean_label; | 
| 110 |  |  |  |  |  |  | # Shorten | 
| 111 | 15 |  |  |  |  | 86 | return substr( $name, 0, 19 ); | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub clean_label { | 
| 115 | 20 |  |  | 20 | 1 | 74 | my $self = shift; | 
| 116 | 20 |  |  |  |  | 66 | my $name = $self->label; | 
| 117 | 20 | 100 |  |  |  | 189 | if ($name eq "/") { | 
|  |  | 100 |  |  |  |  |  | 
| 118 | 3 |  |  |  |  | 7 | $name = "root"; | 
| 119 |  |  |  |  |  |  | } elsif ( $name =~ s/^\/// ) { | 
| 120 | 6 |  |  |  |  | 24 | $name =~ s/\//_/g; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  | # Convert all other characters | 
| 123 | 20 |  |  |  |  | 92 | $name =~ s/\W/_/g; | 
| 124 | 20 |  |  |  |  | 65 | return $name; | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | # Backward compatibility: create a threshold object on the fly as requested | 
| 128 |  |  |  |  |  |  | sub threshold | 
| 129 |  |  |  |  |  |  | { | 
| 130 | 90 |  |  | 90 | 1 | 55067 | my $self = shift; | 
| 131 | 90 |  |  |  |  | 293 | return Nagios::Plugin::Threshold->set_thresholds( | 
| 132 |  |  |  |  |  |  | warning => $self->warning, critical => $self->critical | 
| 133 |  |  |  |  |  |  | ); | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | # Constructor - unpack thresholds, map args to hashref | 
| 137 |  |  |  |  |  |  | sub new | 
| 138 |  |  |  |  |  |  | { | 
| 139 | 48 |  |  | 48 | 1 | 9307 | my $class = shift; | 
| 140 | 48 |  |  |  |  | 300 | my %arg = @_; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | # Convert thresholds | 
| 143 | 48 | 100 |  |  |  | 396 | if (my $threshold = delete $arg{threshold}) { | 
| 144 | 7 |  | 66 |  |  | 47 | $arg{warning}  ||= $threshold->warning  . ""; | 
| 145 | 7 |  | 66 |  |  | 221 | $arg{critical} ||= $threshold->critical . ""; | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 48 |  |  |  |  | 494 | $class->SUPER::new(\%arg); | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | 1; | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | __END__ |