1 |
#!/usr/bin/perl |
#!/usr/bin/perl -w |
2 |
# |
# |
3 |
# $Id$ |
# $Id$ |
4 |
# $Revision$ |
# $Revision$ |
41 |
|
|
42 |
use DBI; |
use DBI; |
43 |
use Getopt::Long; |
use Getopt::Long; |
44 |
|
use strict; |
45 |
|
|
46 |
GetOptions( \%options,"port=s", "username=s", "password=s", "database=s", "host=s" ); |
my %options; |
47 |
|
|
48 |
# uncomment these two lines and provide suitable information if you don't |
if (! @ARGV) { |
|
# want to pass sensitive information on the command line |
|
|
$options{username} ||= "postgres"; |
|
|
$options{password} ||= ""; |
|
|
|
|
|
$mode="Pg"; |
|
|
if ($options{host} eq ""){ |
|
|
$options{"host"=>"localhost"}; |
|
|
} |
|
|
if ($options{port} eq ""){ |
|
|
$options{"port"=>"5432"}; |
|
|
} |
|
|
|
|
|
if (($options{username} eq "") || (! defined $options{password}) || ($options{database} eq "")){ |
|
49 |
print <<EOP1; |
print <<EOP1; |
50 |
Usage: postresql.monitor [options] |
Usage: postresql.monitor username[:password]\@host/database ... |
|
|
|
|
--database=<Databasename> indicates the database to which is connected |
|
|
--username=<Username> DB-User which is used to connect to the DB |
|
|
--password=<Password> DB-Password which is used to connect to the DB |
|
|
--host=<Databasehost> Host of the Database (optional,default=localhost) |
|
|
--port=<Portnumber> Port on which you want to connect (optional,default=5432) |
|
51 |
EOP1 |
EOP1 |
52 |
die(); |
exit 1; |
53 |
} |
} |
54 |
|
|
55 |
|
my @test_db; |
56 |
|
my @failures; |
57 |
|
|
58 |
|
foreach (@ARGV) { |
59 |
|
if (m/^([^:]+):?([^\@]*)\@([^\/]+)\/?(.*)$/) { |
60 |
|
push @test_db, { user => $1, passwd => $2, host => $3, database => $4 }; |
61 |
|
} else { |
62 |
|
push @failures, "Can't parse configuration: host '$_' not in username:password\@host/database format!"; |
63 |
|
} |
64 |
|
} |
65 |
|
|
66 |
my( $dbh ) = DBI->connect( "DBI:$mode:dbname=$options{database};$options{host};$options{port}", $options{username}, $options{password} ); |
foreach (@test_db) { |
67 |
if( ! $dbh ) { |
my $dbh = DBI->connect( "DBI:Pg:dbname=$_->{database};host=$_->{host};", $_->{user}, $_->{passwd} ); |
68 |
push( @failures, "Could not connect to $mode server $host: " . $DBI::errstr ); |
if( ! $dbh ) { |
69 |
|
push @failures,"Could not connect server $_->{host}, database $_->{database}: " . $DBI::errstr; |
70 |
|
} else { |
71 |
|
my $sth = $dbh->prepare("select count(*) from pg_tables where tablename not like 'pg_%'"); |
72 |
|
if (! $sth->execute() ) { |
73 |
|
push @failures, "Can't find out number of tables on $_->{host}, database $_->{database} " . $DBI::errstr; |
74 |
|
} else { |
75 |
|
my ($nr) = $sth->fetchrow_array; |
76 |
|
if ($nr == 0) { |
77 |
|
push @failures, "No tables on $_->{host}, database $_->{database} (turn off monitoring for this database?)"; |
78 |
|
} |
79 |
|
} |
80 |
|
$sth->finish(); |
81 |
|
} |
82 |
|
if ($dbh) { |
83 |
|
$dbh->disconnect(); |
84 |
|
} |
85 |
} |
} |
86 |
|
|
87 |
if (@failures) { |
if (@failures) { |
88 |
print join (", ", @failures), "\n"; |
print join (", ", @failures), "\n"; |
89 |
exit 1; |
exit 1; |
90 |
}; |
}; |
|
if ($dbh){ |
|
|
$dbh->disconnect(); |
|
|
} |
|
|
|
|
91 |
exit 0; |
exit 0; |