--- pgsql.monitor 2002/07/10 08:50:00 1.2 +++ pgsql.monitor 2002/07/26 10:43:47 1.7 @@ -1,28 +1,31 @@ -#!/usr/bin/perl +#!/usr/bin/perl -w # -# $Id: postgresql.monitor,v 1.3 2001/05/03 11:44:21 severin Exp $ -# $Revision: 1.3 $ -# $Author: severin $ -# -#Usage: postresql.monitor [options] -# -# --database= 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) +# Monitor multiple postgresql databases on different hosts +# +# Dobrica Pavlinusic +# http://www.rot13.org/~dpavlin/sysadm.html +# +# Based on postgresql.monitor 1.3 +# by Severin Luftensteiner +# it's not limited to singe host and/or singe database. +# +# Usage: pgsql.monitor username[:password]@host/database ... # # a monitor to determine if a PostgreSQL database server is operational # # Rather than use tcp.monitor to ensure that your SQL server is responding -# on the proper port, this attempts to connect to and list the databases -# on a given database server. +# on the proper port, this attempts to connect to and count all tables +# in given database on given server. +# +# You can use this monitor along with fping+args which also knows how to +# ping hosts in that user@host/dabase format. # -# This monitor requires the perl5 DBI, DBD::mSQL and DBD::mysql modules, +# This monitor requires the perl5 DBI, DBD::Pg modules, # available from CPAN (http://www.cpan.org) # # Copyright (C) 2001, CubIT IT Solutions # Written by Severin Luftensteiner +# Copyright (C) 2002, Dobrica Pavlinusic # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -40,47 +43,49 @@ # use DBI; -use Getopt::Long; - -GetOptions( \%options,"port=s", "username=s", "password=s", "database=s", "host=s" ); - -# uncomment these two lines and provide suitable information if you don't -# 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"}; -} +use strict; -if (($options{username} eq "") || (! defined $options{password}) || ($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;