]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/nominatim_lag
5afa44bd5a7f722c59fc8cfb6e59a24307d9437d
[chef.git] / cookbooks / munin / files / default / plugins / nominatim_lag
1 #!/usr/bin/perl -w
2 # Plugin to monitor pg_stat_*_tables (user by default)
3 #
4 # Copyright 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 #       Symlink into /etc/munin/plugins/ and add the monitored
12 #       database to the filename. e.g.:
13 #
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.
17 #
18 #       If required, give username, password and/or Postgresql server
19 #       host through environment variables.
20 #
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:
25 #
26 #           stats_start_collector = true
27 #           stats_block_level = true
28 #
29 #
30 # Parameters:
31 #
32 #       config   (required)
33 #
34 # Config variables:
35 #
36 #       dbhost     - Which database server to use. Defaults to
37 #                    'localhost'.
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.
46 #
47 # Magic markers
48 #%# family=auto
49 #%# capabilities=autoconf
50
51 use strict;
52 use DBI;
53 use vars qw ( $debug $configure  );
54 use constant _PGMINI => 70400;
55
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';
62
63 my $dsn = "DBI:Pg:dbname=$dbname";
64 $dsn   .=";host=$dbhost;port=$dbport" if $dbhost;
65 my $pg_server_version;
66
67 if (exists $ARGV[0]) {
68   if ($ARGV[0] eq 'autoconf') {
69     # Check for DBD::Pg
70     if (! eval "require DBD::Pg;") {
71       print "no (DBD::Pg not found)";
72       exit 1;
73     }
74     my $dbh = DBI->connect ($dsn,
75                             $dbuser,
76                             $dbpass,
77                             {RaiseError =>1});
78     if ($dbh) {
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";
83                                 exit 1;
84       }
85       print "yes\n";
86       exit 0;
87     } else {
88       print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
89       exit 1;
90     }
91   } elsif ($ARGV[0] eq 'debug') {
92     # Set debug flag
93     $debug = 1;
94   } elsif ($ARGV[0] eq 'config') {
95     # Set config flag
96     $configure = 1;
97   }
98 }
99
100 print "# $dsn\n" if $debug;
101 my $dbh = DBI->connect ($dsn,
102                         $dbuser,
103                         $dbpass,
104                         {RaiseError =>1});
105
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'};
108
109 if ($configure) {
110   print "graph_title Total Nominatim Lag Time\n";
111   print "graph_vlabel Lag\n";
112   print "graph_category Nominatim \n";
113   print "graph_period minute\n";
114   print "graph_args --base 1000\n";
115
116   print "lag.label Lag\n";
117   print "lag.draw LINE\n";
118   print "lag.type GAUGE\n";
119   print "lag.min 0\n";
120
121 } else {
122
123   my $sql = "select round(extract('epoch' from 'now'::timestamp with time zone - lastimportdate)) from import_status";
124   print "# $sql\n" if $debug;
125   my $sth = $dbh->prepare($sql);
126   $sth->execute();
127   printf ("# Rows: %d\n",  $sth->rows) if $debug;
128   if ($sth->rows > 0) {
129     my ($lag) = $sth->fetchrow_array();
130     print "lag.value $lag\n";
131   }
132 }
133
134 exit 0;
135