line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::GLPlugin::Commandline::Getopt; |
2
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
61
|
|
3
|
2
|
|
|
2
|
|
8
|
use File::Basename; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
130
|
|
4
|
2
|
|
|
2
|
|
1540
|
use Getopt::Long qw(:config no_ignore_case bundling); |
|
2
|
|
|
|
|
17863
|
|
|
2
|
|
|
|
|
7
|
|
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
|
|
7
|
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
|
|
|
|
|
27
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Add attr to private _attr hash (except timeout) |
57
|
3
|
|
|
|
|
16
|
$self->{timeout} = delete $attr{timeout}; |
58
|
3
|
|
|
|
|
12
|
$self->{_attr} = { %attr }; |
59
|
3
|
|
|
|
|
4
|
foreach (keys %{$self->{_attr}}) { |
|
3
|
|
|
|
|
34
|
|
60
|
24
|
100
|
|
|
|
32
|
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
|
|
|
45
|
exists $self->{_attr}->{$_}->{default}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
# Chomp _attr values |
69
|
3
|
|
|
|
|
5
|
chomp foreach values %{$self->{_attr}}; |
|
3
|
|
|
|
|
19
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Setup initial args list |
72
|
3
|
|
|
|
|
4
|
$self->{_args} = [ grep { exists $_->{spec} } @ARGS ]; |
|
18
|
|
|
|
|
23
|
|
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
27
|
$self |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub new { |
78
|
3
|
|
|
3
|
0
|
11
|
my ($class, @params) = @_; |
79
|
3
|
|
|
|
|
5
|
my $self = bless {}, $class; |
80
|
3
|
|
|
|
|
8
|
$self->_init(@params); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub add_arg { |
84
|
75
|
|
|
75
|
0
|
99
|
my ($self, %arg) = @_; |
85
|
75
|
|
|
|
|
47
|
push (@{$self->{_args}}, \%arg); |
|
75
|
|
|
|
|
157
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub mod_arg { |
89
|
0
|
|
|
0
|
0
|
0
|
my ($self, $argname, %arg) = @_; |
90
|
0
|
|
|
|
|
0
|
foreach my $old_arg (@{$self->{_args}}) { |
|
0
|
|
|
|
|
0
|
|
91
|
0
|
0
|
0
|
|
|
0
|
next unless $old_arg->{spec} =~ /(\w+).*/ && $argname eq $1; |
92
|
0
|
|
|
|
|
0
|
foreach my $key (keys %arg) { |
93
|
0
|
|
|
|
|
0
|
$old_arg->{$key} = $arg{$key}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub getopts { |
99
|
2
|
|
|
2
|
0
|
2
|
my ($self) = @_; |
100
|
2
|
|
|
|
|
3
|
my %commandline = (); |
101
|
2
|
|
|
|
|
2
|
my @params = map { $_->{spec} } @{$self->{_args}}; |
|
60
|
|
|
|
|
52
|
|
|
2
|
|
|
|
|
3
|
|
102
|
2
|
50
|
|
|
|
8
|
if (! GetOptions(\%commandline, @params)) { |
103
|
0
|
|
|
|
|
0
|
$self->print_help(); |
104
|
0
|
|
|
|
|
0
|
exit 0; |
105
|
|
|
|
|
|
|
} else { |
106
|
2
|
|
|
2
|
|
1251
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
73
|
|
107
|
2
|
|
|
2
|
|
8
|
no warnings 'redefine'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
4483
|
|
108
|
2
|
50
|
|
|
|
2904
|
do { $self->print_help(); exit 0; } if $commandline{help}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
109
|
2
|
50
|
|
|
|
27
|
do { $self->print_version(); exit 0 } if $commandline{version}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
110
|
2
|
50
|
|
|
|
4
|
do { $self->print_usage(); exit 3 } if $commandline{usage}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
111
|
2
|
|
|
|
|
2
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } @{$self->{_args}}) { |
|
60
|
|
|
|
|
65
|
|
|
60
|
|
|
|
|
66
|
|
|
2
|
|
|
|
|
4
|
|
112
|
60
|
|
|
|
|
44
|
my $field = $_; |
113
|
60
|
|
|
|
|
131
|
*{"$field"} = sub { |
114
|
318
|
|
|
318
|
|
1484
|
return $self->{opts}->{$field}; |
115
|
60
|
|
|
|
|
76
|
}; |
116
|
|
|
|
|
|
|
} |
117
|
2
|
|
|
|
|
5
|
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
4
|
|
118
|
60
|
100
|
|
|
|
89
|
grep { exists $_->{required} && $_->{required} } @{$self->{_args}}) { |
|
2
|
|
|
|
|
9
|
|
119
|
2
|
50
|
|
|
|
7
|
do { $self->print_usage(); exit 0 } if ! exists $commandline{$_}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
120
|
|
|
|
|
|
|
} |
121
|
2
|
|
|
|
|
3
|
foreach (grep { exists $_->{default} } @{$self->{_args}}) { |
|
60
|
|
|
|
|
51
|
|
|
2
|
|
|
|
|
3
|
|
122
|
10
|
|
|
|
|
14
|
$_->{spec} =~ /^([\w\-]+)/; |
123
|
10
|
|
|
|
|
10
|
my $spec = $1; |
124
|
10
|
|
|
|
|
14
|
$self->{opts}->{$spec} = $_->{default}; |
125
|
|
|
|
|
|
|
} |
126
|
2
|
|
|
|
|
6
|
foreach (keys %commandline) { |
127
|
6
|
|
|
|
|
9
|
$self->{opts}->{$_} = $commandline{$_}; |
128
|
|
|
|
|
|
|
} |
129
|
2
|
|
|
|
|
2
|
foreach (grep { exists $_->{env} } @{$self->{_args}}) { |
|
60
|
|
|
|
|
51
|
|
|
2
|
|
|
|
|
2
|
|
130
|
2
|
|
|
|
|
5
|
$_->{spec} =~ /^([\w\-]+)/; |
131
|
2
|
|
|
|
|
2
|
my $spec = $1; |
132
|
2
|
50
|
|
|
|
8
|
if (exists $ENV{'NAGIOS__HOST'.$_->{env}}) { |
133
|
0
|
|
|
|
|
0
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__HOST'.$_->{env}}; |
134
|
|
|
|
|
|
|
} |
135
|
2
|
50
|
|
|
|
7
|
if (exists $ENV{'NAGIOS__SERVICE'.$_->{env}}) { |
136
|
0
|
|
|
|
|
0
|
$self->{opts}->{$spec} = $ENV{'NAGIOS__SERVICE'.$_->{env}}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
2
|
|
|
|
|
2
|
foreach (grep { exists $_->{aliasfor} } @{$self->{_args}}) { |
|
60
|
|
|
|
|
49
|
|
|
2
|
|
|
|
|
3
|
|
140
|
4
|
|
|
|
|
5
|
my $field = $_->{aliasfor}; |
141
|
4
|
|
|
|
|
6
|
$_->{spec} =~ /^([\w\-]+)/; |
142
|
4
|
|
|
|
|
6
|
my $aliasfield = $1; |
143
|
4
|
50
|
|
|
|
10
|
next if $self->{opts}->{$field}; |
144
|
4
|
|
|
|
|
18
|
*{"$field"} = sub { |
145
|
1
|
|
|
1
|
|
4
|
return $self->{opts}->{$aliasfield}; |
146
|
4
|
|
|
|
|
8
|
}; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub create_opt { |
152
|
0
|
|
|
0
|
0
|
0
|
my ($self, $key) = @_; |
153
|
2
|
|
|
2
|
|
12
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
598
|
|
154
|
0
|
|
|
|
|
0
|
*{"$key"} = sub { |
155
|
0
|
|
|
0
|
|
0
|
return $self->{opts}->{$key}; |
156
|
0
|
|
|
|
|
0
|
}; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub override_opt { |
160
|
6
|
|
|
6
|
0
|
7
|
my ($self, $key, $value) = @_; |
161
|
6
|
|
|
|
|
23
|
$self->{opts}->{$key} = $value; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub get { |
165
|
0
|
|
|
0
|
0
|
|
my ($self, $opt) = @_; |
166
|
0
|
|
|
|
|
|
return $self->{opts}->{$opt}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub print_help { |
170
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
171
|
0
|
|
|
|
|
|
$self->print_version(); |
172
|
0
|
|
|
|
|
|
printf "\n%s\n", $self->{_attr}->{license}; |
173
|
0
|
|
|
|
|
|
printf "\n%s\n\n", $self->{_attr}->{blurb}; |
174
|
0
|
|
|
|
|
|
$self->print_usage(); |
175
|
0
|
|
|
|
|
|
foreach (grep { |
176
|
|
|
|
|
|
|
! (exists $_->{hidden} && $_->{hidden}) |
177
|
0
|
|
0
|
|
|
|
} @{$self->{_args}}) { |
|
0
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
printf " %s\n", $_->{help}; |
179
|
|
|
|
|
|
|
} |
180
|
0
|
|
|
|
|
|
exit 0; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub print_usage { |
184
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
185
|
0
|
|
|
|
|
|
printf $self->{_attr}->{usage}, $self->{_attr}->{plugin}; |
186
|
0
|
|
|
|
|
|
print "\n"; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub print_version { |
190
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
191
|
0
|
|
|
|
|
|
printf "%s %s", $self->{_attr}->{plugin}, $self->{_attr}->{version}; |
192
|
0
|
0
|
|
|
|
|
printf " [%s]", $self->{_attr}->{url} if $self->{_attr}->{url}; |
193
|
0
|
|
|
|
|
|
print "\n"; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub print_license { |
197
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
198
|
0
|
|
|
|
|
|
printf "%s\n", $self->{_attr}->{license}; |
199
|
0
|
|
|
|
|
|
print "\n"; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
__END__ |