Plugin/Performance.pm

filter: Statementstmt 100% 91 / 91 Branchbran 100% 22 / 22 Conditioncond 80.0% 16 / 20 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 95.2% 160 / 168 CC 19 SCAR 12.2
CC
19
Cov
97%
_parse
7.0
parse_perfstring
5.0
new
4.0
CRAP 3.4
« Plugin.pm ↑ Summary Plugin/ExitResult.pm »
1 package Nagios::Monitoring::Plugin::Performance;
2
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 __PACKAGE__->mk_ro_accessors(
11 qw(label value uom warning critical min max)
12 );
13
14 4 ▶use Nagios::Monitoring::Plugin::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Nagios::Monitoring::Plugin::Threshold;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Nagios::Monitoring::Plugin::Range;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 our ($VERSION) = $Nagios::Monitoring::Plugin::Functions::VERSION;
18
19 4 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
20 4 my ($class, %attr) = @_;
21 4 ▶ $_ = $attr{use_die} || 0;
Condition100%2/2
$attr{'use_die'} || 0
l!l
13
MC/DC100%1/1
$attr{'use_die'} || 0
$attr{'use_die'}
Truth table100%2/2
A
A= $attr{'use_die'}
Aresult
11
00
22 4 Nagios::Monitoring::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 4 my $value = qr/[-+]?[\d\.,]+/;
28 4 my $value_re = qr/$value(?:e$value)?/;
29 4 my $value_with_negative_infinity = qr/$value_re|~/;
30 41 ▶sub _parse {
Subroutine _parse: called CC=7 SCAR=19.5
31 41 my $class = shift;
32 41 my $string = shift;
33 41 $string =~ /^'?([^'=]+)'?=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o;
34 41 ▶ return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne ""));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 and $1 ne "" and (defined $2 and $2 ne "") 4 37 41
Condition77%7/9
defined $1 and $1 ne "" and (defined $2 and $2 ne "")
!ll&&!rl&&r
3137
defined $1 and $1 ne ""
!ll&&!rl&&r
3038
defined $2 and $2 ne ""
!ll&&!rl&&r
1037
MC/DC50%2/4
defined $1 and $1 ne "" and (defined $2 and $2 ne "")
defined $1 $1 ne "" defined $2 $2 ne ""
Truth table60%3/5
A and B and C and D
A= defined $1B= $1 ne ""C= defined $2D= $2 ne ""
ABCDresult
0XXX0
10XX0
110X0
11100
11111
35 37 my @info = ($1, $2, $3, $4, $5, $6, $7);
36 # We convert any commas to periods, in the value fields
37 37 ▶ map { defined $info[$_] && $info[$_] =~ s/,/./go } (1, 3, 4, 5, 6);
Condition100%3/3
defined $info[$_] && $info[$_] =~ s/,/./go
!ll&&!rl&&r
731057
MC/DC100%2/2
defined $info[$_] && $info[$_] =~ s/,/./go
defined $info[$_] $info[$_] =~ s/,/./go
Truth table100%3/3
A && B
A= defined $info[$_]B= $info[$_] =~ s/,/./go
ABresult
0X0
100
111
38
39 # Check that $info[1] is an actual value
40 # We do this by returning undef if a warning appears
41 37 my $performance_value;
42 {
43 37 my $not_value;
44 37 ▶ local $SIG{__WARN__} = sub { $not_value++ };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 37 $performance_value = $info[1]+0;
46 37 ▶ return undef if $not_value;
Branch: 1 branch
BranchTrueFalseTotal
if $not_value 1 36 37
47 }
48 36 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 return $p;
53 }
54
55 # Map undef to ''
56 185 ▶sub _nvl {
Subroutine _nvl: called CC=2 SCAR=6.9
57 185 my ($self, $value) = @_;
58 185 ▶ defined $value ? $value : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 100 85 185
59 }
60
61 37 ▶sub perfoutput {
Subroutine perfoutput: called CC=2 SCAR=6.9
Pod documented
62 37 my $self = shift;
63 # Add quotes if label contains a space character
64 37 my $label = $self->label;
65 37 ▶ if ($label =~ / /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label =~ / /) 2 35 37
66 2 $label = "'$label'";
67 }
68 37 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 $out =~ s/;;$//;
78 37 return $out;
79 }
80
81 29 ▶sub parse_perfstring {
Subroutine parse_perfstring: called CC=5 SCAR=16.1
Pod documented
82 29 my ($class, $perfstring) = @_;
83 29 my @perfs = ();
84 29 my $obj;
85 29 ▶ while ($perfstring) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perfstring) 41 29 70
86 41 $perfstring =~ s/^\s*//;
87 # If there is more than 1 equals sign, split it out and parse individually
88 41 ▶ if (@{[$perfstring =~ /=/g]} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{[$perfstring =~ /=/g];} > 1) { } 14 27 41
89 14 $perfstring =~ s/^(.*?=.*?)\s//;
90 14 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 13 1 14
91 13 $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 $perfstring = "";
96 1 $obj = $class->_parse($perfstring);
97 }
98 } else {
99 27 $obj = $class->_parse($perfstring);
100 27 $perfstring = "";
101 }
102 41 ▶ push @perfs, $obj if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 36 5 41
103 }
104 29 return @perfs;
105 }
106
107 15 ▶sub rrdlabel {
Subroutine rrdlabel: called CC=1 SCAR=0.0
Pod documented
108 15 my $self = shift;
109 15 my $name = $self->clean_label;
110 # Shorten
111 15 return substr( $name, 0, 19 );
112 }
113
114 20 ▶sub clean_label {
Subroutine clean_label: called CC=3 SCAR=11.0
Pod documented
115 20 my $self = shift;
116 20 my $name = $self->label;
117 20 ▶ if ($name eq "/") {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '/') { } 3 17 20
elsif ($name =~ s[^/][]) { } 6 11 17
118 3 $name = "root";
119 } elsif ( $name =~ s/^\/// ) {
120 6 $name =~ s/\//_/g;
121 }
122 # Convert all other characters
123 20 $name =~ s/\W/_/g;
124 20 return $name;
125 }
126
127 # Backward compatibility: create a threshold object on the fly as requested
128 sub threshold
129 90 ▶{
Subroutine threshold: called CC=1 SCAR=0.0
Pod documented
130 90 my $self = shift;
131 90 return Nagios::Monitoring::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 48 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod documented
139 48 my $class = shift;
140 48 my %arg = @_;
141
142 # Convert thresholds
143 48 ▶ if (my $threshold = delete $arg{threshold}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $threshold = delete $arg{"threshold"}) 7 41 48
144 7 ▶ $arg{warning} ||= $threshold->warning . "";
Condition66%2/3
$arg{"warning"} ||= $threshold->warning . ''
l!l&&r!l&&!r
061
MC/DC50%1/2
$arg{"warning"} ||= $threshold->warning . ''
$arg{"warning"} $threshold->warning . ''
Truth table66%2/3
A ||= B
A= $arg{"warning"}B= $threshold->warning . ''
ABresult
1X1
011
000
145 7 ▶ $arg{critical} ||= $threshold->critical . "";
Condition66%2/3
$arg{"critical"} ||= $threshold->critical . ''
l!l&&r!l&&!r
061
MC/DC50%1/2
$arg{"critical"} ||= $threshold->critical . ''
$arg{"critical"} $threshold->critical . ''
Truth table66%2/3
A ||= B
A= $arg{"critical"}B= $threshold->critical . ''
ABresult
1X1
011
000
146 }
147
148 48 $class->SUPER::new(\%arg);
149 }
150
151 4 1;
152
153 __END__
« Plugin.pm ↑ Summary Plugin/ExitResult.pm »