1 |
dpavlin |
1.4 |
#!/usr/bin/perl -w |
2 |
dpavlin |
1.1 |
# |
3 |
|
|
# $Id: postgresql.monitor,v 1.3 2001/05/03 11:44:21 severin Exp $ |
4 |
|
|
# $Revision: 1.3 $ |
5 |
|
|
# $Author: severin $ |
6 |
|
|
# |
7 |
|
|
#Usage: postresql.monitor [options] |
8 |
|
|
# |
9 |
|
|
# --database=<Databasename> indicates the database to which is connected |
10 |
|
|
# --username=<Username> DB-User which is used to connect to the DB |
11 |
|
|
# --password=<Password> DB-Password which is used to connect to the DB |
12 |
|
|
# --host=<Databasehost> Host of the Database (optional,default=localhost) |
13 |
|
|
# --port=<Portnumber> Port on which you want to connect (optional,default=5432) |
14 |
|
|
# |
15 |
|
|
# a monitor to determine if a PostgreSQL database server is operational |
16 |
|
|
# |
17 |
|
|
# Rather than use tcp.monitor to ensure that your SQL server is responding |
18 |
|
|
# on the proper port, this attempts to connect to and list the databases |
19 |
|
|
# on a given database server. |
20 |
|
|
# |
21 |
|
|
# This monitor requires the perl5 DBI, DBD::mSQL and DBD::mysql modules, |
22 |
|
|
# available from CPAN (http://www.cpan.org) |
23 |
|
|
# |
24 |
|
|
# Copyright (C) 2001, CubIT IT Solutions |
25 |
|
|
# Written by Severin Luftensteiner <severin.luftensteiner@cubit.at> |
26 |
|
|
# |
27 |
|
|
# This program is free software; you can redistribute it and/or modify |
28 |
|
|
# it under the terms of the GNU General Public License as published by |
29 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
30 |
|
|
# (at your option) any later version. |
31 |
|
|
# |
32 |
|
|
# This program is distributed in the hope that it will be useful, |
33 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
34 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
35 |
|
|
# GNU General Public License for more details. |
36 |
|
|
# |
37 |
|
|
# You should have received a copy of the GNU General Public License |
38 |
|
|
# along with this program; if not, write to the Free Software |
39 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
40 |
|
|
# |
41 |
|
|
|
42 |
|
|
use DBI; |
43 |
|
|
use Getopt::Long; |
44 |
dpavlin |
1.4 |
use strict; |
45 |
dpavlin |
1.1 |
|
46 |
dpavlin |
1.4 |
my %options; |
47 |
dpavlin |
1.1 |
|
48 |
dpavlin |
1.4 |
if (! @ARGV) { |
49 |
dpavlin |
1.1 |
print <<EOP1; |
50 |
dpavlin |
1.4 |
Usage: postresql.monitor username[:password]\@host/database ... |
51 |
dpavlin |
1.1 |
EOP1 |
52 |
dpavlin |
1.4 |
exit 1; |
53 |
dpavlin |
1.1 |
} |
54 |
|
|
|
55 |
dpavlin |
1.3 |
my @test_db; |
56 |
dpavlin |
1.4 |
my @failures; |
57 |
dpavlin |
1.3 |
|
58 |
|
|
foreach (@ARGV) { |
59 |
dpavlin |
1.4 |
if (m/^([^:]+):?([^\@]*)\@([^\/]+)\/?(.*)$/) { |
60 |
|
|
push @test_db, { user => $1, passwd => $2, host => $3, database => $4 }; |
61 |
dpavlin |
1.3 |
} else { |
62 |
dpavlin |
1.4 |
push @failures, "Can't parse configuration: host '$_' not in username:password\@host/database format!"; |
63 |
dpavlin |
1.3 |
} |
64 |
|
|
} |
65 |
dpavlin |
1.1 |
|
66 |
dpavlin |
1.4 |
foreach (@test_db) { |
67 |
|
|
my $dbh = DBI->connect( "DBI:Pg:dbname=$_->{database};host=$_->{host};", $_->{user}, $_->{passwd} ); |
68 |
|
|
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 |
dpavlin |
1.1 |
} |
86 |
dpavlin |
1.3 |
|
87 |
dpavlin |
1.1 |
if (@failures) { |
88 |
|
|
print join (", ", @failures), "\n"; |
89 |
|
|
exit 1; |
90 |
|
|
}; |
91 |
|
|
exit 0; |