--- pgsql.monitor 2002/07/10 08:43:16 1.1 +++ pgsql.monitor 2002/07/10 10:05:14 1.4 @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/perl -w # # $Id: postgresql.monitor,v 1.3 2001/05/03 11:44:21 severin Exp $ # $Revision: 1.3 $ @@ -41,46 +41,51 @@ use DBI; use Getopt::Long; +use strict; -GetOptions( \%options,"port=s", "username=s", "password=s", "database=s", "host=s" ); +my %options; -# uncomment these two lines and provide suitable information if you don't -# want to pass sensitive information on the command line -#$options{username} ||= "username"; -#$options{password} ||= "password"; - -$mode="Pg"; -if ($options{host} eq ""){ - $options{"host"=>"localhost"}; -} -if ($options{port} eq ""){ - $options{"port"=>"5432"}; -} - -if (($options{username} eq "") || ($options{password} eq "") || ($options{database} eq "")){ +if (! @ARGV) { print < indicates the database to which is connected - --username= DB-User which is used to connect to the DB - --password= DB-Password which is used to connect to the DB - --host= Host of the Database (optional,default=localhost) - --port= Port on which you want to connect (optional,default=5432) +Usage: postresql.monitor username[:password]\@host/database ... EOP1 - die(); + exit 1; } +my @test_db; +my @failures; + +foreach (@ARGV) { + if (m/^([^:]+):?([^\@]*)\@([^\/]+)\/?(.*)$/) { + push @test_db, { user => $1, passwd => $2, host => $3, database => $4 }; + } else { + push @failures, "Can't parse configuration: host '$_' not in username:password\@host/database format!"; + } +} -my( $dbh ) = DBI->connect( "DBI:$mode:dbname=$options{database};$options{host};$options{port}", $options{username}, $options{password} ); -if( ! $dbh ) { - push( @failures, "Could not connect to $mode server $host: " . $DBI::errstr ); +foreach (@test_db) { + my $dbh = DBI->connect( "DBI:Pg:dbname=$_->{database};host=$_->{host};", $_->{user}, $_->{passwd} ); + if( ! $dbh ) { + push @failures,"Could not connect server $_->{host}, database $_->{database}: " . $DBI::errstr; + } else { + my $sth = $dbh->prepare("select count(*) from pg_tables where tablename not like 'pg_%'"); + if (! $sth->execute() ) { + push @failures, "Can't find out number of tables on $_->{host}, database $_->{database} " . $DBI::errstr; + } else { + my ($nr) = $sth->fetchrow_array; + if ($nr == 0) { + push @failures, "No tables on $_->{host}, database $_->{database} (turn off monitoring for this database?)"; + } + } + $sth->finish(); + } + if ($dbh) { + $dbh->disconnect(); + } } + if (@failures) { print join (", ", @failures), "\n"; exit 1; }; -if ($dbh){ - $dbh->disconnect(); -} - exit 0;