This commit is contained in:
Kevin Duret 2014-12-24 11:31:31 +01:00
commit df94122109
3 changed files with 71 additions and 47 deletions

View File

@ -39,6 +39,8 @@ package centreon::plugins::httplib;
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies;
use URI;
sub get_port {
my ($self, %options) = @_;
@ -56,16 +58,37 @@ sub get_port {
sub connect {
my ($self, %options) = @_;
my $ua = LWP::UserAgent->new( keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $self->{option_results}->{timeout});
my $method = defined($options{method}) ? $options{method} : 'GET';
my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown';
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));
}
my ($response, $content);
my $req;
my ($req, $url);
if (defined($self->{option_results}->{port}) && $self->{option_results}->{port} =~ /^[0-9]+$/) {
$req = HTTP::Request->new( GET => $self->{option_results}->{proto}. "://" . $self->{option_results}->{hostname}.':'. $self->{option_results}->{port} . $self->{option_results}->{url_path});
$url = $self->{option_results}->{proto}. "://" . $self->{option_results}->{hostname}.':'. $self->{option_results}->{port} . $self->{option_results}->{url_path};
} else {
$req = HTTP::Request->new( GET => $self->{option_results}->{proto}. "://" . $self->{option_results}->{hostname} . $self->{option_results}->{url_path});
$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);
if ($method eq 'POST') {
my $uri_post = URI->new();
if (defined($options{query_form_post})) {
$uri->query_form($options{query_form_post});
}
$req->content_type('application/x-www-form-urlencoded');
$req->content($uri_post->query);
}
if (defined($options{headers})) {

View File

@ -313,11 +313,11 @@ Allows to use regexp non case-sensitive (with --regexp).
=item B<--oid-filter>
Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint).
Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSMountPoint).
=item B<--oid-display>
Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint).
Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSMountPoint).
=item B<--display-transform-src>

View File

@ -165,27 +165,30 @@ sub run {
$self->{snmp}->load(oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed],
instances => $self->{storage_id_selected}, nothing_quit => 1);
my $result = $self->{snmp}->get_leef();
if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp})) {
my $multiple = 0;
if (scalar(@{$self->{storage_id_selected}}) > 1) {
$multiple = 1;
}
if ($multiple == 1) {
$self->{output}->output_add(severity => 'OK',
short_msg => 'All storages are ok.');
}
my $num_disk_check = 0;
foreach (sort @{$self->{storage_id_selected}}) {
# Skipped disks
my $storage_type = $self->{statefile_cache}->get(name => "hrstoragetype_" . $_);
next if (!defined($storage_type) ||
($storage_types_manage{$storage_type} !~ /$self->{option_results}->{filter_storage_type}/i));
my $name_storage = $self->get_display_value(id => $_);
$num_disk_check++;
# in bytes hrStorageAllocationUnits
my $total_size = $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_};
if ($total_size <= 0) {
$self->{output}->add_option_msg(long_msg => sprintf("Skipping storage '%d': total size is <= 0 (%s)",
$name_storage, int($total_size)));
if ($multiple == 0) {
$self->{output}->add_option_msg(severity => 'UNKNOWN',
short_msg => sprintf("Skipping storage '%d': total size is <= 0 (%s)",
$name_storage, int($total_size)));
} else {
$self->{output}->add_option_msg(long_msg => sprintf("Skipping storage '%d': total size is <= 0 (%s)",
$name_storage, int($total_size)));
}
next;
}
@ -223,7 +226,7 @@ sub run {
$total_size_value . " " . $total_size_unit,
$total_used_value . " " . $total_used_unit, $prct_used,
$total_free_value . " " . $total_free_unit, $prct_free));
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1) || (defined($self->{option_results}->{storage}) && !defined($self->{option_results}->{use_regexp}))) {
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1) || ($multiple == 0)) {
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Storage '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name_storage,
$total_size_value . " " . $total_size_unit,
@ -238,7 +241,7 @@ sub run {
$value_perf = $total_free;
}
my $extra_label = '';
$extra_label = '_' . $name_storage if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp}));
$extra_label = '_' . $name_storage if ($multiple == 1);
my %total_options = ();
if ($self->{option_results}->{units} eq '%') {
$total_options{total} = $total_size - $reserved_value;
@ -251,11 +254,6 @@ sub run {
min => 0, max => int($total_size - $reserved_value));
}
if ($num_disk_check == 0) {
$self->{output}->add_option_msg(short_msg => "Not a disk with a good 'type'.");
$self->{output}->option_exit();
}
$self->{output}->display();
$self->{output}->exit();
}
@ -306,6 +304,17 @@ sub reload_cache {
$self->{statefile_cache}->write(data => $datas);
}
sub filter_type {
my ($self, %options) = @_;
my $storage_type = $self->{statefile_cache}->get(name => "hrstoragetype_" . $options{id});
if (defined($storage_type) &&
($storage_types_manage{$storage_type} =~ /$self->{option_results}->{filter_storage_type}/i)) {
return 1;
}
return 0;
}
sub manage_selection {
my ($self, %options) = @_;
@ -329,41 +338,33 @@ sub manage_selection {
my $all_ids = $self->{statefile_cache}->get(name => 'all_ids');
if (!defined($self->{option_results}->{use_name}) && defined($self->{option_results}->{storage})) {
# get by ID
push @{$self->{storage_id_selected}}, $self->{option_results}->{storage};
my $name = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_display} . "_" . $self->{option_results}->{storage});
if (!defined($name)) {
$self->{output}->add_option_msg(short_msg => "No storage found for id '" . $self->{option_results}->{storage} . "'.");
$self->{output}->option_exit();
}
my $name = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_filter} . "_" . $self->{option_results}->{storage});
push @{$self->{storage_id_selected}}, $self->{option_results}->{storage} if (defined($name) && $self->filter_type(id => $self->{option_results}->{storage}));
} else {
foreach my $i (@{$all_ids}) {
my $filter_name = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_filter} . "_" . $i);
next if (!defined($filter_name));
if (!defined($self->{option_results}->{storage})) {
push @{$self->{storage_id_selected}}, $i;
push @{$self->{storage_id_selected}}, $i if ($self->filter_type(id => $i));
next;
}
if (defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/i) {
push @{$self->{storage_id_selected}}, $i;
push @{$self->{storage_id_selected}}, $i if ($self->filter_type(id => $i));
}
if (defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/) {
push @{$self->{storage_id_selected}}, $i;
push @{$self->{storage_id_selected}}, $i if ($self->filter_type(id => $i));
}
if (!defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name eq $self->{option_results}->{storage}) {
push @{$self->{storage_id_selected}}, $i;
push @{$self->{storage_id_selected}}, $i if ($self->filter_type(id => $i));
}
}
if (scalar(@{$self->{storage_id_selected}}) <= 0) {
if (defined($self->{option_results}->{storage})) {
$self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "' (maybe you should reload cache file).");
} else {
$self->{output}->add_option_msg(short_msg => "No storage found (maybe you should reload cache file).");
}
$self->{output}->option_exit();
}
}
}
if (scalar(@{$self->{storage_id_selected}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No storage found. Can be: filters, cache file.");
$self->{output}->option_exit();
}
}
sub get_display_value {
@ -423,11 +424,11 @@ Time in seconds before reloading cache file (default: 180).
=item B<--oid-filter>
Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint).
Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSMountPoint).
=item B<--oid-display>
Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint).
Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSMountPoint).
=item B<--display-transform-src>