--- parse_pg_stat.pl 2003/08/08 10:49:52 1.2 +++ parse_pg_stat.pl 2003/11/03 10:30:25 1.3 @@ -1,16 +1,51 @@ #!/usr/bin/perl -w +# +# cricket module which draws statistics of PostgreSQL commits, rollbacks and +# number of backends +# +# http://www.rot13.org/~dpavlin/sysadm.html +# +# Usage: +# +# parse_pg_stat.pl host [user [password [port tunnel_command]]] +# +# tunnel command is used to invoke tunnel which will bring up tunnel port +# (for example ssh :-) It's extremly important that tunnel return something +# (using yes in this example) otherwise, this command will block and die +# after 10 seconds e.g: +# +# parse_pg_stat.pl localhost dpavlin "" 15432 "ssh -L 15432:localhost:5432 -N -i ~cricket/.ssh/hostname hostname" +# parse_pg_stat.pl localhost dpavlin "" 15432 "ssh -L 15432:localhost:5432 -i ~cricket/.ssh/izuh izuh yes" use strict; use DBI; +my $info = ""; # optional parametars to database my $host = shift @ARGV || ''; -$host = ";host=$host" if ($host); +$info .= ";host=$host" if ($host); my $user = shift @ARGV || "dpavlin"; my $pass = shift @ARGV || ""; +my $port = shift @ARGV; +my $tunnel = join(" ",@ARGV); +$info .= ";port=$port" if ($port); my $sql = "select sum(numbackends),sum(xact_commit),sum(xact_rollback),sum(blks_read),sum(blks_hit) from pg_stat_database"; -my $dbh = DBI->connect("DBI:Pg:dbname=template1$host",$user,$pass) || die $DBI::errstr; +my $t_fd; + +if ($tunnel) { + print STDERR "using tunnel '$tunnel'\n"; +# eval { + local $SIG{ALRM} = sub { kill 9,-$$; }; + alarm 10; # wait for ssh to connect and return first line + my $pid; + open($t_fd,"$tunnel |") || die "$tunnel: $!"; + my $foo=<$t_fd>; + print STDERR "tunnel returned: $foo\n"; +# }; +} + +my $dbh = DBI->connect("DBI:Pg:dbname=template1$info",$user,$pass) || die $DBI::errstr; my $sth=$dbh->prepare($sql); if ($sth->execute()) { @@ -19,3 +54,9 @@ undef $sth; $dbh->disconnect(); + +if ($tunnel) { + print STDERR "kill tunnel\n"; + kill 9,-$$; + close($t_fd); +}