root / plugins / postgresql / postgres_space_ @ 17f78427
Historique | Voir | Annoter | Télécharger (5,88 ko)
| 1 | 590eaa06 | Bj?rn Ruberg | #!/usr/bin/perl |
|---|---|---|---|
| 2 | |||
| 3 | # (Temporary) source: http://munin.projects.linpro.no/ticket/63 |
||
| 4 | # Written by Bj?rn Ruberg (bjorn@linpro.no) 2006 |
||
| 5 | # Rewritten by Moses Moore 2006-04-08 moc.iazom@sesom |
||
| 6 | # Licenced under GPL |
||
| 7 | |||
| 8 | # Magic markers |
||
| 9 | #%# family=auto |
||
| 10 | #%# capabilities=autoconf suggest |
||
| 11 | |||
| 12 | use strict; |
||
| 13 | use DBI; |
||
| 14 | use vars qw ( $debug $suggest $configure $dbh ); |
||
| 15 | |||
| 16 | # Package maintainers should provide an environment |
||
| 17 | # file for the /etc/munin/plugin-conf.d/ directory |
||
| 18 | # to override these values if necessary. |
||
| 19 | # NOTE: The plugin (also when auto configured) should |
||
| 20 | # be run by the postgresql user account. |
||
| 21 | |||
| 22 | # Need these variables at an early stage to enable |
||
| 23 | # autoconf and suggest |
||
| 24 | my $dbhost = $ENV{'dbhost'} || ''; # Connect to localhost by default
|
||
| 25 | my $dbname = $ENV{'dbname'} || 'template1';
|
||
| 26 | my $dbuser = $ENV{'dbuser'} || 'postgres';
|
||
| 27 | my $dbpass = $ENV{'dbpass'} || '';
|
||
| 28 | |||
| 29 | if (exists $ARGV[0]) {
|
||
| 30 | if ($ARGV[0] eq 'autoconf') {
|
||
| 31 | # Check for DBD::Pg |
||
| 32 | if (! eval "require DBD::Pg;") {
|
||
| 33 | print "no (DBD::Pg not found)"; |
||
| 34 | exit 1; |
||
| 35 | } |
||
| 36 | # Then we try to detect Postgres presence by connecting to |
||
| 37 | # 'template1'. |
||
| 38 | my $dsn = "dbi:Pg:dbname=template1"; |
||
| 39 | $dsn .= ";host=$dbhost" if $dbhost; |
||
| 40 | my $tempdbh = DBI->connect ($dsn, $dbuser, $dbpass); |
||
| 41 | if ($tempdbh) {
|
||
| 42 | print "yes\n"; |
||
| 43 | exit 0; |
||
| 44 | } else {
|
||
| 45 | print "no (Can't connect to given host, please check environment settings)\n"; |
||
| 46 | exit 1; |
||
| 47 | } |
||
| 48 | } elsif ($ARGV[0] and $ARGV[0] eq 'debug') {
|
||
| 49 | # Set config flag |
||
| 50 | $debug = 1; |
||
| 51 | } elsif ($ARGV[0] and $ARGV[0] eq 'config') {
|
||
| 52 | # Set config flag |
||
| 53 | $configure = 1; |
||
| 54 | } elsif ($ARGV[0] eq 'suggest') {
|
||
| 55 | # doesn't always work |
||
| 56 | my @datasources = DBI->data_sources ('Pg');
|
||
| 57 | foreach my $dsn (grep !/\=template\d$/, @datasources) {
|
||
| 58 | (my $db = $dsn) =~ s/^.*=//; |
||
| 59 | print "$db\n"; |
||
| 60 | } |
||
| 61 | exit 0; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | # Must do this here, after checking for autoconf/suggest/etc, because the |
||
| 66 | # plugin must be able to run before it is linked to the databases. |
||
| 67 | my (undef, undef, $dbname) = split (/_/, $0, 3); |
||
| 68 | die "No dbname configured (did you make the proper symlink?)" unless $dbname; |
||
| 69 | |||
| 70 | my $dsn = "DBI:Pg:dbname=$dbname"; |
||
| 71 | $dsn .= ";host=$dbhost" if $dbhost; |
||
| 72 | print "#$dsn\n" if $debug; |
||
| 73 | my $dbh = DBI->connect ($dsn, $dbuser, $dbpass, {RaiseError =>1});
|
||
| 74 | unless($dbh) {
|
||
| 75 | die("Database $dbname\@$dbhost (". $DBI::errstr .")\n");
|
||
| 76 | } |
||
| 77 | |||
| 78 | if ($configure) {
|
||
| 79 | print <<_EOM; |
||
| 80 | graph_title Postgres database $dbname |
||
| 81 | graph_args -l 0 --base 1024 |
||
| 82 | graph_vlabel bytes |
||
| 83 | 29bdf34e | dipohl | graph_category db |
| 84 | 590eaa06 | Bj?rn Ruberg | graph_info Size |
| 85 | size.label Database size (bytes) |
||
| 86 | size.info Database size |
||
| 87 | size.type GAUGE |
||
| 88 | size.draw AREA |
||
| 89 | indexsize.label Index size (bytes) |
||
| 90 | indexsize.info Index size |
||
| 91 | indexsize.type GAUGE |
||
| 92 | indexsize.draw STACK |
||
| 93 | metasize.label Meta database size (bytes) |
||
| 94 | metasize.info Meta database size |
||
| 95 | metasize.type GAUGE |
||
| 96 | metasize.draw STACK |
||
| 97 | metaindexsize.label Meta index size (bytes) |
||
| 98 | metaindexsize.info Meta index size |
||
| 99 | metaindexsize.type GAUGE |
||
| 100 | metaindexsize.draw STACK |
||
| 101 | _EOM |
||
| 102 | } else {
|
||
| 103 | my $database_pages = 0; |
||
| 104 | my $database_indexes = 0; |
||
| 105 | my $metadatabase_pages = 0; |
||
| 106 | my $metadatabase_indexes = 0; |
||
| 107 | my @names = $dbh->tables; |
||
| 108 | 17f78427 | Lars Kruse | |
| 109 | 590eaa06 | Bj?rn Ruberg | # Find relfilenode and relpages from the given table |
| 110 | my $q_ind = "SELECT relkind, relfilenode, relpages FROM pg_class |
||
| 111 | WHERE relname = ? |
||
| 112 | UNION |
||
| 113 | SELECT relkind, relfilenode, relpages FROM pg_class |
||
| 114 | 17f78427 | Lars Kruse | WHERE relfilenode IN (SELECT indexrelid FROM pg_index |
| 115 | 590eaa06 | Bj?rn Ruberg | WHERE indrelid IN (SELECT relfilenode FROM pg_class |
| 116 | WHERE relname = ?))"; |
||
| 117 | my $sth = $dbh->prepare ($q_ind) or die $dbh->errstr; |
||
| 118 | 17f78427 | Lars Kruse | |
| 119 | 590eaa06 | Bj?rn Ruberg | # Iterate over the tables in the database |
| 120 | foreach my $table (@names) {
|
||
| 121 | my $meta = 1; |
||
| 122 | print "#TABLE: $table\n" if $debug; |
||
| 123 | my $table_pages = 0; |
||
| 124 | my $table_indexes = 0; |
||
| 125 | my $metatable_pages = 0; |
||
| 126 | my $metatable_indexes = 0; |
||
| 127 | # "public" tables are the user data |
||
| 128 | $meta = 0 if $table =~ /^public\./; |
||
| 129 | $table =~ s/^.*\.//; |
||
| 130 | 17f78427 | Lars Kruse | |
| 131 | 590eaa06 | Bj?rn Ruberg | # Call the query with $table twice for each side of the UNION |
| 132 | $sth->execute ($table, $table) or die $dbh->errstr; |
||
| 133 | while (my ($relkind, $relfilenode, $relpages) = $sth->fetchrow_array) {
|
||
| 134 | if ($relkind eq 'r') {
|
||
| 135 | $table_pages += $relpages if $meta == 0; |
||
| 136 | $metatable_pages += $relpages if $meta == 1; |
||
| 137 | } elsif ($relkind eq 'i') {
|
||
| 138 | $table_indexes += $relpages if $meta == 0; |
||
| 139 | $metatable_indexes += $relpages if $meta == 1; |
||
| 140 | } |
||
| 141 | # Define the query |
||
| 142 | 17f78427 | Lars Kruse | my $q2 = "SELECT SUM(relpages) |
| 143 | FROM pg_class |
||
| 144 | 590eaa06 | Bj?rn Ruberg | WHERE relname IN (?, ?)"; |
| 145 | my $sth2 = $dbh->prepare ($q2); |
||
| 146 | $sth2->execute ("pg_toast_${relfilenode}",
|
||
| 147 | "pg_toast_${relfilenode}_index");
|
||
| 148 | my $relpages = $sth2->fetchrow_array; |
||
| 149 | if ($relkind eq 'r') {
|
||
| 150 | $table_pages += $relpages if $meta == 0; |
||
| 151 | $metatable_pages += $relpages if $meta == 1; |
||
| 152 | } elsif ($relkind eq 'i') {
|
||
| 153 | $table_indexes += $relpages if $meta == 0; |
||
| 154 | $metatable_indexes += $relpages if $meta == 1; |
||
| 155 | } |
||
| 156 | print "#\tR:$relfilenode\tP:$table_pages\tI:$table_indexes\n" if $debug; |
||
| 157 | } |
||
| 158 | $database_pages += $table_pages; |
||
| 159 | $database_indexes += $table_indexes; |
||
| 160 | $metadatabase_pages += $metatable_pages; |
||
| 161 | $metadatabase_indexes += $metatable_indexes; |
||
| 162 | 17f78427 | Lars Kruse | } |
| 163 | 590eaa06 | Bj?rn Ruberg | $sth->finish; |
| 164 | $dbh->disconnect; |
||
| 165 | print "size\.value " . $database_pages * 8192 . "\n"; |
||
| 166 | print "indexsize\.value " . $database_indexes * 8192 . "\n"; |
||
| 167 | print "metasize\.value " . $metadatabase_pages * 8192 . "\n"; |
||
| 168 | print "metaindexsize\.value " . $metadatabase_indexes * 8192 . "\n"; |
||
| 169 | } |
