Plugin.pm

filter: Statementstmt 96.8% 93 / 96 Branchbran 87.5% 28 / 32 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 90.9% 20 / 22 Podpod 85.7% 12 / 14 totaltotal 90.0% 172 / 191 CC 24 SCAR 14.3
CC
24
Cov
92%
check_threshold
8.1
add_message
7.0
check_messages
6.1
CRAP 4.2
« Plugin/Threshold.pm ↑ Summary Plugin/Performance.pm »
1
2 package Nagios::Monitoring::Plugin;
3
4 12 ▶use Nagios::Monitoring::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 12 Nagios::Monitoring::Plugin->mk_accessors(qw(
14 shortname
15 perfdata
16 messages
17 opts
18 threshold
19 ));
20
21 12 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 our @ISA = qw(Exporter);
23 12 our @EXPORT = (@STATUS_CODES);
24 12 our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT);
25
26 # CPAN stupidly won't index this module without a literal $VERSION here,
27 # so we're forced to duplicate it explicitly
28 # Make sure you update $Nagios::Monitoring::Plugin::Functions::VERSION too
29 12 our $VERSION = "0.51";
30
31 31 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
32 31 my $class = shift;
33 # my %args = @_;
34
35 31 my %args = validate( @_,
36 {
37 shortname => 0,
38 usage => 0,
39 version => 0,
40 url => 0,
41 plugin => 0,
42 blurb => 0,
43 extra => 0,
44 license => 0,
45 timeout => 0
46 },
47 );
48
49 31 my $shortname = Nagios::Monitoring::Plugin::Functions::get_shortname(\%args);
50 31 ▶ delete $args{shortname} if (exists $args{shortname});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"shortname"} 4 27 31
51 31 my $self = {
52 shortname => $shortname,
53 perfdata => [], # to be added later
54 messages => {
55 warning => [],
56 critical => [],
57 ok => []
58 },
59 opts => undef, # see below
60 threshold => undef, # defined later
61 };
62 31 bless $self, $class;
63 31 ▶ if (exists $args{usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"usage"}) 8 23 31
64 8 require Nagios::Monitoring::Plugin::Getopt;
65 8 $self->opts( new Nagios::Monitoring::Plugin::Getopt(%args) );
66 }
67 31 return $self;
68 }
69
70 2 ▶sub add_perfdata {
Subroutine add_perfdata: called CC=1 SCAR=0.0
Pod documented
71 2 my ($self, %args) = @_;
72 2 require Nagios::Monitoring::Plugin::Performance;
73 2 my $perf = Nagios::Monitoring::Plugin::Performance->new(%args);
74 2 push @{$self->perfdata}, $perf;
75 }
76 61 ▶sub all_perfoutput {
Subroutine all_perfoutput: called CC=1 SCAR=0.0
Pod undocumented
77 61 my $self = shift;
78 61 return join(" ", map {$_->perfoutput} (@{$self->perfdata}));
79 }
80
81 9 ▶sub set_thresholds {
Subroutine set_thresholds: called CC=1 SCAR=0.0
Pod documented
82 9 my $self = shift;
83 9 require Nagios::Monitoring::Plugin::Threshold;
84 9 return $self->threshold( Nagios::Monitoring::Plugin::Threshold->set_thresholds(@_));
85 }
86
87 # NP::Functions wrappers
88 22 ▶sub nagios_exit {
Subroutine nagios_exit: called CC=1 SCAR=0.0
Pod documented
89 22 my $self = shift;
90 22 Nagios::Monitoring::Plugin::Functions::nagios_exit(@_, { plugin => $self });
91 }
92 25 ▶sub nagios_die {
Subroutine nagios_die: called CC=1 SCAR=0.0
Pod documented
93 25 my $self = shift;
94 25 Nagios::Monitoring::Plugin::Functions::nagios_die(@_, { plugin => $self });
95 }
96 6 ▶sub die {
Subroutine die: called CC=1 SCAR=0.0
Pod documented
97 6 my $self = shift;
98 6 Nagios::Monitoring::Plugin::Functions::nagios_die(@_, { plugin => $self });
99 }
100 0 ▶sub max_state {
Subroutine max_state: not called CC=1 SCAR=6.9
Pod documented
101 0 Nagios::Monitoring::Plugin::Functions::max_state(@_);
102 }
103 0 ▶sub max_state_alt {
Subroutine max_state_alt: not called CC=1 SCAR=6.9
Pod documented
104 0 Nagios::Monitoring::Plugin::Functions::max_state_alt(@_);
105 }
106
107 # top level interface to Nagios::Monitoring::Plugin::Threshold
108 23 ▶sub check_threshold {
Subroutine check_threshold: called CC=8 SCAR=20.9
Pod documented
109 23 my $self = shift;
110
111 23 my %args;
112
113 23 ▶ if ( $#_ == 0 && (! ref $_[0] || ref $_[0] eq "ARRAY" )) { # one positional param
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0 and !ref($_[0]) || ref $_[0] eq 'ARRAY') { } 17 6 23
Condition66%4/6
$#_ == 0 and !ref($_[0]) || ref $_[0] eq "ARRAY"
!ll&&!rl&&r
6017
not ref $_[0] or ref $_[0] eq "ARRAY"
l!l&&r!l&&!r
1430
MC/DC33%1/3
$#_ == 0 and !ref($_[0]) || ref $_[0] eq "ARRAY"
$#_ == 0 not ref $_[0] ref $_[0] eq "ARRAY"
Truth table75%3/4
A and not(B) or C
A= $#_ == 0B= not ref $_[0]C= ref $_[0] eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
114 17 %args = (check => shift);
115 }
116 else {
117 6 %args = validate ( @_, { # named params
118 check => 1,
119 warning => 0,
120 critical => 0,
121 } );
122 }
123
124 # in order of preference, get warning and critical from
125 # 1. explicit arguments to check_threshold
126 # 2. previously explicitly set threshold object
127 # 3. implicit options from Getopts object
128 22 ▶ if ( exists $args{warning} || exists $args{critical} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args{'warning'} or exists $args{'critical'}) { } 3 19 22
elsif (defined $self->threshold) { } 11 8 19
elsif (defined $self->opts) { } 4 4 8
Condition66%2/3
exists $args{"warning"} or exists $args{"critical"}
l!l&&r!l&&!r
3019
MC/DC50%1/2
exists $args{"warning"} or exists $args{"critical"}
exists $args{"warning"} exists $args{"critical"}
Truth table66%2/3
A or B
A= exists $args{"warning"}B= exists $args{"critical"}
ABresult
1X1
011
000
129 $self->set_thresholds(
130 warning => $args{warning},
131 critical => $args{critical},
132 3 );
133 }
134 elsif ( defined $self->threshold ) {
135 # noop
136 }
137 elsif ( defined $self->opts ) {
138 4 $self->set_thresholds(
139 warning => $self->opts->warning,
140 critical => $self->opts->critical,
141 );
142 }
143 else {
144 4 return UNKNOWN;
145 }
146
147 18 return $self->threshold->get_status($args{check});
148 }
149
150 # top level interface to my Nagios::Monitoring::Plugin::Getopt object
151 24 ▶sub add_arg {
Subroutine add_arg: called CC=2 SCAR=7.2
Pod documented
152 24 my $self = shift;
153 24 ▶ $self->opts->arg(@_) if $self->_check_for_opts;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_for_opts 23 0 23
154 }
155 9 ▶sub getopts {
Subroutine getopts: called CC=2 SCAR=7.2
Pod documented
156 9 my $self = shift;
157 9 ▶ $self->opts->getopts(@_) if $self->_check_for_opts;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_for_opts 8 0 8
158 }
159
160 33 ▶sub _check_for_opts {
Subroutine _check_for_opts: called CC=2 SCAR=6.9
161 33 my $self = shift;
162 33 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->opts eq "Nagios::Monitoring::Plugin::Getopt" 2 31 33
163 "You have to supply a 'usage' param to Nagios::Monitoring::Plugin::new() if you want to use Getopts from your Nagios::Monitoring::Plugin object."
164 unless ref $self->opts() eq 'Nagios::Monitoring::Plugin::Getopt';
165 31 return $self;
166 }
167
168
169
170 # -------------------------------------------------------------------------
171 # NP::Functions::check_messages helpers and wrappers
172
173 18 ▶sub add_message {
Subroutine add_message: called CC=7 SCAR=19.5
Pod documented
174 18 my $self = shift;
175 18 my ($code, @messages) = @_;
176
177 croak "Invalid error code '$code'"
178 18 ▶ unless defined($ERRORS{uc $code}) || defined($STATUS_TEXT{$code});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Nagios::Monitoring::Plugin::ERRORS{uc $code} or defined $Nagios::Monitoring::Plugin::STATUS_TEXT{$code} 2 16 18
Condition100%3/3
defined $Nagios::Monitoring::Plugin::ERRORS{uc $code} or defined $Nagios::Monitoring::Plugin::STATUS_TEXT{$code}
l!l&&r!l&&!r
972
MC/DC100%2/2
defined $Nagios::Monitoring::Plugin::ERRORS{uc $code} or defined $Nagios::Monitoring::Plugin::STATUS_TEXT{$code}
defined $Nagios::Monitoring::Plugin::ERRORS{uc $code} defined $Nagios::Monitoring::Plugin::STATUS_TEXT{$code}
Truth table100%3/3
A or B
A= defined $Nagios::Monitoring::Plugin::ERRORS{uc $code}B= defined $Nagios::Monitoring::Plugin::STATUS_TEXT{$code}
ABresult
1X1
011
000
179
180 # Store messages using strings rather than numeric codes
181 16 ▶ $code = $STATUS_TEXT{$code} if $STATUS_TEXT{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $Nagios::Monitoring::Plugin::STATUS_TEXT{$code} 7 9 16
182 16 $code = lc $code;
183 16 ▶ croak "Error code '$code' not supported by add_message"
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "unknown" or $code eq "dependent" 2 14 16
Condition100%3/3
$code eq "unknown" or $code eq "dependent"
l!l&&r!l&&!r
1114
MC/DC100%2/2
$code eq "unknown" or $code eq "dependent"
$code eq "unknown" $code eq "dependent"
Truth table100%3/3
A or B
A= $code eq "unknown"B= $code eq "dependent"
ABresult
1X1
011
000
184 if $code eq 'unknown' || $code eq 'dependent';
185
186 14 ▶ $self->messages($code, []) unless $self->messages->{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->messages->{$code} 0 14 14
187 14 push @{$self->messages->{$code}}, @messages;
188 }
189
190 30 ▶sub check_messages {
Subroutine check_messages: called CC=6 SCAR=18.1
Pod documented
191 30 my $self = shift;
192 30 my %args = @_;
193
194 # Add object messages to any passed in as args
195 30 for my $code (qw(critical warning ok)) {
196 90 ▶ my $messages = $self->messages->{$code} || [];
Condition50%1/2
$self->messages->{$code} || []
l!l
900
MC/DC0%0/1
$self->messages->{$code} || []
$self->messages->{$code}
Truth table50%1/2
A
A= $self->messages->{$code}
Aresult
11
00
197 90 ▶ if ($args{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$code}) { } 48 42 90
198 48 ▶ unless (ref $args{$code} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{$code} eq "ARRAY") 4 44 48
199 4 ▶ if ($code eq 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 'ok') { } 4 0 4
200 4 $args{$code} = [ $args{$code} ];
201 } else {
202 0 croak "Invalid argument '$code'"
203 }
204 }
205 48 push @{$args{$code}}, @$messages;
206 }
207 else {
208 42 $args{$code} = $messages;
209 }
210 }
211
212 30 Nagios::Monitoring::Plugin::Functions::check_messages(%args);
213 }
214
215 # -------------------------------------------------------------------------
216
217 12 1;
218
219 #vim:et:sw=4
220
221 __END__
« Plugin/Threshold.pm ↑ Summary Plugin/Performance.pm »