From a77e1e0bd10aa53a83aaf96021ff5d905d67d203 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 24 Sep 2019 14:50:37 +0200 Subject: [PATCH] add vdisks mode for p2000 hp plugin --- storage/hp/p2000/xmlapi/custom.pm | 41 ++++- storage/hp/p2000/xmlapi/mode/vdisks.pm | 212 +++++++++++++++++++++++++ storage/hp/p2000/xmlapi/plugin.pm | 1 + 3 files changed, 246 insertions(+), 8 deletions(-) create mode 100644 storage/hp/p2000/xmlapi/mode/vdisks.pm diff --git a/storage/hp/p2000/xmlapi/custom.pm b/storage/hp/p2000/xmlapi/custom.pm index 92de80eda..1198707d7 100644 --- a/storage/hp/p2000/xmlapi/custom.pm +++ b/storage/hp/p2000/xmlapi/custom.pm @@ -49,6 +49,9 @@ sub new { 'username:s@' => { name => 'username' }, 'password:s@' => { name => 'password' }, 'timeout:s@' => { name => 'timeout' }, + 'unknown-http-status:s' => { name => 'unknown_http_status' }, + 'warning-http-status:s' => { name => 'warning_http_status' }, + 'critical-http-status:s' => { name => 'critical_http_status' }, }); } $options{options}->add_help(package => __PACKAGE__, sections => 'P2000 OPTIONS', once => 1); @@ -96,6 +99,9 @@ sub check_options { $self->{port} = (defined($self->{option_results}->{port})) ? shift(@{$self->{option_results}->{port}}) : undef; $self->{proto} = (defined($self->{option_results}->{proto})) ? shift(@{$self->{option_results}->{proto}}) : 'http'; $self->{url_path} = (defined($self->{option_results}->{url_path})) ? shift(@{$self->{option_results}->{url_path}}) : '/api/'; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300' ; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; if (!defined($self->{hostname})) { $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.'); @@ -164,9 +170,16 @@ sub DESTROY { my $self = shift; if ($self->{logon} == 1) { - $self->{http}->request(url_path => $self->{url_path} . 'exit', - header => ['Cookie: wbisessionkey=' . $self->{session_id} . '; wbiusername=' . $self->{username}, - 'dataType: api', 'sessionKey: '. $self->{session_id}]); + $self->{http}->request( + url_path => $self->{url_path} . 'exit', + header => [ + 'Cookie: wbisessionkey=' . $self->{session_id} . '; wbiusername=' . $self->{username}, + 'dataType: api', 'sessionKey: '. $self->{session_id} + ], + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status}, + ); } } @@ -177,10 +190,17 @@ sub get_infos { $self->login(); my $cmd = $options{cmd}; $cmd =~ s/ /\//g; - my $response = $self->{http}->request(url_path => $self->{url_path} . $cmd, - header => ['Cookie: wbisessionkey=' . $self->{session_id} . '; wbiusername=' . $self->{username}, - 'dataType: api', 'sessionKey: '. $self->{session_id}]); - + my $response = $self->{http}->request( + url_path => $self->{url_path} . $cmd, + header => [ + 'Cookie: wbisessionkey=' . $self->{session_id} . '; wbiusername=' . $self->{username}, + 'dataType: api', 'sessionKey: '. $self->{session_id} + ], + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status}, + ); + eval { $xpath = XML::XPath->new(xml => $response); $nodeset = $xpath->find("//OBJECT[\@basetype='" . $options{base_type} . "']"); @@ -246,7 +266,12 @@ sub login { # Login First my $md5_hash = md5_hex($self->{username} . '_' . $self->{password}); - my $response = $self->{http}->request(url_path => $self->{url_path} . 'login/' . $md5_hash); + my $response = $self->{http}->request( + url_path => $self->{url_path} . 'login/' . $md5_hash, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status}, + ); $self->check_login(content => $response); } diff --git a/storage/hp/p2000/xmlapi/mode/vdisks.pm b/storage/hp/p2000/xmlapi/mode/vdisks.pm new file mode 100644 index 000000000..2d81ae006 --- /dev/null +++ b/storage/hp/p2000/xmlapi/mode/vdisks.pm @@ -0,0 +1,212 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# 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 storage::hp::p2000::xmlapi::mode::vdisks; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = 'status : ' . $self->{result_values}->{status}; + return $msg; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space_absolute}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space_absolute}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space_absolute}); + my $msg = sprintf('space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)', + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space_absolute}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space_absolute} + ); + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'vdisk', type => 1, cb_prefix_output => 'prefix_vdisk_output', message_multiple => 'All virtual disks are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{vdisk} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'display' } ], + closure_custom_calc => \&catalog_status_calc, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + { label => 'usage', nlabel => 'vdisk.space.usage.bytes', set => { + key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { value => 'used_space_absolute', template => '%d', min => 0, max => 'total_space_absolute', + unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'usage-free', nlabel => 'vdisk.space.free.bytes', display_ok => 0, set => { + key_values => [ { name => 'free_space' }, { name => 'used_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { value => 'free_space_absolute', template => '%d', min => 0, max => 'total_space_absolute', + unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'usage-prct', nlabel => 'vdisk.space.usage.percentage', display_ok => 0, set => { + key_values => [ { name => 'prct_used_space' }, { name => 'display' } ], + output_template => 'used : %.2f %%', + perfdatas => [ + { value => 'prct_used_space_absolute', template => '%.2f', min => 0, max => 100, + unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' }, + 'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /unknown/i' }, + 'warning-status:s' => { name => 'warning_status', default => '%{status} =~ /degraded/i' }, + 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /failed/i' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); +} + +sub prefix_vdisk_output { + my ($self, %options) = @_; + + return "Virtual disk '" . $options{instance_value}->{display} . "' "; +} + +sub manage_selection { + my ($self, %options) = @_; + + my ($result) = $options{custom}->get_infos( + cmd => 'show vdisks', + base_type => 'virtual-disks', + key => 'name', + properties_name => '^(?:health-numeric|size-numeric|freespace-numeric)$' + ); + + my %health = ( + 0 => 'ok', + 1 => 'degraded', + 2 => 'failed', + 3 => 'unknown', + 4 => 'not available', + ); + + $self->{vdisk} = {}; + foreach my $name (keys %$result) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping virtual disk '" . $name . "': no matching filter.", debug => 1); + next; + } + + $result->{$name}->{'size-numeric'} *= 512; + $result->{$name}->{'freespace-numeric'} *= 512; + $self->{vdisk}->{$name} = { + display => $name, + status => $health{ $result->{$name}->{'health-numeric'} }, + total_space => $result->{$name}->{'size-numeric'}, + used_space => $result->{$name}->{'size-numeric'} - $result->{$name}->{'freespace-numeric'}, + free_space => $result->{$name}->{'freespace-numeric'}, + prct_used_space => + defined($result->{$name}->{'size-numeric'}) ? (($result->{$name}->{'size-numeric'} - $result->{$name}->{'freespace-numeric'}) * 100 / $result->{$name}->{'size-numeric'}) : undef, + prct_free_space => + defined($result->{$name}->{'size-numeric'}) ? ($result->{$name}->{'freespace-numeric'} * 100 / $result->{$name}->{'size-numeric'}) : undef, + }; + } + + if (scalar(keys %{$self->{vdisk}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No virtual disk found"); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check virtual disks. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^usage$' + +=item B<--filter-name> + +Filter virtual disk name (can be a regexp). + +=item B<--unknown-status> + +Set unknon threshold for status (Default: '%{status} =~ /unknown/i'). +Can used special variables like: %{status}, %{display} + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /degraded/i'). +Can used special variables like: %{status}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /failed/i'). +Can used special variables like: %{status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + +=back + +=cut diff --git a/storage/hp/p2000/xmlapi/plugin.pm b/storage/hp/p2000/xmlapi/plugin.pm index 327229683..e7c1e194a 100644 --- a/storage/hp/p2000/xmlapi/plugin.pm +++ b/storage/hp/p2000/xmlapi/plugin.pm @@ -34,6 +34,7 @@ sub new { %{$self->{modes}} = ( 'health' => 'storage::hp::p2000::xmlapi::mode::health', 'list-volumes' => 'storage::hp::p2000::xmlapi::mode::listvolumes', + 'vdisks' => 'storage::hp::p2000::xmlapi::mode::vdisks', 'volume-stats' => 'storage::hp::p2000::xmlapi::mode::volumesstats', ); $self->{custom_modes}{p2000xml} = 'storage::hp::p2000::xmlapi::custom';