+ update tablessize.pm after PR comment

see https://github.com/centreon/centreon-plugins/pull/299
This commit is contained in:
Sims24 2016-01-23 20:05:10 +01:00
parent eeba74091c
commit 4c16b235dc
1 changed files with 25 additions and 30 deletions

View File

@ -35,7 +35,7 @@ sub new {
{
"warning:s" => { name => 'warning', },
"critical:s" => { name => 'critical', },
"db-table:s" => { name => 'db_table', },
"db-table:s@" => { name => 'db_table', },
});
return $self;
@ -53,13 +53,18 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{db_table}) || $self->{option_results}->{db_table} !~ /\./) {
$self->{output}->add_option_msg(short_msg => "Check --db-table option (mandatory) formatting");
if (!defined(@{$self->{option_results}->{db_table}})) {
$self->{output}->add_option_msg(short_msg => "Please define --db-table option");
$self->{output}->option_exit();
}
($self->{db}, $self->{table}) = split(/\./, $self->{option_results}->{db_table}) if (defined ($self->{option_results}->{db_table}));
foreach (@{$self->{option_results}->{db_table}}) {
my ($db, $table) = split /\./;
if (!defined($db) || !defined($table)) {
$self->{output}->add_option_msg(short_msg => "Check --db-table option formatting : '" . $_ . "'");
$self->{output}->option_exit();
}
$self->{filter}->{$db.$table} = 1;
}
}
sub run {
@ -67,13 +72,9 @@ sub run {
# $options{sql} = sqlmode object
$self->{sql} = $options{sql};
$self->{sql}->connect();
my $multiple = 0;
my $query = "SELECT table_name AS NAME, ROUND(data_length+index_length)
FROM information_schema.TABLES
WHERE table_schema = '" . $self->{db}. "' AND table_name LIKE '" . $self->{table} . "'";
$self->{sql}->query(query => $query);
$self->{sql}->query(query => "SELECT table_schema AS DB, table_name AS NAME, ROUND(data_length + index_length)
FROM information_schema.TABLES");
my $result = $self->{sql}->fetchall_arrayref();
if (!($self->{sql}->is_version_minimum(version => '5'))) {
@ -81,28 +82,24 @@ sub run {
$self->{output}->option_exit();
}
if (scalar (@$result) > 1) {
$self->{output}->output_add(severity => 'OK',
short_msg => "All tables are ok.");
$multiple = 1;
}
$self->{output}->output_add(severity => 'OK',
short_msg => "All tables are ok.");
foreach my $row (@$result) {
my $exit_code = $self->{perfdata}->threshold_check(value => $$row[1], threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
my ($value, $value_unit) = $self->{perfdata}->change_bytes(value => $$row[1]);
$self->{output}->output_add(long_msg => sprintf("Table '" . $$row[0] . "' size: %s%s", $value, $value_unit));
if ($multiple == 0 || !$self->{output}->is_status(value => $exit_code, compare => 'ok', litteral => 1)) {
next if (!defined($self->{filter}->{$$row[0].$$row[1]}) || !defined($$row[2]));
my $exit_code = $self->{perfdata}->threshold_check(value => $$row[2], threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
my ($value, $value_unit) = $self->{perfdata}->change_bytes(value => $$row[2]);
$self->{output}->output_add(long_msg => sprintf("Database: '%s' Table: '%s' Size: '%s%s'", $$row[0], $$row[1], $value, $value_unit));
if (!$self->{output}->is_status(value => $exit_code, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit_code,
short_msg => sprintf("Table '%s' size is '%i%s'", $$row[0], $value, $value_unit));
short_msg => sprintf("Table '%s' size in db '%s' is '%i%s'", $$row[0], $$row[1], $value, $value_unit));
}
$self->{output}->perfdata_add(label => $$row[0] . '_size', unit => 'B',
value => $$row[1],
$self->{output}->perfdata_add(label => $$row[0] . '_' . $$row[1] . '_size', unit => 'B',
value => $$row[2],
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
min => 0);
}
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => sprintf("Didn't find table '%s' in '%s' database !", $self->{table}, $self->{db})) if (scalar(@$result) == 0);
$self->{output}->display();
$self->{output}->exit();
@ -128,9 +125,7 @@ Threshold critical in bytes.
=item B<--db-table>
Filter database and table to check [use '%' wildcard with caution!]
e.g unique : --db-table database.table_name
e.g multiple : --db-table database.table_%
Filter database and table to check (multiple: eg: --db-table centreon_storage.data_bin --db-table centreon_storage.logs)
=back