Skip to content

Commit

Permalink
Add a new service pgss_dealloc
Browse files Browse the repository at this point in the history
  • Loading branch information
anayrat committed Sep 14, 2022
1 parent 80b26f9 commit 523bd0f
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion check_pgactivity
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ my %services = (
'sub' => \&check_uptime,
'desc' => 'Time since postmaster start or configurtion reload.'
},
'pgss_dealloc' => {
'sub' => \&check_pg_stat_statements_dealloc,
'desc' => 'Check rate of pg_stat_statements dealloc.'
},
);


Expand Down Expand Up @@ -6227,7 +6231,6 @@ sub check_oldest_xmin {
return status_ok( $me, \@msg, \@perfdata );
}


=item B<pg_dump_backup>
Check the age and size of backups.
Expand Down Expand Up @@ -6547,6 +6550,114 @@ sub check_pg_dump_backup {
return status_ok( $me, [], \@perfdata );
}

=item B<pgss_dealloc>
Check number of pg_stat_statements dealloc.
Many dealloc can have performance impact. This could be due to:
Application performing many different queries:
* ORM
* different utility statements (SET application_name...). In this case,
try to disable pg_stat_statements.track_utility.
pg_stat_statements.max is too low
This service uses the status file (see C<--status-file> parameter).
The C<--critical> and C<--warning> thresholds are optional.
Warning or critical will be raised if dealloc rate are above those thresholds.
Perfdata returns the dealloc rate since last call.
Required privileges: unprivileged role able to log in database where
pg_stat_statements extensions has been created.
=cut

sub check_pg_stat_statements_dealloc {

my @rs;
my @perfdata;
my @perf_limits;
my @hosts;
my %dealloc;
my %new_dealloc;
my $dealloc_rate;
my $crit;
my $warn;
my %args = %{ $_[0] };
my $me = 'POSTGRES_PG_STAT_STATEMENTS_DEALLOC';
my $sql = q{
SELECT
floor(extract(EPOCH from now())),
dealloc
FROM pg_stat_statements_info
};

@hosts = @{ parse_hosts %args };

pod2usage(
-message => 'FATAL: you must give only one host with service "commit_ratio".',
-exitval => 127
) if @hosts != 1;

is_compat $hosts[0], 'check_pg_stat_statements_dealloc', $PG_VERSION_140 or exit 1;

# Warning and critical are optional
pod2usage(
-message => "FATAL: you must specify both critical and warning thresholds.",
-exitval => 127
) if ((defined $args{'warning'} and not defined $args{'critical'})
or (not defined $args{'warning'} and defined $args{'critical'})) ;

if (defined $args{'warning'} and defined $args{'critical'}) {
pod2usage(
-message => "FATAL: warning or critical threshold must be numeric.",
-exitval => 127
) if ($args{'warning'} !~ m/^([0-9.]+)/) or ($args{'critical'} !~ m/^([0-9.]+)/) ;

$warn = $args{'warning'};
$crit = $args{'critical'};
@perf_limits = ( $warn, $crit );
}

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

@rs = @{ query( $hosts[0], $sql ) };

%new_dealloc = (
'ts' => $rs[0][0],
'dealloc' => $rs[0][1]
);

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

return status_ok( $me, ['First call'] ) unless %dealloc;

$dealloc_rate = ($new_dealloc{'dealloc'} - $dealloc{'dealloc'}) / ($new_dealloc{'ts'} - $dealloc{'ts'});

push @perfdata => (
[ "pgss_dealloac_rate", sprintf( "%.2f", $dealloc_rate ), 'tps' , @perf_limits]
);

if ( $dealloc_rate < 0) {
return status_unknown( $me, [ sprintf "Dealloc rate can't be negative: %.2f dealloc/s",$dealloc_rate], \@perfdata)
}

if ( $crit and $dealloc_rate > $crit) {
return status_critical( $me, [ sprintf "Dealloc rate: %.2f dealloc/s",$dealloc_rate], \@perfdata)
}

if ( $warn and $dealloc_rate > $warn) {
return status_warning( $me, [ sprintf "Dealloc rate: %.2f dealloc/s",$dealloc_rate], \@perfdata)
}

return status_ok( $me, [sprintf "Dealloc rate: %.2f dealloc/s",$dealloc_rate], \@perfdata);
}

=item B<pga_version>
Check if this script is running the given version of check_pgactivity.
Expand Down

0 comments on commit 523bd0f

Please sign in to comment.