Plugin/Getopt.pm

filter: Statementstmt 98.1% 214 / 218 Branchbran 88.6% 78 / 88 Conditioncond 63.6% 28 / 44 MC/DCmcdc 33.3% 9 / 27 Subroutinesub 100% 29 / 29 Podpod 33.3% 1 / 3 totaltotal 87.7% 359 / 409 CC 69 SCAR 19.5
CC
69
Cov
91%
_cmdline
15.1
_options
14.0
_spec_to_help
7.0
CRAP 7.1
« Plugin/Range.pm ↑ Summary Plugin/Threshold.pm »
1 #
2 # Nagios::Monitoring::Plugin::Getopt - OO perl module providing standardised argument
3 # processing for nagios plugins
4 #
5
6 package Nagios::Monitoring::Plugin::Getopt;
7
8 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Getopt::Long qw(:config no_ignore_case bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 13 ▶use Nagios::Monitoring::Plugin::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Nagios::Monitoring::Plugin::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 $VERSION = $Nagios::Monitoring::Plugin::Functions::VERSION;
19
20 # Standard defaults
21 13 my %DEFAULT = (
22 timeout => 15,
23 verbose => 0,
24 license =>
25 "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
26 It may be used, redistributed and/or modified under the terms of the GNU
27 General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).",
28 );
29 # Standard arguments
30 my @ARGS = ({
31 spec => 'usage|?',
32 help => "-?, --usage\n Print usage information",
33 }, {
34 spec => 'help|h',
35 help => "-h, --help\n Print detailed help screen",
36 }, {
37 spec => 'version|V',
38 help => "-V, --version\n Print version information",
39 }, {
40 spec => 'extra-opts:s@',
41 help => "--extra-opts=[section][\@file]\n Read options from an ini file. See https://nagios-plugins.org/doc/extra-opts.html\n for usage and examples.",
42 }, {
43 spec => 'timeout|t=i',
44 help => "-t, --timeout=INTEGER\n Seconds before plugin times out (default: %s)",
45 default => $DEFAULT{timeout},
46 }, {
47 spec => 'verbose|v+',
48 help => "-v, --verbose\n Show details for command-line debugging (can repeat up to 3 times)",
49 default => $DEFAULT{verbose},
50 },
51 13 );
52 # Standard arguments we traditionally display last in the help output
53 13 my %DEFER_ARGS = map { $_ => 1 } qw(timeout verbose);
54
55 # -------------------------------------------------------------------------
56 # Private methods
57
58 sub _die
59 15 ▶{
Subroutine _die: called CC=2 SCAR=6.9
60 15 my $self = shift;
61 15 my ($msg) = @_;
62 15 ▶ $msg .= "\n" unless substr($msg, -1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($msg, -1) eq "\n" 3 12 15
63 15 Nagios::Monitoring::Plugin::Functions::_nagios_exit(3, $msg);
64 }
65
66 # Return the given attribute, if set, including a final newline
67 sub _attr
68 29 ▶{
Subroutine _attr: called CC=3 SCAR=11.0
69 29 my $self = shift;
70 29 my ($item, $extra) = @_;
71 29 ▶ $extra = '' unless defined $extra;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extra 14 15 29
72 29 ▶ return '' unless $self->{_attr}->{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_attr"}{$item} 5 24 29
73 24 $self->{_attr}->{$item} . "\n" . $extra;
74 }
75
76 # Turn argument spec into help-style output
77 sub _spec_to_help
78 26 ▶{
Subroutine _spec_to_help: called CC=7 SCAR=19.5
79 26 my ($self, $spec, $label) = @_;
80
81 26 my ($opts, $type) = split /=/, $spec, 2;
82 26 my (@short, @long);
83 26 for (split /\|/, $opts) {
84 44 ▶ if (length $_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 16 28 44
85 16 push @short, "-$_";
86 } else {
87 28 push @long, "--$_";
88 }
89 }
90
91 26 my $help = join(', ', @short, @long);
92 26 ▶ if ($type) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type) { } 24 2 26
elsif ($label) { } 0 2 2
93 24 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) { } 10 14 24
94 10 $help .= '=' . $label;
95 }
96 else {
97 14 ▶ $help .= $type eq 'i' ? '=INTEGER' : '=STRING';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'i' ? : 8 6 14
98 }
99 }
100 elsif ($label) {
101 0 carp "Label specified, but there's no type in spec '$spec'";
102 }
103 26 $help .= "\n ";
104 26 return $help;
105 }
106
107 # Options output for plugin -h
108 sub _options
109 10 ▶{
Subroutine _options: called CC=14 SCAR=26.4
110 10 my $self = shift;
111
112 10 my @args = ();
113 10 my @defer = ();
114 10 for (@{$self->{_args}}) {
115 90 ▶ if (exists $DEFER_ARGS{$_->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DEFER_ARGS{$_->{'name'}}) { } 20 70 90
116 20 push @defer, $_;
117 } else {
118 70 push @args, $_;
119 }
120 }
121
122 10 my @options = ();
123 10 for my $arg (@args, @defer) {
124 90 ▶ my $help_array = ref $arg->{help} && ref $arg->{help} eq 'ARRAY' ? $arg->{help} : [ $arg->{help} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'help'} && ref $arg->{'help'} eq 'ARRAY' ? : 4 86 90
Condition66%2/3
ref $arg->{"help"} and ref $arg->{"help"} eq "ARRAY"
!ll&&!rl&&r
8604
MC/DC50%1/2
ref $arg->{"help"} and ref $arg->{"help"} eq "ARRAY"
ref $arg->{"help"} ref $arg->{"help"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $arg->{"help"}B= ref $arg->{"help"} eq "ARRAY"
ABresult
0X0
100
111
125 90 ▶ my $label_array = $arg->{label} && ref $arg->{label} && ref $arg->{label} eq 'ARRAY' ? $arg->{label} : [ $arg->{label} ];
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'label'} && ref $arg->{'label'} && ref $arg->{'label'} eq 'ARRAY' ? : 4 86 90
Condition83%5/6
$arg->{"label"} and ref $arg->{"label"} and ref $arg->{"label"} eq "ARRAY"
!ll&&!rl&&r
8604
$arg->{"label"} and ref $arg->{"label"}
!ll&&!rl&&r
8244
MC/DC66%2/3
$arg->{"label"} and ref $arg->{"label"} and ref $arg->{"label"} eq "ARRAY"
$arg->{"label"} ref $arg->{"label"} ref $arg->{"label"} eq "ARRAY"
Truth table75%3/4
A and B and C
A= $arg->{"label"}B= ref $arg->{"label"}C= ref $arg->{"label"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
126 90 my $help_string = '';
127 90 ▶ for (my $i = 0; $i <= $#$help_array; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $help_array->$#*) 96 90 186
128 96 my $help = $help_array->[$i];
129 # Add spec arguments to help if not already there
130 96 ▶ if ($help =~ m/^\s*-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help =~ /^\s*-/) { } 70 26 96
131 70 $help_string .= $help;
132 }
133 else {
134 26 $help_string .= $self->_spec_to_help($arg->{spec}, $label_array->[$i]) . $help;
135 26 ▶ $help_string .= "\n " if $i < $#$help_array;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $help_array->$#* 6 20 26
136 }
137 }
138
139 # Add help_string to @options
140 90 ▶ if ($help_string =~ m/%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help_string =~ /%s/) { } 14 76 90
141 14 ▶ my $default = defined $arg->{default} ? $arg->{default} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'default'} ? : 14 0 14
142 # We only handle '%s' formats here, so escape everything else
143 14 $help_string =~ s/%(?!s)/%%/g;
144 14 push @options, sprintf($help_string, $default, $default, $default, $default);
145 } else {
146 76 push @options, $help_string;
147 }
148 }
149
150 10 return ' ' . join("\n ", @options);
151 }
152
153 # Output for plugin -? (or missing/invalid args)
154 sub _usage
155 14 ▶{
Subroutine _usage: called CC=1 SCAR=0.0
156 14 my $self = shift;
157 14 sprintf $self->_attr('usage'), $self->{_attr}->{plugin};
158 }
159
160 # Output for plugin -V
161 sub _revision
162 8 ▶{
Subroutine _revision: called CC=2 SCAR=6.9
163 8 my $self = shift;
164 8 my $revision = sprintf "%s %s", $self->{_attr}->{plugin}, $self->{_attr}->{version};
165 8 ▶ $revision .= sprintf " [%s]", $self->{_attr}->{url} if $self->{_attr}->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_attr"}{"url"} 5 3 8
166 8 $revision .= "\n";
167 8 $revision;
168 }
169
170 # Output for plugin -h
171 sub _help
172 5 ▶{
Subroutine _help: called CC=3 SCAR=11.1
173 5 my $self = shift;
174 5 my $help = '';
175 5 $help .= $self->_revision . "\n";
176 5 $help .= $self->_attr('license', "\n");
177 5 $help .= $self->_attr('blurb', "\n");
178 5 ▶ $help .= $self->_usage ? $self->_usage . "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->_usage ? : 5 0 5
179 5 ▶ $help .= $self->_options ? $self->_options . "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->_options ? : 5 0 5
180 5 $help .= $self->_attr('extra', "\n");
181 5 return $help;
182 }
183
184 # Return a Getopt::Long-compatible option array from the current set of specs
185 sub _process_specs_getopt_long
186 40 ▶{
Subroutine _process_specs_getopt_long: called CC=3 SCAR=11.0
187 40 my $self = shift;
188
189 40 my @opts = ();
190 40 for my $arg (@{$self->{_args}}) {
191 435 push @opts, $arg->{spec};
192 # Setup names and defaults
193 435 my $spec = $arg->{spec};
194 # Use first arg as name (like Getopt::Long does)
195 435 $spec =~ s/[=:].*$//;
196 435 my $name = (split /\s*\|\s*/, $spec)[0];
197 435 $arg->{name} = $name;
198 435 ▶ if (defined $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$name}) { } 40 395 435
199 40 $arg->{default} = $self->{$name};
200 } else {
201 395 $self->{$name} = $arg->{default};
202 }
203 }
204
205 40 return @opts;
206 }
207
208 # Check for existence of required arguments
209 sub _check_required_opts
210 28 ▶{
Subroutine _check_required_opts: called CC=5 SCAR=16.1
211 28 my $self = shift;
212
213 28 my @missing = ();
214 28 for my $arg (@{$self->{_args}}) {
215 317 ▶ if ($arg->{required} && ! defined $self->{$arg->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"required"} and not defined $$self{$arg->{"name"}}) 2 315 317
Condition100%3/3
$arg->{"required"} and not defined $$self{$arg->{"name"}}
!ll&&!rl&&r
31142
MC/DC100%2/2
$arg->{"required"} and not defined $$self{$arg->{"name"}}
$arg->{"required"} not defined $$self{$arg->{"name"}}
Truth table100%3/3
A and not(B)
A= $arg->{"required"}B= not defined $$self{$arg->{"name"}}
ABresult
0X0
100
111
216 2 push @missing, $arg->{name};
217 }
218 }
219 28 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 2 26 28
220 $self->_die($self->_usage . "\n" .
221 2 join("\n", map { sprintf "Missing argument: %s", $_ } @missing) . "\n");
222 }
223 }
224
225 # Process and handle any immediate options
226 sub _process_opts
227 38 ▶{
Subroutine _process_opts: called CC=4 SCAR=13.9
228 38 my $self = shift;
229
230 # Print message and exit for usage, version, help
231 38 ▶ $self->_die($self->_usage) if $self->{usage};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"usage"} 2 36 38
232 36 ▶ $self->_die($self->_revision) if $self->{version};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"version"} 3 33 36
233 33 ▶ $self->_die($self->_help) if $self->{help};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"help"} 5 28 33
234 }
235
236 # -------------------------------------------------------------------------
237 # Default opts methods
238
239 sub _load_config_section
240 13 ▶{
Subroutine _load_config_section: called CC=5 SCAR=16.8
241 13 my $self = shift;
242 13 my ($section, $file, $flags) = @_;
243 13 ▶ $section ||= $self->{_attr}->{plugin};
Condition33%1/3
$section ||= $self->{"_attr"}{"plugin"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
$section ||= $self->{"_attr"}{"plugin"}
$section $self->{"_attr"}{"plugin"}
Truth table33%1/3
A ||= B
A= $sectionB= $self->{"_attr"}{"plugin"}
ABresult
1X1
011
000
244
245 13 my $Config;
246 13 eval { $Config = Nagios::Monitoring::Plugin::Config->read($file); };
247 13 ▶ $self->_die($@) if ($@); #TODO: add test?
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
248
249 # TODO: is this check sane? Does --extra-opts=foo require a [foo] section?
250 ## Nevertheless, if we die as UNKNOWN here we should do the same on default
251 ## file *added eval/_die above*.
252 13 ▶ $file ||= $Config->np_getfile();
Condition33%1/3
$file ||= $Config->np_getfile
l!l&&r!l&&!r
0130
MC/DC0%0/2
$file ||= $Config->np_getfile
$file $Config->np_getfile
Truth table33%1/3
A ||= B
A= $fileB= $Config->np_getfile
ABresult
1X1
011
000
253 $self->_die("Invalid section '$section' in config file '$file'")
254 13 ▶ unless exists $Config->{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Config->{$section} 2 11 13
255
256 11 return $Config->{$section};
257 }
258
259 # Helper method to setup a hash of spec definitions for _cmdline
260 sub _setup_spec_index
261 23 ▶{
Subroutine _setup_spec_index: called CC=2 SCAR=6.9
262 23 my $self = shift;
263 23 ▶ return if defined $self->{_spec};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_spec"} 11 12 23
264 12 $self->{_spec} = { map { $_->{name} => $_->{spec} } @{$self->{_args}} };
265 }
266
267 # Quote values that require it
268 sub _cmdline_value
269 58 ▶{
Subroutine _cmdline_value: called CC=5 SCAR=17.5
270 58 my $self = shift;
271 58 local $_ = shift;
272 58 ▶ if (m/\s/ && (m/^[^"']/ || m/[^"']$/)) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\s/ and /^[^"']/ || /[^"']$/) { } 0 58 58
elsif ($_ eq '') { } 1 57 58
Condition50%3/6
/\s/ and /^[^"']/ || /[^"']$/
!ll&&!rl&&r
5440
/^[^"']/ or /[^"']$/
l!l&&r!l&&!r
004
MC/DC0%0/3
/\s/ and /^[^"']/ || /[^"']$/
/\s/ /^[^"']/ /[^"']$/
Truth table50%2/4
A and B or C
A= /\s/B= /^[^"']/C= /[^"']$/
ABCresult
0XX0
1000
11X1
1011
273 0 return qq("$_");
274 }
275 elsif ($_ eq '') {
276 1 return q("");
277 }
278 else {
279 57 return $_;
280 }
281 }
282
283 # Helper method to format key/values in $hash in a quasi-commandline format
284 sub _cmdline
285 23 ▶{
Subroutine _cmdline: called CC=15 SCAR=27.2
286 23 my $self = shift;
287 23 my ($hash) = @_;
288 23 ▶ $hash ||= $self;
Condition66%2/3
$hash ||= $self
l!l&&r!l&&!r
11120
MC/DC0%0/2
$hash ||= $self
$hash $self
Truth table66%2/3
A ||= B
A= $hashB= $self
ABresult
1X1
011
000
289
290 23 $self->_setup_spec_index;
291
292 23 my @args = ();
293 23 for my $key (sort keys %$hash) {
294 # Skip internal keys
295 248 ▶ next if $key =~ m/^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^_/ 36 212 248
296
297 # Skip defaults and internals
298 212 ▶ next if exists $DEFAULT{$key} && $hash->{$key} eq $DEFAULT{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DEFAULT{$key} and $hash->{$key} eq $DEFAULT{$key} 24 188 212
Condition66%2/3
exists $DEFAULT{$key} and $hash->{$key} eq $DEFAULT{$key}
!ll&&!rl&&r
188024
MC/DC50%1/2
exists $DEFAULT{$key} and $hash->{$key} eq $DEFAULT{$key}
exists $DEFAULT{$key} $hash->{$key} eq $DEFAULT{$key}
Truth table66%2/3
A and B
A= exists $DEFAULT{$key}B= $hash->{$key} eq $DEFAULT{$key}
ABresult
0X0
100
111
299 188 ▶ next if grep { $key eq $_ } qw(help usage version extra-opts);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$key eq $_;} "help", "usage", "version", "extra-opts" 48 140 188
300 140 ▶ next unless defined $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{$key} 94 46 140
301
302 # Render arg
303 46 ▶ my $spec = $self->{_spec}->{$key} || '';
Condition50%1/2
$self->{'_spec'}{$key} || ''
l!l
460
MC/DC0%0/1
$self->{'_spec'}{$key} || ''
$self->{'_spec'}{$key}
Truth table50%1/2
A
A= $self->{'_spec'}{$key}
Aresult
11
00
304 46 ▶ if ($spec =~ m/[=:].+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /[=:].+$/) { } 44 2 46
305 # Arg takes value - may be a scalar or an arrayref
306 44 ▶ for my $value (ref $hash->{$key} eq 'ARRAY' ? @{$hash->{$key}} : ( $hash->{$key} )) {
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{$key} eq 'ARRAY' ? : 27 17 44
307 58 $value = $self->_cmdline_value($value);
308 58 ▶ if (length($key) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > 1) { } 32 26 58
309 32 push @args, sprintf "--%s=%s", $key, $value;
310 }
311 else {
312 26 push @args, "-$key", $value;
313 }
314 }
315 }
316
317 else {
318 # Flag - render long or short based on option length
319 2 ▶ push @args, (length($key) > 1 ? '--' : '-') . $key;
Branch: 1 branch
BranchTrueFalseTotal
length $key > 1 ? : 0 2 2
320 }
321 }
322
323 23 ▶ return wantarray ? @args : join(' ', @args);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 12 23
324 }
325
326 # Process and load extra-opts sections
327 sub _process_extra_opts
328 40 ▶{
Subroutine _process_extra_opts: called CC=6 SCAR=18.1
329 40 my $self = shift;
330 40 my ($args) = @_;
331
332 40 my $extopts_list = $args->{'extra-opts'};
333
334 40 my @sargs = ();
335 40 for my $extopts (@$extopts_list) {
336 13 ▶ $extopts ||= $self->{_attr}->{plugin};
Condition66%2/3
$extopts ||= $self->{"_attr"}{"plugin"}
l!l&&r!l&&!r
760
MC/DC0%0/2
$extopts ||= $self->{"_attr"}{"plugin"}
$extopts $self->{"_attr"}{"plugin"}
Truth table66%2/3
A ||= B
A= $extoptsB= $self->{"_attr"}{"plugin"}
ABresult
1X1
011
000
337 13 my $section = $extopts;
338 13 my $file = '';
339
340 # Parse section@file
341 13 ▶ if ($extopts =~ m/^([^@]*)@(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extopts =~ /^([^\@]*)@(.*?)\s*$/) 0 13 13
342 0 $section = $1;
343 0 $file = $2;
344 }
345
346 # Load section args
347 13 my $shash = $self->_load_config_section($section, $file);
348
349 # Turn $shash into a series of commandline-like arguments
350 11 push @sargs, $self->_cmdline($shash);
351 }
352
353 # Reset ARGV to extra-opts + original
354 38 @ARGV = ( @sargs, @{$self->{_attr}->{argv}} );
355
356 printf "[extra-opts] %s %s\n", $self->{_attr}->{plugin}, join(' ', @ARGV)
357 38 ▶ if $args->{verbose} && $args->{verbose} >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} and $args->{"verbose"} >= 3 2 36 38
Condition100%3/3
$args->{"verbose"} and $args->{"verbose"} >= 3
!ll&&!rl&&r
3062
MC/DC100%2/2
$args->{"verbose"} and $args->{"verbose"} >= 3
$args->{"verbose"} $args->{"verbose"} >= 3
Truth table100%3/3
A and B
A= $args->{"verbose"}B= $args->{"verbose"} >= 3
ABresult
0X0
100
111
358 }
359
360 # -------------------------------------------------------------------------
361 # Public methods
362
363 # Define plugin argument
364 sub arg
365 195 ▶{
Subroutine arg: called CC=3 SCAR=11.0
Pod undocumented
366 195 my $self = shift;
367 195 my %args;
368
369 # Named args
370 195 ▶ if ($_[0] =~ m/^(spec|help|required|default)$/ && scalar(@_) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^(spec|help|required|default)$/ and scalar(@_) % 2 == 0) { } 178 17 195
Condition66%2/3
$_[0] =~ /^(spec|help|required|default)$/ and scalar(@_) % 2 == 0
!ll&&!rl&&r
170178
MC/DC50%1/2
$_[0] =~ /^(spec|help|required|default)$/ and scalar(@_) % 2 == 0
$_[0] =~ /^(spec|help|required|default)$/ scalar(@_) % 2 == 0
Truth table66%2/3
A and B
A= $_[0] =~ /^(spec|help|required|default)$/B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
371 178 %args = validate( @_, {
372 spec => 1,
373 help => 1,
374 default => 0,
375 required => 0,
376 label => 0,
377 });
378 }
379
380 # Positional args
381 else {
382 17 my @args = validate_pos(@_, 1, 1, 0, 0, 0);
383 17 %args = (
384 spec => $args[0],
385 help => $args[1],
386 default => $args[2],
387 required => $args[3],
388 label => $args[4],
389 );
390 }
391
392 # Add to private args arrayref
393 195 push @{$self->{_args}}, \%args;
394 }
395
396 # Process the @ARGV array using the current _args list (possibly exiting)
397 sub getopts
398 40 ▶{
Subroutine getopts: called CC=3 SCAR=11.0
Pod undocumented
399 40 my $self = shift;
400
401 # Collate spec arguments for Getopt::Long
402 40 my @opt_array = $self->_process_specs_getopt_long;
403
404 # Capture original @ARGV (for extra-opts games)
405 40 $self->{_attr}->{argv} = [ @ARGV ];
406
407 # Call GetOptions using @opt_array
408 40 my $args1 = {};
409 40 my $ok = GetOptions($args1, @opt_array);
410 # Invalid options - give usage message and exit
411 40 ▶ $self->_die($self->_usage) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 40 40
412
413 # Process extra-opts
414 40 $self->_process_extra_opts($args1);
415
416 # Call GetOptions again, this time including extra-opts
417 38 $ok = GetOptions($self, @opt_array);
418 # Invalid options - give usage message and exit
419 38 ▶ $self->_die($self->_usage) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 38 38
420
421 # Process immediate options (possibly exiting)
422 38 $self->_process_opts;
423
424 # Required options (possibly exiting)
425 28 $self->_check_required_opts;
426
427 # Setup accessors for options
428 26 $self->mk_ro_accessors(grep ! /^_/, keys %$self);
429
430 # Setup default alarm handler for alarm($ng->timeout) in plugin
431 1 ▶ $SIG{ALRM} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
432 1 my $plugin = uc $self->{_attr}->{plugin};
433 1 $plugin =~ s/^check_//;
434 1 $self->_die(
435 sprintf("%s UNKNOWN - plugin timed out (timeout %ss)",
436 $plugin, $self->timeout));
437 26 };
438 }
439
440 # -------------------------------------------------------------------------
441 # Constructor
442
443 sub _init
444 40 ▶{
Subroutine _init: called CC=3 SCAR=11.1
445 40 my $self = shift;
446
447 # Check params
448 40 ▶ my $plugin = basename($ENV{NAGIOS_PLUGIN} || $0);
Condition33%1/3
$ENV{'NAGIOS_PLUGIN'} || $0
l!l&&r!l&&!r
0400
MC/DC0%0/2
$ENV{'NAGIOS_PLUGIN'} || $0
$ENV{'NAGIOS_PLUGIN'} $0
Truth table33%1/3
A || B
A= $ENV{'NAGIOS_PLUGIN'}B= $0
ABresult
1X1
011
000
449 my %attr = validate( @_, {
450 usage => 1,
451 version => 0,
452 url => 0,
453 plugin => { default => $plugin },
454 blurb => 0,
455 extra => 0,
456 'extra-opts' => 0,
457 license => { default => $DEFAULT{license} },
458 timeout => { default => $DEFAULT{timeout} },
459 40 });
460
461 # Add attr to private _attr hash (except timeout)
462 40 $self->{timeout} = delete $attr{timeout};
463 40 $self->{_attr} = { %attr };
464 # Chomp _attr values
465 40 chomp foreach values %{$self->{_attr}};
466
467 # Setup initial args list
468 40 $self->{_args} = [ @ARGS ];
469
470 40 $self
471 }
472
473 sub new
474 40 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
475 40 my $class = shift;
476 40 my $self = bless {}, $class;
477 40 $self->_init(@_);
478 }
479
480 # -------------------------------------------------------------------------
481
482 13 1;
483
484 __END__
« Plugin/Range.pm ↑ Summary Plugin/Threshold.pm »