This commit is contained in:
qgarnier 2020-12-28 17:08:31 +01:00 committed by GitHub
parent f99839813e
commit 6d1c41c0ea
7 changed files with 40 additions and 46 deletions

View File

@ -188,7 +188,9 @@ sub get_auth_token {
method => 'POST', method => 'POST',
url_path => $self->{url_path} . '/Login', url_path => $self->{url_path} . '/Login',
query_form_post => $encoded, query_form_post => $encoded,
warning_status => '', unknown_status => '', critical_status => '' warning_status => '',
unknown_status => '',
critical_status => ''
); );
if ($self->{http}->get_code() != 200) { if ($self->{http}->get_code() != 200) {
@ -222,7 +224,6 @@ sub request_internal {
} }
my $content = $self->{http}->request( my $content = $self->{http}->request(
method => 'GET',
url_path => $self->{url_path} . $options{endpoint}, url_path => $self->{url_path} . $options{endpoint},
get_param => $options{get_param}, get_param => $options{get_param},
warning_status => '', warning_status => '',

View File

@ -118,8 +118,10 @@ sub load_response {
my ($self, %options) = @_; my ($self, %options) = @_;
if ($self->{response_type} eq 'xml') { if ($self->{response_type} eq 'xml') {
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'XML::XPath', centreon::plugins::misc::mymodule_load(
error_msg => "Cannot load module 'XML::XPath'."); output => $self->{output}, module => 'XML::XPath',
error_msg => "Cannot load module 'XML::XPath'."
);
eval { eval {
$self->{xpath_response} = XML::XPath->new(xml => $options{response}); $self->{xpath_response} = XML::XPath->new(xml => $options{response});
}; };

View File

@ -123,7 +123,6 @@ sub settings {
$self->{http}->add_header(key => 'Accept', value => 'application/json'); $self->{http}->add_header(key => 'Accept', value => 'application/json');
if (defined($self->{auth_token})) { if (defined($self->{auth_token})) {
$self->{http}->add_header(key => 'Centreon-Auth-Token', value => $self->{auth_token}); $self->{http}->add_header(key => 'Centreon-Auth-Token', value => $self->{auth_token});
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
} }
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
} }
@ -190,7 +189,10 @@ sub request_api {
method => $options{method}, method => $options{method},
url_path => $options{url_path}, url_path => $options{url_path},
query_form_post => $encoded_form_post, query_form_post => $encoded_form_post,
critical_status => '', warning_status => '', unknown_status => '' header => $options{header},
critical_status => '',
warning_status => '',
unknown_status => ''
); );
my $decoded; my $decoded;
@ -211,7 +213,8 @@ sub submit_result {
my ($response, $raw) = $self->request_api( my ($response, $raw) = $self->request_api(
method => 'POST', method => 'POST',
url_path => $self->{api_path} . '?action=submit&object=centreon_submit_results', url_path => $self->{api_path} . '?action=submit&object=centreon_submit_results',
query_form_post => $options{post_data} query_form_post => $options{post_data},
header => ['Content-Type: application/json']
); );
return ($response, $raw); return ($response, $raw);

View File

@ -37,26 +37,26 @@ sub set_counters {
key_values => [ { name => '202' } ], key_values => [ { name => '202' } ],
output_template => '202: %d', output_template => '202: %d',
perfdatas => [ perfdatas => [
{ value => '202', template => '%d', min => 0 }, { template => '%d', min => 0 }
], ]
} }
}, },
{ label => 'results-400', nlabel => 'results.400.count', set => { { label => 'results-400', nlabel => 'results.400.count', set => {
key_values => [ { name => '400' } ], key_values => [ { name => '400' } ],
output_template => '400: %d', output_template => '400: %d',
perfdatas => [ perfdatas => [
{ value => '400', template => '%d', min => 0 }, { template => '%d', min => 0 }
], ]
} }
}, },
{ label => 'results-404', nlabel => 'results.404.count', set => { { label => 'results-404', nlabel => 'results.404.count', set => {
key_values => [ { name => '404' } ], key_values => [ { name => '404' } ],
output_template => '404: %d', output_template => '404: %d',
perfdatas => [ perfdatas => [
{ value => '404', template => '%d', min => 0 }, { template => '%d', min => 0 }
], ]
} }
}, }
]; ];
} }
@ -72,21 +72,16 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"host:s@" => { name => 'host' }, 'host:s@' => { name => 'host' },
"service:s@" => { name => 'service' }, 'service:s@' => { name => 'service' },
"status:s@" => { name => 'status' }, 'status:s@' => { name => 'status' },
"output:s@" => { name => 'output' }, 'output:s@' => { name => 'output' },
"perfdata:s@" => { name => 'perfdata' }, 'perfdata:s@' => { name => 'perfdata' }
}); });
return $self; return $self;
} }
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;

View File

@ -115,7 +115,6 @@ sub settings {
return if (defined($self->{settings_done})); return if (defined($self->{settings_done}));
$self->build_options_for_httplib(); $self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json'); $self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
$self->{settings_done} = 1; $self->{settings_done} = 1;
} }

View File

@ -161,10 +161,6 @@ sub set_method {
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => undef); $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => undef);
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HTTPGET'), parameter => 1); $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HTTPGET'), parameter => 1);
if ($options{request}->{method} eq 'GET') {
return ;
}
if ($options{content_type_forced} == 1) { if ($options{content_type_forced} == 1) {
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => $options{request}->{query_form_post}) $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => $options{request}->{query_form_post})
if (defined($options{request}->{query_form_post})); if (defined($options{request}->{query_form_post}));
@ -175,6 +171,10 @@ sub set_method {
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => $uri_post->query); $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POSTFIELDS'), parameter => $uri_post->query);
} }
if ($options{request}->{method} eq 'GET') {
return ;
}
if ($options{request}->{method} eq 'POST') { if ($options{request}->{method} eq 'POST') {
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POST'), parameter => 1); $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_POST'), parameter => 1);
} }

View File

@ -191,27 +191,21 @@ sub request {
} }
$req = HTTP::Request->new($request_options->{method}, $uri); $req = HTTP::Request->new($request_options->{method}, $uri);
my $content_type_forced; my $content_type_forced = 0;
foreach my $key (keys %{$request_options->{headers}}) { foreach my $key (keys %{$request_options->{headers}}) {
if ($key !~ /content-type/i) { $req->header($key => $request_options->{headers}->{$key});
$req->header($key => $request_options->{headers}->{$key}); if ($key =~ /content-type/i) {
} else { $content_type_forced = 1;
$content_type_forced = $request_options->{headers}->{$key};
} }
} }
if ($request_options->{method} ne 'GET') { if ($content_type_forced == 1) {
if (defined($content_type_forced)) { $req->content($request_options->{query_form_post});
$req->content_type($content_type_forced); } elsif (defined($options{request}->{post_params})) {
$req->content($request_options->{query_form_post}); my $uri_post = URI->new();
} else { $uri_post->query_form($request_options->{post_params});
my $uri_post = URI->new(); $req->content_type('application/x-www-form-urlencoded');
if (defined($request_options->{post_params})) { $req->content($uri_post->query);
$uri_post->query_form($request_options->{post_params});
}
$req->content_type('application/x-www-form-urlencoded');
$req->content($uri_post->query);
}
} }
if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) { if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) {