From 151181d2834b385b0c6007956c6b0504c9d0a4e5 Mon Sep 17 00:00:00 2001 From: Giles Westwood Date: Tue, 1 Nov 2022 21:58:58 +0000 Subject: [PATCH] add-option-to-check_postgres-to-run-over-all-databases: adding alldb option to make some actions more generic --- check_postgres.pl | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/check_postgres.pl b/check_postgres.pl index 37d334a..35f8eb0 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -1687,6 +1687,9 @@ package check_postgres; 'critical=s', 'include=s@', 'exclude=s@', + 'alldb', + 'includedb=s@', + 'excludedb=s@', 'includeuser=s@', 'excludeuser=s@', @@ -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 @@ -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; @@ -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) { @@ -4516,6 +4560,8 @@ sub check_bloat { } } + } + ## Set a sorted limited perf $db->{perf} = ''; my $count = 0; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy