From 8e4d9aed7e6f336b4cabede863dfc1e3f026b678 Mon Sep 17 00:00:00 2001 From: Joshua Tolley Date: Fri, 27 Aug 2010 14:05:51 -0600 Subject: Add several checks for pgbouncer pool statistics --- check_postgres.pl | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 3 deletions(-) (limited to 'check_postgres.pl') diff --git a/check_postgres.pl b/check_postgres.pl index 90f168bbc..e4f885ce6 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_wait => [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_wait') if $action eq 'pgb_pool_cl_wait'; + +## 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 { -- cgit v1.2.3 From 73267d8da17ba4c207950463ee458338a70273a1 Mon Sep 17 00:00:00 2001 From: Joshua Tolley Date: Mon, 30 Aug 2010 08:04:40 -0600 Subject: Fix txn_time to work properly given psql with -x --- check_postgres.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'check_postgres.pl') diff --git a/check_postgres.pl b/check_postgres.pl index e4f885ce6..424b23960 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -6851,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}; @@ -6878,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) { -- cgit v1.2.3 From 7361edc0feb0f3a45f8627467170ad1fa417da32 Mon Sep 17 00:00:00 2001 From: Joshua Tolley Date: Mon, 30 Aug 2010 08:41:31 -0600 Subject: Add docs for new pgb_ checks --- check_postgres.pl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'check_postgres.pl') diff --git a/check_postgres.pl b/check_postgres.pl index 424b23960..6a9a66d8c 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -8091,6 +8091,60 @@ 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 + +=over 4 + +=item pgb_pool_cl_active + +(C) Connections from clients that +are currently associated with a connection to the database + +=item pgb_pool_cl_wait + +(C) Connections from clients that are +waiting for a database connection + +=item pgb_pool_sv_active + +(C) PostgreSQL connections currently +associated with a client + +=item pgb_pool_sv_idle + +(C) PostgreSQL connections ready to be +associated with an incoming client + +=item pgb_pool_sv_used + +(C) PostgreSQL connections just +unlinked from a client and not yet returned to the idle pool + +=item pgb_pool_sv_tested + +(C) PostgreSQL connections currently +being tested + +=item pgb_pool_sv_login + +(C) PostgreSQL connections currently +in the login process + +=item pgb_pool_maxwait + +(C) Time in seconds that the oldest +client in the waiting pool has been waiting for a connection + +=back + +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 associated with an active +server connection. Client connections may also be "waiting", meaning they have +not yet been allocated a server connection. + =head2 B (C) Check on the age of any existing prepared transactions. -- cgit v1.2.3 From 543834722cbcf766e45238d99911f65826769e27 Mon Sep 17 00:00:00 2001 From: Joshua Tolley Date: Mon, 30 Aug 2010 08:54:00 -0600 Subject: Fix typo --- check_postgres.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'check_postgres.pl') diff --git a/check_postgres.pl b/check_postgres.pl index 6a9a66d8c..d003c9ed1 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -851,7 +851,7 @@ our $action_info = { 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_wait => [1, 'Check the number of watiing 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.'], @@ -1490,7 +1490,7 @@ check_pgbouncer_checksum() if $action eq 'pgbouncer_checksum'; 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_wait') if $action eq 'pgb_pool_cl_wait'; +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'; @@ -8100,9 +8100,9 @@ current checksum. (C) Connections from clients that are currently associated with a connection to the database -=item pgb_pool_cl_wait +=item pgb_pool_cl_waiting -(C) Connections from clients that are +(C) Connections from clients that are waiting for a database connection =item pgb_pool_sv_active -- cgit v1.2.3 From cf5fa688c75fdd4429769ab1efdcb3e9c64a3323 Mon Sep 17 00:00:00 2001 From: Joshua Tolley Date: Mon, 30 Aug 2010 10:26:35 -0600 Subject: Edit docs to match other actions better --- check_postgres.pl | 59 ++++++++++++++++++------------------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) (limited to 'check_postgres.pl') diff --git a/check_postgres.pl b/check_postgres.pl index d003c9ed1..0b17a87e6 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -8091,59 +8091,38 @@ 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 +=head2 B -=over 4 - -=item pgb_pool_cl_active - -(C) Connections from clients that -are currently associated with a connection to the database - -=item pgb_pool_cl_waiting - -(C) Connections from clients that are -waiting for a database connection - -=item pgb_pool_sv_active +=head2 B -(C) PostgreSQL connections currently -associated with a client +=head2 B -=item pgb_pool_sv_idle +=head2 B -(C) PostgreSQL connections ready to be -associated with an incoming client +=head2 B -=item pgb_pool_sv_used +=head2 B -(C) PostgreSQL connections just -unlinked from a client and not yet returned to the idle pool +=head2 B -=item pgb_pool_sv_tested +=head2 B -(C) PostgreSQL connections currently -being tested - -=item pgb_pool_sv_login - -(C) PostgreSQL connections currently -in the login process - -=item pgb_pool_maxwait - -(C) Time in seconds that the oldest -client in the waiting pool has been waiting for a connection - -=back +(symlinks: C, C, +C, C, +C, C, +C, and C) 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 associated with an active -server connection. Client connections may also be "waiting", meaning they have -not yet been allocated a server connection. +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 -- cgit v1.2.3