line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::GLPlugin::Commandline::Getopt; |
2
|
2
|
|
|
2
|
|
8
|
use strict; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
49
|
|
3
|
2
|
|
|
2
|
|
7
|
use File::Basename; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
103
|
|
4
|
2
|
|
|
2
|
|
1258
|
use Getopt::Long qw(:config no_ignore_case bundling); |
|
2
|
|
|
|
|
17957
|
|
|
2
|
|
|
|
|
8
|
|
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
|
3
|
|
|
3
|
|
10
|
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
|
3
|
|
|
|
|
23
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Add attr to private _attr hash (except timeout) |
57
|
3
|
|
|
|
|
17
|
$self->{timeout} = delete $attr{timeout}; |
58
|
3
|
|
|
|
|
13
|
$self->{_attr} = { %attr }; |
59
|
3
|
|
|
|
|
4
|
foreach (keys %{$self->{_attr}}) { |
|
3
|
|
|
|
|
10
|
|
60
|
24
|
100
|
|
|
|
35
|
if (exists $params{$_}) { |
61
|
12
|
|
|
|
|
16
|
$self->{_attr}->{$_} = $params{$_}; |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
$self->{_attr}->{$_} = $self->{_attr}->{$_}->{default} |
64
|
|
|
|
|
|
|
if ref ($self->{_attr}->{$_}) eq 'HASH' && |
65
|
12
|
50
|
66
|
|
|
40
|
exists $self->{_attr}->{$_}->{default}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
# Chomp _attr values |
69
|
3
|
|
|
|
|
5
|
chomp foreach values %{$self->{_attr}}; |
|
3
|
|
|
|
|
17
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Setup initial args list |
72
|
3
|
|
|
|
|
4
|
$self->{_args} = [ grep { exists $_->{spec} } @ARGS ]; |
|
18
|
|
|
|
|
23
|
|
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
25
|
$self |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub new { |
78
|
3
|
|
|
3
|
0
|
11
|
my ($class, @params) = @_; |
79
|
|
|
|
|
|
|
require Monitoring::GLPlugin::Commandline::Extraopts |
80
|
3
|
100
|
|
|
|
1129
|
if ! grep /BEGIN/, keys %Monitoring::GLPlugin::Commandline::Extraopts::; |
81
|
3
|
|
|
|
|
11
|
my $self = bless {}, $class; |
82
|
3
|
|
|
|
|
10
|
$self->_init(@params); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub add_arg { |
86
|
84
|
|
|
84
|
0
|
110
|
my ($self, %arg) = @_; |
87
|
84
|
|
|
|
|
54
|
push (@{$self->{_args}}, \%arg); |
|
84
|
|
|
|
|
170
|
|
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
|
2
|
|
|
2
|
0
|
3
|
my ($self) = @_; |
102
|
2
|
|
|
|
|
3
|
my %commandline = (); |
103
|
2
|
|
|
|
|
4
|
$self->{opts}->{all_my_opts} = {}; |
104
|
2
|
|
|
|
|
2
|
my @params = map { $_->{spec} } @{$self->{_args}}; |
|
66
|
|
|
|
|
57
|
|
|
2
|
|
|
|
|
3
|
|
105
|
2
|
50
|
|
|
|
9
|
if (! GetOptions(\%commandline, @params)) { |
106
|
0
|
|
|
|
|
0
|
$self->print_help(); |
107
|
0
|
|
|
|
|
0
|
exit 3; |
108
|
|
|
|
|
|
|
} else { |
109
|
2
|
|
|
2
|
|
1474
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
81
|
|
110
|
2
|
|
|
2
|
|
9
|
no warnings 'redefine'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
6226
|
|
111
|
2
|
50
|
|
|
|
3477
|
if (exists $commandline{'extra-opts'}) { |
112
|
|
|
|
|
|
|
# read the extra file and overwrite other parameters |
113
|
|
|
|
|
|
|
my $extras = Monitoring::GLPlugin::Commandline::Extraopts->new( |
114
|
0
|
|
|
|
|
0
|
file => $commandline{'extra-opts'}, |
115
|
|
|
|
|
|
|
commandline => \%commandline |
116
|
|
|
|
|
|
|
); |
117
|
0
|
0
|
|
|
|
0
|
if (! $extras->is_valid()) { |
118
|
0
|
|
|
|
|
0
|
printf "UNKNOWN - extra-opts are not valid: %s\n", $extras->errors(); |
119
|
0
|
|
|
|
|
0
|
exit 3; |
120
|
|
|
|
|
|
|
} else { |
121
|
0
|
|
|
|
|
0
|
$extras->overwrite(); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
2
|
50
|
|
|
|
6
|
do { $self->print_help(); exit 0; } if $commandline{help}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
125
|
2
|
50
|
|
|
|
4
|
do { $self->print_version(); exit 0 } if $commandline{version}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
126
|
2
|
50
|
|
|
|
4
|
do { $self->print_usage(); exit 3 } if $commandline{usage}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
127
|
2
|
|
|
|
|
2
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } @{$self->{_args}}) { |
|
66
|
|
|
|
|
74
|
|
|
66
|
|
|
|
|
67
|
|
|
2
|
|
|
|
|
4
|
|
128
|
66
|
|
|
|
|
52
|
my $field = $_; |
129
|
66
|
|
|
|
|
157
|
*{"$field"} = sub { |
130
|
273
|
|
|
273
|
|
1345
|
return $self->{opts}->{$field}; |
131
|
66
|
|
|
|
|
87
|
}; |
132
|
|
|
|
|
|
|
} |
133
|
2
|
|
|
|
|
6
|
*{"all_my_opts"} = sub { |
134
|
0
|
|
|
0
|
|
0
|
return $self->{opts}->{all_my_opts}; |
135
|
2
|
|
|
|
|
8
|
}; |
136
|
2
|
|
|
|
|
3
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
8
|
|
137
|
66
|
100
|
|
|
|
121
|
grep { exists $_->{required} && $_->{required} } @{$self->{_args}}) { |
|
2
|
|
|
|
|
12
|
|
138
|
2
|
50
|
|
|
|
8
|
do { $self->print_usage(); exit 3 } if ! exists $commandline{$_}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
139
|
|
|
|
|
|
|
} |
140
|
2
|
|
|
|
|
3
|
foreach (grep { exists $_->{default} } @{$self->{_args}}) { |
|
66
|
|
|
|
|
70
|
|
|
2
|
|
|
|
|
5
|
|
141
|
10
|
|
|
|
|
18
|
$_->{spec} =~ /^([\w\-]+)/; |
142
|
10
|
|
|
|
|
15
|
my $spec = $1; |
143
|
10
|
|
|
|
|
20
|
$self->{opts}->{$spec} = $_->{default}; |
144
|
|
|
|
|
|
|
} |
145
|
2
|
|
|
|
|
6
|
foreach (keys %commandline) { |
146
|
6
|
|
|
|
|
10
|
$self->{opts}->{$_} = $commandline{$_}; |
147
|
6
|
|
|
|
|
11
|
$self->{opts}->{all_my_opts}->{$_} = $commandline{$_}; |
148
|
|
|
|
|
|
|
} |
149
|
2
|
|
|
|
|
2
|
foreach (grep { exists $_->{env} } @{$self->{_args}}) { |
|
66
|
|
|
|
|
68
|
|
|
2
|
|
|
|
|
5
|
|
150
|
2
|
|
|
|
|
7
|
$_->{spec} =~ /^([\w\-]+)/; |
151
|
2
|
|
|
|
|
4
|
my $spec = $1; |
152
|
2
|
50
|
|
|
|
9
|
if (exists $ENV{'NAGIOS__HOST'.$_->{env}}) { |
153
|
0
|
|
|
|
|
0
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__HOST'.$_->{env}}; |
154
|
|
|
|
|
|
|
} |
155
|
2
|
50
|
|
|
|
12
|
if (exists $ENV{'NAGIOS__SERVICE'.$_->{env}}) { |
156
|
0
|
|
|
|
|
0
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__SERVICE'.$_->{env}}; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
2
|
|
|
|
|
3
|
foreach (grep { exists $_->{aliasfor} } @{$self->{_args}}) { |
|
66
|
|
|
|
|
71
|
|
|
2
|
|
|
|
|
4
|
|
160
|
2
|
|
|
|
|
4
|
my $field = $_->{aliasfor}; |
161
|
2
|
|
|
|
|
6
|
$_->{spec} =~ /^([\w\-]+)/; |
162
|
2
|
|
|
|
|
3
|
my $aliasfield = $1; |
163
|
2
|
50
|
|
|
|
7
|
next if $self->{opts}->{$field}; |
164
|
2
|
|
|
|
|
22
|
*{"$field"} = sub { |
165
|
2
|
|
|
2
|
|
10
|
return $self->{opts}->{$aliasfield}; |
166
|
2
|
|
|
|
|
6
|
}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub create_opt { |
172
|
0
|
|
|
0
|
0
|
0
|
my ($self, $key) = @_; |
173
|
2
|
|
|
2
|
|
14
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
791
|
|
174
|
0
|
|
|
|
|
0
|
*{"$key"} = sub { |
175
|
0
|
|
|
0
|
|
0
|
return $self->{opts}->{$key}; |
176
|
0
|
|
|
|
|
0
|
}; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub override_opt { |
180
|
6
|
|
|
6
|
0
|
10
|
my ($self, $key, $value) = @_; |
181
|
6
|
|
|
|
|
23
|
$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__ |