Initial checkin of xvm-munin-config package
[invirt/packages/xvm-munin-config.git] / master / usr / share / xvm-munin-master-config / plugins / postgres_locks
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3 #
4 # Show postgres lock statistics
5 #
6 # Parameters:
7 #
8 #       config   (required)
9 #
10 # Config variables:
11 #
12 #       dbhost     - Which database server to use. Defaults to
13 #                    'localhost'. 
14 #       dbport     - Which port on the database server to connect to.
15 #                    Defaults to '5432'.
16 #       dbuser     - A Postgresql user account with read permission to
17 #                    the given database. Defaults to
18 #                    'postgres'. Anyway, Munin must be told which user
19 #                    this plugin should be run as. 
20 #       dbpass     - The corresponding password, if
21 #                    applicable. Default to undef. Remember that
22 #                    pg_hba.conf must be configured accordingly.
23 #
24 # Magic markers
25 #%# family=auto
26 #%# capabilities=suggest
27
28 use strict;
29 use DBI;
30
31 # See postgress_block_read_ for docs
32
33 my $dbhost = $ENV{'dbhost'} || '';
34 my $dbport = $ENV{'dbport'} || '';
35 my $dbname = $ENV{'dbname'} || 'template1';
36 my $dbuser = $ENV{'dbuser'} || 'postgres';
37
38 if ($ARGV[0] && $ARGV[0] eq "config") {
39     print <<EOF;
40 graph_title Postgres locks
41 graph_args --base 1000
42 graph_vlabel Locks
43 graph_category Postgresql
44 graph_info Shows Postgresql locks
45 locks.label Locks
46 locks.info Locks (more info here, please... :)
47 locks.type GAUGE
48 locks.warning 5
49 locks.critical 10
50 exlocks.label Exclusive locks
51 exlocks.info Exclusive locks (here too, please... :)
52 exlocks.type GAUGE
53 exlocks.warning 5
54 exlocks.critical 10
55 EOF
56 } else {
57     my $Con = "DBI:Pg:dbname=$dbname";
58     $Con .= ";host=$dbhost" if $dbhost;
59     $Con .= ";port=$dbport" if $dbport;
60     my $Dbh = DBI->connect ($Con, $dbuser,
61                             '',
62                             {RaiseError =>1}) || die "Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
63
64     my $sql="SELECT mode,COUNT(mode) FROM pg_locks GROUP BY mode ORDER BY mode;";
65     my $sth = $Dbh->prepare ($sql);
66     $sth->execute ();
67     my $locks = 0;
68     my $exlocks = 0;
69     while (my ($mode, $count) = $sth->fetchrow ()) {
70         if ($mode =~ /exclusive/i) {
71             $exlocks = $exlocks + $count;
72         }
73         $locks = $locks+$count;
74     }
75     print "locks.value $locks\n";
76     print "exlocks.value $exlocks\n";
77 }