Skip to content

Commit 1e06b0d

Browse files
Joshua Elsasserdf7cb
Joshua Elsasser
authored andcommitted
Allow a wrapper function for wal_files and archive_ready as a non-superuser.
The wrapper function around pg_ls_dir() must hardcode the pg_xlog or pgxlog/archive_status path, and should take no arguments. It must be created as a superuser with SECURITY DEFINER.
1 parent 6113979 commit 1e06b0d

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

check_postgres.pl

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ package check_postgres;
12431243
'filter=s@', ## used by same_schema only
12441244
'suffix=s', ## used by same_schema only
12451245
'replace', ## used by same_schema only
1246+
'lsfunc=s', ## used by wal_files and archive_ready
12461247
);
12471248

12481249
die $USAGE if ! keys %opt and ! @ARGV;
@@ -8195,8 +8196,11 @@ sub check_wal_files {
81958196

81968197
my ($warning, $critical) = validate_range($arg);
81978198

8199+
my $lsfunc = $opt{lsfunc} || 'pg_ls_dir';
8200+
my $lsargs = $opt{lsfunc} ? "" : "'pg_xlog$subdir'";
8201+
81988202
## Figure out where the pg_xlog directory is
8199-
$SQL = qq{SELECT count(*) AS count FROM pg_ls_dir('pg_xlog$subdir') WHERE pg_ls_dir ~ E'^[0-9A-F]{24}$extrabit\$'}; ## no critic (RequireInterpolationOfMetachars)
8203+
$SQL = qq{SELECT count(*) AS count FROM $lsfunc($lsargs) WHERE $lsfunc ~ E'^[0-9A-F]{24}$extrabit\$'}; ## no critic (RequireInterpolationOfMetachars)
82008204

82018205
my $info = run_command($SQL, {regex => qr[\d] });
82028206

@@ -8587,7 +8591,7 @@ =head2 B<archive_ready>
85878591
85888592
(C<symlink: check_postgres_archive_ready>) Checks how many WAL files with extension F<.ready>
85898593
exist in the F<pg_xlog/archive_status> directory, which is found
8590-
off of your B<data_directory>. This action must be run as a superuser, in order to access the
8594+
off of your B<data_directory>. If the I<--lsfunc> option is not used then this action must be run as a superuser, in order to access the
85918595
contents of the F<pg_xlog/archive_status> directory. The minimum version to use this action is
85928596
Postgres 8.1. The I<--warning> and I<--critical> options are simply the number of
85938597
F<.ready> files in the F<pg_xlog/archive_status> directory.
@@ -8597,9 +8601,26 @@ =head2 B<archive_ready>
85978601
If the archive command fail, number of WAL in your F<pg_xlog> directory will grow until
85988602
exhausting all the disk space and force PostgreSQL to stop immediately.
85998603
8600-
Example 1: Check that the number of ready WAL files is 10 or less on host "pluto"
8604+
To avoid connecting as a database superuser, a wrapper function around
8605+
C<pg_ls_dir()> should be defined as a superuser with SECURITY DEFINER,
8606+
and the I<--lsfunc> option used. This example function, if defined by
8607+
a superuser, will allow the script to connect as a normal user
8608+
I<nagios> with I<--lsfunc=ls_archive_status_dir>
8609+
8610+
BEGIN;
8611+
CREATE FUNCTION ls_archive_status_dir()
8612+
RETURNS SETOF TEXT
8613+
AS $$ SELECT pg_ls_dir('pg_xlog/archive_status') $$
8614+
LANGUAGE SQL
8615+
SECURITY DEFINER;
8616+
REVOKE ALL ON FUNCTION ls_archive_status_dir() FROM PUBLIC;
8617+
GRANT EXECUTE ON FUNCTION ls_archive_status_dir() to nagios;
8618+
COMMIT;
86018619
8602-
check_postgres_archive_ready --host=pluto --critical=10
8620+
Example 1: Check that the number of ready WAL files is 10 or less on host "pluto",
8621+
using a wrapper function C<ls_archive_status_dir> to avoid the need for superuser permissions
8622+
8623+
check_postgres_archive_ready --host=pluto --critical=10 --lsfunc=ls_archive_status_dir
86038624
86048625
For MRTG output, reports the number of ready WAL files on line 1.
86058626
@@ -9862,7 +9883,7 @@ =head2 B<wal_files>
98629883
98639884
(C<symlink: check_postgres_wal_files>) Checks how many WAL files exist in the F<pg_xlog> directory, which is found
98649885
off of your B<data_directory>, sometimes as a symlink to another physical disk for
9865-
performance reasons. This action must be run as a superuser, in order to access the
9886+
performance reasons. If the I<--lsfunc> option is not used then this action must be run as a superuser, in order to access the
98669887
contents of the F<pg_xlog> directory. The minimum version to use this action is
98679888
Postgres 8.1. The I<--warning> and I<--critical> options are simply the number of
98689889
files in the F<pg_xlog> directory. What number to set this to will vary, but a general
@@ -9874,9 +9895,26 @@ =head2 B<wal_files>
98749895
create too many files. Ultimately, this will cause the disk they are on to run
98759896
out of space, at which point Postgres will shut down.
98769897
9877-
Example 1: Check that the number of WAL files is 20 or less on host "pluto"
9878-
9879-
check_postgres_wal_files --host=pluto --critical=20
9898+
To avoid connecting as a database superuser, a wrapper function around
9899+
C<pg_ls_dir()> should be defined as a superuser with SECURITY DEFINER,
9900+
and the I<--lsfunc> option used. This example function, if defined by
9901+
a superuser, will allow the script to connect as a normal user
9902+
I<nagios> with I<--lsfunc=ls_xlog_dir>
9903+
9904+
BEGIN;
9905+
CREATE FUNCTION ls_xlog_dir()
9906+
RETURNS SETOF TEXT
9907+
AS $$ SELECT pg_ls_dir('pg_xlog') $$
9908+
LANGUAGE SQL
9909+
SECURITY DEFINER;
9910+
REVOKE ALL ON FUNCTION ls_xlog_dir() FROM PUBLIC;
9911+
GRANT EXECUTE ON FUNCTION ls_xlog_dir() to nagios;
9912+
COMMIT;
9913+
9914+
Example 1: Check that the number of ready WAL files is 10 or less on host "pluto",
9915+
using a wrapper function C<ls_xlog_dir> to avoid the need for superuser permissions
9916+
9917+
check_postgres_archive_ready --host=pluto --critical=10 --lsfunc=ls_xlog_dir
98809918
98819919
For MRTG output, reports the number of WAL files on line 1.
98829920

0 commit comments

Comments
 (0)