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

260 lines
9.1 KiB
Perl
Raw Normal View History

2013-12-13 16:14:12 +01:00
#
2020-01-06 15:19:23 +01:00
# Copyright 2020 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::databasessize;
2019-07-10 15:30:23 +02:00
use base qw(centreon::plugins::templates::counter);
2013-12-13 16:14:12 +01:00
use strict;
use warnings;
2019-07-10 15:30:23 +02:00
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
2019-10-22 11:13:27 +02:00
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', cb_init => 'skip_global' },
{ name => 'database', type => 3, cb_prefix_output => 'prefix_database_output',
cb_long_output => 'database_long_output', indent_long_output => ' ',
message_multiple => 'All databases are ok',
2019-07-10 15:30:23 +02:00
group => [
{ name => 'global_db', type => 0, skipped_code => { -10 => 1 } },
2019-10-22 11:13:27 +02:00
{ name => 'table', display_long => 0, cb_prefix_output => 'prefix_table_output',
message_multiple => 'All tables are ok', type => 1, skipped_code => { -10 => 1 } },
2019-07-10 15:30:23 +02:00
]
}
];
$self->{maps_counters}->{global} = [
{ label => 'total-usage', nlabel => 'databases.space.usage.bytes', set => {
key_values => [ { name => 'used' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Used Space: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'used', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0 },
],
}
},
{ label => 'total-free', nlabel => 'databases.space.free.bytes', set => {
key_values => [ { name => 'free' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Free Space: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'free', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0 },
],
}
},
];
$self->{maps_counters}->{global_db} = [
{ label => 'db-usage', nlabel => 'database.space.usage.bytes', set => {
key_values => [ { name => 'used' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Used: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'used', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0, label_extra_instance => 1 },
],
}
},
{ label => 'db-free', nlabel => 'database.space.free.bytes', set => {
key_values => [ { name => 'free' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Free: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'free', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0, label_extra_instance => 1 },
],
}
},
];
2020-01-20 13:06:23 +01:00
2019-07-10 15:30:23 +02:00
$self->{maps_counters}->{table} = [
{ label => 'table-usage', nlabel => 'table.space.usage.bytes', set => {
key_values => [ { name => 'used' }, { name => 'display' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Used: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'used', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0, label_extra_instance => 1 },
],
}
},
{ label => 'table-free', nlabel => 'table.space.free.bytes', set => {
key_values => [ { name => 'free' }, { name => 'display' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Free: %s %s',
2019-07-10 15:30:23 +02:00
output_change_bytes => 1,
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'free', template => '%s', unit => 'B',
2019-07-10 15:30:23 +02:00
min => 0, label_extra_instance => 1 },
],
}
},
{ label => 'table-frag', nlabel => 'table.fragmentation.percentage', set => {
key_values => [ { name => 'frag' }, { name => 'display' } ],
2019-10-22 11:13:27 +02:00
output_template => 'Fragmentation: %.2f %%',
2019-07-10 15:30:23 +02:00
perfdatas => [
2020-05-13 14:18:28 +02:00
{ value => 'frag', template => '%.2f', unit => '%',
2019-07-10 15:30:23 +02:00
min => 0, max => 100, label_extra_instance => 1 },
],
}
},
];
}
2013-12-13 16:14:12 +01:00
2019-10-22 11:13:27 +02:00
sub skip_global {
my ($self, %options) = @_;
scalar(keys %{$self->{database}}) > 1 ? return(0) : return(1);
}
2019-07-10 15:30:23 +02:00
sub prefix_global_output {
my ($self, %options) = @_;
2019-10-22 11:13:27 +02:00
return "Databases Total ";
2013-12-13 16:14:12 +01:00
}
2019-07-10 15:30:23 +02:00
sub prefix_database_output {
2013-12-13 16:14:12 +01:00
my ($self, %options) = @_;
2019-07-10 15:30:23 +02:00
return "Database '" . $options{instance_value}->{display} . "' ";
}
sub database_long_output {
my ($self, %options) = @_;
2019-10-22 11:13:27 +02:00
return "Checking Database '" . $options{instance_value}->{display} . "'";
2013-12-13 16:14:12 +01:00
}
2019-07-10 15:30:23 +02:00
sub prefix_table_output {
2013-12-13 16:14:12 +01:00
my ($self, %options) = @_;
2019-07-10 15:30:23 +02:00
2019-10-22 11:13:27 +02:00
return "Table '" . $options{instance_value}->{display} . "' ";
2019-07-10 15:30:23 +02:00
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
2020-01-20 13:06:23 +01:00
2019-07-10 15:30:23 +02:00
$options{options}->add_options(arguments => {
2020-02-05 14:58:37 +01:00
'filter-database:s' => { name => 'filter_database' },
'filter-table:s' => { name => 'filter_table' },
2019-07-10 15:30:23 +02:00
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
$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.");
$self->{output}->option_exit();
2019-07-10 15:30:23 +02:00
}
$options{sql}->query(
query => q{show variables like 'innodb_file_per_table'}
);
my ($name, $value) = $options{sql}->fetchrow_array();
my $innodb_per_table = 0;
$innodb_per_table = 1 if ($value =~ /on/i);
$options{sql}->query(
query => q{SELECT table_schema, table_name, engine, data_free, data_length+index_length as data_used, (DATA_FREE / (DATA_LENGTH+INDEX_LENGTH)) as TAUX_FRAG FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND engine IN ('InnoDB', 'MyISAM')}
);
my $result = $options{sql}->fetchall_arrayref();
2020-01-20 13:06:23 +01:00
2019-07-10 15:30:23 +02:00
my $innodb_ibdata_done = 0;
$self->{global} = { free => 0, used => 0 };
$self->{database} = {};
2013-12-13 16:14:12 +01:00
foreach my $row (@$result) {
2020-02-05 14:58:37 +01:00
if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' &&
$row->[0] !~ /$self->{option_results}->{filter_database}/) {
$self->{output}->output_add(long_msg => "skipping '" . $row->[0] . '.' . $row->[1] . "': no matching filter.", debug => 1);
next
}
if (defined($self->{option_results}->{filter_table}) && $self->{option_results}->{filter_table} ne '' &&
$row->[1] !~ /$self->{option_results}->{filter_table}/) {
$self->{output}->output_add(long_msg => "skipping '" . $row->[0] . '.' . $row->[1] . "': no matching filter.", debug => 1);
next
}
2020-01-20 13:06:23 +01:00
if (!defined($self->{database}->{$row->[0]})) {
2020-01-02 14:18:35 +01:00
$self->{database}->{$row->[0]} = {
display => $row->[0],
2019-07-10 15:30:23 +02:00
global_db => { free => 0, used => 0 },
table => {}
};
}
2020-01-20 13:06:23 +01:00
$self->{database}->{$row->[0]}->{table}->{$row->[1]} = {
display => $row->[1]
};
# For a table located in the shared tablespace, this is the free space of the shared tablespace.
if ($row->[2] !~ /innodb/i || $innodb_per_table == 1) {
2020-01-02 14:18:35 +01:00
$self->{global}->{free} += $row->[3];
$self->{database}->{$row->[0]}->{global_db}->{free} += $row->[3];
2020-01-20 13:06:23 +01:00
$self->{database}->{$row->[0]}->{table}->{$row->[1]}->{free} = $row->[3];
$self->{database}->{$row->[0]}->{table}->{$row->[1]}->{frag} = $row->[5];
} elsif ($innodb_ibdata_done == 0) {
$self->{global}->{free} += $row->[3];
$innodb_ibdata_done = 1;
2013-12-13 16:14:12 +01:00
}
2020-01-20 13:06:23 +01:00
$self->{global}->{used} += $row->[4];
$self->{database}->{$row->[0]}->{global_db}->{used} += $row->[4];
$self->{database}->{$row->[0]}->{table}->{$row->[1]}->{used} = $row->[4];
2013-12-13 16:14:12 +01:00
}
2019-10-22 11:13:27 +02:00
if (scalar(keys %{$self->{database}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No database found.");
$self->{output}->option_exit();
}
2013-12-13 16:14:12 +01:00
}
1;
__END__
=head1 MODE
2020-02-05 14:58:37 +01:00
Check MySQL databases size and tables.
2013-12-13 16:14:12 +01:00
=over 8
2019-07-10 15:30:23 +02:00
=item B<--filter-database>
2013-12-13 16:14:12 +01:00
2019-10-22 11:13:27 +02:00
Filter database to checks (Can be a regexp).
2020-02-05 14:58:37 +01:00
=item B<--filter-table>
Filter table name (can be a regexp).
2019-10-22 11:13:27 +02:00
=item B<--warning-*> B<--critical-*>
Thresholds (Can be: 'total-usage', 'total-free', 'db-usage',
'db-free', 'table-usage', 'table-free', 'table-frag').
2013-12-13 16:14:12 +01:00
=back
=cut