From fdbe0bb7d5f9c222a88f2c1b005eaa6bc81e6cc2 Mon Sep 17 00:00:00 2001 From: Simon Bomm Date: Fri, 28 Oct 2022 10:36:59 +0200 Subject: [PATCH] (plugin) database::mssql::mode::* - Refactor some old-fashionned modes ... (#4023) * Refactor legacy MSSQL modes * Typos --- .../database/mssql/mode/cachehitratio.pm | 71 +++++------- .../database/mssql/mode/connectedusers.pm | 61 ++++------ .../database/mssql/mode/deadlocks.pm | 109 ++++++++---------- .../database/mssql/mode/lockswaits.pm | 108 ++++++++--------- 4 files changed, 146 insertions(+), 203 deletions(-) diff --git a/centreon-plugins/database/mssql/mode/cachehitratio.pm b/centreon-plugins/database/mssql/mode/cachehitratio.pm index 29a2a05fd..e817f2f81 100644 --- a/centreon-plugins/database/mssql/mode/cachehitratio.pm +++ b/centreon-plugins/database/mssql/mode/cachehitratio.pm @@ -20,46 +20,44 @@ package database::mssql::mode::cachehitratio; -use base qw(centreon::plugins::mode); - use strict; use warnings; +use base qw(centreon::plugins::templates::counter); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'hit_ratio', type => 0 }, + ]; + + $self->{maps_counters}->{hit_ratio} = [ + { label => 'hit-ratio', nlabel => 'mssql.cache.hitratio.percentage', set => { + key_values => [ { name => 'value' } ], + output_template => 'Buffer cache hit ratio is %.2f%%', + perfdatas => [ + { template => '%s', unit => '%', min => 0, max => 100 }, + ], + } + }, + ]; +} sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => {}); 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(); - } - 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{ + $options{sql}->connect(); + $options{sql}->query(query => q{ SELECT CAST( ( SELECT CAST (cntr_value AS BIGINT) @@ -74,21 +72,10 @@ sub run { ) AS NUMERIC(6,3) ) }); + my $hitratio = $self->{sql}->fetchrow_array(); + $self->{hit_ratio}->{value} = $hitratio; - my $exit_code = $self->{perfdata}->threshold_check(value => $hitratio, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("Buffer cache hit ratio is %.2f%%", $hitratio)); - $self->{output}->perfdata_add(label => 'cache_hitratio', - value => sprintf("%d",$hitratio), - unit => '%', - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0, - max => 100); - - $self->{output}->display(); - $self->{output}->exit(); } 1; @@ -101,11 +88,11 @@ Check MSSQL buffer cache hit ratio. =over 8 -=item B<--warning> +=item B<--warning-hit-ratio> Threshold warning. -=item B<--critical> +=item B<--critical-hit-ratio> Threshold critical. diff --git a/centreon-plugins/database/mssql/mode/connectedusers.pm b/centreon-plugins/database/mssql/mode/connectedusers.pm index c476e8482..8a1b7d0bf 100644 --- a/centreon-plugins/database/mssql/mode/connectedusers.pm +++ b/centreon-plugins/database/mssql/mode/connectedusers.pm @@ -20,59 +20,48 @@ package database::mssql::mode::connectedusers; -use base qw(centreon::plugins::mode); - use strict; use warnings; +use base qw(centreon::plugins::templates::counter); sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => {}); return $self; } -sub check_options { +sub set_counters { 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(); - } - 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(); - } + $self->{maps_counters_type} = [ + { name => 'connected_user', type => 0 }, + ]; + + $self->{maps_counters}->{connected_user} = [ + { label => 'connected-user', nlabel => 'mssql.users.connected.count', set => { + key_values => [ { name => 'value' } ], + output_template => '%i connected user(s)', + perfdatas => [ + { template => '%i', min => 0 }, + ], + } + }, + ]; } -sub run { +sub manage_selection { my ($self, %options) = @_; - # $options{sql} = sqlmode object - $self->{sql} = $options{sql}; - $self->{sql}->connect(); - $self->{sql}->query(query => q{SELECT count(*) FROM master..sysprocesses WHERE spid >= '51'}); - my $users = $self->{sql}->fetchrow_array(); + $options{sql}->connect(); + $options{sql}->query(query => q{SELECT count(*) FROM master..sysprocesses WHERE spid >= '51'}); - my $exit_code = $self->{perfdata}->threshold_check(value => $users, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("%i Connected user(s).", $users)); - $self->{output}->perfdata_add(label => 'connected_users', - value => $users, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); + my $connected_count = $self->{sql}->fetchrow_array(); + $self->{connected_user}->{value} = $connected_count; - $self->{output}->display(); - $self->{output}->exit(); } 1; @@ -85,11 +74,11 @@ Check MSSQL connected users. =over 8 -=item B<--warning> +=item B<--warning-connected-user> Threshold warning. -=item B<--critical> +=item B<--critical-connected-user> Threshold critical. diff --git a/centreon-plugins/database/mssql/mode/deadlocks.pm b/centreon-plugins/database/mssql/mode/deadlocks.pm index 25da20e32..1a2e46f83 100644 --- a/centreon-plugins/database/mssql/mode/deadlocks.pm +++ b/centreon-plugins/database/mssql/mode/deadlocks.pm @@ -20,82 +20,65 @@ package database::mssql::mode::deadlocks; -use base qw(centreon::plugins::mode); - use strict; use warnings; +use base qw(centreon::plugins::templates::counter); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'deadlocks', type => 0 }, + ]; + + $self->{maps_counters}->{deadlocks} = [ + { label => 'deadlocks', nlabel => 'mssql.deadlocks.count', set => { + key_values => [ { name => 'value' } ], + output_template => '%.2f dead locks/s', + perfdatas => [ + { template => '%.2f', min => 0 }, + ], + } + }, + ]; +} + sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "filter:s" => { name => 'filter', }, - }); + $options{options}->add_options(arguments => { + "filter-database:s" => { name => 'filter_database' }, + }); 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(); - } - 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(); - } -} + $options{sql}->connect(); + $options{sql}->query(query => q{ + SELECT + instance_name, cntr_value + FROM + sys.dm_os_performance_counters + WHERE + object_name = 'SQLServer:Locks' + AND + counter_name = 'Number of Deadlocks/sec%' + }); -sub run { - my ($self, %options) = @_; - # $options{sql} = sqlmode object - $self->{sql} = $options{sql}; - - $self->{sql}->connect(); - my $query = "SELECT - instance_name, cntr_value - FROM - sys.dm_os_performance_counters - WHERE - object_name = 'SQLServer:Locks' - AND - counter_name = 'Number of Deadlocks/sec%' - "; - - $self->{sql}->query(query => $query); - my $result = $self->{sql}->fetchall_arrayref(); + my $query_result = $self->{sql}->fetchall_arrayref(); + $self->{deadlocks}->{value} = 0; - my $locks = 0; - - $self->{output}->output_add(severity => 'OK', - short_msg => "0 dead locks/s."); - foreach my $row (@$result) { - next if (defined($self->{option_results}->{filter}) && $$row[0] !~ /$self->{option_results}->{filter}/); - $locks += $$row[1]; + foreach my $row (@{$query_result}) { + next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' + && $$row[0] !~ /$self->{option_results}->{filter_database}/); + $self->{deadlocks}->{value} += $$row[1]; } - my $exit_code = $self->{perfdata}->threshold_check(value => $locks, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add(long_msg => sprintf( "%i dead locks/s.", $locks)); - if (!$self->{output}->is_status(value => $exit_code, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("%i dead locks/s.", $locks)); - } - $self->{output}->perfdata_add(label => 'dead_locks', - value => $locks, - unit => '/s', - 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; @@ -108,15 +91,15 @@ Check MSSQL dead locks per second =over 8 -=item B<--warning> +=item B<--warning-deadlocks> Threshold warning number of dead locks per second. -=item B<--critical> +=item B<--critical-deadlocks> Threshold critical number of dead locks per second. -=item B<--filter> +=item B<--filter-database> Filter database to check. diff --git a/centreon-plugins/database/mssql/mode/lockswaits.pm b/centreon-plugins/database/mssql/mode/lockswaits.pm index 889e9d791..eb624dc65 100644 --- a/centreon-plugins/database/mssql/mode/lockswaits.pm +++ b/centreon-plugins/database/mssql/mode/lockswaits.pm @@ -20,81 +20,65 @@ package database::mssql::mode::lockswaits; -use base qw(centreon::plugins::mode); - use strict; use warnings; +use base qw(centreon::plugins::templates::counter); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'lockswaits', type => 0 }, + ]; + + $self->{maps_counters}->{deadlocks} = [ + { label => 'lockswaits', nlabel => 'mssql.lockswaits.count', set => { + key_values => [ { name => 'value' } ], + output_template => '%.2f dead locks/s', + perfdatas => [ + { template => '%.2f', min => 0 }, + ], + } + }, + ]; +} + sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "filter:s" => { name => 'filter', }, - }); + $options{options}->add_options(arguments => { + "filter-database:s" => { name => 'filter_database' }, + }); 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(); - } - 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(); - } -} + $options{sql}->connect(); + $options{sql}->query(query => q{ + SELECT + instance_name, cntr_value + FROM + sys.dm_os_performance_counters + WHERE + object_name = 'SQLServer:Locks' + AND + counter_name = 'Lock Waits/sec%' + }); -sub run { - my ($self, %options) = @_; - # $options{sql} = sqlmode object - $self->{sql} = $options{sql}; - - $self->{sql}->connect(); - my $query = "SELECT - instance_name, cntr_value - FROM - sys.dm_os_performance_counters - WHERE - object_name = 'SQLServer:Locks' - AND - counter_name = 'Lock Waits/sec%' - "; - - $self->{sql}->query(query => $query); - my $result = $self->{sql}->fetchall_arrayref(); + my $query_result = $self->{sql}->fetchall_arrayref(); + $self->{lockswaits}->{value} = 0; - my $locks = 0; - - $self->{output}->output_add(severity => 'OK', - short_msg => "0 Locks Waits/s."); - foreach my $row (@$result) { - next if (defined($self->{option_results}->{filter}) && $$row[0] !~ /$self->{option_results}->{filter}/); - $locks += $$row[1]; + foreach my $row (@{$query_result}) { + next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne '' + && $$row[0] !~ /$self->{option_results}->{filter_database}/); + $self->{lockswaits}->{value} += $$row[1]; } - my $exit_code = $self->{perfdata}->threshold_check(value => $locks, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - if (!$self->{output}->is_status(value => $exit_code, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("%i Locks Waits/s.", $locks)); - } - $self->{output}->perfdata_add(label => 'locks_waits', - value => $locks, - unit => '/s', - 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; @@ -107,15 +91,15 @@ Check MSSQL locks-waits per second =over 8 -=item B<--warning> +=item B<--warning-lockswait> Threshold warning number of lock-waits per second. -=item B<--critical> +=item B<--critical-lockswait> Threshold critical number of lock-waits per second. -=item B<--filter> +=item B<--filter-database> Filter database to check.