From be5b649e57e6560c32223ff7b49ba5bb85500f89 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Mon, 3 Jan 2022 15:22:28 +0100 Subject: [PATCH] add(plugin): HPE Simplivity Rest API --- .../hpe/simplivity/restapi/custom/api.pm | 333 ++++++++++++++++++ .../hpe/simplivity/restapi/mode/discovery.pm | 151 ++++++++ .../hpe/simplivity/restapi/mode/hosts.pm | 325 +++++++++++++++++ .../hpe/simplivity/restapi/mode/listhosts.pm | 117 ++++++ .../restapi/mode/omnistackclusters.pm | 202 +++++++++++ .../restapi/mode/virtualmachines.pm | 195 ++++++++++ .../hpe/simplivity/restapi/plugin.pm | 57 +++ network/microsens/g6/snmp/mode/sfp.pm | 2 +- 8 files changed, 1381 insertions(+), 1 deletion(-) create mode 100644 apps/virtualization/hpe/simplivity/restapi/custom/api.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/mode/discovery.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/mode/hosts.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/mode/listhosts.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/mode/omnistackclusters.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/mode/virtualmachines.pm create mode 100644 apps/virtualization/hpe/simplivity/restapi/plugin.pm diff --git a/apps/virtualization/hpe/simplivity/restapi/custom/api.pm b/apps/virtualization/hpe/simplivity/restapi/custom/api.pm new file mode 100644 index 000000000..56d39e9ca --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/custom/api.pm @@ -0,0 +1,333 @@ +# +# Copyright 2021 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. +# Authors : Roman Morandell - ivertix +# + +package apps::virtualization::hpe::simplivity::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use Digest::MD5 qw(md5_hex); + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'timeout:s' => { name => 'timeout' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'OMNISTACK REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->new(%options); + $self->{cache} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.'); + $self->{output}->option_exit(); + } + if ($self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if ($self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + $self->{cache}->check_options(option_results => $self->{option_results}); + return 0; +} + +sub get_connection_infos { + my ($self, %options) = @_; + + return $self->{hostname} . '_' . $self->{http}->get_port(); +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub json_decode { + my ($self, %options) = @_; + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($options{content}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub clean_token { + my ($self, %options) = @_; + + my $datas = {}; + $options{statefile}->write(data => $datas); + $self->{access_token} = undef; + $self->{http}->add_header(key => 'Authorization', value => undef); +} + +sub get_auth_token { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'hpe_simplivity_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username})); + my $access_token = $options{statefile}->get(name => 'access_token'); + my $expires_on = $options{statefile}->get(name => 'expires_on'); + my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret'); + my $md5_secret = md5_hex($self->{api_username} . $self->{api_password}); + + if ($has_cache_file == 0 || !defined($access_token) || (time() > $expires_on) || + (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)) { + my ($content) = $self->{http}->request( + method => 'POST', + url_path => '/api/oauth/token', + post_param => [ + 'grant_type=password', + 'username=' . $self->{api_username}, + 'password=' . $self->{api_password} + ], + warning_status => '', + unknown_status => '', + critical_status => '' + ); + + if ($self->{http}->get_code() != 200) { + $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded = $self->json_decode(content => $content); + if (!defined($decoded->{access_token})) { + $self->{output}->add_option_msg(short_msg => "Cannot get token"); + $self->{output}->option_exit(); + } + + $access_token = $decoded->{access_token}; + my $datas = { + access_token => $access_token, + expires_on => time() + $decoded->{expires_in}, + md5_secret => $md5_secret + }; + $options{statefile}->write(data => $datas); + } + + $self->{access_token} = $access_token; + $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token}); +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + if (!defined($self->{access_token})) { + $self->get_auth_token(statefile => $self->{cache}); + } + + my $content = $self->{http}->request( + method => 'GET', + url_path => $options{endpoint}, + get_param => $options{get_param}, + warning_status => '', + unknown_status => '', + critical_status => '' + ); + + # Maybe there is an issue with the token. So we retry. + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->clean_token(statefile => $self->{cache}); + $self->get_auth_token(statefile => $self->{cache}); + $content = $self->{http}->request( + url_path => $options{endpoint}, + get_param => $options{get_param}, + warning_status => '', unknown_status => '', critical_status => '' + ); + } + + my $decoded = $self->json_decode(content => $content); + if (!defined($decoded)) { + $self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)'); + $self->{output}->option_exit(); + } + + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + my $message = 'api request error'; + if (defined($decoded->{message})) { + $message .= ': ' . $decoded->{message}; + } + $self->{output}->add_option_msg(short_msg => $message); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub get_hosts { + my ($self, %options) = @_; + + return $self->request_api( + endpoint => '/api/hosts', + get_param => ['show_optional_fields=true', 'offset=0', 'limit=5000'] + ); +} + +sub get_host_hardware { + my ($self, %options) = @_; + + return $self->request_api( + endpoint => '/api/hosts/' . $options{id} . '/hardware', + get_param => [] + ); +} + +sub get_omnistack_clusters { + my ($self, %options) = @_; + + return $self->request_api( + endpoint => '/api/omnistack_clusters', + get_param => ['show_optional_fields=true', 'offset=0', 'limit=5000'] + ); +} + +sub get_virtual_machines { + my ($self, %options) = @_; + + return $self->request_api( + endpoint => '/api/virtual_machines', + get_param => ['show_optional_fields=true', 'offset=0', 'limit=5000'] + ); +} + +1; + +__END__ + +=head1 NAME + +OmniStack API + +=head1 SYNOPSIS + +OmniStack api + +=head1 OMNISTACK REST API OPTIONS + +=over 8 + +=item B<--hostname> + +OmniStack API hostname. + +=item B<--port> + +OmniStack API port (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +OmniStack API username + +=item B<--api-password> + +OmniStack API password + +=item B<--timeout> + +Set HTTP timeout + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/mode/discovery.pm b/apps/virtualization/hpe/simplivity/restapi/mode/discovery.pm new file mode 100644 index 000000000..3cb174844 --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/mode/discovery.pm @@ -0,0 +1,151 @@ +# +# Copyright 2021 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 apps::virtualization::hpe::simplivity::restapi::mode::discovery; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use JSON::XS; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'resource-type:s' => { name => 'resource_type' }, + 'prettify' => { name => 'prettify' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') { + $self->{option_results}->{resource_type} = 'host'; + } + if ($self->{option_results}->{resource_type} !~ /^host|vm$/) { + $self->{output}->add_option_msg(short_msg => 'unknown resource type'); + $self->{output}->option_exit(); + } +} + +sub discovery_vm { + my ($self, %options) = @_; + + my $vms = $options{custom}->get_virtual_machines(); + + my $disco_data = []; + foreach my $vm (@{$vms->{virtual_machines}}) { + my $node = {}; + $node->{id} = $vm->{id}; + $node->{name} = $vm->{name}; + $node->{host_name} = $vm->{host_name}; + $node->{state} = lc($vm->{state}); + $node->{omnistack_cluster_name} = $vm->{omnistack_cluster_name}; + + push @$disco_data, $node; + } + + return $disco_data; +} + +sub discovery_host { + my ($self, %options) = @_; + + my $hosts = $options{custom}->get_hosts(); + + my $disco_data = []; + foreach my $host (@{$hosts->{hosts}}) { + my $node = {}; + $node->{uuid} = $host->{id}; + $node->{name} = $host->{name}; + $node->{model} = $host->{model}; + $node->{state} = lc($host->{state}); + $node->{management_ip} = $host->{management_ip}; + $node->{omnistack_cluster_name} = $host->{omnistack_cluster_name}; + + push @$disco_data, $node; + } + + return $disco_data; +} + +sub run { + my ($self, %options) = @_; + + my $disco_stats; + $disco_stats->{start_time} = time(); + + my $results = []; + if ($self->{option_results}->{resource_type} eq 'vm') { + $results = $self->discovery_vm( + custom => $options{custom} + ); + } else { + $results = $self->discovery_host( + custom => $options{custom} + ); + } + + $disco_stats->{end_time} = time(); + $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; + $disco_stats->{discovered_items} = scalar(@$results); + $disco_stats->{results} = $results; + + my $encoded_data; + eval { + if (defined($self->{option_results}->{prettify})) { + $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats); + } else { + $encoded_data = JSON::XS->new->utf8->encode($disco_stats); + } + }; + if ($@) { + $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; + } + + $self->{output}->output_add(short_msg => $encoded_data); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Resources discovery. + +=over 8 + +=item B<--resource-type> + +Choose the type of resources to discover (Can be: 'vm', 'host'). + +=back + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/mode/hosts.pm b/apps/virtualization/hpe/simplivity/restapi/mode/hosts.pm new file mode 100644 index 000000000..106f70171 --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/mode/hosts.pm @@ -0,0 +1,325 @@ +# +# Copyright 2021 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. +# Authors : Roman Morandell - ivertix +# + +package apps::virtualization::hpe::simplivity::restapi::mode::hosts; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'number of hosts '; +} + +sub prefix_components_output { + my ($self, %options) = @_; + + return 'number of components '; +} + +sub host_long_output { + my ($self, %options) = @_; + + return sprintf( + "checking host '%s'", + $options{instance} + ); +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return sprintf( + "host '%s' ", + $options{instance} + ); +} + +sub prefix_ldrive_output { + my ($self, %options) = @_; + + return "logical drive '" . $options{instance} . "' "; +} + +sub prefix_pdrive_output { + my ($self, %options) = @_; + + return "physical drive '" . $options{instance} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, + { name => 'hosts', type => 3, cb_prefix_output => 'prefix_host_output', cb_long_output => 'host_long_output', indent_long_output => ' ', message_multiple => 'All hosts are ok', + group => [ + { name => 'host_status', type => 0, skipped_code => { -10 => 1 } }, + { name => 'components', type => 0, cb_prefix_output => 'prefix_components_output', skipped_code => { -10 => 1 } }, + { name => 'raid', type => 0, skipped_code => { -10 => 1 } }, + { name => 'ldrives', display_long => 1, cb_prefix_output => 'prefix_ldrive_output', message_multiple => 'logical drives are ok', type => 1, skipped_code => { -10 => 1 } }, + { name => 'pdrives', display_long => 1, cb_prefix_output => 'prefix_pdrive_output', message_multiple => 'physical drives are ok', type => 1, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{global} = []; + foreach ('alive', 'faulty', 'managed', 'removed', 'suspected', 'unknown') { + push @{$self->{maps_counters}->{global}}, { + label => 'hosts-' . $_, nlabel => 'hosts.' . $_ . '.count', display_ok => 0, set => { + key_values => [ { name => $_ } ], + output_template => $_ . ': %s', + perfdatas => [ + { template => '%s' } + ] + } + }; + } + + $self->{maps_counters}->{components} = []; + foreach ('green', 'yellow', 'red', 'unknown') { + push @{$self->{maps_counters}->{components}}, { + label => 'host-components-' . $_, nlabel => 'host.components.' . $_ . '.count', display_ok => 0, set => { + key_values => [ { name => $_ } ], + output_template => $_ . ': %s', + perfdatas => [ + { template => '%s', label_extra_instance => 1 } + ] + } + }; + } + + $self->{maps_counters}->{host_status} = [ + { + label => 'host-status', + type => 2, + unknown_default => '%{status} =~ /unknown/', + warning_default => '%{status} =~ /suspected/', + critical_default => '%{status} =~ /faulty/', + set => { + key_values => [ { name => 'status' }, { name => 'name' } ], + output_template => 'status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{raid} = [ + { + label => 'raid-status', + type => 2, + unknown_default => '%{status} =~ /unknown/', + warning_default => '%{status} =~ /yellow/', + critical_default => '%{status} =~ /red/', + set => { + key_values => [ { name => 'status' }, { name => 'name' } ], + output_template => 'raid card status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{ldrives} = [ + { + label => 'logical-drive-status', + type => 2, + unknown_default => '%{status} =~ /unknown/', + warning_default => '%{status} =~ /yellow/', + critical_default => '%{status} =~ /red/', + set => { + key_values => [ { name => 'status' }, { name => 'name' } ], + output_template => 'status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{pdrives} = [ + { + label => 'physical-drive-status', + type => 2, + unknown_default => '%{status} =~ /unknown/', + warning_default => '%{status} =~ /yellow/', + critical_default => '%{status} =~ /red/', + set => { + key_values => [ { name => 'status' }, { name => 'name' } ], + output_template => 'status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +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' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $hosts = $options{custom}->get_hosts(); + + $self->{global} = { alive => 0, faulty => 0, managed => 0, removed => 0, suspected => 0, unknown => 0 }; + $self->{hosts} = {}; + foreach my $host (@{$hosts->{hosts}}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $host->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $host->{name} . "': no matching filter.", debug => 1); + next; + } + + $host->{state} = lc($host->{state}); + $self->{global}->{ $host->{state} }++; + $self->{hosts}->{ $host->{name} } = { + host_status => { status => $host->{state}, name => $host->{name} }, + components => { green => 0, yellow => 0, red => 0, unknown => 0 }, + ldrives => {}, + pdrives => {} + }; + + my $hw = $options{custom}->get_host_hardware(id => $host->{id}); + $self->{hosts}->{ $host->{name} }->{raid} = { + status => lc($hw->{host}->{raid_card}->{status}) + }; + $self->{hosts}->{ $host->{name} }->{components}->{ lc($hw->{host}->{raid_card}->{status}) }++; + + foreach my $ldrive (@{$hw->{host}->{logical_drives}}) { + $ldrive->{name} = $1 if ($ldrive->{name} =~ /Logical\s+Drive\s+(\d+)/); + $self->{hosts}->{ $host->{name} }->{components}->{ lc($ldrive->{status}) }++; + $self->{hosts}->{ $host->{name} }->{ldrives}->{ $ldrive->{name} } = { + name => $ldrive->{name}, + status => lc($ldrive->{status}) + }; + + foreach my $entry (@{$ldrive->{drive_sets}}) { + foreach my $pdrive (@{$entry->{physical_drives}}) { + $self->{hosts}->{ $host->{name} }->{components}->{ lc($pdrive->{status}) }++; + my $name = $ldrive->{name} . ':' . $pdrive->{drive_position}; + $self->{hosts}->{ $host->{name} }->{pdrives}->{$name} = { + name => $name, + status => lc($pdrive->{status}) + }; + } + } + } + } +} + + +1; + +__END__ + +=head1 MODE + +Check hosts. + +=over 8 + +=item B<--filter-name> + +Filter hosts by name. + +=item B<--unknown-host-status> + +Set unknown threshold for status (Default: '%{status} =~ /unknown/'). +Can used special variables like: %{status}, %{name} + +=item B<--warning-host-status> + +Set warning threshold for status (Default: '%{status} =~ /suspected/'). +Can used special variables like: %{status}, %{name} + +=item B<--critical-host-status> + +Set critical threshold for status (Default: '%{status} =~ /faulty/'). +Can used special variables like: %{status}, %{name} + +=item B<--unknown-raid-status> + +Set unknown threshold for component status (Default: '%{status} =~ /unknown/'). +Can used special variables like: %{status}, %{name} + +=item B<--warning-raid-status> + +Set warning threshold for component status (Default: '%{status} =~ /yellow/'). +Can used special variables like: %{status}, %{name} + +=item B<--critical-raid-status> + +Set critical threshold for component status (Default: '%{status} =~ /red/'). +Can used special variables like: %{status}, %{name} + +=item B<--unknown-logical-drive-status> + +Set unknown threshold for component status (Default: '%{status} =~ /unknown/'). +Can used special variables like: %{status}, %{name} + +=item B<--warning-logical-drive-status> + +Set warning threshold for component status (Default: '%{status} =~ /yellow/'). +Can used special variables like: %{status}, %{name} + +=item B<--critical-logical-drive-status> + +Set critical threshold for component status (Default: '%{status} =~ /red/'). +Can used special variables like: %{status}, %{name} + +=item B<--unknown-physical-drive-status> + +Set unknown threshold for component status (Default: '%{status} =~ /unknown/'). +Can used special variables like: %{status}, %{name} + +=item B<--warning-physical-drive-status> + +Set warning threshold for component status (Default: '%{status} =~ /yellow/'). +Can used special variables like: %{status}, %{name} + +=item B<--critical-physical-drive-status> + +Set critical threshold for component status (Default: '%{status} =~ /red/'). +Can used special variables like: %{status}, %{name} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'hosts-alive', 'hosts-faulty', 'hosts-managed', 'hosts-removed', 'hosts-suspected', 'hosts-unknown', +'host-components-green', 'host-components-yellow', 'host-components-red', 'host-components-unknown'. + +=back + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/mode/listhosts.pm b/apps/virtualization/hpe/simplivity/restapi/mode/listhosts.pm new file mode 100644 index 000000000..96d8d3e0e --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/mode/listhosts.pm @@ -0,0 +1,117 @@ +# +# Copyright 2021 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 apps::virtualization::hpe::simplivity::restapi::mode::listhosts; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $hosts = $options{custom}->get_hosts(); + my $results = []; + foreach my $host (@{$hosts->{hosts}}) { + next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $host->{name} !~ /$self->{option_results}->{filter_name}/); + push @$results, { + id => $host->{id}, + name => $host->{name}, + status => lc($host->{state}) + }; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $hosts = $self->manage_selection(%options); + foreach my $host (@$hosts) { + $self->{output}->output_add( + long_msg => sprintf( + "[id: %s][name: %s][status: %s]", + $host->{id}, + $host->{name}, + $host->{status} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List hosts:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['id', 'name', 'status']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $hosts = $self->manage_selection(%options); + foreach my $host (@$hosts) { + $self->{output}->add_disco_entry(%$host); + } +} + +1; + +__END__ + +=head1 MODE + +List hosts. + +=over 8 + +=item B<--filter-name> + +Filter host name (Can be a regexp). + +=back + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/mode/omnistackclusters.pm b/apps/virtualization/hpe/simplivity/restapi/mode/omnistackclusters.pm new file mode 100644 index 000000000..fe3efe9dc --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/mode/omnistackclusters.pm @@ -0,0 +1,202 @@ +# +# Copyright 2021 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. +# Authors : Roman Morandell - ivertix +# + +package apps::virtualization::hpe::simplivity::restapi::mode::omnistackclusters; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub custom_space_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space}); + return 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}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space} + ); +} + +sub prefix_ratio_output { + my ($self, %options) = @_; + + return 'ratio '; +} + +sub cluster_long_output { + my ($self, %options) = @_; + + return sprintf( + "checking cluster '%s'", + $options{instance}, + ); +} + +sub prefix_cluster_output { + my ($self, %options) = @_; + + return sprintf( + "cluster '%s' ", + $options{instance} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'clusters', type => 3, cb_prefix_output => 'prefix_cluster_output', cb_long_output => 'cluster_long_output', indent_long_output => ' ', message_multiple => 'All omnistack clusters are ok', + group => [ + { name => 'ratio', type => 0, cb_prefix_output => 'prefix_ratio_output', skipped_code => { -10 => 1 } }, + { name => 'space', type => 0, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{space} = [ + { label => 'space-usage', nlabel => 'omnistack_cluster.space.usage.bytes', set => { + key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'space-usage-free', nlabel => 'omnistack_cluster.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' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'space-usage-prct', nlabel => 'omnistack_cluster.space.usage.percentage', display_ok => 0, set => { + key_values => [ { name => 'prct_used_space' }, { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + } + ]; + + $self->{maps_counters}->{ratio} = [ + { label => 'ratio-deduplication', nlabel => 'omnistack_cluster.ratio.deduplication.count', set => { + key_values => [ { name => 'deduplication' } ], + output_template => 'deduplication: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => 'ratio-compression', nlabel => 'omnistack_cluster.ratio.compression.count', set => { + key_values => [ { name => 'compression' } ], + output_template => 'compression: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => 'ratio-efficiency', nlabel => 'omnistack_cluster.ratio.efficiency.count', set => { + key_values => [ { name => 'efficiency' } ], + output_template => 'efficiency: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + } + ]; +} + +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' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $clusters = $options{custom}->get_omnistack_clusters(); + + $self->{clusters} = {}; + foreach my $cluster (@{$clusters->{omnistack_clusters}}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $cluster->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $cluster->{name} . "': no matching filter.", debug => 1); + next; + } + + $self->{clusters}->{ $cluster->{name} } = { + ratio => {}, + space => { + total_space => $cluster->{allocated_capacity}, + used_space => $cluster->{used_capacity}, + free_space => $cluster->{allocated_capacity} - $cluster->{used_capacity}, + prct_used_space => $cluster->{used_capacity} * 100 / $cluster->{allocated_capacity}, + prct_free_space => 100 - ($cluster->{used_capacity} * 100 / $cluster->{allocated_capacity}) + } + }; + + $self->{clusters}->{ $cluster->{name} }->{ratio}->{efficiency} = $1 + if ($cluster->{efficiency_ratio} =~ /^\s*([0-9\.]+)\s*:/); + $self->{clusters}->{ $cluster->{name} }->{ratio}->{deduplication} = $1 + if ($cluster->{deduplication_ratio} =~ /^\s*([0-9\.]+)\s*:/); + $self->{clusters}->{ $cluster->{name} }->{ratio}->{compression} = $1 + if ($cluster->{compression_ratio} =~ /^\s*([0-9\.]+)\s*:/); + } +} + + +1; + +__END__ + +=head1 MODE + +Check omnistack clusters. + +=over 8 + +=item B<--filter-name> + +Filter clusters by name. + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'space-usage', 'space-usage-free', 'space-usage-prct', +'ratio-compression', 'ratio-deduplication', 'ratio-efficiency'. + +=back + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/mode/virtualmachines.pm b/apps/virtualization/hpe/simplivity/restapi/mode/virtualmachines.pm new file mode 100644 index 000000000..5239e4243 --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/mode/virtualmachines.pm @@ -0,0 +1,195 @@ +# +# Copyright 2021 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. +# Authors : Roman Morandell - ivertix +# + +package apps::virtualization::hpe::simplivity::restapi::mode::virtualmachines; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_space_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space}); + return 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}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space} + ); +} + +sub vm_long_output { + my ($self, %options) = @_; + + return sprintf( + "checking virtual machine '%s'", + $options{instance} + ); +} + +sub prefix_vm_output { + my ($self, %options) = @_; + + return sprintf( + "virtual machine '%s' ", + $options{instance} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'vm', type => 3, cb_prefix_output => 'prefix_vm_output', cb_long_output => 'vm_long_output', indent_long_output => ' ', message_multiple => 'All virtual machines are ok', + group => [ + { name => 'status', type => 0, skipped_code => { -10 => 1 } }, + { name => 'space', type => 0, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{status} = [ + { + label => 'ha-status', + type => 2, + unknown_default => '%{ha_status} =~ /unknown/', + warning_default => '%{ha_status} =~ /degraded/', + set => { + key_values => [ { name => 'ha_status' }, { name => 'vm_name' } ], + output_template => 'high-availability status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{space} = [ + { label => 'space-usage', nlabel => 'virtual_machine.space.usage.bytes', set => { + key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'space-usage-free', nlabel => 'virtual_machine.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' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'space-usage-prct', nlabel => 'virtual_machine.space.usage.percentage', display_ok => 0, set => { + key_values => [ { name => 'prct_used_space' }, { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ], + closure_custom_output => $self->can('custom_space_usage_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + } + ]; +} + +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-vm-name:s' => { name => 'filter_vm_name' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $vms = $options{custom}->get_virtual_machines(); + + $self->{vm} = {}; + foreach my $vm (@{$vms->{virtual_machines}}) { + if (defined($self->{option_results}->{filter_vm_name}) && $self->{option_results}->{filter_vm_name} ne '' && + $vm->{name} !~ /$self->{option_results}->{filter_vm_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $vm->{name} . "': no matching filter.", debug => 1); + next; + } + + $self->{vm}->{ $vm->{id} . ':' . $vm->{name} } = { + status => { + vm_name => $vm->{id} . ':' . $vm->{name}, + ha_status => lc($vm->{ha_status}) + }, + space => { + total_space => $vm->{hypervisor_allocated_capacity}, + used_space => $vm->{hypervisor_allocated_capacity} - $vm->{hypervisor_free_space}, + free_space => $vm->{hypervisor_free_space}, + prct_used_space => ($vm->{hypervisor_allocated_capacity} - $vm->{hypervisor_free_space}) * 100 / $vm->{hypervisor_allocated_capacity}, + prct_free_space => $vm->{hypervisor_free_space} * 100 / $vm->{hypervisor_allocated_capacity} + } + }; + } +} + + +1; + +__END__ + +=head1 MODE + +Check virtual machines. + +=over 8 + +=item B<--filter-name> + +Filter virtual machines by virtual machine name. + +=item B<--unknown-ha-status> + +Set unknown threshold for status (Default: '%{status} =~ /unknown/'). +Can used special variables like: %{ha_status}, %{vm_name} + +=item B<--warning-ha-status> + +Set warning threshold for status (Default: '%{status} =~ /degraded/'). +Can used special variables like: %{ha_status}, %{vm_name} + +=item B<--critical-ha-status> + +Set critical threshold for status. +Can used special variables like: %{ha_status}, %{vm_name} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'space-usage', 'space-usage-free', 'space-usage-prct'. + +=back + +=cut diff --git a/apps/virtualization/hpe/simplivity/restapi/plugin.pm b/apps/virtualization/hpe/simplivity/restapi/plugin.pm new file mode 100644 index 000000000..b1fd73d03 --- /dev/null +++ b/apps/virtualization/hpe/simplivity/restapi/plugin.pm @@ -0,0 +1,57 @@ +# +# Copyright 2021 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. +# Authors : Roman Morandell - ivertix +# + +package apps::virtualization::hpe::simplivity::restapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{modes} = { + 'discovery' => 'apps::virtualization::hpe::simplivity::restapi::mode::discovery', + 'hosts' => 'apps::virtualization::hpe::simplivity::restapi::mode::hosts', + 'list-hosts' => 'apps::virtualization::hpe::simplivity::restapi::mode::listhosts', + 'omnistack-clusters' => 'apps::virtualization::hpe::simplivity::restapi::mode::omnistackclusters', + 'virtual-machines' => 'apps::virtualization::hpe::simplivity::restapi::mode::virtualmachines' + }; + + $self->{custom_modes}->{api} = 'apps::virtualization::hpe::simplivity::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check HPE Simplivity using OmniStack Rest API. + +=over 8 + +=back + +=cut diff --git a/network/microsens/g6/snmp/mode/sfp.pm b/network/microsens/g6/snmp/mode/sfp.pm index a5d8ff0cf..f4f93cdcf 100644 --- a/network/microsens/g6/snmp/mode/sfp.pm +++ b/network/microsens/g6/snmp/mode/sfp.pm @@ -39,7 +39,7 @@ sub sfp_long_output { sub prefix_sfp_output { my ($self, %options) = @_; - return return sprintf( + return sprintf( "sfp port '%s'%s ", $options{instance}, $options{instance_value}->{location} ne '' ? ' [location: ' . $options{instance_value}->{location} . ']' : ''