From f34c2a82bf28db5274c5079441c02bcd926edf95 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Mon, 16 May 2022 13:11:42 +0200 Subject: [PATCH] (plugin) apps::ceph::restapi - new (#3663) --- .../apps/ceph/restapi/custom/api.pm | 272 ++++++++++++++++++ .../apps/ceph/restapi/mode/health.pm | 113 ++++++++ .../apps/ceph/restapi/mode/listpools.pm | 107 +++++++ .../apps/ceph/restapi/mode/osd.pm | 160 +++++++++++ .../apps/ceph/restapi/mode/pools.pm | 196 +++++++++++++ centreon-plugins/apps/ceph/restapi/plugin.pm | 51 ++++ 6 files changed, 899 insertions(+) create mode 100644 centreon-plugins/apps/ceph/restapi/custom/api.pm create mode 100644 centreon-plugins/apps/ceph/restapi/mode/health.pm create mode 100644 centreon-plugins/apps/ceph/restapi/mode/listpools.pm create mode 100644 centreon-plugins/apps/ceph/restapi/mode/osd.pm create mode 100644 centreon-plugins/apps/ceph/restapi/mode/pools.pm create mode 100644 centreon-plugins/apps/ceph/restapi/plugin.pm diff --git a/centreon-plugins/apps/ceph/restapi/custom/api.pm b/centreon-plugins/apps/ceph/restapi/custom/api.pm new file mode 100644 index 000000000..52304e226 --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/custom/api.pm @@ -0,0 +1,272 @@ +# +# Copyright 2022 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::ceph::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 => { + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + '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 => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl'); + $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->{option_results}->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{option_results}->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8443; + $self->{option_results}->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{option_results}->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; + $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->{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 ($self->{option_results}->{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 settings { + my ($self, %options) = @_; + + return if (defined($self->{settings_done})); + $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; +} + +sub get_connection_info { + my ($self, %options) = @_; + + return $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port}; +} + +sub get_token { + my ($self, %options) = @_; + + my $has_cache_file = $self->{cache}->read(statefile => 'ceph_' . md5_hex($self->get_connection_info() . '_' . $self->{api_username})); + my $token = $self->{cache}->get(name => 'token'); + 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($token) || + (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret) + ) { + my $json_request = { + username => $self->{api_username}, + password => $self->{api_password} + }; + my $encoded; + eval { + $encoded = encode_json($json_request); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => 'cannot encode json request'); + $self->{output}->option_exit(); + } + + $self->settings(); + my $content = $self->{http}->request( + method => 'POST', + url_path => '/api/auth', + query_form_post => $encoded, + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status}, + header => [ + 'Accept: application/vnd.ceph.api.v1.0+json', + 'Content-Type: application/json' + ] + ); + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + $token = $decoded->{token}; + my $datas = { + updated => time(), + token => $token, + md5_secret => $md5_secret + }; + $self->{cache}->write(data => $datas); + } + + return $token; +} + +sub clean_token { + my ($self, %options) = @_; + + my $datas = { updated => time() }; + $self->{cache}->write(data => $datas); +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + my $token = $self->get_token(); + my ($content) = $self->{http}->request( + url_path => $options{endpoint}, + get_param => $options{get_param}, + header => ['Authorization: Bearer ' . $token] + ); + + # Maybe token is invalid. so we retry + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->clean_token(); + $token = $self->get_token(); + $content = $self->{http}->request( + url_path => $options{endpoint}, + get_param => $options{get_param}, + header => ['Authorization: Bearer ' . $token], + unknown_status => $self->{unknown_http_status}, + warning_status => $self->{warning_http_status}, + critical_status => $self->{critical_http_status} + ); + } + + if (!defined($content) || $content eq '') { + $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded; + eval { + $decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($content); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +1; + +__END__ + +=head1 NAME + +Ceph API + +=head1 REST API OPTIONS + +Ceph API + +=over 8 + +=item B<--hostname> + +Set hostname. + +=item B<--port> + +Port used (Default: 8443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +API username. + +=item B<--api-password> + +API password. + +=item B<--timeout> + +Set timeout in seconds (Default: 30). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/apps/ceph/restapi/mode/health.pm b/centreon-plugins/apps/ceph/restapi/mode/health.pm new file mode 100644 index 000000000..e238d8cac --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/mode/health.pm @@ -0,0 +1,113 @@ +# +# Copyright 2022 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::ceph::restapi::mode::health; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = 'health status is ' . $self->{result_values}->{status}; + if ($self->{result_values}->{message} ne '') { + $msg .= ' [message: ' . $self->{result_values}->{message} . ']'; + } + + return $msg; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' } + ]; + + $self->{maps_counters}->{global} = [ + { + label => 'status', + type => 2, + warning_default => '%{status} =~ /warn/i', + critical_default => '%{status} =~ /error/i', + set => { + key_values => [ { name => 'status' }, { name => 'message' } ], + closure_custom_output => $self->can('custom_status_output'), + 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 => {}); + + $self->{cache_status} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $health = $options{custom}->request_api(endpoint => '/api/health/full'); + my $map_status = { HEALTH_OK => 'ok', HEALTH_ERR => 'error', HEALTH_WARN => 'warn' }; + $self->{global} = { status => $map_status->{ $health->{health}->{status} } }; + + my ($message, $append) = ('', ''); + foreach (@{$health->{health}->{checks}}) { + $message .= $append . $_->{summary}->{message}; + $append = ', '; + } + + $self->{global}->{message} = $message; +} + +1; + +__END__ + +=head1 MODE + +Check overall cluster status. + +=over 8 + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /warn/i') +Can used special variables like: %{status}, %{message} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} =~ /error/i'). +Can used special variables like: %{status}, %{message} + +=back + +=cut diff --git a/centreon-plugins/apps/ceph/restapi/mode/listpools.pm b/centreon-plugins/apps/ceph/restapi/mode/listpools.pm new file mode 100644 index 000000000..daab74afb --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/mode/listpools.pm @@ -0,0 +1,107 @@ +# +# Copyright 2022 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::ceph::restapi::mode::listpools; + +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 => {}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $pools = $options{custom}->request_api(endpoint => '/api/health/full'); + my $results = []; + foreach my $pool (@{$pools->{df}->{pools}}) { + push @$results, $pool; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->output_add( + long_msg => sprintf( + '[id: %s][name: %s]', + $_->{id}, + $_->{name} + ) + ); + } + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List pools:' + ); + + $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']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->add_disco_entry( + id => $_->{id}, + name => $_->{name} + ); + } +} + +1; + +__END__ + +=head1 MODE + +List pools. + +=over 8 + +=back + +=cut diff --git a/centreon-plugins/apps/ceph/restapi/mode/osd.pm b/centreon-plugins/apps/ceph/restapi/mode/osd.pm new file mode 100644 index 000000000..a5e88604a --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/mode/osd.pm @@ -0,0 +1,160 @@ +# +# Copyright 2022 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::ceph::restapi::mode::osd; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub custom_online_output { + my ($self, %options) = @_; + + return sprintf( + 'online %.2f%% (%s on %s)', + $self->{result_values}->{online_prct}, + $self->{result_values}->{online}, + $self->{result_values}->{detected} + ); +} + +sub custom_participating_output { + my ($self, %options) = @_; + + return sprintf( + 'participating %.2f%% (%s on %s)', + $self->{result_values}->{participating_prct}, + $self->{result_values}->{participating}, + $self->{result_values}->{detected} + ); +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'Number of osd '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'osd-detected', nlabel => 'osd.detected.count', set => { + key_values => [ { name => 'detected' } ], + output_template => 'detected: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'osd-online', nlabel => 'osd.online.count', set => { + key_values => [ { name => 'online' }, { name => 'online_prct' }, { name => 'detected' } ], + closure_custom_output => $self->can('custom_online_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'detected' } + ] + } + }, + { label => 'osd-online-prct', nlabel => 'osd.online.percentage', display_ok => 0, set => { + key_values => [ { name => 'online_prct' }, { name => 'online' }, { name => 'detected' } ], + closure_custom_output => $self->can('custom_online_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] + } + }, + { label => 'osd-participating', nlabel => 'osd.participating.count', set => { + key_values => [ { name => 'participating' }, { name => 'participating_prct' }, { name => 'detected' } ], + closure_custom_output => $self->can('custom_participating_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'detected' } + ] + } + }, + { label => 'osd-participating-prct', nlabel => 'osd.participating.percentage', display_ok => 0, set => { + key_values => [ { name => 'participating_prct' }, { name => 'participating' }, { name => 'detected' } ], + closure_custom_output => $self->can('custom_participating_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] + } + } + ]; +} + +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 => {}); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $health = $options{custom}->request_api(endpoint => '/api/health/full'); + + $self->{global} = { detected => 0, online => 0, participating => 0 }; + foreach my $osd (@{$health->{osd_map}->{osds}}) { + $self->{global}->{detected}++; + + $self->{global}->{participating}++ if ($osd->{in} == 1); + $self->{global}->{online}++ if ($osd->{up} == 1); + } + + if ($self->{global}->{detected} > 0) { + $self->{global}->{participating_prct} = $self->{global}->{participating} * 100 / $self->{global}->{detected}; + $self->{global}->{online_prct} = $self->{global}->{online} * 100 / $self->{global}->{detected}; + } +} + +1; + +__END__ + +=head1 MODE + +Check object storage daemons. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='detected' + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'osd-detected', +'osd-online', 'osd-online-prct', +'osd-participating', 'osd-participating-prct'. + +=back + +=cut diff --git a/centreon-plugins/apps/ceph/restapi/mode/pools.pm b/centreon-plugins/apps/ceph/restapi/mode/pools.pm new file mode 100644 index 000000000..ae8ea3e28 --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/mode/pools.pm @@ -0,0 +1,196 @@ +# +# Copyright 2022 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::ceph::restapi::mode::pools; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5; + +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_global_output { + my ($self, %options) = @_; + + return 'Number of pools '; +} + +sub prefix_pool_output { + my ($self, %options) = @_; + + return "Pool '" . $options{instance} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, + { name => 'pools', type => 1, cb_prefix_output => 'prefix_pool_output', message_multiple => 'All pools are ok', skipped_code => { -10 => 1, -11 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'pools-detected', nlabel => 'pools.detected.count', set => { + key_values => [ { name => 'detected' } ], + output_template => 'detected: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{pools} = [ + { label => 'pool-space-usage', nlabel => 'pool.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 => 'pool-space-usage-free', nlabel => 'pool.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 => 'pool-space-usage-prct', nlabel => 'pool.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 } + ] + } + }, + { label => 'pool-read', nlabel => 'pool.read.usage.bytespersecond', set => { + key_values => [ { name => 'read', per_second => 1 } ], + output_template => 'read: %s %s/s', + output_change_bytes => 1, + perfdatas => [ + { template => '%d', unit => 'B/s' } + ] + } + }, + { label => 'pool-write', nlabel => 'pool.write.usage.bytespersecond', set => { + key_values => [ { name => 'write', per_second => 1 } ], + output_template => 'write: %s %s/s', + output_change_bytes => 1, + perfdatas => [ + { template => '%d', unit => 'B/s', min => 0 } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1, statefile => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $health = $options{custom}->request_api(endpoint => '/api/health/full'); + + $self->{global} = { detected => 0 }; + $self->{pools} = {}; + foreach my $pool (@{$health->{df}->{pools}}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $pool->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $pool->{name} . "': no matching filter.", debug => 1); + next; + } + + $self->{global}->{detected}++; + + my $total = $pool->{stats}->{avail_raw} + $pool->{stats}->{bytes_used}; + $self->{pools}->{ $pool->{name} } = { + total_space => $total, + used_space => $pool->{stats}->{bytes_used}, + free_space => $pool->{stats}->{avail_raw}, + prct_used_space => $pool->{stats}->{bytes_used} * 100 / $total, + prct_free_space => $pool->{stats}->{avail_raw} * 100 / $total, + read => $pool->{stats}->{rd_bytes}, + write => $pool->{stats}->{wr_bytes} + }; + } + + $self->{cache_name} = 'ceph_' . $self->{mode} . + Digest::MD5::md5_hex( + $options{custom}->get_connection_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? $self->{option_results}->{filter_counters} : 'all') . '_' . + (defined($self->{option_results}->{filter_name}) ? $self->{option_results}->{filter_name} : 'all') + ); +} + +1; + +__END__ + +=head1 MODE + +Check pools. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='detected' + +=item B<--filter-name> + +Filter pools by name (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'pools-detected', +'pool-space-usage', 'pool-space-usage-free', 'pool-space-usage-prct', +'pool-read', 'pool-write'. + +=back + +=cut diff --git a/centreon-plugins/apps/ceph/restapi/plugin.pm b/centreon-plugins/apps/ceph/restapi/plugin.pm new file mode 100644 index 000000000..307af796e --- /dev/null +++ b/centreon-plugins/apps/ceph/restapi/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2022 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::ceph::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} = { + 'health' => 'apps::ceph::restapi::mode::health', + 'list-pools' => 'apps::ceph::restapi::mode::listpools', + 'osd' => 'apps::ceph::restapi::mode::osd', + 'pools' => 'apps::ceph::restapi::mode::pools' + }; + + $self->{custom_modes}->{api} = 'apps::ceph::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Ceph using Rest API (provided by the dashboard). + +=cut