<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

use Nagios::Plugin;
use DBI;

my $PROGNAME = 'PSQL_QUERY';
my $VERSION  = '0.9';

my $np = Nagios::Plugin-&gt;new(
	usage =&gt; "Usage: %s [ -H &lt;host&gt; ] "
		. "-d &lt;database&gt; -u &lt;username&gt; [ -p &lt;password ]"
		. " [ -c|--critical=&lt;threshold&gt; ] [ -w|--warning=&lt;threshold ] -q &lt;query&gt;",
	version	=&gt; $VERSION,
	plugin 	=&gt; $PROGNAME,
	shortname =&gt; uc $PROGNAME,
	blurb 	=&gt; 'Postgresql query result checking',
	extra	=&gt; "\n\nCopyright (c) 2010 Sergey Zasenko",
);

$np-&gt;add_arg(
	spec =&gt; 'host|H=s',
	help =&gt; '-H, --host=hostname or ip address of pg server',
	required =&gt; 0,
);

$np-&gt;add_arg(
	spec =&gt; 'database|d=s',
	help =&gt; '-d, --database=database to use.',
	required =&gt; 1,
);

$np-&gt;add_arg(
	spec =&gt; 'user|u=s',
	help =&gt; '-u, --user=connect to the database as the user',
	required =&gt; 1,
);


$np-&gt;add_arg(
	spec =&gt; 'password|p=s',
	help =&gt; '-p, --passoword=password for user',
	required =&gt; 0,
);

$np-&gt;add_arg(
	spec =&gt; 'query|q=s',
	help =&gt; '-q, --query=query to execute',
	required =&gt; 1,
);

$np-&gt;add_arg(
	spec =&gt; 'warning|w=s',
	help =&gt; '-w, --warning=THRESHOLD. See '
	. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
	. 'for the threshold format',
);

$np-&gt;add_arg(
	spec =&gt; 'critical|c=s',
	help =&gt; '-c, --critical=THRESHOLD'
);

$np-&gt;getopts;

$np-&gt;set_thresholds( critical =&gt; $np-&gt;opts-&gt;critical, warning =&gt; $np-&gt;opts-&gt;warning );

my $output = 'Unknown error';
my $result = 'UNKNOWN';

eval {
	my $source = 'dbi:Pg:database=' . $np-&gt;opts-&gt;database;
	if ( $np-&gt;opts-&gt;host ) {
		$source .= ';host=' . $np-&gt;opts-&gt;host;
	}

	my $db = DBI-&gt;connect(
		$source,
		$np-&gt;opts-&gt;user, $np-&gt;opts-&gt;password,
		{ ReadOnly =&gt; 1, RaiseError =&gt; 1, AutoCommit =&gt; 0 }
	);

	my $sth = $db-&gt;prepare( $np-&gt;opts-&gt;query );
	$sth-&gt;execute;

	my ( $qresult ) = $sth-&gt;fetchrow_array;

	$sth-&gt;finish;
	$db-&gt;disconnect;

	$np-&gt;add_perfdata(
		label 	=&gt; 'result',
		value 	=&gt; $qresult,
		warning =&gt; $np-&gt;opts-&gt;warning,
		critical =&gt; $np-&gt;opts-&gt;critical,
	);

	$result = $np-&gt;check_threshold( check =&gt; $qresult );
	$output = $np-&gt;opts-&gt;query . " =&gt; $qresult";
};

if ($@) {
	$output = $@;
	$result = 'CRITICAL';
}
$np-&gt;nagios_exit( $result, $output );

	
</pre></body></html>