(plugin) apps::protocol::http - mode collection add constants for http requests (#3490)

This commit is contained in:
qgarnier 2022-02-16 09:39:13 +01:00 committed by GitHub
parent 03b6255973
commit 40ee72be9e

View File

@ -264,28 +264,46 @@ sub call_http {
my $creds = {}; my $creds = {};
if (defined($options{rq}->{authorization}) && defined($options{rq}->{authorization}->{username})) { if (defined($options{rq}->{authorization}) && defined($options{rq}->{authorization}->{username})) {
$options{rq}->{authorization}->{username} = $self->substitute_constants(value => $options{rq}->{authorization}->{username});
$options{rq}->{authorization}->{password} = $self->substitute_constants(value => $options{rq}->{authorization}->{password});
$creds = { $creds = {
credentials => 1, credentials => 1,
%{$options{rq}->{authorization}} %{$options{rq}->{authorization}}
}; };
} }
my $headers;
if (defined($options{rq}->{headers}) && ref($options{rq}->{headers}) eq 'ARRAY') {
$headers = [];
foreach my $header (@{$options{rq}->{headers}}) {
push @$headers, $self->substitute_constants(value => $header);
}
}
my $get_params;
if (defined($options{rq}->{get_params}) && ref($options{rq}->{get_params}) eq 'ARRAY') {
$get_params = [];
foreach my $param (@{$options{rq}->{get_params}}) {
push @$get_params, $self->substitute_constants(value => $param);
}
}
my $post_param = $self->get_payload(rq => $options{rq}); my $post_param = $self->get_payload(rq => $options{rq});
my $http = centreon::plugins::http->new(noptions => 1, output => $self->{output}); my $http = centreon::plugins::http->new(noptions => 1, output => $self->{output});
my $timing0 = [gettimeofday]; my $timing0 = [gettimeofday];
my ($content) = $http->request( my ($content) = $http->request(
backend => $options{rq}->{backend}, backend => $self->substitute_constants(value => $options{rq}->{backend}),
method => $options{rq}->{method}, method => $self->substitute_constants(value => $options{rq}->{method}),
hostname => $options{rq}->{hostname}, hostname => $self->substitute_constants(value => $options{rq}->{hostname}),
proto => $options{rq}->{proto}, proto => $self->substitute_constants(value => $options{rq}->{proto}),
port => $options{rq}->{port}, port => $self->substitute_constants(value => $options{rq}->{port}),
url_path => $options{rq}->{endpoint}, url_path => $self->substitute_constants(value => $options{rq}->{endpoint}),
header => $options{rq}->{headers}, header => $headers,
timeout => $options{rq}->{timeout}, timeout => $self->substitute_constants(value => $options{rq}->{timeout}),
get_param => $options{rq}->{get_params}, get_param => $get_params,
query_form_post => $post_param, query_form_post => $self->substitute_constants(value => $post_param),
insecure => $options{rq}->{insecure}, insecure => $options{rq}->{insecure},
unknown_status => '', unknown_status => '',
warning_status => '', warning_status => '',
@ -895,20 +913,24 @@ sub set_builtin {
} }
} }
sub set_constants { sub create_constants {
my ($self, %options) = @_; my ($self, %options) = @_;
my $constants = {}; $self->{constants} = {};
if (defined($self->{config}->{constants})) { if (defined($self->{config}->{constants})) {
foreach (keys %{$self->{config}->{constants}}) { foreach (keys %{$self->{config}->{constants}}) {
$constants->{'constants.' . $_} = $self->{config}->{constants}->{$_}; $self->{constants}->{'constants.' . $_} = $self->{config}->{constants}->{$_};
} }
} }
foreach (keys %{$self->{option_results}->{constant}}) { foreach (keys %{$self->{option_results}->{constant}}) {
$constants->{'constants.' . $_} = $self->{option_results}->{constant}->{$_}; $self->{constants}->{'constants.' . $_} = $self->{option_results}->{constant}->{$_};
} }
}
return $constants; sub set_constants {
my ($self, %options) = @_;
return { %{$self->{constants}} };
} }
sub set_expand_table { sub set_expand_table {
@ -1329,6 +1351,14 @@ sub set_functions {
} }
} }
sub substitute_constants {
my ($self, %options) = @_;
return undef if (!defined($options{value}));
$options{value} =~ s/%\((constants\.[a-zA-Z0-9\._:]+?)\)/$self->{constants}->{$1}/g;
return $options{value};
}
sub prepare_variables { sub prepare_variables {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -1508,6 +1538,7 @@ sub disco_show {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->read_config(); $self->read_config();
$self->create_constants();
$self->collect_http(); $self->collect_http();
$self->{selections} = {}; $self->{selections} = {};
@ -1529,6 +1560,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->read_config(); $self->read_config();
$self->create_constants();
$self->collect_http(); $self->collect_http();
$self->{selections} = {}; $self->{selections} = {};