#!/usr/bin/perl # -*- perl -*- # # Plugin to monitor number of PostgreSQL database connections. # # Parameters: # # config # autoconf # # Configuration variables: # # PGHOST - Database server to use. Defaults to using ident # authentication with the local server. # PGPORT - Port to connect to. Defaults to '5432'. # PGDATABASE - Database to connect to. Defaults to 'template1'. # PGUSER - User to connect as, if necessary. # PGPASSWORD - Corresponding password to use, if necessary. # # (See libpq documentation for more.) # Note that PGDATABASE will default to 'template1' in this plugin, and # without PGHOST it will try ident authentication with the local server, # as the user that the plugin is running as. # # Configuration example: # # # Use local server, ident authentication with the 'postgres' user. # [postgres_*] # user postgres # # # Use local server, TCP authentication with a username and password. # [postgres_*] # env.PGHOST localhost # env.PGUSER someuser # env.PGPASSWORD somepassword # use strict; use warnings; use DBI; # Default to template1 database. $ENV{'PGDATABASE'} ||= 'template1'; my $dbh = DBI->connect ('dbi:Pg:', '','',{RaiseError =>1}) || die "Unable to access database. Error returned was: ". $DBI::errstr; if ($ARGV[0] && $ARGV[0] eq 'config') { my $sql_max = "SHOW max_connections;"; my $sth_max = $dbh->prepare($sql_max); $sth_max->execute(); my ($max_conn) = $sth_max->fetchrow(); my $warning = int ($max_conn * 0.7); my $critical = int ($max_conn * 0.8); print <prepare($sql_curr); $sth_curr->execute(); my ($curr_conn) = $sth_curr->fetchrow(); print "connections.value $curr_conn\n"; }