diff --git a/apps/redis/restapi/custom/api.pm b/apps/redis/restapi/custom/api.pm new file mode 100644 index 000000000..41eb2496a --- /dev/null +++ b/apps/redis/restapi/custom/api.pm @@ -0,0 +1,224 @@ +# +# Copyright 2017 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::redis::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; + +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' }, + "username:s@" => { name => 'username' }, + "password:s@" => { name => 'password' }, + "proxyurl:s@" => { name => 'proxyurl' }, + "timeout:s@" => { name => 'timeout' }, + "ssl:s@" => { name => 'ssl' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + return $self; + +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? shift(@{$self->{option_results}->{hostname}}) : undef; + $self->{port} = (defined($self->{option_results}->{port})) ? shift(@{$self->{option_results}->{port}}) : 9443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? shift(@{$self->{option_results}->{proto}}) : 'https'; + $self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : ''; + $self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : ''; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10; + $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? shift(@{$self->{option_results}->{proxyurl}}) : undef; + $self->{ssl} = (defined($self->{option_results}->{ssl})) ? shift(@{$self->{option_results}->{ssl}}) : 'tlsv1'; + + if (!defined($self->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); + $self->{output}->option_exit(); + } + + if (!defined($self->{hostname}) || + scalar(@{$self->{option_results}->{hostname}}) == 0) { + return 0; + } + return 1; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{proxyurl} = $self->{proxyurl}; + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{username} = $self->{username}; + $self->{option_results}->{password} = $self->{password}; + $self->{option_results}->{ssl} = $self->{ssl}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_connection_info { + my ($self, %options) = @_; + + return $self->{hostname} . ":" . $self->{port}; +} + +sub get { + my ($self, %options) = @_; + + $self->settings(); + + my $response = $self->{http}->request(url_path => $options{path}); + + my $content; + eval { + $content = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + my $return; + if (ref($content) eq 'ARRAY') { + foreach my $uid (@$content) { + if (defined($uid->{errmsg})) { + $self->{output}->add_option_msg(short_msg => "Cannot get data: " . $uid->{errmsg}); + $self->{output}->option_exit(); + } + $return->{$uid->{uid}} = $uid; + } + } else { + if (defined($content->{errmsg})) { + $self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{errmsg}); + $self->{output}->option_exit(); + } + $return = $content; + } + + return $return; +} + +1; + +__END__ + +=head1 NAME + +RedisLabs Enterprise Cluster REST API + +=head1 SYNOPSIS + +RedisLabs Enterprise Cluster Rest API custom mode + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +Cluster hostname. + +=item B<--port> + +Port used (Default: 9443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--username> + +Cluster username. + +=item B<--password> + +Cluster password. + +=item B<--proxyurl> + +Proxy URL if any + +=item B<--timeout> + +Set HTTP timeout + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/redis/restapi/mode/clusterstats.pm b/apps/redis/restapi/mode/clusterstats.pm new file mode 100644 index 000000000..4cf053c34 --- /dev/null +++ b/apps/redis/restapi/mode/clusterstats.pm @@ -0,0 +1,322 @@ +# +# Copyright 2017 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::redis::restapi::mode::clusterstats; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +my $instance_mode; + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add(label => $self->{result_values}->{perf}, unit => 'B', + value => $self->{result_values}->{used}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{label}, total => $self->{result_values}->{total}, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{label}, total => $self->{result_values}->{total}, cast_int => 1), + min => 0, max => $self->{result_values}->{total}); +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{used}; + $threshold_value = $self->{result_values}->{free} if (defined($instance_mode->{option_results}->{free})); + if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_used}; + $threshold_value = $self->{result_values}->{prct_free} if (defined($instance_mode->{option_results}->{free})); + } + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{result_values}->{label}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{result_values}->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); + my ($free_value, $free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); + my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); + + my $msg = sprintf("%s usage: Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $self->{result_values}->{display}, + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $self->{result_values}->{prct_used}, + $free_value . " " . $free_unit, $self->{result_values}->{prct_free}); + return $msg; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + $self->{result_values}->{label} = $options{extra_options}->{label}; + $self->{result_values}->{perf} = $options{extra_options}->{perf}; + $self->{result_values}->{display} = $options{extra_options}->{display}; + $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{free}}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{total}}; + + if ($self->{result_values}->{total} != 0) { + $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free}; + $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; + $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; + } else { + $self->{result_values}->{used} = '0'; + $self->{result_values}->{prct_used} = '0'; + $self->{result_values}->{prct_free} = '0'; + } + + return 0; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "Cluster '" . $options{instance_value}->{name} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cluster', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All cluster counters are ok' }, + ]; + + $self->{maps_counters}->{cluster} = [ + { label => 'cpu-system', set => { + key_values => [ { name => 'cpu_system' } ], + output_template => 'Cpu system: %.2f %%', + perfdatas => [ + { label => 'cpu_system', value => 'cpu_system_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + { label => 'cpu-user', set => { + key_values => [ { name => 'cpu_user' } ], + output_template => 'Cpu user: %.2f %%', + perfdatas => [ + { label => 'cpu_user', value => 'cpu_user_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + { label => 'requests', set => { + key_values => [ { name => 'total_req', diff => 1 } ], + output_template => 'Requests per seconds: %s', + per_second => 1, + perfdatas => [ + { label => 'requests', value => 'total_req_per_second', template => '%s', + min => 0, unit => '/s' }, + ], + } + }, + { label => 'memory-used', set => { + key_values => [ { name => 'free_memory' }, { name => 'total_memory' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Ram', label => 'memory-used', perf => 'memory_used', + free => 'free_memory', total => 'total_memory' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'persistent-storage-used', set => { + key_values => [ { name => 'persistent_storage_free' }, { name => 'persistent_storage_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Persistent storage', label => 'persistent-storage-used', perf => 'persistent_storage_used', + free => 'persistent_storage_free', total => 'persistent_storage_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'ephemeral-storage-used', set => { + key_values => [ { name => 'ephemeral_storage_free' }, { name => 'ephemeral_storage_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Ephemeral storage', label => 'ephemeral-storage-used', perf => 'ephemeral_storage_used', + free => 'ephemeral_storage_free', total => 'ephemeral_storage_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'flash-storage-used', set => { + key_values => [ { name => 'available_flash' }, { name => 'bigstore_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Flash storage', label => 'flash-storage-used', perf => 'flash_storage_used', + free => 'available_flash', total => 'bigstore_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'flash-iops', set => { + key_values => [ { name => 'bigstore_iops' } ], + output_template => 'Flash IOPS: %s', + perfdatas => [ + { label => 'flash_iops', value => 'bigstore_iops_absolute', template => '%s', + min => 0, unit => '/s' }, + ], + } + }, + { label => 'flash-throughput', set => { + key_values => [ { name => 'bigstore_throughput' } ], + output_template => 'Flash throughput: %s %s/s', + output_change_bytes => 1, + perfdatas => [ + { label => 'flash_throughput', value => 'bigstore_throughput_absolute', template => '%s', + min => 0, unit => 'B/s' }, + ], + } + }, + { label => 'connections', set => { + key_values => [ { name => 'conns' } ], + output_template => 'Connections: %s', + perfdatas => [ + { label => 'connections', value => 'conns_absolute', template => '%s', + min => 0 }, + ], + } + }, + { label => 'traffic-in', set => { + key_values => [ { name => 'ingress' } ], + output_template => 'Traffic In: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'traffic_in', value => 'ingress_absolute', template => '%d', min => 0, unit => 'b/s' }, + ], + }, + }, + { label => 'traffic-out', set => { + key_values => [ { name => 'egress' } ], + output_template => 'Traffic Out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'traffic_out', value => 'egress_absolute', template => '%d', min => 0, unit => 'b/s' }, + ], + }, + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "redis_restapi_" . $self->{mode} . '_' . $options{custom}->get_connection_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + my $result = $options{custom}->get(path => '/v1/cluster/stats/last'); + my $result2 = $options{custom}->get(path => '/v1/cluster'); + my $result3 = $options{custom}->get(path => '/v1/nodes'); + + my $total_memory = 0; + my $persistent_storage_size = 0; + my $ephemeral_storage_size = 0; + my $bigstore_size = 0; + foreach my $node (keys $result3) { + $total_memory = $total_memory + $result3->{$node}->{total_memory}; + $persistent_storage_size = $persistent_storage_size + $result3->{$node}->{persistent_storage_size}; + $ephemeral_storage_size = $ephemeral_storage_size + $result3->{$node}->{ephemeral_storage_size}; + $bigstore_size = $bigstore_size + $result3->{$node}->{bigstore_size}; + } + + $self->{cluster}->{$result2->{name}} = { + name => $result2->{name}, + cpu_system => $result->{cpu_system} * 100, + cpu_user => $result->{cpu_user} * 100, + free_memory => $result->{free_memory}, + total_memory => $total_memory, + persistent_storage_free => $result->{persistent_storage_free}, + persistent_storage_size => $persistent_storage_size, + ephemeral_storage_free => $result->{ephemeral_storage_free}, + ephemeral_storage_size => $ephemeral_storage_size, + available_flash => $result->{available_flash}, + bigstore_size => $bigstore_size, + bigstore_iops => $result->{bigstore_iops}, + bigstore_kv_ops => $result->{bigstore_kv_ops}, + bigstore_throughput => $result->{bigstore_throughput}, + conns => $result->{conns}, + ingress => $result->{ingress_bytes} * 8, + egress => $result->{egress_bytes} * 8, + total_req => $result->{total_req}, + }; +} + +1; + +__END__ + +=head1 MODE + +Check RedisLabs Enterprise Cluster statistics. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^cpu' + +=item B<--warning-*> + +Threshold warning. +Can be: 'cpu-system', 'cpu-user', +'requests', 'memory-used', 'flash-storage-used', +'persistent-storage-used', 'ephemeral-storage-used', +'flash-iops', 'flash-throughput', 'connections', +'traffic-in', 'traffic-out'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'cpu-system', 'cpu-user', +'requests', 'memory-used', 'flash-storage-used', +'persistent-storage-used', 'ephemeral-storage-used', +'flash-iops', 'flash-throughput', 'connections', +'traffic-in', 'traffic-out'. + +=back + +=cut diff --git a/apps/redis/restapi/mode/databasesstats.pm b/apps/redis/restapi/mode/databasesstats.pm new file mode 100644 index 000000000..13af3b40a --- /dev/null +++ b/apps/redis/restapi/mode/databasesstats.pm @@ -0,0 +1,308 @@ +# +# Copyright 2017 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::redis::restapi::mode::databasesstats; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && + eval "$instance_mode->{option_results}->{critical_status}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && + eval "$instance_mode->{option_results}->{warning_status}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf("Status is '%s' [type: %s] [sync: %s] [backup status: %s] [export status: %s] [shard list: %s]", + $self->{result_values}->{status}, + $self->{result_values}->{type}, + $self->{result_values}->{sync}, + $self->{result_values}->{backup_status}, + $self->{result_values}->{export_status}, + $self->{result_values}->{shard_list}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{type} = $options{new_datas}->{$self->{instance} . '_type'}; + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{sync} = $options{new_datas}->{$self->{instance} . '_sync'}; + $self->{result_values}->{backup_status} = $options{new_datas}->{$self->{instance} . '_backup_status'}; + $self->{result_values}->{export_status} = $options{new_datas}->{$self->{instance} . '_export_status'}; + $self->{result_values}->{shard_list} = $options{new_datas}->{$self->{instance} . '_shard_list'}; + return 0; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "Database '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'databases', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All databases counters are ok' }, + ]; + + $self->{maps_counters}->{databases} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'type' }, { name => 'sync' }, { name => 'backup_status' }, { name => 'export_status' }, { name => 'shard_list' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + { label => 'ops-per-sec', set => { + key_values => [ { name => 'instantaneous_ops_per_sec' }, { name => 'display' } ], + output_template => 'Operations: %s ops/s', + perfdatas => [ + { label => 'ops_per_sec', value => 'instantaneous_ops_per_sec_absolute', template => '%s', + min => 0, unit => 'ops/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'memory-used', set => { + key_values => [ { name => 'used_memory' }, { name => 'display' } ], + output_template => 'Memory used: %s %s', + output_change_bytes => 1, + perfdatas => [ + { label => 'memory-used', value => 'used_memory_absolute', template => '%s', + min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'total-keys', set => { + key_values => [ { name => 'no_of_keys' }, { name => 'display' } ], + output_template => 'Total keys: %s', + perfdatas => [ + { label => 'total-keys', value => 'no_of_keys_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'read-hits', set => { + key_values => [ { name => 'read_hits' }, { name => 'display' } ], + output_template => 'Read hits: %s /s', + perfdatas => [ + { label => 'read_hits', value => 'read_hits_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'read-misses', set => { + key_values => [ { name => 'read_misses' }, { name => 'display' } ], + output_template => 'Read misses: %s /s', + perfdatas => [ + { label => 'read_misses', value => 'read_misses_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'write-hits', set => { + key_values => [ { name => 'write_hits' }, { name => 'display' } ], + output_template => 'Write hits: %s /s', + perfdatas => [ + { label => 'write_hits', value => 'write_hits_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'write-misses', set => { + key_values => [ { name => 'write_misses' }, { name => 'display' } ], + output_template => 'Write misses: %s /s', + perfdatas => [ + { label => 'write_misses', value => 'write_misses_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'evicted-objects', set => { + key_values => [ { name => 'evicted_objects' }, { name => 'display' } ], + output_template => 'Evicted objects: %s', + perfdatas => [ + { label => 'evicted_objects', value => 'evicted_objects_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'expired-objects', set => { + key_values => [ { name => 'expired_objects' }, { name => 'display' } ], + output_template => 'Evicted objects: %s', + perfdatas => [ + { label => 'expired_objects', value => 'expired_objects_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'mem-frag-ratio', set => { + key_values => [ { name => 'mem_frag_ratio' }, { name => 'display' } ], + output_template => 'Memory fragmentation ratio: %s', + perfdatas => [ + { label => 'mem_frag_ratio', value => 'mem_frag_ratio_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-database:s" => { name => 'filter_database' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "redis_restapi_" . $self->{mode} . '_' . $options{custom}->get_connection_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + my $result = $options{custom}->get(path => '/v1/bdbs/stats/last'); + my $result2 = $options{custom}->get(path => '/v1/bdbs'); + + foreach my $database (keys $result) { + if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' && + $database !~ /$self->{option_results}->{filter_database}/) { + $self->{output}->output_add(long_msg => "skipping database '" . $database . "': no matching filter.", debug => 1); + next; + } + + my $shard_list = '-'; + if (@{$result2->{$database}->{shard_list}}) { $shard_list = join(", ", @{$result2->{$database}->{shard_list}}); } + + $self->{databases}->{$database} = { + display => $result2->{$database}->{name}, + status => defined($result2->{$database}->{status}) ? $result2->{$database}->{status} : '-', + type => defined($result2->{$database}->{type}) ? $result2->{$database}->{type} : '-', + sync => defined($result2->{$database}->{sync}) ? $result2->{$database}->{sync} : '-', + backup_status => defined($result2->{$database}->{backup_status}) ? $result2->{$database}->{backup_status} : '-', + export_status => defined($result2->{$database}->{export_status}) ? $result2->{$database}->{export_status} : '-', + shard_list => $shard_list, + used_memory => $result->{$database}->{used_memory}, + instantaneous_ops_per_sec => $result->{$database}->{instantaneous_ops_per_sec}, + no_of_keys => $result->{$database}->{no_of_keys}, + evicted_objects => $result->{$database}->{evicted_objects}, + expired_objects => $result->{$database}->{expired_objects}, + read_hits => $result->{$database}->{read_hits}, + read_misses => $result->{$database}->{read_misses}, + write_hits => $result->{$database}->{write_hits}, + write_misses => $result->{$database}->{write_misses}, + mem_frag_ratio => $result->{$database}->{mem_frag_ratio}, + }; + + if (scalar(keys %{$self->{databases}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No databases detected, check your filter ? '); + $self->{output}->option_exit(); + } + } +} + +1; + +__END__ + +=head1 MODE + +Check RedisLabs Enterprise Cluster databases statistics. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='clients' + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{type}, %{sync}, +%{backup_status}, %{export_status}, %{shard_list} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} !~ /active/i'). +Can used special variables like: %{status}, %{type}, %{sync}, +%{backup_status}, %{export_status}, %{shard_list} + +=item B<--warning-*> + +Threshold warning. +Can be: 'ops-per-sec', 'memory-used', +'total-keys', 'read-hits', 'read-misses', +'write-hits', 'write-misses', 'evicted-objects', +'expired-objects', 'mem-frag-ratio'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'ops-per-sec', 'memory-used', +'total-keys', 'read-hits', 'read-misses', +'write-hits', 'write-misses', 'evicted-objects', +'expired-objects', 'mem-frag-ratio'. + +=back + +=cut diff --git a/apps/redis/restapi/mode/nodesstats.pm b/apps/redis/restapi/mode/nodesstats.pm new file mode 100644 index 000000000..3df536048 --- /dev/null +++ b/apps/redis/restapi/mode/nodesstats.pm @@ -0,0 +1,419 @@ +# +# Copyright 2017 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::redis::restapi::mode::nodesstats; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +my $instance_mode; + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add(label => $self->{result_values}->{perf}, unit => 'B', + value => $self->{result_values}->{used}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{label}, total => $self->{result_values}->{total}, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{label}, total => $self->{result_values}->{total}, cast_int => 1), + min => 0, max => $self->{result_values}->{total}); +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{used}; + $threshold_value = $self->{result_values}->{free} if (defined($instance_mode->{option_results}->{free})); + if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_used}; + $threshold_value = $self->{result_values}->{prct_free} if (defined($instance_mode->{option_results}->{free})); + } + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{result_values}->{label}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{result_values}->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); + my ($free_value, $free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); + my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); + + my $msg = sprintf("%s usage: Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $self->{result_values}->{display}, + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $self->{result_values}->{prct_used}, + $free_value . " " . $free_unit, $self->{result_values}->{prct_free}); + return $msg; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + $self->{result_values}->{label} = $options{extra_options}->{label}; + $self->{result_values}->{perf} = $options{extra_options}->{perf}; + $self->{result_values}->{display} = $options{extra_options}->{display}; + $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{free}}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{total}}; + + if ($self->{result_values}->{total} != 0) { + $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free}; + $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; + $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; + } else { + $self->{result_values}->{used} = '0'; + $self->{result_values}->{prct_used} = '0'; + $self->{result_values}->{prct_free} = '0'; + } + + return 0; +} + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && + eval "$instance_mode->{option_results}->{critical_status}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && + eval "$instance_mode->{option_results}->{warning_status}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf("Status is '%s' [shard list: %s] [int addr: %s] [ext addr: %s]", + $self->{result_values}->{status}, + $self->{result_values}->{shard_list}, + $self->{result_values}->{int_addr}, + $self->{result_values}->{ext_addr}); + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{shard_list} = $options{new_datas}->{$self->{instance} . '_shard_list'}; + $self->{result_values}->{int_addr} = $options{new_datas}->{$self->{instance} . '_int_addr'}; + $self->{result_values}->{ext_addr} = $options{new_datas}->{$self->{instance} . '_ext_addr'}; + return 0; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-node:s" => { name => 'filter_node' }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + }); + + return $self; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "Node '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'nodes', type => 1, cb_prefix_output => 'prefix_output', message_separator => ', ', message_multiple => 'All nodes counters are ok' }, + ]; + + $self->{maps_counters}->{nodes} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'shard_list' }, { name => 'int_addr' }, { name => 'ext_addr' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + { label => 'cpu-system', set => { + key_values => [ { name => 'cpu_system' }, { name => 'display' } ], + output_template => 'Cpu system: %.2f %%', + perfdatas => [ + { label => 'cpu_system', value => 'cpu_system_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'cpu-user', set => { + key_values => [ { name => 'cpu_user' }, { name => 'display' } ], + output_template => 'Cpu user: %.2f %%', + perfdatas => [ + { label => 'cpu_user', value => 'cpu_user_absolute', template => '%.2f', + min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'requests', set => { + key_values => [ { name => 'total_req', diff => 1 }, { name => 'display' } ], + output_template => 'Requests per seconds: %s', + per_second => 1, + perfdatas => [ + { label => 'requests', value => 'total_req_per_second', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'memory-used', set => { + key_values => [ { name => 'free_memory' }, { name => 'total_memory' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Ram', label => 'memory-used', perf => 'memory_used', + free => 'free_memory', total => 'total_memory' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'persistent-storage-used', set => { + key_values => [ { name => 'persistent_storage_free' }, { name => 'persistent_storage_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Persistent storage', label => 'persistent-storage-used', perf => 'persistent_storage_used', + free => 'persistent_storage_free', total => 'persistent_storage_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'ephemeral-storage-used', set => { + key_values => [ { name => 'ephemeral_storage_free' }, { name => 'ephemeral_storage_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Ephemeral storage', label => 'ephemeral-storage-used', perf => 'ephemeral_storage_used', + free => 'ephemeral_storage_free', total => 'ephemeral_storage_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'flash-storage-used', set => { + key_values => [ { name => 'available_flash' }, { name => 'bigstore_size' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_calc_extra_options => { display => 'Flash storage', label => 'flash-storage-used', perf => 'flash_storage_used', + free => 'available_flash', total => 'bigstore_size' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'flash-iops', set => { + key_values => [ { name => 'bigstore_iops' }, { name => 'display' } ], + output_template => 'Flash IOPS: %s', + perfdatas => [ + { label => 'flash_iops', value => 'bigstore_iops_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'flash-throughput', set => { + key_values => [ { name => 'bigstore_throughput' }, { name => 'display' } ], + output_template => 'Flash throughput: %s %s/s', + output_change_bytes => 1, + perfdatas => [ + { label => 'flash_throughput', value => 'bigstore_throughput_absolute', template => '%s', + min => 0, unit => 'B/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'connections', set => { + key_values => [ { name => 'conns' }, { name => 'display' } ], + output_template => 'Connections: %s', + perfdatas => [ + { label => 'connections', value => 'conns_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'traffic-in', set => { + key_values => [ { name => 'ingress' }, { name => 'display' } ], + output_template => 'Traffic In: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'traffic_in', value => 'ingress_absolute', template => '%d', + min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + }, + }, + { label => 'traffic-out', set => { + key_values => [ { name => 'egress' }, { name => 'display' } ], + output_template => 'Traffic Out: %s %s/s', + output_change_bytes => 2, + perfdatas => [ + { label => 'traffic_out', value => 'egress_absolute', template => '%d', + min => 0, unit => 'b/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + }, + }, + { label => 'shard-count', set => { + key_values => [ { name => 'shard_count' }, { name => 'display' } ], + output_template => 'Shard count: %d', + perfdatas => [ + { label => 'shard_count', value => 'shard_count_absolute', template => '%d', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + }, + }, + { label => 'uptime', set => { + key_values => [ { name => 'uptime' }, { name => 'uptime_sec' }, { name => 'display' } ], + output_template => 'Uptime: %s', + perfdatas => [ + { label => 'uptime', value => 'uptime_sec_absolute', template => '%d', + min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + }, + }, + ]; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "redis_restapi_" . $self->{mode} . '_' . $options{custom}->get_connection_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + my $result = $options{custom}->get(path => '/v1/nodes/stats/last'); + my $result2 = $options{custom}->get(path => '/v1/nodes'); + + foreach my $node (keys $result) { + if (defined($self->{option_results}->{filter_node}) && $self->{option_results}->{filter_node} ne '' && + $node !~ /$self->{option_results}->{filter_node}/) { + $self->{output}->output_add(long_msg => "skipping node '" . $node . "': no matching filter.", debug => 1); + next; + } + + my $shard_list = '-'; + if (@{$result2->{$node}->{shard_list}}) { $shard_list = join(", ", @{$result2->{$node}->{shard_list}}); } + my $ext_addr = '-'; + if (@{$result2->{$node}->{external_addr}}) { $ext_addr = join(", ", @{$result2->{$node}->{external_addr}}); } + + $self->{nodes}->{$node} = { + display => $node, + status => defined($result2->{$node}->{status}) ? $result2->{$node}->{status} : '-', + shard_list => $shard_list, + int_addr => $result2->{$node}->{addr}, + ext_addr => $ext_addr, + cpu_system => $result->{$node}->{cpu_system} * 100, + cpu_user => $result->{$node}->{cpu_user} * 100, + free_memory => $result->{$node}->{free_memory}, + total_memory => $result2->{$node}->{total_memory}, + persistent_storage_free => $result->{$node}->{persistent_storage_free}, + persistent_storage_size => $result2->{$node}->{persistent_storage_size}, + ephemeral_storage_free => $result->{$node}->{ephemeral_storage_free}, + ephemeral_storage_size => $result2->{$node}->{ephemeral_storage_size}, + available_flash => $result->{$node}->{available_flash}, + bigstore_size => $result2->{$node}->{bigstore_size}, + bigstore_iops => $result->{$node}->{bigstore_iops}, + bigstore_throughput => $result->{$node}->{bigstore_throughput}, + conns => $result->{$node}->{conns}, + ingress => $result->{$node}->{ingress_bytes} * 8, + egress => $result->{$node}->{egress_bytes} * 8, + total_req => $result->{$node}->{total_req}, + shard_count => $result2->{$node}->{shard_count}, + uptime => centreon::plugins::misc::change_seconds(value => $result2->{$node}->{uptime}), + uptime_sec => $result2->{$node}->{uptime}, + }; + + if (scalar(keys %{$self->{nodes}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No nodes detected, check your filter ? '); + $self->{output}->option_exit(); + } + } +} + +1; + +__END__ + +=head1 MODE + +Check RedisLabs Enterprise Cluster nodes statistics. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^cpu' + + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{shard_list}, %{int_addr}, %{ext_addr}. + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} !~ /active/i'). +Can used special variables like: %{status}, %{shard_list}, %{int_addr}, %{ext_addr}. + +=item B<--warning-*> + +Threshold warning. +Can be: 'cpu-system', 'cpu-user', +'requests', 'memory-used', 'flash-storage-used', +'persistent-storage-used', 'ephemeral-storage-used', +'flash-iops', 'flash-throughput', 'connections', +'traffic-in', 'traffic-out', 'uptime'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'cpu-system', 'cpu-user', +'requests', 'memory-used', 'flash-storage-used', +'persistent-storage-used', 'ephemeral-storage-used', +'flash-iops', 'flash-throughput', 'connections', +'traffic-in', 'traffic-out', 'shard-count', 'uptime'. + +=back + +=cut diff --git a/apps/redis/restapi/mode/shardsstats.pm b/apps/redis/restapi/mode/shardsstats.pm new file mode 100644 index 000000000..a2d79f38d --- /dev/null +++ b/apps/redis/restapi/mode/shardsstats.pm @@ -0,0 +1,353 @@ +# +# Copyright 2017 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::redis::restapi::mode::shardsstats; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +my $instance_mode; + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && + eval "$instance_mode->{option_results}->{critical_status}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && + eval "$instance_mode->{option_results}->{warning_status}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + + my $msg = sprintf("Status is '%s' (%s) [role: %s] [loading status: %s] [backup status: %s]", + $self->{result_values}->{status}, + $self->{result_values}->{detailed_status}, + $self->{result_values}->{role}, + $self->{result_values}->{loading}, + $self->{result_values}->{backup}); + if ($self->{result_values}->{role} eq 'slave') { + $msg .= sprintf(" [sync status: %s]", + $self->{result_values}->{sync}); + } + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{role} = $options{new_datas}->{$self->{instance} . '_role'}; + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; + $self->{result_values}->{detailed_status} = $options{new_datas}->{$self->{instance} . '_detailed_status'}; + $self->{result_values}->{loading} = $options{new_datas}->{$self->{instance} . '_loading'}; + $self->{result_values}->{sync} = $options{new_datas}->{$self->{instance} . '_sync'}; + $self->{result_values}->{backup} = $options{new_datas}->{$self->{instance} . '_backup'}; + return 0; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "Shard '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'shards', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All shards counters are ok' }, + ]; + + $self->{maps_counters}->{shards} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' }, { name => 'detailed_status' }, { name => 'role' }, { name => 'loading' }, { name => 'sync' }, { name => 'backup' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + { label => 'ops-per-sec', set => { + key_values => [ { name => 'instantaneous_ops_per_sec' }, { name => 'display' } ], + output_template => 'Operations: %s ops/s', + perfdatas => [ + { label => 'ops_per_sec', value => 'instantaneous_ops_per_sec_absolute', template => '%s', + min => 0, unit => 'ops/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'memory-used', set => { + key_values => [ { name => 'used_memory' }, { name => 'display' } ], + output_template => 'Memory used: %s %s', + output_change_bytes => 1, + perfdatas => [ + { label => 'memory-used', value => 'used_memory_absolute', template => '%s', + min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'total-keys', set => { + key_values => [ { name => 'no_of_keys' }, { name => 'display' } ], + output_template => 'Total keys: %s', + perfdatas => [ + { label => 'total-keys', value => 'no_of_keys_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'connected-clients', set => { + key_values => [ { name => 'connected_clients' }, { name => 'display' } ], + output_template => 'Connected clients: %s', + perfdatas => [ + { label => 'connected_clients', value => 'connected_clients_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'blocked-clients', set => { + key_values => [ { name => 'blocked_clients' }, { name => 'display' } ], + output_template => 'Blocked clients: %s', + perfdatas => [ + { label => 'blocked_clients', value => 'blocked_clients_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'read-hits', set => { + key_values => [ { name => 'read_hits' }, { name => 'display' } ], + output_template => 'Read hits: %s /s', + perfdatas => [ + { label => 'read_hits', value => 'read_hits_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'read-misses', set => { + key_values => [ { name => 'read_misses' }, { name => 'display' } ], + output_template => 'Read misses: %s /s', + perfdatas => [ + { label => 'read_misses', value => 'read_misses_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'write-hits', set => { + key_values => [ { name => 'write_hits' }, { name => 'display' } ], + output_template => 'Write hits: %s /s', + perfdatas => [ + { label => 'write_hits', value => 'write_hits_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'write-misses', set => { + key_values => [ { name => 'write_misses' }, { name => 'display' } ], + output_template => 'Write misses: %s /s', + perfdatas => [ + { label => 'write_misses', value => 'write_misses_absolute', template => '%s', + min => 0, unit => '/s', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'evicted-objects', set => { + key_values => [ { name => 'evicted_objects' }, { name => 'display' } ], + output_template => 'Evicted objects: %s', + perfdatas => [ + { label => 'evicted_objects', value => 'evicted_objects_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'expired-objects', set => { + key_values => [ { name => 'expired_objects' }, { name => 'display' } ], + output_template => 'Evicted objects: %s', + perfdatas => [ + { label => 'expired_objects', value => 'expired_objects_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'rdb-changes-since-last-save', set => { + key_values => [ { name => 'rdb_changes_since_last_save' }, { name => 'display' } ], + output_template => 'Rdb changes since last save: %s', + perfdatas => [ + { label => 'rdb_changes_since_last_save', value => 'rdb_changes_since_last_save_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'last-save-time', set => { + key_values => [ { name => 'last_save_time' }, { name => 'last_save_time_sec' }, { name => 'display' } ], + output_template => 'Last same time: %s', + perfdatas => [ + { label => 'last_save_time', value => 'last_save_time_sec_absolute', template => '%s', + min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'mem-frag-ratio', set => { + key_values => [ { name => 'mem_frag_ratio' }, { name => 'display' } ], + output_template => 'Memory fragmentation ratio: %s', + perfdatas => [ + { label => 'mem_frag_ratio', value => 'mem_frag_ratio_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-shard:s" => { name => 'filter_shard' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = "redis_restapi_" . $self->{mode} . '_' . $options{custom}->get_connection_info() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + + my $result = $options{custom}->get(path => '/v1/shards/stats/last'); + my $result2 = $options{custom}->get(path => '/v1/shards'); + + foreach my $shard (keys $result) { + if (defined($self->{option_results}->{filter_shard}) && $self->{option_results}->{filter_shard} ne '' && + $shard !~ /$self->{option_results}->{filter_shard}/) { + $self->{output}->output_add(long_msg => "skipping shard '" . $shard . "': no matching filter.", debug => 1); + next; + } + + $self->{shards}->{$shard} = { + display => $shard, + status => defined($result2->{$shard}->{status}) ? $result2->{$shard}->{status} : '-', + detailed_status => defined($result2->{$shard}->{detailed_status}) ? $result2->{$shard}->{detailed_status} : '-', + role => defined($result2->{$shard}->{role}) ? $result2->{$shard}->{role} : '-', + loading => defined($result2->{$shard}->{loading}->{status}) ? $result2->{$shard}->{loading}->{status} : '-', + sync => defined($result2->{$shard}->{sync}->{status}) ? $result2->{$shard}->{sync}->{status} : '-', + backup => defined($result2->{$shard}->{backup}->{status}) ? $result2->{$shard}->{backup}->{status} : '-', + used_memory => $result->{$shard}->{used_memory}, + instantaneous_ops_per_sec => $result->{$shard}->{instantaneous_ops_per_sec}, + no_of_keys => $result->{$shard}->{no_of_keys}, + connected_clients => $result->{$shard}->{connected_clients}, + blocked_clients => $result->{$shard}->{blocked_clients}, + evicted_objects => $result->{$shard}->{evicted_objects}, + expired_objects => $result->{$shard}->{expired_objects}, + read_hits => $result->{$shard}->{read_hits}, + read_misses => $result->{$shard}->{read_misses}, + write_hits => $result->{$shard}->{write_hits}, + write_misses => $result->{$shard}->{write_misses}, + mem_frag_ratio => $result->{$shard}->{mem_frag_ratio}, + rdb_changes_since_last_save => $result->{$shard}->{rdb_changes_since_last_save}, + last_save_time => centreon::plugins::misc::change_seconds(value => time() - $result->{$shard}->{last_save_time}), + last_save_time_sec => time() - $result->{$shard}->{last_save_time}, + }; + + if (scalar(keys %{$self->{shards}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No shards detected, check your filter ? '); + $self->{output}->option_exit(); + } + } +} + +1; + +__END__ + +=head1 MODE + +Check RedisLabs Enterprise Cluster shards statistics. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='clients' + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{detailed_status}, +%{role}, %{loading}, %{sync}, %{backup}. + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} !~ /active/i'). +Can used special variables like: %{status}, %{detailed_status}, +%{role}, %{loading}, %{sync}, %{backup}. + +=item B<--warning-*> + +Threshold warning. +Can be: 'ops-per-sec', 'memory-used', 'total-keys', +'connected-clients', 'blocked-clients', +'read-hits', 'read-misses', 'write-hits', +'write-misses', 'evicted-objects', 'expired-objects', +'rdb-changes-since-last-save', 'last-save-time', +'mem-frag-ratio'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'ops-per-sec', 'memory-used', 'total-keys', +'connected-clients', 'blocked-clients', +'read-hits', 'read-misses', 'write-hits', +'write-misses', 'evicted-objects', 'expired-objects', +'rdb-changes-since-last-save', 'last-save-time', +'mem-frag-ratio'. + +=back + +=cut diff --git a/apps/redis/restapi/plugin.pm b/apps/redis/restapi/plugin.pm new file mode 100644 index 000000000..e2f85f7ea --- /dev/null +++ b/apps/redis/restapi/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2017 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::redis::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->{version} = '0.1'; + %{$self->{modes}} = ( + 'databasesstats' => 'apps::redis::restapi::mode::databasesstats', + 'clusterstats' => 'apps::redis::restapi::mode::clusterstats', + 'nodesstats' => 'apps::redis::restapi::mode::nodesstats', + 'shardsstats' => 'apps::redis::restapi::mode::shardsstats', + ); + $self->{custom_modes}{api} = 'apps::redis::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check RedisLabs Enterprise Cluster through HTTP/REST API. + +=cut