plugin-pg__all_tables_scan: pg__all_tables_scan

File pg__all_tables_scan, 3.4 kB (added by ajaja, 5 years ago)
Line 
1 #!/usr/bin/perl -w
2 # Plugin for monitor postgres number of scans initiated
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__all_tables__scan \
12 #         /etc/munin/plugins/pg_<databasename>_all_tables_scan
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/8.1/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 my $statscope = $ENV{'statscope'} || 'user';
57
58 # Check for DBD::Pg
59 if (! eval "require DBD::Pg;") {
60      exit 1;
61 }
62
63 my $dsn = "DBI:Pg:dbname=$dbname;host=$dbhost;port=$dbport";
64 #print "$dsn\n";
65 my $dbh = DBI->connect ($dsn, $dbuser,
66                         $dbpass,
67                         {RaiseError =>1}) || die "";
68
69
70
71 if (exists $ARGV[0]) {
72     if ($ARGV[0] eq 'autoconf') {
73         # Check for DBD::Pg
74         if (! eval "require DBD::Pg;") {
75              print "no (DBD::Pg not found)";
76              exit 1;
77         }
78         if ($dbh) {
79             print "yes\n";
80             exit 0;
81         } else {
82             print "no Unable to access Database $dbname on host $dbhost as user $dbuser.\nError returned was: ". $DBI::errstr;
83             exit 1;
84         }
85     }
86
87     if ($ARGV[0] eq "config") {
88         print "graph_title PostgreSQL scans initiated from $dbname\n";
89         print "graph_vlabel Number / \${graph_period}\n";
90         print "graph_category Postgresql \n";
91         print "graph_args --base 1000\n";
92
93         print "seq_scan.label  Sequential scans initiated\n";
94         print "seq_scan.draw LINE2\n";
95         print "seq_scan.type DERIVE\n";
96         print "seq_scan.min 0\n";
97         print "seq_scan.graph_info Number of sequential scans initiated\n";
98
99         print "idx_scan.label Index scans initiated\n";
100         print "idx_scan.draw LINE2\n";
101         print "idx_scan.type DERIVE\n";
102         print "idx_scan.min 0\n";
103         print "idx_scan.graph_info Number of index scans initiated\n";
104
105         exit 0;
106     }
107 }
108
109 my $sql = "select sum(seq_scan),sum(idx_scan) from pg_stat_user_tables;";
110 my $sth = $dbh->prepare($sql);
111 $sth->execute();
112 my ($seq_scan,$idx_scan) = $sth->fetchrow();
113 print "seq_scan.value $seq_scan\n";
114 print "idx_scan.value $idx_scan\n";