Skip to content

add-option-to-check_postgres-to-run-over-all-databases: adding alldb option to make some actions more generic #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions check_postgres.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,9 @@ package check_postgres;
'critical=s',
'include=s@',
'exclude=s@',
'alldb',
'includedb=s@',
'excludedb=s@',
'includeuser=s@',
'excludeuser=s@',

Expand Down Expand Up @@ -1960,6 +1963,9 @@ package check_postgres;
-c value, --critical=value the critical threshold, range depends on the action
--include=name(s) items to specifically include (e.g. tables), depends on the action
--exclude=name(s) items to specifically exclude (e.g. tables), depends on the action
--alldb list all postgres databases and run action over them
--excludedb=name regex filter for the alldb option to select only certain databases
--includedb=name regex filter for the alldb option to select only certain databases
--includeuser=include objects owned by certain users
--excludeuser=exclude objects owned by certain users

Expand Down Expand Up @@ -2103,6 +2109,43 @@ sub msg_en {
$opt{defaultdb} = $psql_version >= 8.0 ? 'postgres' : 'template1';
$opt{defaultdb} = 'pgbouncer' if $action =~ /^pgb/;

## If alldb is set then run a psql command to find out all the databases
if (defined $opt{alldb}){

my $pg_port = $opt{defaultport};
if ($opt{port}[0]){
$pg_port = $opt{port}[0];
}
my $psql_output = join(",", map /^([\w|-]+?)\|/, qx{$PSQL -A -l -t -p $pg_port });
my $pg_db;
# optionally exclude or include each db
my @psql_output_array = split(/,/, $psql_output);
for $pg_db (@psql_output_array) {
if (defined $opt{includedb}){
if ($pg_db =~ /$opt{includedb}[0]/) {
# do nothing
} else {
# strip the database from the listing
$psql_output =~ s/($pg_db),//;
}
}
if (defined $opt{excludedb}){
if ($pg_db =~ /$opt{excludedb}[0]/) {
# strip the database from the listing
$psql_output =~ s/($pg_db),//;
} else {
# do nothing
}
}
}
# strip out some dbs we're not interested in
$psql_output =~ s/(template0,)//;
$psql_output =~ s/(root,)//;
# pg8.4
$psql_output =~ s/(,:)//g;
$opt{dbname}[0] = $psql_output;
}

## Check the current database mode
our $STANDBY = 0;
our $MASTER = 0;
Expand Down Expand Up @@ -4450,6 +4493,7 @@ sub check_bloat {
## The perf must be added before the add_x, so we defer the settings:
my (@addwarn, @addcrit);

for $db (@{$info->{db}}) {
for my $r (@{ $db->{slurp} }) {

for my $v (values %$r) {
Expand Down Expand Up @@ -4516,6 +4560,8 @@ sub check_bloat {
}
}

}

## Set a sorted limited perf
$db->{perf} = '';
my $count = 0;
Expand Down