Skip to content

Add support to exclude by application name on txn_* checks #78

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 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Add support for application_name filter on long running transactions and
long idle in transaction.

Good for filtering applications like pg_dump, but also others without
having to run them with a specific user.
  • Loading branch information
martinmarques committed Jun 4, 2015
commit b3f7d7b4eba7de5f7ce07bc4cd685fde05232174
57 changes: 54 additions & 3 deletions check_postgres.pl
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ package check_postgres;

## These options are multiples ('@s')
for my $arr (qw/include exclude includeuser excludeuser host port
dbuser dbname dbpass dbservice schema/) {
dbuser dbname dbpass dbservice schema excludeapp
includeapp/) {
next if $name ne $arr and $name ne "${arr}2";
push @{$tempopt{$name}} => $value;
## Don't set below as a normal value
Expand Down Expand Up @@ -961,6 +962,8 @@ package check_postgres;
'exclude=s@',
'includeuser=s@',
'excludeuser=s@',
'excludeapp=s@',
'includeapp=s@',

'host|dbhost|H|dbhost1|H1=s@',
'port|dbport|p|port1|dbport1|p1=s@',
Expand Down Expand Up @@ -1224,6 +1227,8 @@ package check_postgres;
--exclude=name(s) items to specifically exclude (e.g. tables), depends on the action
--includeuser=include objects owned by certain users
--excludeuser=exclude objects owned by certain users
--excludeapp=exclude objects by application_name
--includeapp=include objects by application_name

Other options:
--assume-standby-mode assume that server in continious WAL recovery mode
Expand Down Expand Up @@ -1871,6 +1876,52 @@ sub finishup {
}
}

our $APPWHERECLAUSE = '';
if ($opt{includeapp}) {
my %applist;
for my $app (@{$opt{includeapp}}) {
for my $a2 (split /,/ => $app) {
$applist{$a2}++;
}
}
my $safeapp;
if (1 == keys %applist) {
($safeapp = each %applist) =~ s/'/''/g;
$APPWHERECLAUSE = " AND application_name = '$safeapp'";
}
else {
$APPWHERECLAUSE = ' AND application_name IN (';
for my $app (sort keys %applist) {
($safeapp = $app) =~ s/'/''/g;
$APPWHERECLAUSE .= "'$safeapp',";
}
chop $APPWHERECLAUSE;
$APPWHERECLAUSE .= ')';
}
}
elsif ($opt{excludeapp}) {
my %applist;
for my $app (@{$opt{excludeapp}}) {
for my $a2 (split /,/ => $app) {
$applist{$a2}++;
}
}
my $safeapp;
if (1 == keys %applist) {
($safeapp = each %applist) =~ s/'/''/g;
$APPWHERECLAUSE = " AND application_name <> '$safeapp'";
}
else {
$APPWHERECLAUSE = ' AND application_name NOT IN (';
for my $app (sort keys %applist) {
($safeapp = $app) =~ s/'/''/g;
$APPWHERECLAUSE .= "'$safeapp',";
}
chop $APPWHERECLAUSE;
$APPWHERECLAUSE .= ')';
}
}

## Check number of connections, compare to max_connections
check_backends() if $action eq 'backends';

Expand Down Expand Up @@ -7599,7 +7650,7 @@ sub check_txn_idle {
$SQL = q{SELECT datname, datid, procpid AS pid, usename, client_addr, xact_start, current_query AS current_query, '' AS state, }.
q{CASE WHEN client_port < 0 THEN 0 ELSE client_port END AS client_port, }.
qq{COALESCE(ROUND(EXTRACT(epoch FROM now()-$start)),0) AS seconds }.
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE }.
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE $APPWHERECLAUSE }.
q{ORDER BY xact_start, query_start, procpid DESC};
## Craft an alternate version for old servers that do not have the xact_start column:
($SQL2 = $SQL) =~ s/xact_start/query_start AS xact_start/;
Expand All @@ -7609,7 +7660,7 @@ sub check_txn_idle {
$SQL2 = $SQL = q{SELECT datname, datid, procpid AS pid, usename, client_addr, current_query AS current_query, '' AS state, }.
q{CASE WHEN client_port < 0 THEN 0 ELSE client_port END AS client_port, }.
qq{COALESCE(ROUND(EXTRACT(epoch FROM now()-$start)),0) AS seconds }.
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE }.
qq{FROM pg_stat_activity WHERE ($clause)$USERWHERECLAUSE $APPWHERECLAUSE }.
q{ORDER BY query_start, procpid DESC};
}

Expand Down
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