Skip to content
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

#301 session/active time #305

Closed
wants to merge 2 commits into from
Closed
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
60 changes: 58 additions & 2 deletions check_pgactivity
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ my $PG_VERSION_100 = 100000;
my $PG_VERSION_110 = 110000;
my $PG_VERSION_120 = 120000;
my $PG_VERSION_130 = 130000;
my $PG_VERSION_140 = 140000;

# reference to the output sub
my $output_fmt;
Expand Down Expand Up @@ -2153,6 +2154,10 @@ sub check_backends {
my @perfdata;
my @msg;
my @hosts;
my %stime;
my %new_stime;
my $global_session_time;
my $global_active_time;
my %args = %{ $_[0] };
my $me = 'POSTGRES_BACKENDS';
my $num_backends = 0;
Expand Down Expand Up @@ -2225,6 +2230,18 @@ sub check_backends {
JOIN pg_catalog.pg_database AS d ON d.oid = s.datid
WHERE backend_type = 'client backend'
GROUP BY d.datname
) AS s },
$PG_VERSION_140 => q{
SELECT s.datname, s.count, current_setting('max_connections')::int
- current_setting('superuser_reserved_connections')::int,
s.session_time, s.active_time
FROM (
SELECT d.datname, st.session_time, st.active_time, count(*)
FROM pg_catalog.pg_stat_activity AS s
JOIN pg_catalog.pg_database AS d ON d.oid = s.datid
JOIN pg_catalog.pg_stat_database AS st ON st.datid = s.datid
WHERE backend_type = 'client backend'
GROUP BY d.datname, st.session_time, st.active_time
) AS s }
);

Expand All @@ -2248,9 +2265,22 @@ sub check_backends {
-exitval => 127
) if @hosts != 1;


@rs = @{ query_ver( $hosts[0], %queries ) };

if (check_compat $hosts[0], $PG_VERSION_140) {

%stime = %{ load( $hosts[0], 'backends', $args{'status-file'} ) || {} };

$new_stime{$_->[0]} = {
'session' => $_->[3],
'active' => $_->[4]
} foreach @rs;

save $hosts[0], 'backends', \%new_stime, $args{'status-file'};

return status_ok( $me, ['First call'] ) unless keys %stime;
}

$args{'critical'} = int( $rs[0][2] * $1 / 100 )
if $args{'critical'} =~ /^([0-9.]+)%$/;

Expand All @@ -2259,9 +2289,30 @@ sub check_backends {

LOOP_DB: foreach my $db (@rs) {
$num_backends += $db->[1];
my $dbn = $db->[0]; # database name

push @perfdata, [
$db->[0], $db->[1], '', $args{'warning'}, $args{'critical'}, 0, $db->[2]
$dbn, $db->[1], '', $args{'warning'}, $args{'critical'}, 0, $db->[2]
];

if (check_compat $hosts[0], $PG_VERSION_140 and keys %{$stime{$dbn}}) {

my $ratio = 0;
my $session_time = $new_stime{$dbn}{'session'} - $stime{$dbn}{'session'};
my $active_time = $new_stime{$dbn}{'active'} - $stime{$dbn}{'active'};

$global_session_time += $session_time;
$global_active_time += $active_time;

$ratio = $active_time * 100 / ( $session_time + $active_time )
unless $active_time == 0;

push @perfdata => (
[ "$dbn"."_session_time", sprintf( "%.2f", $session_time ), 'ms' ],
[ "$dbn"."_active_time", sprintf( "%.2f", $active_time ), 'ms' ],
[ "$dbn"."_active_ratio", sprintf( "%.2f", $ratio ), '%' ]
);
}
}

push @perfdata, [
Expand All @@ -2270,6 +2321,11 @@ sub check_backends {

push @msg => "$num_backends connections on $rs[0][2]";

if (check_compat $hosts[0], $PG_VERSION_140) {
push @msg => "Session time: " . sprintf( "%.2f", $global_session_time ) . "ms";
push @msg => "Active time: " . sprintf( "%.2f", $global_active_time ) . "ms";
}

return status_critical( $me, \@msg, \@perfdata )
if $num_backends >= $args{'critical'};

Expand Down