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 queries processed\n";
 
 111   print "graph_vlabel Number / \${graph_period}\n";
 
 112   print "graph_category Nominatim \n";
 
 113   print "graph_args --base 1000\n";
 
 115   print "search.label Queries performed\n";
 
 116   print "search.draw LINE\n";
 
 117   print "search.type DERIVE\n";
 
 118   print "search.min 0\n";
 
 119   print "search.info Number of search queries performed\n";
 
 121   print "search_completed.label Queries completed\n";
 
 122   print "search_completed.draw LINE\n";
 
 123   print "search_completed.type DERIVE\n";
 
 124   print "search_completed.min 0\n";
 
 125   print "search_completed.info Number of search queries completed\n";
 
 127   print "search_failed.label Queries failed\n";
 
 128   print "search_failed.draw LINE\n";
 
 129   print "search_failed.type DERIVE\n";
 
 130   print "search_failed.min 0\n";
 
 131   print "search_failed.info Number of search queries failed to find a match\n";
 
 134   my $sql = "select count(*),sum(case when results > 0 THEN 1 ELSE 0 END),sum(case when results = 0 THEN 1 ELSE 0 END) from query_log";
 
 135   print "# $sql\n" if $debug;
 
 136   my $sth = $dbh->prepare($sql);
 
 138   printf ("# Rows: %d\n",  $sth->rows) if $debug;
 
 139   if ($sth->rows > 0) {
 
 140     my ($search, $search_completed, $search_failed) = $sth->fetchrow_array();
 
 141     print "search.value $search\n";
 
 142     print "search_completed.value $search_completed\n";
 
 143     print "search_failed.value $search_failed\n";