Skip to content
Merged
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
175 changes: 175 additions & 0 deletions check_postgres.pl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ package check_postgres;
'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 --PGBINDIR or --PSQL option when NO_PSQL_OPTION is on},
'partman-premake-ok' => q{All premade partitions are present},
'partman-conf-tbl' => q{misconfigured in partman.part_config},
'partman-conf-mis' => q{missing table in partman.part_config},
'pgagent-jobs-ok' => q{No failed jobs},
'pgbouncer-pool' => q{Pool=$1 $2=$3},
'pgb-backends-mrtg' => q{DB=$1 Max connections=$2},
Expand Down Expand Up @@ -1899,6 +1902,7 @@ package check_postgres;
new_version_cp => [0, 'Checks if a newer version of check_postgres.pl is available.'],
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.'],
partman_premake => [1, 'Checks if premake partitions are present.'],
pgb_pool_cl_active => [1, 'Check the number of active clients in each pgbouncer pool.'],
pgb_pool_cl_waiting => [1, 'Check the number of waiting clients in each pgbouncer pool.'],
pgb_pool_sv_active => [1, 'Check the number of active server connections in each pgbouncer pool.'],
Expand Down Expand Up @@ -2735,6 +2739,9 @@ sub finishup {
## Make sure Slony is behaving
check_slony_status() if $action eq 'slony_status';

## Make sure Partman premake is working
check_partman_premake() if $action eq 'partman_premake';

## Verify that the pgbouncer settings are what we think they should be
check_pgbouncer_checksum() if $action eq 'pgbouncer_checksum';

Expand Down Expand Up @@ -6525,6 +6532,171 @@ sub check_pgagent_jobs {
return;
}

sub check_partman_premake {

## Checks if all premade partitions are present
## Monthly and daily interval only
## Supports: Nagios

my $msg = msg('partman-premake-ok');
my $found = 0;
my ($warning, $critical) = validate_range
({
type => 'integer', # in days
default_warning => '1',
default_critical => '3',
});

# check missing Config for range partitioned tables

my $SQL = q{
SELECT
current_database() AS database,
c.relnamespace::regnamespace || '.' || c.relname AS parent_table
FROM
pg_class c
JOIN pg_partitioned_table t ON t.partrelid = c.oid
WHERE
c.relkind = 'p'
AND t.partstrat = 'r'
AND NOT EXISTS (
SELECT
1
FROM
partman.part_config
WHERE
parent_table = c.relnamespace::regnamespace || '.' || c.relname);
};

my $info = run_command($SQL, {regex => qr[\w+], emptyok => 1 } );
my (@crit,@warn,@ok);

for $db (@{$info->{db}}) {
my ($maxage,$maxdb) = (0,''); ## used by MRTG only
ROW: for my $r (@{$db->{slurp}}) {
my ($dbname,$parent_table) = ($r->{database},$r->{parent_table});
$found = 1 if ! $found;
next ROW if skip_item($dbname);
$found = 2;

$msg = "$dbname=$parent_table " . msg('partman-conf-mis');
push @crit => $msg;
};
};

# check Config Errors

$SQL = q{
SELECT
current_database() as database,
parent_table
FROM (
SELECT
parent_table,
retention,
partition_interval,
EXTRACT(EPOCH FROM retention::interval) / EXTRACT(EPOCH FROM partition_interval::interval) AS configured_partitions
FROM
partman.part_config) p
WHERE
configured_partitions < 1;
};

$info = run_command($SQL, {regex => qr[\w+], emptyok => 1 } );

for $db (@{$info->{db}}) {
my ($maxage,$maxdb) = (0,''); ## used by MRTG only
ROW: for my $r (@{$db->{slurp}}) {
my ($dbname,$parent_table) = ($r->{database},$r->{parent_table});
$found = 1 if ! $found;
next ROW if skip_item($dbname);
$found = 2;

$msg = "$dbname=$parent_table " . msg('partman-conf-tbl');
push @warn => $msg;
};
};


$SQL = q{
SELECT
current_database() as database,
a.parent_table,
b.date - a.date::date AS missing_days
FROM
(
SELECT parent_table, date
FROM ( SELECT
i.inhparent::regclass as parent_table,
substring(pg_catalog.pg_get_expr(c.relpartbound, i.inhrelid)::text FROM '%TO __#"_{10}#"%' FOR '#') as date,
rank() OVER (PARTITION BY i.inhparent ORDER BY pg_catalog.pg_get_expr(c.relpartbound, i.inhrelid) DESC)
FROM pg_inherits i
JOIN pg_class c ON c.oid = i.inhrelid
WHERE c.relkind = 'r'
AND pg_catalog.pg_get_expr(c.relpartbound, i.inhrelid) != 'DEFAULT') p
WHERE
p.rank = 1
) a
JOIN
(
SELECT
parent_table,
(now() + premake * partition_interval::interval)::date
FROM
partman.part_config
) b
ON
a.parent_table::text = b.parent_table::text
WHERE
b.date - a.date::date > 0
ORDER BY 3, 2 DESC
};

$info = run_command($SQL, {regex => qr[\w+], emptyok => 1 } );

for $db (@{$info->{db}}) {
my ($maxage,$maxdb) = (0,''); ## used by MRTG only
ROW: for my $r (@{$db->{slurp}}) {
my ($dbname,$parent_table,$missing_days) = ($r->{database},$r->{parent_table},$r->{missing_days});
$found = 1 if ! $found;
next ROW if skip_item($dbname);
$found = 2;

$msg = "$dbname=$parent_table ($missing_days)";
print "$msg";
$db->{perf} .= sprintf ' %s=%sd;%s;%s',
perfname($dbname), $missing_days, $warning, $critical;
if (length $critical and $missing_days >= $critical) {
push @crit => $msg;
}
elsif (length $warning and $missing_days >= $warning) {
push @warn => $msg;
}
else {
push @ok => $msg;
}
}
if (0 == $found) {
add_ok msg('partman-premake-ok');
}
elsif (1 == $found) {
add_unknown msg('no-match-db');
}
elsif (@crit) {
add_critical join ' ' => @crit;
}
elsif (@warn) {
add_warning join ' ' => @warn;
}
else {
add_ok join ' ' => @ok;
}
}

return;

} ## end of check_partman_premake

sub check_pgbouncer_checksum {

## Verify the checksum of all pgbouncer settings
Expand Down Expand Up @@ -11028,12 +11200,15 @@ =head1 HISTORY

Add --role flag to explicitly set the role of the user after connecting (David Christensen)

Add Partman premake check (Jens Wilke)

Add to docs how to exclude all items in the 'pg_temp_nnn' per-session temporary schemas (Michael Banck)

Various fixes for the CI system (Emre Hasegeli)

Various improvements to the tests (Christoph Berg, Emre Hasegeli)


=item B<Version 2.25.0> Released February 3, 2020

Allow same_schema objects to be included or excluded with --object and --skipobject
Expand Down