/[cricket]/parse_pg_stat.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /parse_pg_stat.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by dpavlin, Fri Aug 8 10:49:52 2003 UTC revision 1.3 by dpavlin, Mon Nov 3 10:30:25 2003 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    #
3    # cricket module which draws statistics of PostgreSQL commits, rollbacks and
4    # number of backends
5    #
6    # http://www.rot13.org/~dpavlin/sysadm.html
7    #
8    # Usage:
9    #
10    #       parse_pg_stat.pl host [user [password [port tunnel_command]]]
11    #
12    # tunnel command is used to invoke tunnel which will bring up tunnel port
13    # (for example ssh :-) It's extremly important that tunnel return something
14    # (using yes in this example) otherwise, this command will block and die
15    # after 10 seconds e.g:
16    #
17    # parse_pg_stat.pl localhost dpavlin "" 15432 "ssh -L 15432:localhost:5432 -N -i ~cricket/.ssh/hostname hostname"
18    # parse_pg_stat.pl localhost dpavlin "" 15432 "ssh -L 15432:localhost:5432 -i ~cricket/.ssh/izuh izuh yes"
19    
20  use strict;  use strict;
21  use DBI;  use DBI;
22    
23    my $info = "";  # optional parametars to database
24  my $host = shift @ARGV || '';  my $host = shift @ARGV || '';
25  $host = ";host=$host" if ($host);  $info .= ";host=$host" if ($host);
26  my $user = shift @ARGV || "dpavlin";  my $user = shift @ARGV || "dpavlin";
27  my $pass = shift @ARGV || "";  my $pass = shift @ARGV || "";
28    my $port = shift @ARGV;
29    my $tunnel = join(" ",@ARGV);
30    $info .= ";port=$port" if ($port);
31    
32  my $sql = "select sum(numbackends),sum(xact_commit),sum(xact_rollback),sum(blks_read),sum(blks_hit) from pg_stat_database";  my $sql = "select sum(numbackends),sum(xact_commit),sum(xact_rollback),sum(blks_read),sum(blks_hit) from pg_stat_database";
33    
34  my $dbh = DBI->connect("DBI:Pg:dbname=template1$host",$user,$pass) || die $DBI::errstr;  my $t_fd;
35    
36    if ($tunnel) {
37            print STDERR "using tunnel '$tunnel'\n";
38    #       eval {
39                    local $SIG{ALRM} = sub { kill 9,-$$; };
40                    alarm 10;       # wait for ssh to connect and return first line
41                    my $pid;
42                    open($t_fd,"$tunnel |") || die "$tunnel: $!";
43                    my $foo=<$t_fd>;
44                    print STDERR "tunnel returned: $foo\n";
45    #       };
46    }
47    
48    my $dbh = DBI->connect("DBI:Pg:dbname=template1$info",$user,$pass) || die $DBI::errstr;
49  my $sth=$dbh->prepare($sql);  my $sth=$dbh->prepare($sql);
50    
51  if ($sth->execute()) {  if ($sth->execute()) {
# Line 19  if ($sth->execute()) { Line 54  if ($sth->execute()) {
54    
55  undef $sth;  undef $sth;
56  $dbh->disconnect();  $dbh->disconnect();
57    
58    if ($tunnel) {
59            print STDERR "kill tunnel\n";
60            kill 9,-$$;
61            close($t_fd);
62    }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.26