From a5d43fc0b428def7ecfdc72a949fd971495072cf Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Mon, 23 Jan 2023 11:32:27 +0100 Subject: [PATCH] (enh) double-dash support for arguments (#4151) --- src/apps/protocols/nrpe/mode/query.pm | 13 ++++++++++++- src/centreon/plugins/alternative/Getopt.pm | 9 +++++++++ src/centreon/plugins/mode.pm | 1 + src/centreon/plugins/options.pm | 4 ++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/apps/protocols/nrpe/mode/query.pm b/src/apps/protocols/nrpe/mode/query.pm index a6963ce3a..126fc3622 100644 --- a/src/apps/protocols/nrpe/mode/query.pm +++ b/src/apps/protocols/nrpe/mode/query.pm @@ -33,6 +33,7 @@ sub new { $options{options}->add_options(arguments => { 'command:s' => { name => 'command' }, 'arg:s@' => { name => 'arg' }, + 'extra-args' => { name => 'extra_args' }, 'sanitize-message:s' => { name => 'sanitize_message' } }); @@ -70,9 +71,12 @@ sub sanitize_message { sub run { my ($self, %options) = @_; + my @arg = defined($self->{option_results}->{arg}) ? @{$self->{option_results}->{arg}} : (); + push @arg, @{$self->{option_extras}} if ($self->{option_results}->{extra_args}); + my $result = $options{custom}->request( command => $self->{option_results}->{command}, - arg => $self->{option_results}->{arg} + arg => \@arg ); $self->{output}->output_add( @@ -112,6 +116,13 @@ In nrpe use following command to get server version: --command='_NRPE_CHECK' Set arguments (Multiple option. Example: --arg='arg1' --arg='arg2'). +=item B<--extra-args> + +Use extra arguments from command line (all values placed after a double-dash '--') +as additional "--arg" options for the NRPE command. + +Example: --arg='arg1' --extra-args -- 'arg2' 'arg3' 'arg4' + =item B<--sanitize-message> Sanitize message by removing heading code and diff --git a/src/centreon/plugins/alternative/Getopt.pm b/src/centreon/plugins/alternative/Getopt.pm index 3f33c66ea..94b8a6f61 100644 --- a/src/centreon/plugins/alternative/Getopt.pm +++ b/src/centreon/plugins/alternative/Getopt.pm @@ -62,6 +62,15 @@ sub GetOptions { if (defined($ARGV[$i]) && $ARGV[$i] =~ /^--(.*?)(?:=|$)((?s).*)/) { my ($option, $value) = ($1, $2); + # The special argument "--" forces an end of option-scanning. + # All arguments placed after are stored in a list with the special option key '_double_dash_'. + if ($option eq '' && $value eq '') { + my @values = splice @ARGV, $i + 1, $num_args - $i - 1; + push @{${$opts{'_double_dash_'}}}, @values; + splice @ARGV, $i, 1; + last; + } + # find type of option if ($search_str !~ /,((?:[^,]*?\|){0,}$option(?:\|.*?){0,}(:.*?){0,1}),/) { warn "Unknown option: $option" if ($warn_message == 1); diff --git a/src/centreon/plugins/mode.pm b/src/centreon/plugins/mode.pm index 82db8eb8b..8e3d9f2ce 100644 --- a/src/centreon/plugins/mode.pm +++ b/src/centreon/plugins/mode.pm @@ -32,6 +32,7 @@ sub new { $self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output}); %{$self->{option_results}} = (); + @{$self->{option_extras}} = @{$options{options}->{extra_arguments}}; $self->{output} = $options{output}; $self->{output}->use_new_perfdata(value => 1) if (defined($options{force_new_perfdata}) && $options{force_new_perfdata} == 1); diff --git a/src/centreon/plugins/options.pm b/src/centreon/plugins/options.pm index 6ef70f060..28dd8e18c 100644 --- a/src/centreon/plugins/options.pm +++ b/src/centreon/plugins/options.pm @@ -37,6 +37,7 @@ sub new { $self->{options} = {}; @{$self->{pod_package}} = (); $self->{pod_packages_once} = {}; + $self->{extra_arguments} = []; if ($alternative == 0) { require Getopt::Long; @@ -145,6 +146,9 @@ sub parse_options { }; } + # Store all arguments placed after the special argument "--" in the 'extra_arguments' list + $self->{options}->{'_double_dash_'} = \$self->{extra_arguments}; + GetOptions( %{$self->{options}} );