]> git.openstreetmap.org Git - nominatim.git/blob - munin/nominatim_query_speed
README: tiny markdown syntax error
[nominatim.git] / munin / nominatim_query_speed
1 #!/usr/bin/perl -w
2 # Plugin to monitor response time of search queries.
3 #
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>
6 #
7 # Licenced under GPL v2.
8 #
9 # Usage:
10 #
11 #       If required, give username, password and/or Postgresql server
12 #       host through environment variables. See man page of psql for
13 #       more info.
14 #
15 # Parameters:
16 #
17 #       config   (required)
18 #
19 # Magic markers
20 #%# family=auto
21 #%# capabilities=autoconf
22
23 use strict;
24 use DBI;
25 use vars qw ( $debug $configure  );
26 use constant _PGMINI => 70400;
27
28 my $dbhost = $ENV{'PGHOST'} || '';
29 my $dbname = $ENV{'PGDATABASE'} || 'nominatim';
30 my $dbuser = $ENV{'PGUSER'} || 'postgres';
31 my $dbport = $ENV{'PGPORT'} || '5432';
32
33 my $dsn = "DBI:Pg:dbname=$dbname";
34 $dsn   .=";host=$dbhost;port=$dbport" if $dbhost;
35 my $pg_server_version;
36
37 if (exists $ARGV[0]) {
38   if ($ARGV[0] eq 'autoconf') {
39     # Check for DBD::Pg
40     if (! eval "require DBD::Pg;") {
41       print "no (DBD::Pg not found)";
42       exit 1;
43     }
44     my $dbh = DBI->connect ($dsn,
45                             $dbuser,
46                             '',
47                             {RaiseError =>1});
48     if ($dbh) {
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";
53                                 exit 1;
54       }
55       print "yes\n";
56       exit 0;
57     } else {
58       print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
59       exit 1;
60     }
61   } elsif ($ARGV[0] eq 'debug') {
62     # Set debug flag
63     $debug = 1;
64   } elsif ($ARGV[0] eq 'config') {
65     # Set config flag
66     $configure = 1;
67   }
68 }
69
70 print "# $dsn\n" if $debug;
71 my $dbh = DBI->connect ($dsn,
72                         $dbuser,
73                         '',
74                         {RaiseError =>1});
75
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'};
78
79 if ($configure) {
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";
85
86   print "avg.label Average time to response\n";
87   print "avg.draw LINE\n";
88   print "avg.type GAUGE\n";
89   print "avg.min 0\n";
90   print "avg.info Moving 5 minute average time to perform search\n";
91   print "avg.label Average time to response\n";
92
93   print "min.label Fastest time to response\n";
94   print "min.draw LINE\n";
95   print "min.type GAUGE\n";
96   print "min.min 0\n";
97   print "min.info Fastest query in last 5 minutes\n";
98
99   print "max.label Slowest time to response\n";
100   print "max.draw LINE\n";
101   print "max.type GAUGE\n";
102   print "max.min 0\n";
103   print "max.info Slowest query in last 5 minutes\n";
104
105 } else {
106
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);
110   $sth->execute();
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";
117   }
118 }
119
120 exit 0;
121