2015-04-24 13:40:13 +02:00
|
|
|
#
|
2015-07-21 11:51:02 +02:00
|
|
|
# Copyright 2015 Centreon (http://www.centreon.com/)
|
2015-04-24 13:40:13 +02:00
|
|
|
#
|
2015-07-21 11:51:02 +02:00
|
|
|
# Centreon is a full-fledged industry-strength solution that meets
|
|
|
|
# the needs in IT infrastructure and application monitoring for
|
|
|
|
# service performance.
|
2015-04-24 13:40:13 +02:00
|
|
|
#
|
2015-07-21 11:51:02 +02:00
|
|
|
# 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
|
2015-04-24 13:40:13 +02:00
|
|
|
#
|
2015-07-21 11:51:02 +02:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-24 13:40:13 +02:00
|
|
|
#
|
2015-07-21 11:51:02 +02:00
|
|
|
# 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.
|
2015-04-24 13:40:13 +02:00
|
|
|
#
|
2014-03-18 22:19:49 +01:00
|
|
|
|
2014-04-17 11:16:45 +02:00
|
|
|
package centreon::plugins::httplib;
|
2014-03-18 22:19:49 +01:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use LWP::UserAgent;
|
2014-12-24 11:28:43 +01:00
|
|
|
use HTTP::Cookies;
|
|
|
|
use URI;
|
2015-07-16 14:26:32 +02:00
|
|
|
use IO::Socket::SSL;
|
2014-03-18 22:19:49 +01:00
|
|
|
|
2014-05-09 17:52:44 +02:00
|
|
|
sub get_port {
|
|
|
|
my ($self, %options) = @_;
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-05-09 17:52:44 +02:00
|
|
|
my $cache_port = '';
|
|
|
|
if (defined($self->{option_results}->{port}) && $self->{option_results}->{port} ne '') {
|
|
|
|
$cache_port = $self->{option_results}->{port};
|
|
|
|
} else {
|
|
|
|
$cache_port = 80 if ($self->{option_results}->{proto} eq 'http');
|
|
|
|
$cache_port = 443 if ($self->{option_results}->{proto} eq 'https');
|
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-05-09 17:52:44 +02:00
|
|
|
return $cache_port;
|
|
|
|
}
|
|
|
|
|
2014-03-18 22:19:49 +01:00
|
|
|
sub connect {
|
|
|
|
my ($self, %options) = @_;
|
2014-12-24 11:28:43 +01:00
|
|
|
my $method = defined($options{method}) ? $options{method} : 'GET';
|
2014-03-19 17:03:13 +01:00
|
|
|
my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown';
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-12-24 11:28:43 +01:00
|
|
|
my $ua = LWP::UserAgent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $self->{option_results}->{timeout},
|
|
|
|
requests_redirectable => [ 'GET', 'HEAD', 'POST' ]);
|
|
|
|
if (defined($options{cookies_file})) {
|
|
|
|
$ua->cookie_jar(HTTP::Cookies->new(file => $options{cookies_file},
|
|
|
|
autosave => 1));
|
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-03-27 20:17:38 +01:00
|
|
|
my ($response, $content);
|
2014-12-24 11:28:43 +01:00
|
|
|
my ($req, $url);
|
2014-04-24 12:53:48 +02:00
|
|
|
if (defined($self->{option_results}->{port}) && $self->{option_results}->{port} =~ /^[0-9]+$/) {
|
2014-12-24 11:28:43 +01:00
|
|
|
$url = $self->{option_results}->{proto}. "://" . $self->{option_results}->{hostname}.':'. $self->{option_results}->{port} . $self->{option_results}->{url_path};
|
2014-04-24 12:06:10 +02:00
|
|
|
} else {
|
2014-12-24 11:28:43 +01:00
|
|
|
$url = $self->{option_results}->{proto}. "://" . $self->{option_results}->{hostname} . $self->{option_results}->{url_path};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $uri = URI->new($url);
|
|
|
|
if (defined($options{query_form_get})) {
|
|
|
|
$uri->query_form($options{query_form_get});
|
|
|
|
}
|
|
|
|
$req = HTTP::Request->new($method => $uri);
|
|
|
|
|
2015-03-02 14:09:20 +01:00
|
|
|
my $content_type_forced;
|
2014-07-24 17:37:33 +02:00
|
|
|
if (defined($options{headers})) {
|
|
|
|
foreach my $key (keys %{$options{headers}}) {
|
2015-03-02 14:09:20 +01:00
|
|
|
if ($key !~ /content-type/i) {
|
|
|
|
$req->header($key => $options{headers}->{$key});
|
|
|
|
} else {
|
|
|
|
$content_type_forced = $options{headers}->{$key};
|
|
|
|
}
|
2014-07-24 17:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2015-03-02 14:09:20 +01:00
|
|
|
if ($method eq 'POST') {
|
|
|
|
if (defined($content_type_forced)) {
|
|
|
|
$req->content_type($content_type_forced);
|
|
|
|
$req->content($options{query_form_post});
|
|
|
|
} else {
|
|
|
|
my $uri_post = URI->new();
|
|
|
|
if (defined($options{query_form_post})) {
|
|
|
|
$uri_post->query_form($options{query_form_post});
|
|
|
|
}
|
|
|
|
$req->content_type('application/x-www-form-urlencoded');
|
|
|
|
$req->content($uri_post->query);
|
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 10:35:06 +02:00
|
|
|
if (defined($self->{option_results}->{credentials}) && defined($self->{option_results}->{ntlm})) {
|
|
|
|
$ua->credentials($self->{option_results}->{hostname} . ':' . $self->{option_results}->{port}, '', $self->{option_results}->{username}, $self->{option_results}->{password});
|
|
|
|
} elsif (defined($self->{option_results}->{credentials})) {
|
2014-03-27 20:17:38 +01:00
|
|
|
$req->authorization_basic($self->{option_results}->{username}, $self->{option_results}->{password});
|
2014-03-18 22:19:49 +01:00
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-03-27 20:17:38 +01:00
|
|
|
if (defined($self->{option_results}->{proxyurl})) {
|
2014-04-17 11:16:45 +02:00
|
|
|
$ua->proxy(['http', 'https'], $self->{option_results}->{proxyurl});
|
2014-03-18 22:19:49 +01:00
|
|
|
}
|
2015-03-12 11:54:23 +01:00
|
|
|
|
2015-04-24 13:40:13 +02:00
|
|
|
if (defined($self->{option_results}->{cert_pkcs12}) && $self->{option_results}->{cert_file} ne '' && $self->{option_results}->{cert_pwd} ne '') {
|
|
|
|
eval "use Net::SSL"; die $@ if $@;
|
|
|
|
$ENV{HTTPS_PKCS12_FILE} = $self->{option_results}->{cert_file};
|
|
|
|
$ENV{HTTPS_PKCS12_PASSWORD} = $self->{option_results}->{cert_pwd};
|
|
|
|
}
|
|
|
|
|
2015-07-16 14:26:32 +02:00
|
|
|
my $ssl_context;
|
|
|
|
if (defined($self->{option_results}->{ssl}) && $self->{option_results}->{ssl} ne '') {
|
|
|
|
$ssl_context = { SSL_version => $self->{option_results}->{ssl} };
|
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
if (defined($self->{option_results}->{cert_file}) && !defined($self->{option_results}->{cert_pkcs12})) {
|
2015-07-16 14:26:32 +02:00
|
|
|
$ssl_context = {} if (!defined($ssl_context));
|
|
|
|
$ssl_context->{SSL_use_cert} = 1;
|
|
|
|
$ssl_context->{SSL_cert_file} = $self->{option_results}->{cert_file};
|
|
|
|
$ssl_context->{SSL_key_file} = $self->{option_results}->{key_file} if (defined($self->{option_results}->{key_file}));
|
|
|
|
$ssl_context->{SSL_ca_file} = $self->{option_results}->{cacert_file} if (defined($self->{option_results}->{cacert_file}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($ssl_context)) {
|
|
|
|
my $context = new IO::Socket::SSL::SSL_Context(%{$ssl_context});
|
2015-07-01 18:14:07 +02:00
|
|
|
IO::Socket::SSL::set_default_context($context);
|
2015-04-24 13:40:13 +02:00
|
|
|
}
|
|
|
|
|
2014-03-27 20:17:38 +01:00
|
|
|
$response = $ua->request($req);
|
2014-03-18 22:19:49 +01:00
|
|
|
|
2014-03-27 20:17:38 +01:00
|
|
|
if ($response->is_success) {
|
2014-03-18 22:19:49 +01:00
|
|
|
$content = $response->content;
|
|
|
|
return $content;
|
2014-03-27 20:17:38 +01:00
|
|
|
}
|
2015-04-24 13:40:13 +02:00
|
|
|
|
2014-03-27 20:17:38 +01:00
|
|
|
$self->{output}->output_add(severity => $connection_exit,
|
2015-04-24 13:40:13 +02:00
|
|
|
short_msg => $response->status_line);
|
2014-03-27 20:17:38 +01:00
|
|
|
$self->{output}->display();
|
|
|
|
$self->{output}->exit();
|
2014-03-18 22:19:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|