Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/docker/testing/Dockerfile.testing-plugins-alma10
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dnf clean all
dnf install -y python3.13 python3.13-pip
pip3.13 install robotframework robotframework-examples
# Install snmpsim
pip3.13 install snmpsim pysmi
pip3.13 install snmpsim pysmi cryptography

dnf install -y nodejs24
curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=${PNPM_VERSION} bash -
Expand Down
23 changes: 22 additions & 1 deletion src/centreon/plugins/dbi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,25 @@ sub fetchrow_hashref {
return $self->{statement_handle}->fetchrow_hashref();
}

=head2 $self->query(\%options)

Constructor of the vault object.

%options must provide:

- C<query>: An SQL query to execute on the already connected database (ex: SELECT * FROM table WHERE col like ?)

%options might provide:

- C<continue_error>: either 1 or 0. if 1 and the query preparation fail, plugin exit. if 0, method return 1 if query fail.

- C<param>: array of parameter to use as bind_param. the number of element in the array should match the number of bind made in the query (number of '?')

=cut
sub query {
my ($self, %options) = @_;
my $continue_error = defined($options{continue_error}) && $options{continue_error} == 1 ? 1 : 0;

my $param = $options{param};
$self->{statement_handle} = $self->{instance}->prepare($options{query});
if (!defined($self->{statement_handle})) {
return 1 if ($continue_error == 1);
Expand All @@ -316,6 +331,12 @@ sub query {
$self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
}

if ($param and ref($param) eq 'ARRAY' && scalar(@$param) > 0){
for (my $i = 1; $i <= @$param; $i++) {
$self->{statement_handle}->bind_param($i, @$param[$i-1]);
}

}
my $rv;
if (defined($self->{exec_timeout})) {
my $mask = POSIX::SigSet->new(SIGALRM);
Expand Down
28 changes: 22 additions & 6 deletions src/database/postgres/mode/locks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;

use centreon::plugins::constants qw(:values);
use centreon::plugins::misc qw/is_excluded/;
use centreon::plugins::constants qw(:values :counters);
use centreon::plugins::misc qw/is_excluded is_empty/;

sub new {
my ($class, %options) = @_;
Expand All @@ -39,7 +39,9 @@ sub new {
'include-database:s' => { name => 'include_database', default => '' },
'exclude-database:s' => { name => 'exclude_database', default => '' },
'include:s' => { name => 'include_database' },
'exclude:s' => { name => 'exclude_database' }
'exclude:s' => { name => 'exclude_database' },
'include-locktype:s' => {name => 'include_locktype', default => 'relation'},
'exclude-locktype:s' => {name => 'exclude_locktype', default => ''},
});

return $self;
Expand Down Expand Up @@ -83,7 +85,7 @@ sub set_counters {
my ($self, %options) = @_;

$self->{maps_counters_type} = [
{ name => 'locks', type => 1, cb_prefix_output => 'custom_locks_prefix_output', message_multiple => 'All databases locks are ok', skipped_code => { NO_VALUE() => 1 } },
{ name => 'locks', type => COUNTER_TYPE_INSTANCE(), cb_prefix_output => 'custom_locks_prefix_output', message_multiple => 'All databases locks are ok', skipped_code => { NO_VALUE() => 1 } },
];

$self->{items} = { 'total', 'warning' };
Expand Down Expand Up @@ -136,6 +138,7 @@ sub check_options {
$self->{output}->option_exit(short_msg => "Critical warning ('$label' locks) threshold '" . $value . "'.")
unless $self->{perfdata}->threshold_validate(label => 'crit-' . $label, value => $value);
}

}

sub manage_selection {
Expand All @@ -144,14 +147,17 @@ sub manage_selection {
$options{sql}->connect();

$options{sql}->query(query => q{
SELECT granted, mode, datname FROM pg_database d LEFT JOIN pg_locks l ON (d.oid=l.database) WHERE d.datallowconn
SELECT granted, mode, datname, locktype FROM pg_database d LEFT JOIN pg_locks l ON (d.oid=l.database) WHERE d.datallowconn
});

my $result = $options{sql}->fetchall_arrayref();
$self->{output}->option_exit(short_msg => "No databases found. Do you have sufficient permissions ?") if scalar(@{$result}) == 0;

my $dblocks = {};
foreach my $row (@{$result}) {
my ($granted, $mode, $dbname) = ($$row[0], $$row[1], $$row[2]);
my ($granted, $mode, $dbname, $locktype) = ($$row[0], $$row[1], $$row[2], $$row[3]);
next if is_excluded($dbname, $self->{option_results}->{include_database}, $self->{option_results}->{exclude_database});
next if is_excluded($locktype, $self->{option_results}->{include_locktype}, $self->{option_results}->{exclude_locktype});
if (!defined($dblocks->{$dbname})) {
$dblocks->{$dbname} = {total => 0, waiting => 0, database => $dbname };
# Empty. no lock (left join)
Expand Down Expand Up @@ -182,6 +188,8 @@ sub manage_selection {
push @{$self->{maps_counters}->{locks}}, $new_counter;
}
}
$self->{output}->option_exit(short_msg => "No metrics found. please check the include and exclude filter.")
if !%$dblocks;

$self->{locks} = $dblocks;
}
Expand Down Expand Up @@ -214,6 +222,14 @@ Filter databases using a regular expression.

Exclude databases using a regular expression.

=item B<--include-locktype>

Filter lock type by C<pg_locks.locktype> column. (default: 'relation').

=item B<--exclude-locktype>

Filter lock type by C<pg_locks.locktype> column.

=back

=cut
Loading