Skip to content

Commit c78f6ec

Browse files
committed
enh(postgres): use include and exclude filter
Refs:CTOR-2319
1 parent 8008a61 commit c78f6ec

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/database/postgres/mode/locks.pm

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use base qw(centreon::plugins::templates::counter);
2525
use strict;
2626
use warnings;
2727

28-
use centreon::plugins::constants qw(:values);
28+
use centreon::plugins::constants qw(:values :counters);
2929
use centreon::plugins::misc qw/is_excluded is_empty/;
3030

3131
sub new {
@@ -40,7 +40,8 @@ sub new {
4040
'exclude-database:s' => { name => 'exclude_database', default => '' },
4141
'include:s' => { name => 'include_database' },
4242
'exclude:s' => { name => 'exclude_database' },
43-
'include-locktype:s' => {name => 'include_locktype', default => '%'},
43+
'include-locktype:s' => {name => 'include_locktype', default => 'relation'},
44+
'exclude-locktype:s' => {name => 'exclude_locktype', default => ''},
4445
});
4546

4647
return $self;
@@ -137,10 +138,7 @@ sub check_options {
137138
$self->{output}->option_exit(short_msg => "Critical warning ('$label' locks) threshold '" . $value . "'.")
138139
unless $self->{perfdata}->threshold_validate(label => 'crit-' . $label, value => $value);
139140
}
140-
# the add_options 'default' property do not transform an empty string to the default value, so we do it here too
141-
if (is_empty($self->{option_results}->{include_locktype})) {
142-
$self->{option_results}->{include_locktype} = '%';
143-
}
141+
144142
}
145143

146144
sub manage_selection {
@@ -149,17 +147,17 @@ sub manage_selection {
149147
$options{sql}->connect();
150148

151149
$options{sql}->query(query => q{
152-
SELECT granted, mode, datname FROM pg_database d LEFT JOIN pg_locks l ON (d.oid=l.database AND locktype like ?) WHERE d.datallowconn
153-
},
154-
param=> [$self->{option_results}->{include_locktype}]);
150+
SELECT granted, mode, datname, locktype FROM pg_database d LEFT JOIN pg_locks l ON (d.oid=l.database) WHERE d.datallowconn
151+
});
155152

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

159156
my $dblocks = {};
160157
foreach my $row (@{$result}) {
161-
my ($granted, $mode, $dbname) = ($$row[0], $$row[1], $$row[2]);
158+
my ($granted, $mode, $dbname, $locktype) = ($$row[0], $$row[1], $$row[2], $$row[3]);
162159
next if is_excluded($dbname, $self->{option_results}->{include_database}, $self->{option_results}->{exclude_database});
160+
next if is_excluded($locktype, $self->{option_results}->{include_locktype}, $self->{option_results}->{exclude_locktype});
163161
if (!defined($dblocks->{$dbname})) {
164162
$dblocks->{$dbname} = {total => 0, waiting => 0, database => $dbname };
165163
# Empty. no lock (left join)
@@ -226,7 +224,11 @@ Exclude databases using a regular expression.
226224
227225
=item B<--include-locktype>
228226
229-
Include lock type using a SQL 'like' format. by default all lock type are included (default: '%').
227+
Filter lock type by C<pg_locks.locktype> column. (default: 'relation').
228+
229+
=item B<--exclude-locktype>
230+
231+
Filter lock type by C<pg_locks.locktype> column.
230232
231233
=back
232234

0 commit comments

Comments
 (0)