convert blocked processes counters (#938)

* Convert database::mssql::mode::blockedprocesses to the counters library

* Using fetchrow_hashref instead of fetchall_arrayref according to @Sims24 recommendation

* Rationalizing a bit the output/long output/debug output
This commit is contained in:
omercier 2018-07-13 15:49:08 +02:00 committed by Colin Gagnaire
parent 8974eb6775
commit 1bc52b5825
1 changed files with 80 additions and 42 deletions

View File

@ -20,60 +20,90 @@
package database::mssql::mode::blockedprocesses;
use base qw(centreon::plugins::mode);
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
{ name => 'processwaittime', type => 1, message_multiple => 'No processes have been waiting for longer than thresholds' },
];
$self->{maps_counters}->{global} = [
{ label => 'total', set => {
key_values => [ { name => 'total' } ],
output_template => 'There are %s blocked processes',
perfdatas => [
{ label => 'total_blocked_processes', value => 'total_absolute', template => '%s',
unit => '', min => 0 },
],
}
},
];
$self->{maps_counters}->{processwaittime} = [
{ label => 'processwaittime', set => {
key_values => [ { name => 'waittime' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_processwaittime_output'),
}
},
];
}
sub custom_processwaittime_output {
my ($self, %options) = @_;
return $self->{result_values}->{display_absolute};
}
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 =>
{
"warning:s" => { name => 'warning', },
"critical:s" => { name => 'critical', },
{
"filter-program:s" => { name => 'filter_program' },
"filter-command:s" => { name => 'filter_command' },
});
return $self;
}
sub check_options {
sub manage_selection {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
$self->{output}->option_exit();
$options{sql}->connect();
$options{sql}->query(query => "SELECT spid, trim(program_name) as program_name, trim(cmd) as cmd, trim(status) as status, waittime FROM master.dbo.sysprocesses WHERE blocked <> '0'");
$self->{global} = { total => 0 };
$self->{processwaittime} = {};
while (my $row = $options{sql}->fetchrow_hashref()) {
# waittime is given in milliseconds, so we convert it to seconds
my $proc_waittime = $row->{waittime} / 1000;
my $proc_identity_verbose = "command '".$row->{cmd}."' (spid ".$row->{spid}.((defined $row->{program_name} && $row->{program_name} ne '')?(" from program '".$row->{program_name}."'"):'').", status '".$row->{status}."') waited ".$proc_waittime."s";
my $proc_identity = 'spid_'.$row->{spid};
if (defined($self->{option_results}->{filter_program}) && $self->{option_results}->{filter_program} ne '' &&
$row->{program_name} !~ /$self->{option_results}->{filter_program}/) {
$self->{output}->output_add(debug => 1, long_msg => "Skipping process having " . $proc_identity_verbose . ": because program is not matching filter.");
next;
}
if (defined($self->{option_results}->{filter_command}) && $self->{option_results}->{filter_command} ne '' &&
$row->{cmd} !~ /$self->{option_results}->{filter_command}/) {
$self->{output}->output_add(debug => 1, long_msg => "Skipping process having " . $proc_identity_verbose . ": because command is not matching filter.");
next
}
# We increment the total number of blocked processes
$self->{global}->{total} += 1;
$self->{processwaittime}->{$proc_identity} = { waittime => $proc_waittime, display => $proc_identity_verbose };
}
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
$self->{output}->option_exit();
}
}
sub run {
my ($self, %options) = @_;
# $options{sql} = sqlmode object
$self->{sql} = $options{sql};
$self->{sql}->connect();
$self->{sql}->query(query => q{SELECT count(*) FROM master.dbo.sysprocesses WHERE blocked <> '0'});
my $blocked = $self->{sql}->fetchrow_array();
my $exit_code = $self->{perfdata}->threshold_check(value => $blocked, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit_code,
short_msg => sprintf("%i blocked process(es).", $blocked));
$self->{output}->perfdata_add(label => 'blocked_processes',
value => $blocked,
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
min => 0);
$self->{output}->display();
$self->{output}->exit();
}
1;
@ -82,17 +112,25 @@ __END__
=head1 MODE
Check MSSQL blocked processes.
Checks if some processes are in a blocked state on a MSSQL Server instance.
=over 8
=item B<--warning>
=item B<--filter-program>
Threshold warning.
Filter results based on the program (client) name (can be a regexp).
=item B<--critical>
=item B<--filter-command>
Threshold critical.
Filter results based on the command name (can be a regexp).
=item B<--warning-*>
Set warning threshold for number of user. Can be : 'total', 'processwaittime'
=item B<--critical-*>
Set critical threshold for number of user. Can be : 'total', 'processwaittime'
=back