#!/usr/bin/perl use strict; use DBI; use Getopt::Long; use Sys::Hostname; my $database = 'atloffice'; my $user = 'postgres'; my $hostname = '10.28.128.143'; my %querys = ( # State "activeconn" => qq{SELECT SUM(numbackends) FROM pg_stat_database}, # Locks "exclusivelock" => qq{SELECT COUNT(*) FROM pg_locks WHERE mode='ExclusiveLock'}, # Checkpoints "buffers_alloc" => qq{SELECT buffers_alloc FROM pg_stat_bgwriter}, ); GetOptions( 'help!' => \&usage, 'user=s' => \$user, 'database=s' => \$database, 'activeconn' => sub { print query_database($querys{activeconn}) }, 'exclusivelock' => sub { print query_database($querys{exclusivelock}) }, 'buffers_alloc' => sub { print query_database($querys{buffers_alloc}) } ) or die "$0: try --help for more information\n"; sub query_database { my $query = shift(@_); my $dbh = DBI->connect("dbi:Pg:dbname=$database;host=$hostname",$user); my $sth = $dbh->prepare("$query") or die $|; $sth->execute; while (my @array = $sth->fetchrow_array) { return @array[0]; } } sub usage { print "[-] $0 script to monitor PostgreSQL databases,\n"; print "Usage: $0 [--OPTION]\n"; while ( my($key, undef) = each %querys ) { print "\t--".$key."\n"; } exit 0 } if (!$ARGV) { usage }