centreon-plugins/centreon/plugins/http.pm

273 lines
8.3 KiB
Perl
Raw Normal View History

2015-07-28 18:17:04 +02:00
#
2021-02-08 09:55:50 +01:00
# Copyright 2021 Centreon (http://www.centreon.com/)
2015-07-28 18:17:04 +02: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::http;
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
2019-03-06 16:47:46 +01:00
if (!defined($options{noptions}) || $options{noptions} != 1) {
$options{options}->add_options(arguments => {
2019-09-03 09:21:41 +02:00
'http-peer-addr:s' => { name => 'http_peer_addr' },
'proxyurl:s' => { name => 'proxyurl' },
'proxypac:s' => { name => 'proxypac' },
2020-12-16 11:44:33 +01:00
'insecure' => { name => 'insecure' },
2019-09-03 09:21:41 +02:00
'http-backend:s' => { name => 'http_backend', default => 'lwp' },
2019-03-06 16:47:46 +01:00
});
$options{options}->add_help(package => __PACKAGE__, sections => 'HTTP GLOBAL OPTIONS');
}
2019-12-04 11:25:46 +01:00
centreon::plugins::misc::mymodule_load(
output => $options{output},
module => 'centreon::plugins::backend::http::lwp',
error_msg => "Cannot load module 'centreon::plugins::backend::http::lwp'."
);
2019-03-06 16:47:46 +01:00
$self->{backend_lwp} = centreon::plugins::backend::http::lwp->new(%options);
2019-12-04 11:25:46 +01:00
centreon::plugins::misc::mymodule_load(
output => $options{output},
module => 'centreon::plugins::backend::http::curl',
error_msg => "Cannot load module 'centreon::plugins::backend::http::curl'."
);
2019-03-06 16:47:46 +01:00
$self->{backend_curl} = centreon::plugins::backend::http::curl->new(%options);
2015-07-28 18:17:04 +02:00
$self->{output} = $options{output};
$self->{options} = {
proto => 'http',
url_path => '/',
timeout => 5,
method => 'GET',
unknown_status => '%{http_code} < 200 or %{http_code} >= 300',
warning_status => undef,
critical_status => undef,
};
2019-03-06 16:47:46 +01:00
$self->{add_headers} = {};
2015-07-28 18:17:04 +02:00
return $self;
}
sub set_options {
my ($self, %options) = @_;
2015-07-28 18:17:04 +02:00
$self->{options} = { %{$self->{options}} };
foreach (keys %options) {
$self->{options}->{$_} = $options{$_} if (defined($options{$_}));
}
}
sub add_header {
my ($self, %options) = @_;
2015-07-28 18:17:04 +02:00
$self->{add_headers}->{$options{key}} = $options{value};
}
2020-04-03 10:50:06 +02:00
sub remove_header {
my ($self, %options) = @_;
delete $self->{add_headers}->{$options{key}} if (defined($self->{add_headers}->{$options{key}}));
}
2015-07-28 18:17:04 +02:00
sub check_options {
my ($self, %options) = @_;
2019-03-06 16:47:46 +01:00
$options{request}->{http_backend} = 'lwp'
if (!defined($options{request}->{http_backend}) || $options{request}->{http_backend} eq '');
$self->{http_backend} = $options{request}->{http_backend};
if ($self->{http_backend} !~ /^\s*lwp|curl\s*$/i) {
$self->{output}->add_option_msg(short_msg => "Unsupported http backend specified '" . $self->{http_backend} . "'.");
$self->{output}->option_exit();
}
2019-12-04 11:25:46 +01:00
if (defined($options{request}->{$self->{http_backend} . '_backend_options'})) {
foreach (keys %{$options{request}->{$self->{http_backend} . '_backend_options'}}) {
$options{request}->{$_} = $options{request}->{$self->{http_backend} . '_backend_options'}->{$_};
2019-09-03 09:21:41 +02:00
}
}
2015-07-28 18:17:04 +02:00
if (($options{request}->{proto} ne 'http') && ($options{request}->{proto} ne 'https')) {
$self->{output}->add_option_msg(short_msg => "Unsupported protocol specified '" . $self->{option_results}->{proto} . "'.");
$self->{output}->option_exit();
}
if (!defined($options{request}->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Please set the hostname option");
$self->{output}->option_exit();
}
if ((defined($options{request}->{credentials})) && (!defined($options{request}->{username}) || !defined($options{request}->{password}))) {
$self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= options when --credentials is used");
$self->{output}->option_exit();
}
2019-03-06 16:47:46 +01:00
if ((defined($options{request}->{cert_pkcs12})) && (!defined($options{request}->{cert_file}) && !defined($options{request}->{cert_pwd}))) {
2015-07-28 18:17:04 +02:00
$self->{output}->add_option_msg(short_msg => "You need to set --cert-file= and --cert-pwd= options when --pkcs12 is used");
$self->{output}->option_exit();
}
2019-03-06 16:47:46 +01:00
$options{request}->{port_force} = $self->get_port();
2015-07-28 18:17:04 +02:00
$options{request}->{headers} = {};
if (defined($options{request}->{header})) {
foreach (@{$options{request}->{header}}) {
2019-12-04 11:25:46 +01:00
if (/^(:.+?|.+?):(.*)/) {
2015-07-28 18:17:04 +02:00
$options{request}->{headers}->{$1} = $2;
}
}
}
foreach (keys %{$self->{add_headers}}) {
$options{request}->{headers}->{$_} = $self->{add_headers}->{$_};
}
2015-07-28 18:17:04 +02:00
foreach my $method (('get', 'post')) {
if (defined($options{request}->{$method . '_param'})) {
2019-03-06 16:47:46 +01:00
$options{request}->{$method . '_params'} = {};
2015-07-28 18:17:04 +02:00
foreach (@{$options{request}->{$method . '_param'}}) {
2020-02-03 23:19:53 +01:00
if (/^([^=]+)={0,1}(.*)$/s) {
2015-07-28 18:17:04 +02:00
my $key = $1;
my $value = defined($2) ? $2 : 1;
2019-03-06 16:47:46 +01:00
if (defined($options{request}->{$method . '_params'}->{$key})) {
if (ref($options{request}->{$method . '_params'}->{$key}) ne 'ARRAY') {
$options{request}->{$method . '_params'}->{$key} = [ $options{request}->{$method . '_params'}->{$key} ];
2015-07-28 18:17:04 +02:00
}
2019-03-06 16:47:46 +01:00
push @{$options{request}->{$method . '_params'}->{$key}}, $value;
2015-07-28 18:17:04 +02:00
} else {
2019-03-06 16:47:46 +01:00
$options{request}->{$method . '_params'}->{$key} = $value;
2015-07-28 18:17:04 +02:00
}
}
}
}
}
2019-12-04 14:14:39 +01:00
2019-03-06 16:47:46 +01:00
$self->{'backend_' . $self->{http_backend}}->check_options(%options);
2015-07-28 18:17:04 +02:00
}
sub get_port {
my ($self, %options) = @_;
my $port = '';
if (defined($self->{options}->{port}) && $self->{options}->{port} ne '') {
$port = $self->{options}->{port};
} else {
$port = 80 if ($self->{options}->{proto} eq 'http');
$port = 443 if ($self->{options}->{proto} eq 'https');
}
return $port;
}
2015-07-29 14:35:55 +02:00
sub get_port_request {
my ($self, %options) = @_;
2015-07-29 14:35:55 +02:00
my $port = '';
if (defined($self->{options}->{port}) && $self->{options}->{port} ne '') {
$port = $self->{options}->{port};
}
return $port;
}
2015-07-28 18:17:04 +02:00
sub request {
my ($self, %options) = @_;
2018-01-19 12:11:16 +01:00
my $request_options = { %{$self->{options}} };
2015-07-28 18:17:04 +02:00
foreach (keys %options) {
$request_options->{$_} = $options{$_} if (defined($options{$_}));
}
$self->check_options(request => $request_options);
2019-03-06 16:47:46 +01:00
return $self->{'backend_' . $self->{http_backend}}->request(request => $request_options);
}
2015-07-28 18:17:04 +02:00
sub get_first_header {
my ($self, %options) = @_;
return $self->{'backend_' . $self->{http_backend}}->get_first_header(%options);
}
2019-03-06 16:47:46 +01:00
sub get_header {
my ($self, %options) = @_;
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
return $self->{'backend_' . $self->{http_backend}}->get_header(%options);
}
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
sub get_code {
my ($self, %options) = @_;
2019-03-06 16:47:46 +01:00
return $self->{'backend_' . $self->{http_backend}}->get_code();
}
2015-07-28 18:17:04 +02:00
sub get_message {
my ($self, %options) = @_;
return $self->{'backend_' . $self->{http_backend}}->get_message();
}
2021-02-18 11:04:21 +01:00
sub get_certificate {
my ($self, %options) = @_;
return $self->{'backend_' . $self->{http_backend}}->get_certificate();
}
2019-03-06 16:47:46 +01:00
1;
2019-03-06 16:47:46 +01:00
__END__
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
=head1 NAME
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
HTTP abstraction layer.
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
=head1 SYNOPSIS
2019-03-06 16:47:46 +01:00
HTTP abstraction layer for lwp and curl backends
2018-01-17 14:49:37 +01:00
2019-03-06 16:47:46 +01:00
=head1 HTTP GLOBAL OPTIONS
2019-03-06 16:47:46 +01:00
=over 8
2019-03-06 16:47:46 +01:00
=item B<--http-peer-addr>
2019-03-06 16:47:46 +01:00
Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve)
2015-07-28 18:17:04 +02:00
2019-03-06 16:47:46 +01:00
=item B<--proxyurl>
2016-02-05 13:11:33 +01:00
2019-03-06 16:47:46 +01:00
Proxy URL
2016-02-05 13:11:33 +01:00
2019-03-06 16:47:46 +01:00
=item B<--proxypac>
Proxy pac file (can be an url or local file)
2020-12-16 11:44:33 +01:00
=item B<--insecure>
Insecure SSL connections.
2019-03-06 16:47:46 +01:00
=item B<--http-backend>
Set the backend used (Default: 'lwp')
For curl: --http-backend=curl
=back
=head1 DESCRIPTION
B<http>.
=cut