centreon-plugins/centreon/plugins/backend/http/lwp.pm

383 lines
13 KiB
Perl
Raw Normal View History

2019-03-06 16:47:46 +01:00
#
2021-02-08 09:55:50 +01:00
# Copyright 2021 Centreon (http://www.centreon.com/)
2019-03-06 16:47:46 +01:00
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package centreon::plugins::backend::http::lwp;
use strict;
use warnings;
use centreon::plugins::backend::http::useragent;
use URI;
use IO::Socket::SSL;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{noptions}) || $options{noptions} != 1) {
$options{options}->add_options(arguments => {
2019-12-04 14:14:39 +01:00
'ssl:s' => { name => 'ssl' },
'ssl-opt:s@' => { name => 'ssl_opt' },
2019-03-06 16:47:46 +01:00
});
$options{options}->add_help(package => __PACKAGE__, sections => 'BACKEND LWP OPTIONS', once => 1);
}
$self->{output} = $options{output};
$self->{ua} = undef;
2019-07-01 17:49:57 +02:00
$self->{debug_handlers} = 0;
2019-12-04 14:14:39 +01:00
2019-03-06 16:47:46 +01:00
return $self;
}
sub check_options {
my ($self, %options) = @_;
foreach (('unknown_status', 'warning_status', 'critical_status')) {
if (defined($options{request}->{$_})) {
2021-04-01 09:48:14 +02:00
$options{request}->{$_} =~ s/%\{http_code\}/\$values->{code}/g;
2019-03-06 16:47:46 +01:00
}
}
$self->{ssl_context} = '';
if (!defined($options{request}->{ssl_opt})) {
$options{request}->{ssl_opt} = [];
}
if (defined($options{request}->{ssl}) && $options{request}->{ssl} ne '') {
push @{$options{request}->{ssl_opt}}, 'SSL_version => ' . $options{request}->{ssl};
}
if (defined($options{request}->{cert_file}) && !defined($options{request}->{cert_pkcs12})) {
push @{$options{request}->{ssl_opt}}, 'SSL_use_cert => 1';
push @{$options{request}->{ssl_opt}}, 'SSL_cert_file => "' . $options{request}->{cert_file} . '"';
push @{$options{request}->{ssl_opt}}, 'SSL_key_file => "' . $options{request}->{key_file} . '"'
if (defined($options{request}->{key_file}));
push @{$options{request}->{ssl_opt}}, 'SSL_ca_file => "' . $options{request}->{cacert_file} . '"'
if (defined($options{request}->{cacert_file}));
}
2020-12-16 11:44:33 +01:00
if ($options{request}->{insecure}) {
push @{$options{request}->{ssl_opt}}, 'SSL_verify_mode => SSL_VERIFY_NONE';
}
2019-03-06 16:47:46 +01:00
my $append = '';
foreach (@{$options{request}->{ssl_opt}}) {
if ($_ ne '') {
$self->{ssl_context} .= $append . $_;
$append = ', ';
}
}
}
sub set_proxy {
my ($self, %options) = @_;
if (defined($options{request}->{proxypac}) && $options{request}->{proxypac} ne '') {
2019-11-25 17:23:29 +01:00
centreon::plugins::misc::mymodule_load(
output => $self->{output}, module => 'HTTP::ProxyPAC',
error_msg => "Cannot load module 'HTTP::ProxyPAC'."
);
2019-03-06 16:47:46 +01:00
my ($pac, $pac_uri);
eval {
if ($options{request}->{proxypac} =~ /^(http|https):\/\//) {
$pac_uri = URI->new($options{request}->{proxypac});
$pac = HTTP::ProxyPAC->new($pac_uri);
} else {
$pac = HTTP::ProxyPAC->new($options{request}->{proxypac});
}
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'issue to load proxypac: ' . $@);
$self->{output}->option_exit();
}
my $res = $pac->find_proxy($options{url});
if (defined($res->direct) && $res->direct != 1) {
my $proxy_uri = URI->new($res->proxy);
$proxy_uri->userinfo($pac_uri->userinfo) if (defined($pac_uri->userinfo));
$self->{ua}->proxy(['http', 'https'], $proxy_uri->as_string);
}
}
if (defined($options{request}->{proxyurl}) && $options{request}->{proxyurl} ne '') {
2019-10-16 08:59:42 +02:00
my $proxyurl = $options{request}->{proxyurl};
2020-02-13 15:12:31 +01:00
if ($options{request}->{proto} eq "https" ||
(defined($options{request}->{full_url}) && $options{request}->{full_url} =~ /^https/)) {
2019-10-16 08:59:42 +02:00
$proxyurl = 'connect://' . $2 if ($proxyurl =~ /^(http|https):\/\/(.*)/);
}
$self->{ua}->proxy(['http', 'https'], $proxyurl);
2019-03-06 16:47:46 +01:00
}
}
sub request {
my ($self, %options) = @_;
2021-02-18 11:04:21 +01:00
my %user_agent_params = (keep_alive => 1);
if (defined($options{request}->{certinfo}) && $options{request}->{certinfo} == 1) {
centreon::plugins::misc::mymodule_load(
output => $self->{output}, module => 'LWP::ConnCache',
error_msg => "Cannot load module 'LWP::ConnCache'."
);
$self->{cache} = LWP::ConnCache->new();
$self->{cache}->total_capacity(1);
%user_agent_params = (conn_cache => $self->{cache});
}
2019-03-06 16:47:46 +01:00
my $request_options = $options{request};
if (!defined($self->{ua})) {
2021-03-02 14:33:23 +01:00
my $timeout;
$timeout = $1 if (defined($request_options->{timeout}) && $request_options->{timeout} =~ /(\d+)/);
2019-03-06 16:47:46 +01:00
$self->{ua} = centreon::plugins::backend::http::useragent->new(
2021-02-18 11:04:21 +01:00
%user_agent_params,
protocols_allowed => ['http', 'https'],
2021-03-02 14:33:23 +01:00
timeout => $timeout,
credentials => $request_options->{credentials},
username => $request_options->{username},
password => $request_options->{password}
);
2019-03-06 16:47:46 +01:00
if (defined($request_options->{cookies_file})) {
2019-10-29 14:44:26 +01:00
centreon::plugins::misc::mymodule_load(
output => $self->{output},
module => 'HTTP::Cookies',
error_msg => "Cannot load module 'HTTP::Cookies'."
);
$self->{ua}->cookie_jar(
HTTP::Cookies->new(
file => $request_options->{cookies_file},
autosave => 1
)
);
2019-03-06 16:47:46 +01:00
}
}
2019-12-04 14:14:39 +01:00
2019-07-01 17:49:57 +02:00
if ($self->{output}->is_debug() && $self->{debug_handlers} == 0) {
$self->{debug_handlers} = 1;
2019-10-29 14:44:26 +01:00
$self->{ua}->add_handler('request_send', sub {
2019-04-06 21:39:33 +02:00
my ($response, $ua, $handler) = @_;
2019-10-29 14:44:26 +01:00
$self->{output}->output_add(long_msg => '======> request send', debug => 1);
2019-04-06 21:39:33 +02:00
$self->{output}->output_add(long_msg => $response->as_string, debug => 1);
return ;
});
$self->{ua}->add_handler("response_done", sub {
my ($response, $ua, $handler) = @_;
2019-12-04 14:14:39 +01:00
2019-10-29 14:44:26 +01:00
$self->{output}->output_add(long_msg => '======> response done', debug => 1);
2019-04-06 21:39:33 +02:00
$self->{output}->output_add(long_msg => $response->as_string, debug => 1);
return ;
});
}
2019-12-04 14:14:39 +01:00
2019-03-06 16:47:46 +01:00
if (defined($request_options->{no_follow})) {
$self->{ua}->requests_redirectable(undef);
} else {
2020-06-05 09:45:29 +02:00
$self->{ua}->requests_redirectable([ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH' ]);
2019-03-06 16:47:46 +01:00
}
if (defined($request_options->{http_peer_addr})) {
push @LWP::Protocol::http::EXTRA_SOCK_OPTS, PeerAddr => $request_options->{http_peer_addr};
}
my ($req, $url);
if (defined($request_options->{full_url})) {
$url = $request_options->{full_url};
} elsif (defined($request_options->{port}) && $request_options->{port} =~ /^[0-9]+$/) {
2019-10-29 14:44:26 +01:00
$url = $request_options->{proto}. '://' . $request_options->{hostname} . ':' . $request_options->{port} . $request_options->{url_path};
2019-03-06 16:47:46 +01:00
} else {
2019-10-29 14:44:26 +01:00
$url = $request_options->{proto}. '://' . $request_options->{hostname} . $request_options->{url_path};
2019-03-06 16:47:46 +01:00
}
my $uri = URI->new($url);
if (defined($request_options->{get_params})) {
$uri->query_form($request_options->{get_params});
}
2021-12-03 08:57:23 +01:00
2019-03-06 16:47:46 +01:00
$req = HTTP::Request->new($request_options->{method}, $uri);
2020-12-28 17:08:31 +01:00
my $content_type_forced = 0;
2019-03-06 16:47:46 +01:00
foreach my $key (keys %{$request_options->{headers}}) {
2020-12-28 17:08:31 +01:00
$req->header($key => $request_options->{headers}->{$key});
if ($key =~ /content-type/i) {
$content_type_forced = 1;
2019-03-06 16:47:46 +01:00
}
}
2020-12-28 17:08:31 +01:00
if ($content_type_forced == 1) {
$req->content($request_options->{query_form_post});
} elsif (defined($options{request}->{post_params})) {
my $uri_post = URI->new();
$uri_post->query_form($request_options->{post_params});
$req->content_type('application/x-www-form-urlencoded');
$req->content($uri_post->query);
2019-03-06 16:47:46 +01:00
}
2021-12-03 08:57:23 +01:00
if (defined($request_options->{form})) {
$self->{output}->add_option_msg(short_msg => 'unsupported form param');
$self->{output}->option_exit();
}
2019-03-06 16:47:46 +01:00
if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) {
2019-10-29 14:44:26 +01:00
centreon::plugins::misc::mymodule_load(
output => $self->{output},
module => 'Authen::NTLM',
error_msg => "Cannot load module 'Authen::NTLM'."
);
2019-03-06 16:47:46 +01:00
Authen::NTLM::ntlmv2(1);
}
if (defined($request_options->{credentials}) && defined($request_options->{basic})) {
$req->authorization_basic($request_options->{username}, $request_options->{password});
}
$self->set_proxy(request => $request_options, url => $url);
if (defined($request_options->{cert_pkcs12}) && $request_options->{cert_file} ne '' && $request_options->{cert_pwd} ne '') {
2019-10-29 14:44:26 +01:00
eval 'use Net::SSL'; die $@ if $@;
2019-03-06 16:47:46 +01:00
$ENV{HTTPS_PKCS12_FILE} = $request_options->{cert_file};
$ENV{HTTPS_PKCS12_PASSWORD} = $request_options->{cert_pwd};
}
if (defined($self->{ssl_context}) && $self->{ssl_context} ne '') {
my $context = new IO::Socket::SSL::SSL_Context(eval $self->{ssl_context});
IO::Socket::SSL::set_default_context($context);
}
$self->{response} = $self->{ua}->request($req);
# Check response
my $status = 'ok';
my $code = $self->{response}->code();
if (defined($request_options->{critical_status}) && $request_options->{critical_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{critical_status}, values => { code => $code })) {
$status = 'critical';
} elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{warning_status}, values => { code => $code })) {
$status = 'warning';
} elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{unknown_status}, values => { code => $code })) {
$status = 'unknown';
2019-03-06 16:47:46 +01:00
}
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
my $short_msg = $self->{response}->status_line;
if ($short_msg =~ /^401/) {
2019-04-17 14:16:25 +02:00
$short_msg .= ' (' . $1 . ' authentication expected)' if (defined($self->{response}->www_authenticate) &&
$self->{response}->www_authenticate =~ /(\S+)/);
2019-03-06 16:47:46 +01:00
}
2019-12-04 14:14:39 +01:00
$self->{output}->output_add(
severity => $status,
short_msg => $short_msg
);
2019-03-06 16:47:46 +01:00
$self->{output}->display();
$self->{output}->exit();
}
$self->{headers} = $self->{response}->headers();
return $self->{response}->content;
}
sub get_headers {
my ($self, %options) = @_;
2019-12-04 14:14:39 +01:00
my $headers = '';
foreach ($options{response}->header_field_names()) {
2019-10-29 14:44:26 +01:00
my $value = $options{response}->header($_);
$headers .= "$_: " . (defined($value) ? $value : '') . "\n";
}
2019-12-04 14:14:39 +01:00
return $headers;
}
sub get_first_header {
my ($self, %options) = @_;
my @redirects = $self->{response}->redirects();
if (!defined($options{name})) {
return $self->get_headers(response => defined($redirects[0]) ? $redirects[0] : $self->{response});
}
return
defined($redirects[0]) ?
$redirects[0]->headers()->header($options{name}) :
$self->{headers}->header($options{name})
;
}
2019-03-06 16:47:46 +01:00
sub get_header {
my ($self, %options) = @_;
if (!defined($options{name})) {
return $self->get_headers(response => $self->{response});
}
2019-03-06 16:47:46 +01:00
return $self->{headers}->header($options{name});
}
sub get_code {
my ($self, %options) = @_;
return $self->{response}->code();
}
sub get_message {
my ($self, %options) = @_;
return $self->{response}->message();
}
2021-02-18 11:04:21 +01:00
sub get_certificate {
my ($self, %options) = @_;
my ($con) = $self->{cache}->get_connections('https');
return ('socket', $con);
}
sub get_times {
my ($self, %options) = @_;
return undef;
}
2019-03-06 16:47:46 +01:00
1;
__END__
=head1 NAME
HTTP LWP backend layer.
=head1 SYNOPSIS
HTTP LWP backend layer.
=head1 BACKEND LWP OPTIONS
=over 8
=item B<--ssl-opt>
Set SSL Options (--ssl-opt="SSL_version => TLSv1" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE").
=item B<--ssl>
Set SSL version (--ssl=TLSv1).
=back
=head1 DESCRIPTION
B<http>.
=cut