[1761] | 1 | #!/usr/bin/perl -w |
---|
| 2 | # -*- perl -*- |
---|
| 3 | # |
---|
| 4 | |
---|
| 5 | use strict; |
---|
| 6 | use DBI; |
---|
| 7 | |
---|
| 8 | my $dbhost = $ENV{'dbhost'} || ''; |
---|
| 9 | my $dbport = $ENV{'dbport'} || ''; |
---|
| 10 | my $dbname = $ENV{'dbname'} || 'template1'; |
---|
| 11 | my $dbuser = $ENV{'dbuser'} || 'postgres'; |
---|
| 12 | |
---|
| 13 | my $Con = "DBI:Pg:dbname=$dbname"; |
---|
| 14 | $Con .= ";host=$dbhost" if $dbhost; |
---|
| 15 | $Con .= ";port=$dbport" if $dbport; |
---|
| 16 | my $Dbh = DBI->connect ($Con, $dbuser,'',{RaiseError =>1}) || |
---|
| 17 | die "Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr; |
---|
| 18 | |
---|
| 19 | if ($ARGV[0] && $ARGV[0] eq 'config') { |
---|
| 20 | my $sql_max = "SHOW max_connections;"; |
---|
| 21 | my $sth_max = $Dbh->prepare($sql_max); |
---|
| 22 | $sth_max->execute(); |
---|
| 23 | my ($max_conn) = $sth_max->fetchrow(); |
---|
| 24 | my $warning = int ($max_conn * 0.7); |
---|
| 25 | my $critical = int ($max_conn * 0.8); |
---|
| 26 | print <<EOF; |
---|
| 27 | graph_title Postgres active connections |
---|
| 28 | graph_args -l 0 --base 1000 |
---|
| 29 | graph_vlabel Active connections |
---|
| 30 | graph_category Postgresql |
---|
| 31 | graph_info Shows active Postgresql connections |
---|
| 32 | connections.label Active connections |
---|
| 33 | connections.info Active connections |
---|
| 34 | connections.type GAUGE |
---|
| 35 | connections.warning $warning |
---|
| 36 | connections.critical $critical |
---|
| 37 | EOF |
---|
| 38 | } else { |
---|
| 39 | my $sql_curr = "SELECT COUNT (*) FROM pg_stat_activity;"; |
---|
| 40 | my $sth_curr = $Dbh->prepare($sql_curr); |
---|
| 41 | $sth_curr->execute(); |
---|
| 42 | my ($curr_conn) = $sth_curr->fetchrow(); |
---|
| 43 | print "connections.value $curr_conn\n"; |
---|
| 44 | } |
---|