centreon-plugins/database/mysql/mode/threadsconnected.pm

201 lines
6.6 KiB
Perl
Raw Normal View History

2013-12-13 16:14:12 +01:00
#
2021-02-08 09:55:50 +01:00
# Copyright 2021 Centreon (http://www.centreon.com/)
2015-07-21 11:51:02 +02:00
#
# 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.
#
2013-12-13 16:14:12 +01:00
package database::mysql::mode::threadsconnected;
2019-06-13 16:35:18 +02:00
use base qw(centreon::plugins::templates::counter);
2013-12-13 16:14:12 +01:00
use strict;
use warnings;
2019-06-13 16:35:18 +02:00
sub custom_usage_output {
my ($self, %options) = @_;
2021-01-13 15:47:24 +01:00
return sprintf(
'Client connected threads total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
2020-05-13 14:18:28 +02:00
$self->{result_values}->{total},
$self->{result_values}->{used},
$self->{result_values}->{prct_used},
$self->{result_values}->{free},
2021-01-13 15:47:24 +01:00
$self->{result_values}->{prct_free}
);
2019-06-13 16:35:18 +02:00
}
2021-01-13 15:47:24 +01:00
sub prefix_databse_output {
my ($self, %options) = @_;
return "Database '" . $options{instance_value}->{name} . "' ";
}
2019-06-13 16:35:18 +02:00
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } },
2021-01-13 15:47:24 +01:00
{ name => 'databases', type => 1, cb_prefix_output => 'prefix_database_output', skipped_code => { -10 => 1 } }
2019-06-13 16:35:18 +02:00
];
$self->{maps_counters}->{global} = [
{ label => 'usage', nlabel => 'threads.connected.count', set => {
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [
2021-01-13 15:47:24 +01:00
{ template => '%d', min => 0, max => 'total' },
]
2019-06-13 16:35:18 +02:00
}
},
2021-01-13 15:47:24 +01:00
{ label => 'usage-prct', nlabel => 'threads.connected.percentage', display_ok => 0, set => {
key_values => [ { name => 'prct_used' }, { name => 'free' }, { name => 'used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_usage_output'),
2019-06-13 16:35:18 +02:00
perfdatas => [
2021-01-13 15:47:24 +01:00
{ template => '%.2f', min => 0, max => 100, unit => '%' }
]
2019-06-13 16:35:18 +02:00
}
2021-01-13 15:47:24 +01:00
}
];
$self->{maps_counters}->{databases} = [
{ label => 'database-threads-connected', nlabel => 'database.threads.connected.count', display_ok => 0, set => {
key_values => [ { name => 'threads_connected' } ],
output_template => 'threads connected: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
}
2019-06-13 16:35:18 +02:00
];
}
2013-12-13 16:14:12 +01:00
sub new {
my ($class, %options) = @_;
2021-01-13 15:47:24 +01:00
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
2013-12-13 16:14:12 +01:00
bless $self, $class;
2021-01-13 15:47:24 +01:00
$options{options}->add_options(arguments => {
'add-databases' => { name => 'add_databases' }
2019-06-13 16:35:18 +02:00
});
2013-12-13 16:14:12 +01:00
return $self;
}
2021-01-13 15:47:24 +01:00
sub add_databases {
my ($self, %options) = @_;
$options{sql}->query(query => q{
SELECT
schema_name,
SUM(case when DB is not null then 1 else 0 end) as numbers
FROM information_schema.schemata LEFT JOIN information_schema.processlist ON schemata.schema_name = DB
GROUP BY schemata.schema_name
});
$self->{databases} = {};
my $result = $options{sql}->fetchall_arrayref();
foreach my $row (@$result) {
$self->{databases}->{ $row->[0] } = {
name => $row->[0],
threads_connected => $row->[1]
};
}
}
2019-06-13 16:35:18 +02:00
sub manage_selection {
2013-12-13 16:14:12 +01:00
my ($self, %options) = @_;
2017-12-06 15:50:16 +01:00
$options{sql}->connect();
if (!($options{sql}->is_version_minimum(version => '5'))) {
2013-12-13 16:14:12 +01:00
$self->{output}->add_option_msg(short_msg => "MySQL version '" . $self->{sql}->{version} . "' is not supported (need version >= '5.x').");
$self->{output}->option_exit();
}
2019-06-13 16:35:18 +02:00
my $infos = {};
2019-08-07 10:09:15 +02:00
if (!$options{sql}->is_mariadb() && $options{sql}->is_version_minimum(version => '5.7.6')) {
2019-08-02 17:46:08 +02:00
$options{sql}->query(query => q{
SELECT 'max_connections' as name, @@GLOBAL.max_connections as value
UNION
SELECT VARIABLE_NAME as name, VARIABLE_VALUE as value FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Threads_connected'
});
while (my ($name, $value) = $options{sql}->fetchrow_array()) {
$infos->{lc($name)} = $value;
}
} elsif ($options{sql}->is_version_minimum(version => '5.1.12')) {
$options{sql}->query(query => q{
SELECT 'max_connections' as name, @@GLOBAL.max_connections as value
UNION
SELECT VARIABLE_NAME as name, VARIABLE_VALUE as value FROM information_schema.GLOBAL_STATUS WHERE VARIABLE_NAME = 'Threads_connected'
});
while (my ($name, $value) = $options{sql}->fetchrow_array()) {
$infos->{lc($name)} = $value;
}
} else {
$options{sql}->query(query => q{SELECT 'max_connections' as name, @@GLOBAL.max_connections as value});
if (my ($name, $value) = $options{sql}->fetchrow_array()) {
$infos->{lc($name)} = $value
}
$options{sql}->query(query => q{SHOW /*!50000 global */ STATUS LIKE 'Threads_connected'});
if (my ($name, $value) = $options{sql}->fetchrow_array()) {
$infos->{lc($name)} = $value
}
2019-06-13 16:35:18 +02:00
}
if (scalar(keys %$infos) == 0) {
2021-01-13 15:47:24 +01:00
$self->{output}->add_option_msg(short_msg => 'Cannot get number of open connections.');
2013-12-13 16:14:12 +01:00
$self->{output}->option_exit();
}
2019-06-13 16:35:18 +02:00
my $prct_used = $infos->{threads_connected} * 100 / $infos->{max_connections};
$self->{global} = {
total => $infos->{max_connections},
used => $infos->{threads_connected},
free => $infos->{max_connections} - $infos->{threads_connected},
prct_used => $prct_used,
2021-01-13 15:47:24 +01:00
prct_free => 100 - $prct_used
2019-06-13 16:35:18 +02:00
};
2021-01-13 15:47:24 +01:00
if (defined($self->{option_results}->{add_databases})) {
$self->add_databases(sql => $options{sql});
}
2013-12-13 16:14:12 +01:00
}
1;
__END__
=head1 MODE
Check number of open connections.
=over 8
2021-01-13 15:47:24 +01:00
=item B<--add-databases>
Add threads by databases.
=item B<--warning-*> B<--critical-*>
2013-12-13 16:14:12 +01:00
Thresholds.
2021-01-13 15:47:24 +01:00
Can be: 'usage', 'usage-prct' (%), 'database-threads-connected'.
2013-12-13 16:14:12 +01:00
=back
=cut