enh mssql plugin database, add list-database mode (#1091)

This commit is contained in:
Colin Gagnaire 2018-08-16 14:56:36 +00:00 committed by Simon Bomm
parent cd3f347d87
commit c77b04491c
4 changed files with 193 additions and 59 deletions

View File

@ -31,16 +31,16 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'database', type => 1, cb_prefix_output => 'prefix_database_output', message_multiple => 'All databases are OK' },
{ name => 'databases', type => 1, cb_prefix_output => 'prefix_database_output', message_multiple => 'All databases are ok' },
];
$self->{maps_counters}->{database} = [
$self->{maps_counters}->{databases} = [
{ label => 'database', set => {
key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'total' }, { name => 'display' } ],
closure_custom_calc => \&custom_usage_calc,
closure_custom_output => \&custom_usage_output,
closure_custom_perfdata => \&custom_usage_perfdata,
closure_custom_threshold_check => \&custom_usage_threshold,
key_values => [ { name => 'free' }, { name => 'total' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_usage_calc'),
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'),
}
},
];
@ -49,14 +49,14 @@ sub set_counters {
sub custom_usage_perfdata {
my ($self, %options) = @_;
my $label = 'db_' . $self->{result_values}->{display} . '_used';
my $label = 'used';
my $value_perf = $self->{result_values}->{used};
if (defined($instance_mode->{option_results}->{free})) {
$label = 'db_' . $self->{result_values}->{display} . '_free';
$label = 'free';
$value_perf = $self->{result_values}->{free};
}
my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
my %total_options = ();
if ($instance_mode->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
@ -80,7 +80,9 @@ sub custom_usage_threshold {
$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->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
$exit = $self->{perfdata}->threshold_check(value => $threshold_value,
threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' },
{ label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
return $exit;
}
@ -102,10 +104,9 @@ sub custom_usage_calc {
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
$self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_prct_used'};
$self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
$self->{result_values}->{prct_used} = $self->{result_values}->{used} / $self->{result_values}->{total} * 100;
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
return 0;
@ -119,9 +120,9 @@ sub new {
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"filter-database:s" => { name => 'filter_database' },
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
"filter-database:s" => { name => 'filter_database' },
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
});
return $self;
}
@ -141,36 +142,29 @@ sub check_options {
sub manage_selection {
my ($self, %options) = @_;
# $options{sql} = sqlmode object
$self->{sql} = $options{sql};
$self->{sql}->connect();
$self->{sql}->query(query => q{DBCC SQLPERF(LOGSPACE)});
my $result = $self->{sql}->fetchall_arrayref();
my @databases_selected;
foreach my $row (@$result) {
next if (defined($self->{option_results}->{filter_database}) && $$row[0] !~ /$self->{option_results}->{filter_database}/);
push @databases_selected, $$row[0];
}
foreach my $database (@databases_selected) {
$self->{sql}->query(query => "use [$database]; exec sp_spaceused;");
foreach my $database (@$result) {
if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' &&
$$database[0] !~ /$self->{option_results}->{filter_database}/i) {
$self->{output}->output_add(debug => 1, long_msg => "Skipping database " . $$database[0] . ": no matching filter.");
next;
}
$self->{sql}->query(query => "use [" . $$database[0] . "]; exec sp_spaceused;");
my $result2 = $self->{sql}->fetchall_arrayref();
foreach my $row (@$result2) {
my $size_brut = $$row[1];
my $size = convert_bytes($size_brut);
my $free_brut = $$row[2];
my $free = convert_bytes($free_brut);
my $used = $size - $free;
my $percent_used = ($used / $size) * 100;
$self->{database}->{$database} = { used => $used,
free => $free,
total => $size,
prct_used => $percent_used,
display => lc $database };
$self->{databases}->{$$row[0]} = {
display => $$row[0],
total => convert_bytes($$row[1]),
free => convert_bytes($$row[2]),
};
}
}
}
@ -200,6 +194,10 @@ Check MSSQL Database usage
=over 8
=item B<--filter-database>
Filter database by name (Can be a regex).
=item B<--warning-database>
Threshold warning.
@ -208,10 +206,6 @@ Threshold warning.
Threshold critical.
=item B<--filter-database>
Filter database by name. Can be a regex
=item B<--units>
Default is '%', can be 'B'

View File

@ -0,0 +1,137 @@
#
# Copyright 2018 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::mssql::mode::listdatabases;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
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::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{sql} = $options{sql};
$self->{sql}->connect();
$self->{sql}->query(query => q{DBCC SQLPERF(LOGSPACE)});
my $result = $self->{sql}->fetchall_arrayref();
foreach my $database (@$result) {
if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' &&
$$database[0] !~ /$self->{option_results}->{filter_database}/i) {
next;
}
$self->{sql}->query(query => "use [" . $$database[0] . "]; exec sp_spaceused;");
my $result2 = $self->{sql}->fetchall_arrayref();
foreach my $row (@$result2) {
$self->{databases}->{$$row[0]} = {
display => $$row[0],
total => convert_bytes($$row[1]),
};
}
}
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $database (sort keys %{$self->{databases}}) {
$self->{output}->output_add(long_msg => sprintf("[name = %s] [total = %s]",
$self->{databases}->{$database}->{name}, $self->{databases}->{$database}->{total}));
}
$self->{output}->output_add(severity => 'OK',
short_msg => 'List databases:');
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['name', 'total']);
}
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $database (sort keys %{$self->{databases}}) {
$self->{output}->add_disco_entry(
name => $self->{databases}->{$database}->{name},
total => $self->{databases}->{$database}->{total},
);
}
}
sub convert_bytes {
my ($brut) = @_;
my ($value,$unit) = split(/\s+/,$brut);
if ($unit =~ /kb*/i) {
$value = $value * 1024;
} elsif ($unit =~ /mb*/i) {
$value = $value * 1024 * 1024;
} elsif ($unit =~ /gb*/i) {
$value = $value * 1024 * 1024 * 1024;
} elsif ($unit =~ /tb*/i) {
$value = $value * 1024 * 1024 * 1024 * 1024;
}
return $value;
}
1;
__END__
=head1 MODE
List MSSQL databases
=over 8
=item B<--filter-database>
Filter database by name (Can be a regex).
=back
=cut

View File

@ -31,7 +31,7 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'log', type => 1, cb_prefix_output => 'prefix_log_output', message_multiple => 'All logs are OK' },
{ name => 'log', type => 1, cb_prefix_output => 'prefix_log_output', message_multiple => 'All logs are ok' },
];
$self->{maps_counters}->{log} = [
@ -49,10 +49,10 @@ sub set_counters {
sub custom_usage_perfdata {
my ($self, %options) = @_;
my $label = 'log_' . $self->{result_values}->{display} . '_used';
my $label = 'log_' . lc($self->{result_values}->{display}) . '_used';
my $value_perf = $self->{result_values}->{used};
if (defined($instance_mode->{option_results}->{free})) {
$label = 'log_' . $self->{result_values}->{display} . '_free';
$label = 'log_' . lc($self->{result_values}->{display}) . '_free';
$value_perf = $self->{result_values}->{free};
}
my %total_options = ();
@ -78,8 +78,9 @@ sub custom_usage_threshold {
$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->{label}, exit_litteral => 'critical' },
{ label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
$exit = $self->{perfdata}->threshold_check(value => $threshold_value,
threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' },
{ label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
return $exit;
}
@ -102,7 +103,6 @@ sub custom_usage_calc {
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
$self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_prct_used'};
$self->{result_values}->{used} = $self->{result_values}->{prct_used} * $self->{result_values}->{total} / 100;
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
@ -118,9 +118,9 @@ sub new {
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"filter-log:s" => { name => 'filter_log' },
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
"filter-log:s" => { name => 'filter_log' },
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
});
return $self;
}
@ -157,9 +157,11 @@ sub manage_selection {
my $total = $$row[1] * 1024 * 1024;
my $prct_used = $$row[2];
$self->{log}->{$$row[0]} = { total => $total,
prct_used => $prct_used,
display => lc $$row[0] };
$self->{log}->{$$row[0]} = {
total => $total,
prct_used => $prct_used,
display => $$row[0]
};
}
if (scalar(keys %{$self->{log}}) <= 0) {
@ -178,6 +180,10 @@ Check MSSQL Log usage
=over 8
=item B<--filter-log>
Filter log by name (Can be a regex).
=item B<--warning-log>
Threshold warning.
@ -186,10 +192,6 @@ Threshold warning.
Threshold critical.
=item B<--filter-log>
Filter log by name. Can be a regex
=item B<--units>
Default is '%', can be 'B'

View File

@ -40,6 +40,7 @@ sub new {
'dead-locks' => 'database::mssql::mode::deadlocks',
'databases-size' => 'database::mssql::mode::databasessize',
'failed-jobs' => 'database::mssql::mode::failedjobs',
'list-databases' => 'database::mssql::mode::listdatabases',
'locks-waits' => 'database::mssql::mode::lockswaits',
'logs-size' => 'database::mssql::mode::logssize',
'sql' => 'centreon::common::protocols::sql::mode::sql',