[check_postgres] [commit] Add support for pg_service.conf, major version bump.

check_postgres at bucardo.org check_postgres at bucardo.org
Thu Dec 4 17:15:48 UTC 2008


Committed by Greg Sabino Mullane <greg at endpoint.com>

Add support for pg_service.conf, major version bump.

---
 check_postgres.pl      |   68 +++++++++++++++++++++++++++++++++++------------
 check_postgres.pl.html |   18 ++++++++++++-
 index.html             |    8 +++---
 3 files changed, 71 insertions(+), 23 deletions(-)

diff --git a/check_postgres.pl b/check_postgres.pl
index 854d717..adba695 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -28,7 +28,7 @@ $Data::Dumper::Varname = 'POSTGRES';
 $Data::Dumper::Indent = 2;
 $Data::Dumper::Useqq = 1;
 
-our $VERSION = '2.4.3';
+our $VERSION = '2.5.0';
 
 use vars qw/ %opt $PSQL $res $COM $SQL $db /;
 
@@ -110,12 +110,14 @@ die $USAGE unless
 			   'dbname|db=s@',
 			   'dbuser|u=s@',
 			   'dbpass=s@',
+			   'dbservice=s@',
 
 			   'host2|dbhost2|H2=s@',
 			   'port2|dbport2=s@',
 			   'dbname2|db2=s@',
 			   'dbuser2|u2=s@',
 			   'dbpass2=s@',
+			   'dbservice2=s@',
 
 			   'PSQL=s',
 
@@ -240,11 +242,12 @@ Returns with an exit code of 0 (success), 1 (warning), 2 (critical), or 3 (unkno
 This is version $VERSION.
 
 Common connection options:
- -H,  --host=NAME    hostname(s) to connect to; defaults to none (Unix socket)
- -p,  --port=NUM     port(s) to connect to; defaults to $opt{defaultport}.
- -db, --dbname=NAME  database name(s) to connect to; defaults to 'postgres' or 'template1'
- -u   --dbuser=NAME  database user(s) to connect as; defaults to '$opt{defaultuser}'
-      --dbpass=PASS  database password(s); use a .pgpass file instead when possible
+ -H,  --host=NAME       hostname(s) to connect to; defaults to none (Unix socket)
+ -p,  --port=NUM        port(s) to connect to; defaults to $opt{defaultport}.
+ -db, --dbname=NAME     database name(s) to connect to; defaults to 'postgres' or 'template1'
+ -u   --dbuser=NAME     database user(s) to connect as; defaults to '$opt{defaultuser}'
+      --dbpass=PASS     database password(s); use a .pgpass file instead when possible
+      --dbservice=NAME  service name to use inside of pg_service.conf
 
 Connection options can be grouped: --host=a,b --host=c --port=1234 --port=3344
 would connect to a-1234, b-1234, and c-3344
@@ -352,9 +355,9 @@ sub add_response {
 	my ($type,$msg) = @_;
 
 	my $header = sprintf q{%s%s%s},
-		$action_info->{$action}[0] ? '' : qq{DB "$db->{dbname}" },
+		$action_info->{$action}[0] ? '' : defined $db->{dbservice} ? qq{service=$db->{dbservice} } : qq{DB "$db->{dbname}" },
 			$db->{host} eq '<none>' ? '' : qq{(host:$db->{host}) },
-				$db->{port} eq $opt{defaultport} ? '' : qq{(port=$db->{port}) };
+				defined $db->{port} ? ($db->{port} eq $opt{defaultport} ? '' : qq{(port=$db->{port}) }) : '';
 	$header =~ s/\s+$//;
 	my $perf = ($opt{showtime} and $db->{totaltime}) ? "time=$db->{totaltime}" : '';
 	if ($db->{perf}) {
@@ -912,13 +915,21 @@ sub run_command {
 	## Default connection options
 	my $conn =
 		{
-		 host   => ['<none>'],
-		 port   => [$opt{defaultport}],
-		 dbname => [$opt{defaultdb}],
-		 dbuser => [$opt{defaultuser}],
-		 dbpass => [''],
+		 host   =>    ['<none>'],
+		 port   =>    [$opt{defaultport}],
+		 dbname =>    [$opt{defaultdb}],
+		 dbuser =>    [$opt{defaultuser}],
+		 dbpass =>    [''],
+		 dbservice => [''],
 		 };
 
+
+	## Don't set any default values if a service is being used
+	if (length $opt{dbservice}->[0]) {
+		$conn->{dbname} = [];
+		$conn->{port} = [];
+		$conn->{dbuser} = [];
+	}
 	my $gbin = 0;
   GROUP: {
 		## This level controls a "group" of targets
@@ -944,7 +955,7 @@ sub run_command {
 			}
 			if (defined $opt{$v}->[$gbin]) {
 				my $new = $opt{$v}->[$gbin];
-				$new =~ s/\s+//g;
+				$new =~ s/\s+//g unless $vname eq 'dbservice';
 				## Set this as the new default
 				$conn->{$vname} = [split /,/ => $new];
 				$foundgroup = 1;
@@ -1004,13 +1015,21 @@ sub run_command {
 		## Store this target in the global target list
 		push @{$info->{db}}, $db;
 
-		$db->{pname} = "port=$db->{port} host=$db->{host} db=$db->{dbname} user=$db->{dbuser}";
-		my @args = ('-q', '-U', "$db->{dbuser}", '-d', $db->{dbname}, '-t');
+		my @args = ('-q', '-t');
+		if (defined $db->{dbservice} and length $db->{dbservice}) { ## XX Check for simple names
+			$db->{pname} = "service=$db->{dbservice}";
+			push @args, qq{service=$db->{dbservice}};
+		}
+		else {
+			$db->{pname} = "port=$db->{port} host=$db->{host} db=$db->{dbname} user=$db->{dbuser}";
+		}
+		defined $db->{dbname} and push @args, '-d', $db->{dbname};
+		defined $db->{dbuser} and push @args, '-U', $db->{dbuser};
+		defined $db->{port} and push @args => '-p', $db->{port};
 		if ($db->{host} ne '<none>') {
 			push @args => '-h', $db->{host};
 			$host{$db->{host}}++; ## For the overall count
 		}
-		push @args => '-p', $db->{port};
 
 		if (defined $db->{dbpass} and length $db->{dbpass}) {
 			## Make a custom PGPASSFILE. Far better to simply use your own .pgpass of course
@@ -3725,7 +3744,7 @@ sub show_dbstats {
 =head1 NAME
 
 B<check_postgres.pl> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others
-This documents describes check_postgres.pl version 2.4.3
+This documents describes check_postgres.pl version 2.5.0
 
 =head1 SYNOPSIS
 
@@ -3856,6 +3875,12 @@ dbuser arguments are allowed. If this is not provided, the default is 'postgres'
 Provides the password to connect to the database with. Use of this option is highly discouraged. 
 Instead, one should use a .pgpass file.
 
+=item B<--dbservice=NAME>
+
+The name of a service inside of the pg_service.conf file. This file is in your home directory by 
+default and contains a simple list of connection options. You can also pass additional information 
+when using this option such as --dbservice="maindatabase sslmode=require"
+
 =back
 
 The database connection options can be grouped: I<--host=a,b --host=c --port=1234 --port=3344>
@@ -3876,6 +3901,9 @@ Examples:
   --host=a,b --host=x --port=5432,5433 --dbuser=alice --dbuser=bob -db=baz
   Connects three times: a-5432-alice-baz b-5433-alice-baz x-5433-bob-baz
 
+  --dbservice="foo" --port=5433
+  Connects using the named service 'foo' in the pg_service.conf file, but overrides the port
+
 =head1 OTHER OPTIONS
 
 Other options include:
@@ -4942,6 +4970,10 @@ Items not specifically attributed are by Greg Sabino Mullane.
 
 =over 4
 
+=item B<Version 2.5.0>
+
+  Add support for the pg_Service.conf file with the --dbservice option.
+
 =item B<Version 2.4.3>
 
   Fix options for replicate_row action, per report from Jason Gordon.
diff --git a/check_postgres.pl.html b/check_postgres.pl.html
index c00eafa..9fe92ba 100644
--- a/check_postgres.pl.html
+++ b/check_postgres.pl.html
@@ -92,7 +92,7 @@
 <hr />
 <h1><a name="name">NAME</a></h1>
 <p><strong>check_postgres.pl</strong> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others
-This documents describes check_postgres.pl version 2.4.3</p>
+This documents describes check_postgres.pl version 2.5.0</p>
 <p>
 </p>
 <hr />
@@ -228,6 +228,13 @@ dbuser arguments are allowed. If this is not provided, the default is 'postgres'
 <p>Provides the password to connect to the database with. Use of this option is highly discouraged. 
 Instead, one should use a .pgpass file.</p>
 </dd>
+<dt><strong><a name="item__2d_2ddbservice_3dname"><strong>--dbservice=NAME</strong></a></strong></dt>
+
+<dd>
+<p>The name of a service inside of the pg_service.conf file. This file is in your home directory by 
+default and contains a simple list of connection options. You can also pass additional information 
+when using this option such as --dbservice=&quot;maindatabase sslmode=require&quot;</p>
+</dd>
 </dl>
 <p>The database connection options can be grouped: <em>--host=a,b --host=c --port=1234 --port=3344</em>
 would connect to a-1234, b-1234, and c-3344. Note that once set, an option 
@@ -245,6 +252,9 @@ carries over until it is changed again.</p>
 <pre>
   --host=a,b --host=x --port=5432,5433 --dbuser=alice --dbuser=bob -db=baz
   Connects three times: a-5432-alice-baz b-5433-alice-baz x-5433-bob-baz</pre>
+<pre>
+  --dbservice=&quot;foo&quot; --port=5433
+  Connects using the named service 'foo' in the pg_service.conf file, but overrides the port</pre>
 <p>
 </p>
 <hr />
@@ -1203,6 +1213,12 @@ feature requests, and commit notices, send email to <a href="mailto:check_postgr
 <h1><a name="history">HISTORY</a></h1>
 <p>Items not specifically attributed are by Greg Sabino Mullane.</p>
 <dl>
+<dt><strong><a name="item_version_2_2e5_2e0"><strong>Version 2.5.0</strong></a></strong></dt>
+
+<dd>
+<pre>
+  Add support for the pg_Service.conf file with the --dbservice option.</pre>
+</dd>
 <dt><strong><a name="item_version_2_2e4_2e3"><strong>Version 2.4.3</strong></a></strong></dt>
 
 <dd>
diff --git a/index.html b/index.html
index 456315d..6b43869 100644
--- a/index.html
+++ b/index.html
@@ -21,14 +21,14 @@ h1 {
 
 <h1>check_postgres.pl</h1>
 
-<p><b>check_postgres.pl</b> is a script for checking the state of one or more Postgres databases and reporting back in a Nagios-friendly manner. It was developed by Greg Sabino Mullane of <a href="http://www.endpoint.com/">End Point Corporation</a> and is BSD-licensed. The latest version is <b>2.4.3</b>, and was released on November 07, 2008.</p>
+<p><b>check_postgres.pl</b> is a script for checking the state of one or more Postgres databases and reporting back in a Nagios-friendly manner. It was developed by Greg Sabino Mullane of <a href="http://www.endpoint.com/">End Point Corporation</a> and is BSD-licensed. The latest version is <b>2.5.0</b>, and was released on December 04, 2008.</p>
 
 <ul>
- <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 2.4.3</a></li>
+ <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 2.5.0</a></li>
 </ul>
 <ul>
- <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 2.4.3</a></li>
- <li><a href="/check_postgres/check_postgres.pl.asc">PGP signature for check_postgres.pl 2.4.3</a></li>
+ <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 2.5.0</a></li>
+ <li><a href="/check_postgres/check_postgres.pl.asc">PGP signature for check_postgres.pl 2.5.0</a></li>
 </ul>
 
 <p>The latest development version can be downloaded via git:</p>
-- 
1.5.5.4



More information about the Check_postgres mailing list