$Data::Dumper::Indent = 2;
$Data::Dumper::Useqq = 1;
-our $VERSION = '2.1.5';
+our $VERSION = '2.2.0';
use vars qw/ %opt $PSQL $res $COM $SQL $db /;
'checktype=s', ## used by custom_query only
'reverse', ## used by custom_query only
'repinfo=s', ## used by replicate_row only
+ 'schema=s', ## used by fsm_* checks only
)
and keys %opt
and ! @ARGV;
custom_query => [0, 'Run a custom query.'],
database_size => [0, 'Report if a database is too big.'],
disk_space => [1, 'Checks space of local disks Postgres is using.'],
+ fsm_pages => [1, 'Checks percentage of pages used in free space map.'],
+ fsm_relations => [1, 'Checks percentage of relations used in free space map.'],
index_size => [0, 'Checks the size of indexes only.'],
table_size => [0, 'Checks the size of tables only.'],
relation_size => [0, 'Checks the size of tables and indexes.'],
txn_idle => 'ON: stats_command_string(<8.3) VERSION: 8.0',
txn_time => 'VERSION: 8.3',
wal_files => 'VERSION: 8.1',
+ fsm_pages => 'VERSION: 8.2',
+ fsm_relations => 'VERSION: 8.2',
);
if ($opt{test}) {
print "BEGIN TEST MODE\n";
## See how close we are to autovacuum_freeze_max_age
check_autovac_freeze() if $action eq 'autovac_freeze';
+## See how many pages we have used up compared to max_fsm_pages
+check_fsm_pages() if $action eq 'fsm_pages';
+
+## See how many relations we have used up compared to max_fsm_relations
+check_fsm_relations() if $action eq 'fsm_relations';
+
finishup();
exit 0;
} ## end of check_disk_space
+sub check_fsm_pages {
+
+ ## Check on the percentage of free space map pages in use
+ ## Supports: Nagios
+ ## Must run as superuser
+ ## Requires pg_freespacemap contrib module
+ ## Takes an optional --schema argument, defaults to 'public'
+ ## Critical and warning are a percentage of max_fsm_pages
+ ## Example: --critical=95
+
+ my ($warning, $critical) = validate_range
+ ({
+ type => 'percent',
+ default_warning => '85%',
+ default_critical => '95%',
+ });
+
+ my $schema = ($opt{schema}) ? $opt{schema} : 'public';
+
+ (my $w = $warning) =~ s/\D//;
+ (my $c = $critical) =~ s/\D//;
+ my $SQL = qq{SELECT pages, maxx, ROUND(100*(pages/maxx)) AS percent\n}.
+ qq{FROM (SELECT\n}.
+ qq{ (SUM(GREATEST(interestingpages,storedpages))+COUNT(DISTINCT(relfilenode)))*8 AS pages,\n}.
+ qq{ (SELECT setting::NUMERIC FROM pg_settings WHERE name = 'max_fsm_pages') AS maxx\n}.
+ qq{ FROM $schema.pg_freespacemap_relations) x};
+
+ my $info = run_command($SQL, {regex => qr[\w+] } );
+
+ for $db (@{$info->{db}}) {
+ SLURP: while ($db->{slurp} =~ /\s*(\d+) \|\s+(\d+) \|\s+(\d+)$/gsm) {
+ my ($pages,$max,$percent) = ($1,$2,$3);
+
+ my $msg = "fsm page slots used: $pages of $max ($percent%)";
+ if (length $critical and $percent >= $c) {
+ add_critical $msg;
+ }
+ elsif (length $warning and $percent >= $w) {
+ add_warning $msg;
+ }
+ else {
+ add_ok $msg;
+ }
+ }
+
+ }
+
+ return;
+
+} ## end of check_fsm_pages
+
+
+sub check_fsm_relations {
+
+ ## Check on the % of free space map relations in use
+ ## Supports: Nagios
+ ## Must run as superuser
+ ## Requires pg_freespacemap contrib module
+ ## Takes an optional --schema argument, defaults to 'public'
+ ## Critical and warning are a percentage of max_fsm_relations
+ ## Example: --critical=95
+
+ my ($warning, $critical) = validate_range
+ ({
+ type => 'percent',
+ default_warning => '85%',
+ default_critical => '95%',
+ });
+
+ my $schema = ($opt{schema}) ? $opt{schema} : 'public';
+
+ (my $w = $warning) =~ s/\D//;
+ (my $c = $critical) =~ s/\D//;
+
+ my $SQL = qq{SELECT maxx, cur, ROUND(100*(cur/maxx))\n}.
+ qq{FROM (SELECT\n}.
+ qq{ (SELECT COUNT(*) FROM $schema.pg_freespacemap_relations) AS cur,\n}.
+ qq{ (SELECT setting::NUMERIC FROM pg_settings WHERE name='max_fsm_relations') AS maxx) x\n};
+
+ my $info = run_command($SQL, {regex => qr[\w+] } );
+
+ for $db (@{$info->{db}}) {
+ SLURP: while ($db->{slurp} =~ /\s*(\d+) \|\s+(\d+) \|\s+(\d+)$/gsm) {
+ my ($max,$cur,$percent) = ($1,$2,$3);
+
+ my $msg = "fsm relations used: $cur of $max ($percent%)";
+ if (length $critical and $percent >= $c) {
+ add_critical $msg;
+ }
+ elsif (length $warning and $percent >= $w) {
+ add_warning $msg;
+ }
+ else {
+ add_ok $msg;
+ }
+ }
+
+ }
+
+ return;
+
+} ## end of check_fsm_relations
+
+
sub check_wal_files {
## Check on the number of WAL files in use
my $found = 0;
for $db (@{$info->{db}}) {
+
if (!exists $db->{ok}) {
ndie 'Query failed';
}
+
if ($db->{slurp} !~ /\w/ and $USERWHERECLAUSE) {
add_ok 'T-EXCLUDE-USEROK';
next;
=head1 NAME
B<check_postgres.pl> - a Postgres monitoring script for Nagios, MRTG, and others
-This documents describes check_postgres.pl version 2.1.5
+This documents describes check_postgres.pl version 2.2.0
=head1 SYNOPSIS
For MRTG output, returns the size in bytes of the file system on the first line,
and the name of the file system on the fourth line.
+=head2 B<fsm_pages>
+
+(C<symlink: check_postgres_fsm_pages>) Checks how close a cluster is to the Postgres B<max_fsm_pages> setting.
+This action will only work for databases of 8.2 or higher, and it requires the contrib
+module B<pg_freespacemap> be installed. The I<--warning> and I<--critical> options should be expressed
+as percentages. The number of used pages in the free-space-map is determined by looking in the
+pg_freespacemap_relations view, and running a formula based on the formula used for
+outputting free-space-map pageslots in the vacuum verbose command. The default values are B<85%> for the
+warning and B<95%> for the critical.
+
+Example 1: Give a warning when our cluster has used up 76% of the free-space pageslots, with pg_freespacemap installed in database robert
+
+ check_postgres_autovac_freeze --dbname=robert --warning="76%"
+
+While you need to pass in the name of the database where pg_freespacemap is installed (and optionally a schema name if you have
+installed the module in a non-standard schema), you only need to run this check once per cluster. Also, checking this information
+does require obtaining special locks on the free-space-map, so it is recommend you do not run this check with short intervals.
+
+=head2 B<fsm_relations>
+
+(C<symlink: check_postgres_fsm_relations>) Checks how close a cluster is to the Postgres B<max_fsm_relations> setting.
+This action will only work for databases of 8.2 or higher, and it requires the contrib module B<pg_freespacemap> be
+installed. The I<--warning> and I<--critical> options should be expressed as percentages. The number of used relations
+in the free-space-map is determined by looking in the pg_freespacemap_relations view. The default values are B<85%> for
+the warning and B<95%> for the critical.
+
+Example 1: Give a warning when our cluster has used up 80% of the free-space relations, with pg_freespacemap installed in database dylan, in non-standard schema emma
+
+ check_postgres_autovac_freeze --dbname=dylan --warning="75%" --schema=emma
+
+While you need to pass in the name of the database where pg_freespacemap is installed (and optionally a schema name
+if you have installed the module in a non-standard schema), you only need to run this check once per cluster. Also,
+checking this information does require obtaining special locks on the free-space-map, so it is recommend you do not
+run this check with short intervals.
+
=head2 B<index_size>
=head2 B<table_size>
=over 4
-=item B<Version 2.1.5> (September 23, 2008)
+=item B<Version 2.2.0> (September 2008)
- Don't use STDERR bareword. (Chris Butler)
+ Add fsm_pages and fsm_relations actions. (Robert Treat)
=item B<Version 2.1.4> (September 22, 2008)
<li><a href="#custom_query"><strong>custom_query</strong></a></li>
<li><a href="#database_size"><strong>database_size</strong></a></li>
<li><a href="#disk_space"><strong>disk_space</strong></a></li>
+ <li><a href="#fsm_pages"><strong>fsm_pages</strong></a></li>
+ <li><a href="#fsm_relations"><strong>fsm_relations</strong></a></li>
<li><a href="#index_size"><strong>index_size</strong></a></li>
<li><a href="#table_size"><strong>table_size</strong></a></li>
<li><a href="#relation_size"><strong>relation_size</strong></a></li>
<hr />
<h1><a name="name">NAME</a></h1>
<p><strong>check_postgres.pl</strong> - a Postgres monitoring script for Nagios, MRTG, and others
-This documents describes check_postgres.pl version 2.1.4</p>
+This documents describes check_postgres.pl version 2.2.0</p>
<p>
</p>
<hr />
You can also filter the databases by use of the
<em>--include</em> and <em>--exclude</em> options. See the <a href="#basic_filtering">BASIC FILTERING</a> section
for more details.</p>
-<p>Example 1: Give a warning when the number of connections on host quirm reaches 120, and a critical if it reaches 140.</p>
+<p>Example 1: Give a warning when the number of connections on host quirm reaches 120, and a critical if it reaches 150.</p>
<pre>
check_postgres_backends --host=quirm --warning=120 --critical=150</pre>
<p>Example 2: Give a critical when we reach 75% of our max_connections setting on hosts lancre or lancre2.</p>
values, you can look for the variables <em>$MINPAGES</em> and <em>$MINIPAGES</em> at the top of the
<code>check_bloat</code> subroutine.</p>
<p>The schema named 'information_schema' is excluded from this test, as the only tables
-it contains are small ans do not change.</p>
+it contains are small and do not change.</p>
<p>Please note that the values computed by this action are not precise, and
should be used as a guideline only. Great effort was made to estimate the
correct size of a table, but in the end it is only an estimate. The correct
<p>Example 1: Warn if any relation over 100 pages is named "rad":</p>
<pre>
check_postgres_custom_query --checktype=string -w "rad" --query="SELECT relname FROM pg_class WHERE relpages > 100" --port=5432</pre>
-<p>Example 2: Give a critical if the "foobar" function returns over 5GB of bytes:</p>
+<p>Example 2: Give a critical if the "foobar" function returns a number over 5MB:</p>
<pre>
check_postgres_custom_query --port=5432 --critical='5MB'--checktype=size --query="SELECT foobar()"</pre>
<p>Example 2: Warn if the function "snazzo" returns less than 42:</p>
and the name of the file system on the fourth line.</p>
<p>
</p>
+<h2><a name="fsm_pages"><strong>fsm_pages</strong></a></h2>
+<p>(<code>symlink: check_postgres_fsm_pages</code>) Checks how close a cluster is to the Postgres <strong>max_fsm_pages</strong> setting.
+This action will only work for databases of 8.2 or higher, and it requires the contrib
+module <strong>pg_freespacemap</strong> be installed. The <em>--warning</em> and <em>--critical</em> options should be expressed
+as percentages. The number of used pages in the free-space-map is determined by looking in the
+pg_freespacemap_relations view, and running a formula based on the formula used for
+outputting free-space-map pageslots in the vacuum verbose command. The default values are <strong>85%</strong> for the
+warning and <strong>95%</strong> for the critical.</p>
+<p>Example 1: Give a warning when our cluster has used up 76% of the free-space pageslots, with pg_freespacemap installed in database robert</p>
+<pre>
+ check_postgres_autovac_freeze --dbname=robert --warning="76%"</pre>
+<p>While you need to pass in the name of the database where pg_freespacemap is installed (and optionally a schema name if you have
+installed the module in a non-standard schema), you only need to run this check once per cluster. Also, checking this information
+does require obtaining special locks on the free-space-map, so it is recommend you do not run this check with short intervals.</p>
+<p>
+</p>
+<h2><a name="fsm_relations"><strong>fsm_relations</strong></a></h2>
+<p>(<code>symlink: check_postgres_fsm_relations</code>) Checks how close a cluster is to the Postgres <strong>max_fsm_relations</strong> setting.
+This action will only work for databases of 8.2 or higher, and it requires the contrib module <strong>pg_freespacemap</strong> be
+installed. The <em>--warning</em> and <em>--critical</em> options should be expressed as percentages. The number of used relations
+in the free-space-map is determined by looking in the pg_freespacemap_relations view. The default values are <strong>85%</strong> for
+the warning and <strong>95%</strong> for the critical.</p>
+<p>Example 1: Give a warning when our cluster has used up 80% of the free-space relations, with pg_freespacemap installed in database dylan, in non-standard schema emma</p>
+<pre>
+ check_postgres_autovac_freeze --dbname=dylan --warning="75%" --schema=emma</pre>
+<p>While you need to pass in the name of the database where pg_freespacemap is installed (and optionally a schema name
+if you have installed the module in a non-standard schema), you only need to run this check once per cluster. Also,
+checking this information does require obtaining special locks on the free-space-map, so it is recommend you do not
+run this check with short intervals.</p>
+<p>
+</p>
<h2><a name="index_size"><strong>index_size</strong></a></h2>
<p>
</p>
<h1><a name="history">HISTORY</a></h1>
<p>Items not specifically attributed are by Greg Sabino Mullane.</p>
<dl>
+<dt><strong><a name="item_0"><strong>Version 2.2.0</strong> (September 2008)</a></strong></dt>
+
+<dd>
+<pre>
+ Add fsm_pages and fsm_relations actions. (Robert Treat)</pre>
+</dd>
<dt><strong><a name="item_4"><strong>Version 2.1.4</strong> (September 22, 2008)</a></strong></dt>
<dd>
<pre>
Don't check databases with datallowconn false for the "autovac_freeze" action.</pre>
</dd>
-<dt><strong><a name="item_0"><strong>Version 2.1.0</strong> (July 18, 2008)</a></strong></dt>
+<dt><strong><strong>Version 2.1.0</strong> (July 18, 2008)</strong></dt>
<dd>
<pre>