2 # Plugin to monitor pg_stat_*_tables (user by default)
 
   4 # Copyright Dalibo <cedric.villemain@dalibo.com> 2007
 
   5 # Based on a plugin (postgres_block_read_) from Björn Ruberg <bjorn@linpro.no>
 
   7 # Licenced under GPL v2.
 
  11 #       Symlink into /etc/munin/plugins/ and add the monitored
 
  12 #       database to the filename. e.g.:
 
  14 #       ln -s /usr/share/munin/plugins/pg__stat_tables \
 
  15 #         /etc/munin/plugins/pg_<databasename>_stat_tables
 
  16 #       This should, however, be given through autoconf and suggest.
 
  18 #       If required, give username, password and/or Postgresql server
 
  19 #       host through environment variables.
 
  21 #       You must also activate Postgresql statistics. See
 
  22 #       http://www.postgresql.org/docs/8.1/interactive/monitoring-stats.html
 
  23 #       for how to enable this. Specifically, the following lines must
 
  24 #       exist in your postgresql.conf:
 
  26 #           stats_start_collector = true
 
  27 #           stats_block_level = true
 
  36 #       dbhost     - Which database server to use. Defaults to
 
  38 #       dbname     - Which database to use. Defaults to template1
 
  39 #       dbuser     - A Postgresql user account with read permission to
 
  40 #                    the given database. Defaults to
 
  41 #                    'postgres'. Anyway, Munin must be told which user
 
  42 #                    this plugin should be run as.
 
  43 #       dbpass     - The corresponding password, if
 
  44 #                    applicable. Default to undef. Remember that
 
  45 #                    pg_hba.conf must be configured accordingly.
 
  49 #%# capabilities=autoconf
 
  53 use vars qw ( $debug $configure  );
 
  54 use constant _PGMINI => 70400;
 
  56 my $dbhost = $ENV{'dbhost'} || '';
 
  57 my $dbname = $ENV{'dbname'} || 'template1';
 
  58 my $dbuser = $ENV{'dbuser'} || 'postgres';
 
  59 my $dbport = $ENV{'dbport'} || '5432';
 
  60 my $dbpass = $ENV{'dbpass'} || '';
 
  61 my $statscope = $ENV{'statscope'} || 'user';
 
  63 my $dsn = "DBI:Pg:dbname=$dbname";
 
  64 $dsn   .=";host=$dbhost;port=$dbport" if $dbhost;
 
  65 my $pg_server_version;
 
  67 if (exists $ARGV[0]) {
 
  68   if ($ARGV[0] eq 'autoconf') {
 
  70     if (! eval "require DBD::Pg;") {
 
  71       print "no (DBD::Pg not found)";
 
  74     my $dbh = DBI->connect ($dsn,
 
  79       $pg_server_version = $dbh->{'pg_server_version'};
 
  80       if ($pg_server_version < (_PGMINI)) {
 
  81         $pg_server_version =~ /(\d)(\d){2,2}(\d){2,2}/;
 
  82         print "PostgreSQL Server version " . (_PGMINI) . " or above is needed. Current is $1.$2.$3 \n";
 
  88       print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
 
  91   } elsif ($ARGV[0] eq 'debug') {
 
  94   } elsif ($ARGV[0] eq 'config') {
 
 100 print "# $dsn\n" if $debug;
 
 101 my $dbh = DBI->connect ($dsn,
 
 106 die ("no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr."\n") unless($dbh);
 
 107 $pg_server_version = $dbh->{'pg_server_version'};
 
 110   print "graph_title Total Nominatim response time\n";
 
 111   print "graph_vlabel Time to response\n";
 
 112   print "graph_category Nominatim \n";
 
 113   print "graph_period minute\n";
 
 114   print "graph_args --base 1000\n";
 
 116   print "avg.label Average time to response\n";
 
 117   print "avg.draw LINE\n";
 
 118   print "avg.type GAUGE\n";
 
 120   print "avg.info Moving 5 minute average time to perform search\n";
 
 121   print "avg.label Average time to response\n";
 
 123   print "min.label Fastest time to response\n";
 
 124   print "min.draw LINE\n";
 
 125   print "min.type GAUGE\n";
 
 127   print "min.info Fastest query in last 5 minutes\n";
 
 129   print "max.label Slowest time to response\n";
 
 130   print "max.draw LINE\n";
 
 131   print "max.type GAUGE\n";
 
 133   print "max.info Slowest query in last 5 minutes\n";
 
 137   my $sql = "select TO_CHAR(avg(endtime-starttime),'SS.MS'),TO_CHAR(min(endtime-starttime),'SS.MS'),TO_CHAR(max(endtime-starttime),'SS.MS') from query_log where starttime > 'now'::timestamp - '5 minutes'::interval";
 
 138   print "# $sql\n" if $debug;
 
 139   my $sth = $dbh->prepare($sql);
 
 141   printf ("# Rows: %d\n",  $sth->rows) if $debug;
 
 142   if ($sth->rows > 0) {
 
 143     my ($avg, $min, $max) = $sth->fetchrow_array();
 
 144     print "avg.value $avg\n";
 
 145     print "min.value $min\n";
 
 146     print "max.value $max\n";