| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Monitoring::GLPlugin::Commandline::Getopt; |
|
2
|
5
|
|
|
5
|
|
28
|
use strict; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
122
|
|
|
3
|
5
|
|
|
5
|
|
24
|
use File::Basename; |
|
|
5
|
|
|
|
|
9
|
|
|
|
5
|
|
|
|
|
315
|
|
|
4
|
5
|
|
|
5
|
|
2239
|
use Getopt::Long qw(:config no_ignore_case bundling); |
|
|
5
|
|
|
|
|
41995
|
|
|
|
5
|
|
|
|
|
24
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# Standard defaults |
|
7
|
|
|
|
|
|
|
my %DEFAULT = ( |
|
8
|
|
|
|
|
|
|
timeout => 15, |
|
9
|
|
|
|
|
|
|
verbose => 0, |
|
10
|
|
|
|
|
|
|
license => |
|
11
|
|
|
|
|
|
|
"This monitoring plugin is free software, and comes with ABSOLUTELY NO WARRANTY. |
|
12
|
|
|
|
|
|
|
It may be used, redistributed and/or modified under the terms of the GNU |
|
13
|
|
|
|
|
|
|
General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).", |
|
14
|
|
|
|
|
|
|
); |
|
15
|
|
|
|
|
|
|
# Standard arguments |
|
16
|
|
|
|
|
|
|
my @ARGS = ({ |
|
17
|
|
|
|
|
|
|
spec => 'usage|?', |
|
18
|
|
|
|
|
|
|
help => "-?, --usage\n Print usage information", |
|
19
|
|
|
|
|
|
|
}, { |
|
20
|
|
|
|
|
|
|
spec => 'help|h', |
|
21
|
|
|
|
|
|
|
help => "-h, --help\n Print detailed help screen", |
|
22
|
|
|
|
|
|
|
}, { |
|
23
|
|
|
|
|
|
|
spec => 'version|V', |
|
24
|
|
|
|
|
|
|
help => "-V, --version\n Print version information", |
|
25
|
|
|
|
|
|
|
}, { |
|
26
|
|
|
|
|
|
|
#spec => 'extra-opts:s@', |
|
27
|
|
|
|
|
|
|
#help => "--extra-opts=[[@]]\n Section and/or config_file from which to load extra options (may repeat)", |
|
28
|
|
|
|
|
|
|
}, { |
|
29
|
|
|
|
|
|
|
spec => 'timeout|t=i', |
|
30
|
|
|
|
|
|
|
help => sprintf("-t, --timeout=INTEGER\n Seconds before plugin times out (default: %s)", $DEFAULT{timeout}), |
|
31
|
|
|
|
|
|
|
default => $DEFAULT{timeout}, |
|
32
|
|
|
|
|
|
|
}, { |
|
33
|
|
|
|
|
|
|
spec => 'verbose|v+', |
|
34
|
|
|
|
|
|
|
help => "-v, --verbose\n Show details for command-line debugging (can repeat up to 3 times)", |
|
35
|
|
|
|
|
|
|
default => $DEFAULT{verbose}, |
|
36
|
|
|
|
|
|
|
}, |
|
37
|
|
|
|
|
|
|
); |
|
38
|
|
|
|
|
|
|
# Standard arguments we traditionally display last in the help output |
|
39
|
|
|
|
|
|
|
my %DEFER_ARGS = map { $_ => 1 } qw(timeout verbose); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _init { |
|
42
|
7
|
|
|
7
|
|
33
|
my ($self, %params) = @_; |
|
43
|
|
|
|
|
|
|
# Check params |
|
44
|
|
|
|
|
|
|
my %attr = ( |
|
45
|
|
|
|
|
|
|
usage => 1, |
|
46
|
|
|
|
|
|
|
version => 0, |
|
47
|
|
|
|
|
|
|
url => 0, |
|
48
|
|
|
|
|
|
|
plugin => { default => $Monitoring::GLPlugin::pluginname }, |
|
49
|
|
|
|
|
|
|
blurb => 0, |
|
50
|
|
|
|
|
|
|
extra => 0, |
|
51
|
|
|
|
|
|
|
'extra-opts' => 0, |
|
52
|
|
|
|
|
|
|
license => { default => $DEFAULT{license} }, |
|
53
|
|
|
|
|
|
|
timeout => { default => $DEFAULT{timeout} }, |
|
54
|
7
|
|
|
|
|
61
|
); |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Add attr to private _attr hash (except timeout) |
|
57
|
7
|
|
|
|
|
40
|
$self->{timeout} = delete $attr{timeout}; |
|
58
|
7
|
|
|
|
|
35
|
$self->{_attr} = { %attr }; |
|
59
|
7
|
|
|
|
|
18
|
foreach (keys %{$self->{_attr}}) { |
|
|
7
|
|
|
|
|
31
|
|
|
60
|
56
|
100
|
|
|
|
385
|
if (exists $params{$_}) { |
|
61
|
28
|
|
|
|
|
56
|
$self->{_attr}->{$_} = $params{$_}; |
|
62
|
|
|
|
|
|
|
} else { |
|
63
|
|
|
|
|
|
|
$self->{_attr}->{$_} = $self->{_attr}->{$_}->{default} |
|
64
|
|
|
|
|
|
|
if ref ($self->{_attr}->{$_}) eq 'HASH' && |
|
65
|
28
|
50
|
66
|
|
|
123
|
exists $self->{_attr}->{$_}->{default}; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
# Chomp _attr values |
|
69
|
7
|
|
|
|
|
15
|
chomp foreach values %{$self->{_attr}}; |
|
|
7
|
|
|
|
|
43
|
|
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Setup initial args list |
|
72
|
7
|
|
|
|
|
17
|
$self->{_args} = [ grep { exists $_->{spec} } @ARGS ]; |
|
|
42
|
|
|
|
|
83
|
|
|
73
|
|
|
|
|
|
|
|
|
74
|
7
|
|
|
|
|
65
|
$self |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub new { |
|
78
|
7
|
|
|
7
|
0
|
34
|
my ($class, @params) = @_; |
|
79
|
|
|
|
|
|
|
require Monitoring::GLPlugin::Commandline::Extraopts |
|
80
|
7
|
100
|
|
|
|
1720
|
if ! grep /BEGIN/, keys %Monitoring::GLPlugin::Commandline::Extraopts::; |
|
81
|
7
|
|
|
|
|
45
|
my $self = bless {}, $class; |
|
82
|
7
|
|
|
|
|
40
|
$self->_init(@params); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub add_arg { |
|
86
|
180
|
|
|
180
|
0
|
411
|
my ($self, %arg) = @_; |
|
87
|
180
|
|
|
|
|
229
|
push (@{$self->{_args}}, \%arg); |
|
|
180
|
|
|
|
|
492
|
|
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub mod_arg { |
|
91
|
0
|
|
|
0
|
0
|
0
|
my ($self, $argname, %arg) = @_; |
|
92
|
0
|
|
|
|
|
0
|
foreach my $old_arg (@{$self->{_args}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
93
|
0
|
0
|
0
|
|
|
0
|
next unless $old_arg->{spec} =~ /(\w+).*/ && $argname eq $1; |
|
94
|
0
|
|
|
|
|
0
|
foreach my $key (keys %arg) { |
|
95
|
0
|
|
|
|
|
0
|
$old_arg->{$key} = $arg{$key}; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub getopts { |
|
101
|
5
|
|
|
5
|
0
|
12
|
my ($self) = @_; |
|
102
|
5
|
|
|
|
|
13
|
my %commandline = (); |
|
103
|
5
|
|
|
|
|
23
|
$self->{opts}->{all_my_opts} = {}; |
|
104
|
5
|
|
|
|
|
9
|
my @params = map { $_->{spec} } @{$self->{_args}}; |
|
|
176
|
|
|
|
|
250
|
|
|
|
5
|
|
|
|
|
12
|
|
|
105
|
5
|
50
|
|
|
|
23
|
if (! GetOptions(\%commandline, @params)) { |
|
106
|
0
|
|
|
|
|
0
|
$self->print_help(); |
|
107
|
0
|
|
|
|
|
0
|
exit 3; |
|
108
|
|
|
|
|
|
|
} else { |
|
109
|
5
|
|
|
5
|
|
3753
|
no strict 'refs'; |
|
|
5
|
|
|
|
|
15
|
|
|
|
5
|
|
|
|
|
191
|
|
|
110
|
5
|
|
|
5
|
|
26
|
no warnings 'redefine'; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
3731
|
|
|
111
|
5
|
100
|
|
|
|
13733
|
if (exists $commandline{'extra-opts'}) { |
|
112
|
|
|
|
|
|
|
# read the extra file and overwrite other parameters |
|
113
|
|
|
|
|
|
|
my $extras = Monitoring::GLPlugin::Commandline::Extraopts->new( |
|
114
|
1
|
|
|
|
|
12
|
file => $commandline{'extra-opts'}, |
|
115
|
|
|
|
|
|
|
commandline => \%commandline |
|
116
|
|
|
|
|
|
|
); |
|
117
|
1
|
50
|
|
|
|
6
|
if (! $extras->is_valid()) { |
|
118
|
1
|
|
|
|
|
5
|
printf "UNKNOWN - extra-opts are not valid: %s\n", $extras->errors(); |
|
119
|
1
|
|
|
|
|
25
|
exit 3; |
|
120
|
|
|
|
|
|
|
} else { |
|
121
|
0
|
|
|
|
|
0
|
$extras->overwrite(); |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
} |
|
124
|
4
|
50
|
|
|
|
16
|
do { $self->print_help(); exit 0; } if $commandline{help}; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
125
|
4
|
50
|
|
|
|
10
|
do { $self->print_version(); exit 0 } if $commandline{version}; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
126
|
4
|
50
|
|
|
|
11
|
do { $self->print_usage(); exit 3 } if $commandline{usage}; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
127
|
4
|
|
|
|
|
8
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } @{$self->{_args}}) { |
|
|
138
|
|
|
|
|
237
|
|
|
|
138
|
|
|
|
|
235
|
|
|
|
4
|
|
|
|
|
12
|
|
|
128
|
138
|
|
|
|
|
194
|
my $field = $_; |
|
129
|
138
|
|
|
|
|
444
|
*{"$field"} = sub { |
|
130
|
440
|
|
|
440
|
|
2715
|
return $self->{opts}->{$field}; |
|
131
|
138
|
|
|
|
|
295
|
}; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
4
|
|
|
|
|
14
|
*{"all_my_opts"} = sub { |
|
134
|
1
|
|
|
1
|
|
5
|
return $self->{opts}->{all_my_opts}; |
|
135
|
4
|
|
|
|
|
17
|
}; |
|
136
|
4
|
|
|
|
|
8
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } |
|
|
4
|
|
|
|
|
15
|
|
|
|
4
|
|
|
|
|
14
|
|
|
137
|
138
|
100
|
|
|
|
295
|
grep { exists $_->{required} && $_->{required} } @{$self->{_args}}) { |
|
|
4
|
|
|
|
|
25
|
|
|
138
|
4
|
50
|
|
|
|
18
|
do { $self->print_usage(); exit 3 } if ! exists $commandline{$_}; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
139
|
|
|
|
|
|
|
} |
|
140
|
4
|
|
|
|
|
8
|
foreach (grep { exists $_->{default} } @{$self->{_args}}) { |
|
|
138
|
|
|
|
|
194
|
|
|
|
4
|
|
|
|
|
9
|
|
|
141
|
20
|
|
|
|
|
43
|
$_->{spec} =~ /^([\w\-]+)/; |
|
142
|
20
|
|
|
|
|
35
|
my $spec = $1; |
|
143
|
20
|
|
|
|
|
42
|
$self->{opts}->{$spec} = $_->{default}; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
4
|
|
|
|
|
12
|
foreach (keys %commandline) { |
|
146
|
15
|
|
|
|
|
33
|
$self->{opts}->{$_} = $commandline{$_}; |
|
147
|
15
|
|
|
|
|
31
|
$self->{opts}->{all_my_opts}->{$_} = $commandline{$_}; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
4
|
|
|
|
|
9
|
foreach (grep { exists $_->{env} } @{$self->{_args}}) { |
|
|
138
|
|
|
|
|
194
|
|
|
|
4
|
|
|
|
|
12
|
|
|
150
|
6
|
|
|
|
|
18
|
$_->{spec} =~ /^([\w\-]+)/; |
|
151
|
6
|
|
|
|
|
11
|
my $spec = $1; |
|
152
|
6
|
50
|
|
|
|
28
|
if (exists $ENV{'NAGIOS__HOST'.$_->{env}}) { |
|
153
|
0
|
|
|
|
|
0
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__HOST'.$_->{env}}; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
6
|
100
|
|
|
|
23
|
if (exists $ENV{'NAGIOS__SERVICE'.$_->{env}}) { |
|
156
|
2
|
|
|
|
|
7
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__SERVICE'.$_->{env}}; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
} |
|
159
|
4
|
|
|
|
|
8
|
foreach (grep { exists $_->{aliasfor} } @{$self->{_args}}) { |
|
|
138
|
|
|
|
|
213
|
|
|
|
4
|
|
|
|
|
12
|
|
|
160
|
4
|
|
|
|
|
15
|
my $field = $_->{aliasfor}; |
|
161
|
4
|
|
|
|
|
13
|
$_->{spec} =~ /^([\w\-]+)/; |
|
162
|
4
|
|
|
|
|
10
|
my $aliasfield = $1; |
|
163
|
4
|
50
|
|
|
|
13
|
next if $self->{opts}->{$field}; |
|
164
|
4
|
|
|
|
|
40
|
*{"$field"} = sub { |
|
165
|
3
|
|
|
3
|
|
12
|
return $self->{opts}->{$aliasfield}; |
|
166
|
4
|
|
|
|
|
13
|
}; |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub create_opt { |
|
172
|
0
|
|
|
0
|
0
|
0
|
my ($self, $key) = @_; |
|
173
|
5
|
|
|
5
|
|
37
|
no strict 'refs'; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
1660
|
|
|
174
|
0
|
|
|
|
|
0
|
*{"$key"} = sub { |
|
175
|
0
|
|
|
0
|
|
0
|
return $self->{opts}->{$key}; |
|
176
|
0
|
|
|
|
|
0
|
}; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub override_opt { |
|
180
|
7
|
|
|
7
|
0
|
22
|
my ($self, $key, $value) = @_; |
|
181
|
7
|
|
|
|
|
36
|
$self->{opts}->{$key} = $value; |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub get { |
|
185
|
0
|
|
|
0
|
0
|
|
my ($self, $opt) = @_; |
|
186
|
0
|
|
|
|
|
|
return $self->{opts}->{$opt}; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub print_help { |
|
190
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
191
|
0
|
|
|
|
|
|
$self->print_version(); |
|
192
|
0
|
|
|
|
|
|
printf "\n%s\n", $self->{_attr}->{license}; |
|
193
|
0
|
|
|
|
|
|
printf "\n%s\n\n", $self->{_attr}->{blurb}; |
|
194
|
0
|
|
|
|
|
|
$self->print_usage(); |
|
195
|
0
|
|
|
|
|
|
foreach (grep { |
|
196
|
|
|
|
|
|
|
! (exists $_->{hidden} && $_->{hidden}) |
|
197
|
0
|
|
0
|
|
|
|
} @{$self->{_args}}) { |
|
|
0
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
printf " %s\n", $_->{help}; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub print_usage { |
|
203
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
204
|
0
|
|
|
|
|
|
printf $self->{_attr}->{usage}, $self->{_attr}->{plugin}; |
|
205
|
0
|
|
|
|
|
|
print "\n"; |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub print_version { |
|
209
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
210
|
0
|
|
|
|
|
|
printf "%s %s", $self->{_attr}->{plugin}, $self->{_attr}->{version}; |
|
211
|
0
|
0
|
|
|
|
|
printf " [%s]", $self->{_attr}->{url} if $self->{_attr}->{url}; |
|
212
|
0
|
|
|
|
|
|
print "\n"; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub print_license { |
|
216
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
217
|
0
|
|
|
|
|
|
printf "%s\n", $self->{_attr}->{license}; |
|
218
|
0
|
|
|
|
|
|
print "\n"; |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
1; |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
__END__ |