ServDoc_0330postgres

Code Index:



NAME

ServDoc_postgres - describe the postgresql daemon


VERSION

$Id: ServDoc_0330postgres,v 1.1 2004/02/19 07:15:16 uherbst Exp $


SYNOPSIS

ServDoc_0330postgres [-h|help] [-v|version] [--debug PSQL,intensity]


DESCRIPTION

ServDoc_postgres describe the postgres daemon (if installed).

We describe only if postgres or postmaster is running.


OPTIONS

-h|help
This help.

-v|version
Version

--debug PSQL,intensity
Turn on Debugging for that module.

The debug feature for that module is named ``PSQL''.


BUGS

Locating the configuration files will fail on systems where the PostgreSQL data directory is not in the environement of the postgres user.


AUTHORS

Bas Couwenberg <sebastic@xs4all.nl>


#!/usr/bin/perl -w

#----------------------------------------------------------------------

# standard perl modules
use strict;                       # print error about unknown variables ...
use English;                      # long internal variable names;
use FindBin;                      # In which directory is ServDoc itself ?
                                  # There has to be the module and the lib dir!
use lib $FindBin::Bin. "/lib";    # Here are the ServDoc-perl-modules

# our own perl modules
use ServDoc;

#----------------------------------------------------------------------

my $options;
$options->{Version} = '$Id: ServDoc_0330postgres,v 1.1 2004/02/19 07:15:16 uherbst Exp $';

# We need the cmdline to call the modules with the same debug options.
$options->{cmdline} = join " ", @ARGV;

sub debug { ServDoc_debug( "PSQL", $options, shift, shift ); }

$options = &process_cmdline($options);

debug( 9, "ServDoc_0330postgres is running" );

#----------------------------------------------------------------------

# Main

# Here is your useful code.

# If we are not on HP/UX,Solaris, AIX, Linux, return silently.
if ( $OSNAME !~ /hpux|linux|solaris|aix/ ) {
  debug( 1, "ServDoc_0330postgres: $OSNAME is not supported" );
  exit 0;
}

# We need root privileges for alot of thing, so no root == no go
if($< != 0) {
	exit 0;
}

my $heading = "PostgreSQL DB%+";

# Check for running postmaster (multiuser-mode) or postgres (single-user mode)
my @postmaster = ps_grep ("postmaster|postgres");
exit 0 if ($#postmaster < 0);

# Use pg_config to retrieve some info
my $pgconfig_cmd = find_path('pg_config');

if (!defined($pgconfig_cmd)) {
	debug(8,"We can't find the pg_config executable");
	exit 0;
}

# Some postgres configuration info

# postgres version
my $version    = do_cmd("$pgconfig_cmd --version");
report_string($heading . "Version", "$pgconfig_cmd --version", 
	'', $version);

# postgres binaries: psql, postmaster, createuser etc.
my $bindir     = do_cmd("$pgconfig_cmd --bindir");
report_string($heading . "Executable directory", "$pgconfig_cmd --bindir", 
	'', $bindir);

# postgres libraries
my $libdir     = do_cmd("$pgconfig_cmd --libdir");
report_string($heading . "Library directory", "$pgconfig_cmd --libdir", 
	'', $libdir);

# ./configure options
my $config_ops = do_cmd("$pgconfig_cmd --configure");
report_string($heading . "Configuration options", "$pgconfig_cmd --configure", 
	'', $config_ops);

# List available databases
chomp($bindir);
$bindir .= '/' if(substr($bindir, -1, 1) ne '/' && -d $bindir);
my $psql = $bindir.'psql';
if(-x $psql) {
	# Execute psql -l as user postgres, 
	# because root my not be a postgres user
	
	my $databases = do_cmd("su - postgres -c \"$psql -l\"");
	report_string($heading . "Available databases", "$psql -l", 
		'', $databases);
}


# Where is the directory with database files, configuration files etc.
#
# Paths are stored in the environement of the postgres user
# To determin the paths, check the postgres env.
# If there is no environement exit quietly (could happen on non-Debian systems)

my $su	     = find_path('su');
my $printenv = find_path('printenv');
if(!$printenv) {
	debug(8, 'Cannot find printenv executable');
	exit 0;
}
if(!$su) {
	debug(8, 'Cannot find su executable');
	exit 0;
}

# User postgres' environement
my $env = do_cmd("$su - postgres -c $printenv");
exit 0 if(!$env);

my $postgres_home    = '';
my $postgres_datadir = '';
foreach(split /\n/, $env) {
	if(/^HOME=(.*)$/) {
		$postgres_home = $1;
	}
	if(/^PGDATA=(.*)$/) {
		$postgres_datadir = $1;
	}
}

if(!$postgres_home || $postgres_home eq '') {
	debug(8, 'Cannot find postgres homedir');
	exit 0;
}
if(!$postgres_datadir || $postgres_datadir eq '') {
	debug(8, 'Cannot find postgres datadir');
	exit 0;
}

if(!-r $postgres_datadir) {
	debug(8, 'Cannot read postgres datadir ($PGDATA environement variable)');
	exit 0;
}

my %configdirs;
my @files = listdir($postgres_datadir);
foreach my $file (@files) {
	debug(3, "File: $file");
	
	# Get the real path of the file incase of symlink
	$file = readlink $file if(-l $file);
	
	# If the file is not in the postgres datadir ($PGDATA) 
	# add it to the list of dirs to check.
	
	if($file !~ /^$postgres_datadir/) {
		# Get the directory, .* is greedy, so it will take
		# all (sub)directories, .*? is not greedy so
		# it will not include any (sub)directories

		if($file =~ m!^(.*/).*?$!) {
			# Add the directory to the list
			$configdirs{$1} += 1;
		}
		
	}
	elsif($file =~ /\.conf$/) {
		$configdirs{$postgres_datadir} += 1;
	}
}

if(%configdirs) {
	# Go through all directories with .conf files
	# Start with the directory with the most .conf files
	# End with the directory with the least .conf files
	
	foreach my $dir (sort { $configdirs{$b} <=> $configdirs{$a} } keys %configdirs) {
		if(-d $dir && -r $dir) {
			foreach my $file (listdir($dir)) {
				if($file =~ /\.conf$/) {
					# Report the content of the .conf file
					report_file($heading . "Configuration files%+$file", 
						'', '', $file);
				}
			}
		}
		else {
			debug(8, "Directory '$dir' is not a directory or is unreadable");
		}
	}
}
else {
	debug(8, 'No directories found with .conf files');
	exit 0;
}

exit 0;