diff options
author | Greg Sabino Mullane | 2010-09-14 15:42:49 +0000 |
---|---|---|
committer | Greg Sabino Mullane | 2010-09-14 15:42:49 +0000 |
commit | c80788afd42c336097b374724efeab898a1e5500 (patch) | |
tree | 06cae72e35856da9458d68c258da1d2d9b629dac /check_postgres.pl | |
parent | 2325db6736f68c27a533f70704b50d19144c76f2 (diff) | |
parent | cf5fa688c75fdd4429769ab1efdcb3e9c64a3323 (diff) |
Merge branch 'master' of [email protected]:/var/lib/git/check_postgres
Diffstat (limited to 'check_postgres.pl')
-rwxr-xr-x | check_postgres.pl | 111 |
1 files changed, 105 insertions, 6 deletions
diff --git a/check_postgres.pl b/check_postgres.pl index 4f34fbc05..2b99cf6b2 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -173,6 +173,7 @@ our %msg = ( 'opt-psql-nofind' => q{Could not find a suitable psql executable}, 'opt-psql-nover' => q{Could not determine psql version}, 'opt-psql-restrict' => q{Cannot use the --PSQL option when NO_PSQL_OPTION is on}, + 'pgbouncer-pool' => q{Pool=$1 $2=$3}, 'PID' => q{PID}, 'port' => q{port}, 'preptxn-none' => q{No prepared transactions found}, @@ -849,6 +850,14 @@ our $action_info = { new_version_pg => [0, 'Checks if a newer version of Postgres is available.'], new_version_tnm => [0, 'Checks if a newer version of tail_n_mail is available.'], pgbouncer_checksum => [0, 'Check that no pgbouncer settings have changed since the last check.'], + pgb_pool_cl_active => [1, 'Check the number of active clients in each pgbouncer pool.'], + pgb_pool_cl_waiting => [1, 'Check the number of watiing clients in each pgbouncer pool.'], + pgb_pool_sv_active => [1, 'Check the number of active server connections in each pgbouncer pool.'], + pgb_pool_sv_idle => [1, 'Check the number of idle server connections in each pgbouncer pool.'], + pgb_pool_sv_used => [1, 'Check the number of used server connections in each pgbouncer pool.'], + pgb_pool_sv_tested => [1, 'Check the number of tested server connections in each pgbouncer pool.'], + pgb_pool_sv_login => [1, 'Check the number of login server connections in each pgbouncer pool.'], + pgb_pool_maxwait => [1, 'Check the current maximum wait time for client connections in pgbouncer pools.'], prepared_txns => [1, 'Checks number and age of prepared transactions.'], query_runtime => [0, 'Check how long a specific query takes to run.'], query_time => [1, 'Checks the maximum running time of current queries.'], @@ -1022,7 +1031,7 @@ our $psql_version = $1; $VERBOSE >= 2 and warn qq{psql=$PSQL version=$psql_version\n}; $opt{defaultdb} = $psql_version >= 8.0 ? 'postgres' : 'template1'; -$opt{defaultdb} = 'pgbouncer' if $action eq 'pgbouncer_checksum'; +$opt{defaultdb} = 'pgbouncer' if ($action eq 'pgbouncer_checksum' || $action =~ /^pgb_/); sub add_response { @@ -1477,6 +1486,30 @@ check_slony_status() if $action eq 'slony_status'; ## Verify that the pgbouncer settings are what we think they should be check_pgbouncer_checksum() if $action eq 'pgbouncer_checksum'; +## Check the number of active clients in each pgbouncer pool +check_pgb_pool('cl_active') if $action eq 'pgb_pool_cl_active'; + +## Check the number of watiing clients in each pgbouncer pool +check_pgb_pool('cl_waiting') if $action eq 'pgb_pool_cl_waiting'; + +## Check the number of active server connections in each pgbouncer pool +check_pgb_pool('sv_active') if $action eq 'pgb_pool_sv_active'; + +## Check the number of idle server connections in each pgbouncer pool +check_pgb_pool('sv_idle') if $action eq 'pgb_pool_sv_idle'; + +## Check the number of used server connections in each pgbouncer pool +check_pgb_pool('sv_used') if $action eq 'pgb_pool_sv_used'; + +## Check the number of tested server connections in each pgbouncer pool +check_pgb_pool('sv_tested') if $action eq 'pgb_pool_sv_tested'; + +## Check the number of login server connections in each pgbouncer pool +check_pgb_pool('sv_login') if $action eq 'pgb_pool_sv_login'; + +## Check the current maximum wait time for client connections in pgbouncer pools +check_pgb_pool('maxwait') if $action eq 'pgb_pool_maxwait'; + ## ## Everything past here does not hit a Postgres database ## @@ -1847,7 +1880,7 @@ sub run_command { my $dbtimeout = $timeout * 1000; alarm 0; - if ($action ne 'pgbouncer_checksum') { + if ($action ne 'pgbouncer_checksum' and $action !~ /^pgb_/) { $string = "BEGIN;SET statement_timeout=$dbtimeout;COMMIT;$string"; } @@ -4433,7 +4466,7 @@ sub check_pgbouncer_checksum { $SQL = 'SHOW CONFIG'; my $info = run_command($SQL, { regex => qr[log_pooler_errors] }); - $db = $info->{db}; + $db = $info->{db}[0]; my $newstring = ''; for my $r (@{$db->{slurp}}) { @@ -4470,6 +4503,39 @@ sub check_pgbouncer_checksum { } ## end of check_pgbouncer_checksum +sub check_pgb_pool { + # Check various bits of the pgbouncer SHOW POOLS ouptut + my $stat = shift; + my ($warning, $critical) = validate_range({type => 'positive integer'}); + + $SQL = 'SHOW POOLS'; + my $info = run_command($SQL, { regex => qr[$stat] }); + + $db = $info->{db}[0]; + my $output = $db->{slurp}; + my $gotone = 0; + for my $i (@$output) { + next if skip_item($i->{database}); + my $msg = "$i->{database}=$i->{$stat}"; + + if ($MRTG) { + $stats{$i->{database}} = $i->{$stat}; + $statsmsg{$i->{database}} = msg('pgbouncer-pool', $i->{database}, $stat, $i->{$stat}); + next; + } + + if ($critical and $i->{$stat} >= $critical) { + add_critical $msg; + } + elsif ($warning and $i->{$stat} >= $warning) { + add_warning $msg; + } + else { + add_ok $msg; + } + } + +} ## end of check_pgb_pool sub check_prepared_txns { @@ -6785,14 +6851,14 @@ SELECT client_addr, client_port, procpid, - ROUND(EXTRACT(epoch FROM now()-xact_start)), + ROUND(EXTRACT(epoch FROM now()-xact_start)) AS maxtime, datname, usename FROM pg_stat_activity WHERE xact_start IS NOT NULL $USERWHERECLAUSE }; - my $info = run_command($SQL, { regex => qr{\d+ \|\s+\s+}, emptyok => 1 } ); + my $info = run_command($SQL, { regex => qr{\| \d+\n}, emptyok => 1 } ); $db = $info->{db}[0]; my $slurp = $db->{slurp}; @@ -6812,7 +6878,7 @@ WHERE xact_start IS NOT NULL $USERWHERECLAUSE ## Read in and parse the psql output for my $r (@{$db->{slurp}}) { - my ($add,$port,$pid,$time,$dbname,$user) = @$r{qw/ client_addr client_port procpid username maxtime maxdb /}; + my ($add,$port,$pid,$time,$dbname,$user) = @$r{qw/ client_addr client_port procpid maxtime datname usename /}; next if skip_item($dbname); if ($time >= $maxtime) { @@ -8025,6 +8091,39 @@ For MRTG output, returns a 1 or 0 indicating success of failure of the checksum checksum must be provided as the C<--mrtg> argument. The fourth line always gives the current checksum. +=head2 B<pgb_pool_cl_active> + +=head2 B<pgb_pool_cl_waiting> + +=head2 B<pgb_pool_sv_active> + +=head2 B<pgb_pool_sv_idle> + +=head2 B<pgb_pool_sv_used> + +=head2 B<pgb_pool_sv_tested> + +=head2 B<pgb_pool_sv_login> + +=head2 B<pgb_pool_maxwait> + +(symlinks: C<check_postgres_pgb_pool_cl_active>, C<check_postgres_pgb_pool_cl_waiting>, +C<check_postgres_pgb_pool_sv_active>, C<check_postgres_pgb_pool_sv_idle>, +C<check_postgres_pgb_pool_sv_used>, C<check_postgres_pgb_pool_sv_tested>, +C<check_postgres_pgb_pool_sv_login>, and C<check_postgres_pgb_pool_maxwait>) + +Examines pgbouncer's pool statistics. Each pool has a set of "client" +connections, referring to connections from external clients, and "server" +connections, referring to connections to PostgreSQL itself. The related +check_postgres actions are prefixed by "cl_" and "sv_", respectively. Active +client connections are those connections currently linked with an active server +connection. Client connections may also be "waiting", meaning they have not yet +been allocated a server connection. Server connections are "active" (linked to +a client), "idle" (standing by for a client connection to link with), "used" +(just unlinked from a client, and not yet returned to the idle pool), and +"login" (in the process of logging in). The maxwait value shows how long in +seconds the oldest waiting client connection has been waiting. + =head2 B<prepared_txns> (C<symlink: check_postgres_prepared_txns>) Check on the age of any existing prepared transactions. |