From f239f24974e4edb9f6671df6b4114cba0fbfe6a4 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 17 Jan 2019 14:30:20 +0100 Subject: [PATCH] add cassandra jmx plugin --- database/cassandra/jmx/mode/cachesusage.pm | 185 ++++++++++++++++++ .../cassandra/jmx/mode/clientrequestsusage.pm | 161 +++++++++++++++ .../cassandra/jmx/mode/threadpoolsusage.pm | 172 ++++++++++++++++ database/cassandra/jmx/plugin.pm | 51 +++++ 4 files changed, 569 insertions(+) create mode 100644 database/cassandra/jmx/mode/cachesusage.pm create mode 100644 database/cassandra/jmx/mode/clientrequestsusage.pm create mode 100644 database/cassandra/jmx/mode/threadpoolsusage.pm create mode 100644 database/cassandra/jmx/plugin.pm diff --git a/database/cassandra/jmx/mode/cachesusage.pm b/database/cassandra/jmx/mode/cachesusage.pm new file mode 100644 index 000000000..0d35b7bb0 --- /dev/null +++ b/database/cassandra/jmx/mode/cachesusage.pm @@ -0,0 +1,185 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package database::cassandra::jmx::mode::cachesusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'ccache', type => 1, cb_prefix_output => 'prefix_ccache_output', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{ccache} = [ + { label => 'used', set => { + key_values => [ { name => 'Capacity_Value' }, { name => 'Size_Value' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + threshold_use => 'prct_used', + perfdatas => [ + { label => 'used', value => 'prct_used', template => '%.2f', + unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'hits', set => { + key_values => [ { name => 'Requests_Count', diff => 1 }, { name => 'Hits_Count', diff => 1 }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_hits_calc'), + output_template => 'Hits = %.2f %%', output_use => 'hits_prct', + perfdatas => [ + { label => 'hits', value => 'hits_prct', template => '%.2f', min => 0, max => 100, unit => '%' }, + ], + } + }, + ]; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); + my $msg = sprintf("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}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); + return $msg; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_Capacity_Value'}; + $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_Size_Value'}; + $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; + $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}; + + return 0; +} + +sub custom_hits_calc { + my ($self, %options) = @_; + + my $label = $options{extra_options}->{label_ref}; + my $delta_value = $options{new_datas}->{$self->{instance} . '_Hits_Count'} - $options{old_datas}->{$self->{instance} . '_Hits_Count'}; + my $delta_total = $options{new_datas}->{$self->{instance} . '_Requests_Count'} - $options{old_datas}->{$self->{instance} . '_Requests_Count'}; + + $self->{result_values}->{hits_prct} = 0; + if ($delta_total > 0) { + $self->{result_values}->{hits_prct} = $delta_value * 100 / $delta_total; + } + 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-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub prefix_ccache_output { + my ($self, %options) = @_; + + return "Cache '" . $options{instance_value}->{display} . "' "; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{ccache} = {}; + $self->{request} = [ + { mbean => 'org.apache.cassandra.metrics:name=Requests,scope=*,type=Cache', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=Hits,scope=*,type=Cache', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=Capacity,scope=*,type=Cache', attributes => [ { name => 'Value' } ] }, # bytes + { mbean => 'org.apache.cassandra.metrics:name=Size,scope=*,type=Cache', attributes => [ { name => 'Value' } ] }, # bytes + ]; + + my $result = $options{custom}->get_attributes(request => $self->{request}, nothing_quit => 1); + foreach my $mbean (keys %{$result}) { + $mbean =~ /scope=(.*?)(?:,|$)/; + my $scope = $1; + $mbean =~ /name=(.*?)(?:,|$)/; + my $name = $1; + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); + next; + } + + $self->{ccache}->{$scope} = { display => $scope } if (!defined($self->{ccache}->{$scope})); + foreach (keys %{$result->{$mbean}}) { + $self->{ccache}->{$scope}->{$name . '_' . $_} = $result->{$mbean}->{$_}; + } + } + + if (scalar(keys %{$self->{ccache}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No cache found."); + $self->{output}->option_exit(); + } + + $self->{cache_name} = "cassandra_" . $self->{mode} . '_' . md5_hex($options{custom}->get_connection_info()) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check cache usage. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='latency' + +=item B<--filter-name> + +Filter name (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'used' (%), 'hits' (%). + +=back + +=cut diff --git a/database/cassandra/jmx/mode/clientrequestsusage.pm b/database/cassandra/jmx/mode/clientrequestsusage.pm new file mode 100644 index 000000000..3b18e8a96 --- /dev/null +++ b/database/cassandra/jmx/mode/clientrequestsusage.pm @@ -0,0 +1,161 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package database::cassandra::jmx::mode::clientrequestsusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'cr', type => 1, cb_prefix_output => 'prefix_cr_output', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{cr} = [ + { label => 'total-latency', set => { + key_values => [ { name => 'TotalLatency_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Total Latency : %s us', + perfdatas => [ + { label => 'total_latency', value => 'TotalLatency_Count_absolute', template => '%s', + min => 0, unit => 'us', label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'timeouts', set => { + key_values => [ { name => 'Timeouts_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Timeouts : %s', + perfdatas => [ + { label => 'timeouts', value => 'Timeouts_Count_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'unavailables', set => { + key_values => [ { name => 'Unavailables_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Unavailables : %s', + perfdatas => [ + { label => 'unavailbles', value => 'Unavailables_Count_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'failures', set => { + key_values => [ { name => 'Failures_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Failures : %s', + perfdatas => [ + { label => 'failures', value => 'Failures_Count_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-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub prefix_thpool_output { + my ($self, %options) = @_; + + return "Client Request '" . $options{instance_value}->{display} . "' "; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cr} = {}; + $self->{request} = [ + { mbean => 'org.apache.cassandra.metrics:name=TotalLatency,scope=*,type=ClientRequest', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=Timeouts,scope=*,type=ClientRequest', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=Unavailables,scope=*,type=ClientRequest', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=Failures,scope=*,type=ClientRequest', attributes => [ { name => 'Count' } ] }, + ]; + + my $result = $options{custom}->get_attributes(request => $self->{request}, nothing_quit => 1); + foreach my $mbean (keys %{$result}) { + $mbean =~ /scope=(.*?)(?:,|$)/; + my $scope = $1; + $mbean =~ /name=(.*?)(?:,|$)/; + my $name = $1; + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); + next; + } + + $self->{cr}->{$scope} = { display => $scope } if (!defined($self->{cr}->{$scope})); + foreach (keys %{$result->{$mbean}}) { + $self->{cr}->{$scope}->{$name . '_' . $_} = $result->{$mbean}->{$_}; + } + } + + if (scalar(keys %{$self->{cr}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No client request found."); + $self->{output}->option_exit(); + } + + $self->{cache_name} = "cassandra_" . $self->{mode} . '_' . md5_hex($options{custom}->get_connection_info()) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check client requests usage. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='latency' + +=item B<--filter-name> + +Filter name (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total-latency', 'timeouts', 'unavailables', 'failures'. + +=back + +=cut diff --git a/database/cassandra/jmx/mode/threadpoolsusage.pm b/database/cassandra/jmx/mode/threadpoolsusage.pm new file mode 100644 index 000000000..c5cabfa5f --- /dev/null +++ b/database/cassandra/jmx/mode/threadpoolsusage.pm @@ -0,0 +1,172 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package database::cassandra::jmx::mode::threadpoolsusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'thpool', type => 1, cb_prefix_output => 'prefix_thpool_output', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{thpool} = [ + { label => 'active-tasks', set => { + key_values => [ { name => 'ActiveTasks_Value' }, { name => 'display' } ], + output_template => 'Current Active Tasks : %s', + perfdatas => [ + { label => 'active_tasks', value => 'ActiveTasks_Value_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'pending-tasks', set => { + key_values => [ { name => 'PendingTasks_Value' }, { name => 'display' } ], + output_template => 'Current Pending Tasks : %s', + perfdatas => [ + { label => 'pending_tasks', value => 'PendingTasks_Value_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'total-completed-tasks', set => { + key_values => [ { name => 'CompletedTasks_Value', diff => 1 }, { name => 'display' } ], + output_template => 'Total Completed Tasks : %s', + perfdatas => [ + { label => 'total_completed_tasks', value => 'CompletedTasks_Value_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'total-blocked-tasks', set => { + key_values => [ { name => 'TotalBlockedTasks_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Total Blocked Tasks : %s', + perfdatas => [ + { label => 'total_blocked_tasks', value => 'TotalBlockedTasks_Count_absolute', template => '%s', + min => 0, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + { label => 'current-blocked-tasks', set => { + key_values => [ { name => 'CurrentlyBlockedTasks_Count', diff => 1 }, { name => 'display' } ], + output_template => 'Total Currently Blocked Tasks : %s', + perfdatas => [ + { label => 'current_blocked_tasks', value => 'CurrentlyBlockedTasks_Count_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-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub prefix_thpool_output { + my ($self, %options) = @_; + + return "Thread Pool '" . $options{instance_value}->{display} . "' "; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{thpool} = {}; + $self->{request} = [ + { mbean => 'org.apache.cassandra.metrics:name=ActiveTasks,path=request,scope=*,type=ThreadPools', attributes => [ { name => 'Value' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=PendingTasks,path=request,scope=*,type=ThreadPools', attributes => [ { name => 'Value' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=CompletedTasks,path=request,scope=*,type=ThreadPools', attributes => [ { name => 'Value' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=TotalBlockedTasks,path=request,scope=*,type=ThreadPools', attributes => [ { name => 'Count' } ] }, + { mbean => 'org.apache.cassandra.metrics:name=CurrentlyBlockedTasks,path=request,scope=*,type=ThreadPools', attributes => [ { name => 'Count' } ] }, + ]; + + my $result = $options{custom}->get_attributes(request => $self->{request}, nothing_quit => 1); + foreach my $mbean (keys %{$result}) { + $mbean =~ /scope=(.*?)(?:,|$)/; + my $scope = $1; + $mbean =~ /name=(.*?)(?:,|$)/; + my $name = $1; + + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); + next; + } + + $self->{thpool}->{$scope} = { display => $scope } if (!defined($self->{thpool}->{$scope})); + foreach (keys %{$result->{$mbean}}) { + $self->{thpool}->{$scope}->{$name . '_' . $_} = $result->{$mbean}->{$_}; + } + } + + if (scalar(keys %{$self->{thpool}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No thread pool found."); + $self->{output}->option_exit(); + } + + $self->{cache_name} = "cassandra_" . $self->{mode} . '_' . md5_hex($options{custom}->get_connection_info()) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check thread pools usage. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='active-tasks' + +=item B<--filter-name> + +Filter name (can be a regexp). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'active-tasks', 'pending-tasks', 'total-completed-tasks', 'total-blocked-tasks', +'current-blocked-tasks'. + +=back + +=cut diff --git a/database/cassandra/jmx/plugin.pm b/database/cassandra/jmx/plugin.pm new file mode 100644 index 000000000..15403c6b6 --- /dev/null +++ b/database/cassandra/jmx/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package database::cassandra::jmx::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}} = ( + 'caches-usage' => 'database::cassandra::jmx::mode::cachesusage', + 'client-requests-usage' => 'database::cassandra::jmx::mode::clientrequestsusage', + 'thread-pools-usage' => 'database::cassandra::jmx::mode::threadpoolsusage', + ); + + $self->{custom_modes}{jolokia} = 'centreon::common::protocols::jmx::custom::jolokia'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Cassandra in JMX. Need Jolokia agent. + +=cut