2 # Plugin to monitor response time of search queries.
 
   4 # Based on a plugin by 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 #       If required, give username, password and/or Postgresql server
 
  12 #       host through environment variables. See man page of psql for
 
  21 #%# capabilities=autoconf
 
  25 use vars qw ( $debug $configure  );
 
  26 use constant _PGMINI => 70400;
 
  28 my $dbhost = $ENV{'PGHOST'} || '';
 
  29 my $dbname = $ENV{'PGDATABASE'} || 'nominatim';
 
  30 my $dbuser = $ENV{'PGUSER'} || 'postgres';
 
  31 my $dbport = $ENV{'PGPORT'} || '5432';
 
  33 my $dsn = "DBI:Pg:dbname=$dbname";
 
  34 $dsn   .=";host=$dbhost;port=$dbport" if $dbhost;
 
  35 my $pg_server_version;
 
  37 if (exists $ARGV[0]) {
 
  38   if ($ARGV[0] eq 'autoconf') {
 
  40     if (! eval "require DBD::Pg;") {
 
  41       print "no (DBD::Pg not found)";
 
  44     my $dbh = DBI->connect ($dsn,
 
  49       $pg_server_version = $dbh->{'pg_server_version'};
 
  50       if ($pg_server_version < (_PGMINI)) {
 
  51         $pg_server_version =~ /(\d)(\d){2,2}(\d){2,2}/;
 
  52         print "PostgreSQL Server version " . (_PGMINI) . " or above is needed. Current is $1.$2.$3 \n";
 
  58       print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
 
  61   } elsif ($ARGV[0] eq 'debug') {
 
  64   } elsif ($ARGV[0] eq 'config') {
 
  70 print "# $dsn\n" if $debug;
 
  71 my $dbh = DBI->connect ($dsn,
 
  76 die ("no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr."\n") unless($dbh);
 
  77 $pg_server_version = $dbh->{'pg_server_version'};
 
  80   print "graph_title Total Nominatim response time\n";
 
  81   print "graph_vlabel Time to response\n";
 
  82   print "graph_category Nominatim \n";
 
  83   print "graph_period minute\n";
 
  84   print "graph_args --base 1000\n";
 
  86   print "avg.label Average time to response\n";
 
  87   print "avg.draw LINE\n";
 
  88   print "avg.type GAUGE\n";
 
  90   print "avg.info Moving 5 minute average time to perform search\n";
 
  91   print "avg.label Average time to response\n";
 
  93   print "min.label Fastest time to response\n";
 
  94   print "min.draw LINE\n";
 
  95   print "min.type GAUGE\n";
 
  97   print "min.info Fastest query in last 5 minutes\n";
 
  99   print "max.label Slowest time to response\n";
 
 100   print "max.draw LINE\n";
 
 101   print "max.type GAUGE\n";
 
 103   print "max.info Slowest query in last 5 minutes\n";
 
 107   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 new_query_log where starttime > 'now'::timestamp - '5 minutes'::interval";
 
 108   print "# $sql\n" if $debug;
 
 109   my $sth = $dbh->prepare($sql);
 
 111   printf ("# Rows: %d\n",  $sth->rows) if $debug;
 
 112   if ($sth->rows > 0) {
 
 113     my ($avg, $min, $max) = $sth->fetchrow_array();
 
 114     print "avg.value $avg\n";
 
 115     print "min.value $min\n";
 
 116     print "max.value $max\n";