plugin-pg__database_blks: pg__database_blks

File pg__database_blks, 3.6 kB (added by ajaja, 6 years ago)
Line 
1 #!/usr/bin/perl -w
2 # Plugin for monitor postgres database reads.
3 #
4 # Licenced under GPL v2.
5 #
6 # Usage:
7 #
8 #       Symlink into /etc/munin/plugins/ and add the monitored
9 #       database to the filename. e.g.:
10 #
11 #       ln -s /usr/share/munin/plugins/pg_database__blks \
12 #         /etc/munin/plugins/pg_database_<databasename>_blks
13 #       This should, however, be given through autoconf and suggest.
14 #
15 #       If required, give username, password and/or Postgresql server
16 #       host through environment variables.
17 #
18 #       You must also activate Postgresql statistics. See
19 #       http://www.postgresql.org/docs/7.4/interactive/monitoring-stats.html
20 #       for how to enable this. Specifically, the following lines must
21 #       exist in your postgresql.conf:
22 #
23 #           stats_start_collector = true
24 #           stats_block_level = true
25 #
26 #
27 # Parameters:
28 #
29 #       config   (required)
30 #
31 # Config variables:
32 #
33 #       dbhost     - Which database server to use. Defaults to
34 #                    'localhost'.
35 #       dbname     - Which database to use. Defaults to template1
36 #       dbuser     - A Postgresql user account with read permission to
37 #                    the given database. Defaults to
38 #                    'postgres'. Anyway, Munin must be told which user
39 #                    this plugin should be run as.
40 #       dbpass     - The corresponding password, if
41 #                    applicable. Default to undef. Remember that
42 #                    pg_hba.conf must be configured accordingly.
43 #
44 # Magic markers
45 #%# family=auto
46 #%# capabilities=autoconf
47
48 use strict;
49 use DBI;
50
51 my $dbhost = $ENV{'dbhost'} || '127.0.0.1';
52 my $dbname = $ENV{'dbname'} || 'template1';
53 my $dbuser = $ENV{'dbuser'} || 'postgres';
54 my $dbport = $ENV{'dbport'} || '5432';
55 my $dbpass = $ENV{'dbpass'} || '';
56
57 # Check for DBD::Pg
58 if (! eval "require DBD::Pg;") {
59      exit 1;
60 }
61
62 my $dsn = "DBI:Pg:dbname=$dbname;host=$dbhost;port=$dbport";
63 #print "$dsn\n";
64 my $dbh = DBI->connect ($dsn, $dbuser,
65                         $dbpass,
66                         {RaiseError =>1}) || die "";
67
68
69
70 if (exists $ARGV[0]) {
71     if ($ARGV[0] eq 'autoconf') {
72         # Check for DBD::Pg
73         if (! eval "require DBD::Pg;") {
74              print "no (DBD::Pg not found)";
75              exit 1;
76         }
77
78         if  ( ! $dbh) {
79             print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
80             exit 1;
81         }
82
83         #is Collects block-level statistics on database activity on?
84         my $sql = "show stats_block_level;";
85         my $sth = $dbh->prepare($sql);
86         $sth->execute();
87         my ($stats_block_level) = $sth->fetchrow();     
88         if ( $stats_block_level eq "off" ) {
89            print "no Collects block-level statistics on database activity is off (stats_block_level=off)\n";
90            exit 1;
91         }
92        
93         # all is OK
94         print "yes\n";
95         exit 0;
96        
97     }
98
99     if ($ARGV[0] eq "config") {
100         print "graph_title PostgreSQL data reads from $dbname\n";
101         print "graph_args --base 1000\n";
102         print "graph_vlabel Blocks read per \${graph_period}\n";
103         print "graph_category Postgresql\n";
104         print "graph_info Shows number of blocks read from disk and from memory\n";
105         print "blks_read.label Read from disk\n";
106         print "blks_read.info Read from disk\n";
107         print "blks_read.type DERIVE\n";
108         print "blks_read.min 0\n";
109         print "blks_read.draw AREA\n";
110         print "blks_hit.label Cached in memory\n";
111         print "blks_hit.info Cached in memory\n";
112         print "blks_hit.type DERIVE\n";
113         print "blks_hit.min 0\n";
114         print "blks_hit.draw STACK\n";
115         exit 0;
116     }
117 }
118
119
120 my $sql = "select blks_read,blks_hit from pg_stat_database where datname='$dbname';";
121 my $sth = $dbh->prepare($sql);
122 $sth->execute();
123 my ($blks_read,$blks_hit) = $sth->fetchrow();
124 print "blks_read.value $blks_read\n";
125 print "blks_hit.value $blks_hit\n";