root / plugins / mysql / mysql_size_all @ ee226a60
Historique | Voir | Annoter | Télécharger (3,57 ko)
| 1 | cba9455c | Rodolphe Quiedeville/NB | #!/usr/bin/perl |
|---|---|---|---|
| 2 | # |
||
| 3 | # Copyright (C) 2007 - Rodolphe Quiedeville <rodolphe@quiedeville.org> |
||
| 4 | # Copyright (C) 2003-2004 - Andreas Buer |
||
| 5 | # |
||
| 6 | # This program is free software; you can redistribute it and/or |
||
| 7 | # modify it under the terms of the GNU General Public License |
||
| 8 | # as published by the Free Software Foundation; version 2 dated June, |
||
| 9 | # 1991. |
||
| 10 | # |
||
| 11 | # This program is distributed in the hope that it will be useful, |
||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | # GNU General Public License for more details. |
||
| 15 | # |
||
| 16 | # You should have received a copy of the GNU General Public License |
||
| 17 | # along with this program; if not, write to the Free Software |
||
| 18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 19 | # |
||
| 20 | # $Log$ |
||
| 21 | # Revision 1.1 2007/01/17 10:41:01 rodo |
||
| 22 | # Change incorrect family |
||
| 23 | # |
||
| 24 | # Revision 1.0 2007/01/16 15:57:01 rodo |
||
| 25 | # Created by Rodolphe Quiedeville |
||
| 26 | # |
||
| 27 | # Parameters: |
||
| 28 | # |
||
| 29 | # config |
||
| 30 | # autoconf |
||
| 31 | # |
||
| 32 | # Configuration variables |
||
| 33 | # |
||
| 34 | # mysqlopts - Options to pass to mysql |
||
| 35 | # mysqladmin - Override location of mysqladmin |
||
| 36 | # |
||
| 37 | #%# family=manual |
||
| 38 | #%# capabilities=autoconf |
||
| 39 | |||
| 40 | use strict; |
||
| 41 | |||
| 42 | # unless ($0 =~ /mysql_size(?:_([^_]+)|)_(.+)\s*$/) |
||
| 43 | # {
|
||
| 44 | # die "Could not parse name $0.\n"; |
||
| 45 | # } |
||
| 46 | # my $db = $2; |
||
| 47 | |||
| 48 | my $COMMAND; |
||
| 49 | my $MYSQLADMIN = $ENV{mysqladmin} || "mysql";
|
||
| 50 | |||
| 51 | my $arg = shift(); |
||
| 52 | |||
| 53 | if ($arg eq 'config') {
|
||
| 54 | print_config(); |
||
| 55 | exit(); |
||
| 56 | } elsif ($arg eq 'autoconf') {
|
||
| 57 | unless (test_service() ) {
|
||
| 58 | print "yes\n"; |
||
| 59 | } else {
|
||
| 60 | print "no\n"; |
||
| 61 | } |
||
| 62 | exit; |
||
| 63 | } |
||
| 64 | |||
| 65 | sub getDBList; |
||
| 66 | foreach my $db (getDBList()) {
|
||
| 67 | |||
| 68 | my (@infos,$info,$i_data,$i_index); |
||
| 69 | |||
| 70 | $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | head -n 1";
|
||
| 71 | |||
| 72 | open(SERVICE, "$COMMAND |") |
||
| 73 | 8713eb37 | Lars Kruse | or die("Could not execute '$COMMAND': $!");
|
| 74 | cba9455c | Rodolphe Quiedeville/NB | |
| 75 | while (<SERVICE>) {
|
||
| 76 | (@infos) = split; |
||
| 77 | } |
||
| 78 | close(SERVICE); |
||
| 79 | |||
| 80 | my $i = 0; |
||
| 81 | foreach $info (@infos) {
|
||
| 82 | $i++; |
||
| 83 | if ($info eq 'Data_length') {
|
||
| 84 | $i_data = $i; |
||
| 85 | next; |
||
| 86 | } |
||
| 87 | if ($info eq 'Index_length') {
|
||
| 88 | $i_index = $i; |
||
| 89 | last; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | my $total_size = 0; |
||
| 93 | if ($i_data>0 && $i_index>0) {
|
||
| 94 | $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | cut -f $i_data,$i_index | grep -v leng";
|
||
| 95 | |||
| 96 | open(SERVICE, "$COMMAND |") |
||
| 97 | 8713eb37 | Lars Kruse | or die("Could not execute '$COMMAND': $!");
|
| 98 | cba9455c | Rodolphe Quiedeville/NB | |
| 99 | while (<SERVICE>) {
|
||
| 100 | (m/(\d+).*?(\d+(?:\.\d+)?)/); |
||
| 101 | 07c854f2 | Lars Kruse | $total_size += $1 + $2; |
| 102 | cba9455c | Rodolphe Quiedeville/NB | } |
| 103 | close(SERVICE); |
||
| 104 | |||
| 105 | } |
||
| 106 | print("$db.value $total_size\n");
|
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | sub print_config {
|
||
| 111 | |||
| 112 | my $num = 0; |
||
| 113 | |||
| 114 | my @dbs = getDBList; |
||
| 115 | |||
| 116 | print("graph_title MySQL databases size\n");
|
||
| 117 | print ('graph_args --base 1024 -l 0
|
||
| 118 | graph_vlabel bytes |
||
| 119 | 8af93fce | dipohl | graph_category db |
| 120 | cba9455c | Rodolphe Quiedeville/NB | graph_info Plugin available at <a href="http://rodolphe.quiedeville.org/hack/munin/">http://rodolphe.quiedeville.org/hack/munin/</a> |
| 121 | '); |
||
| 122 | |||
| 123 | for my $db (@dbs) {
|
||
| 124 | my $title = "$db"; |
||
| 125 | print("$title.label ${title}\n",
|
||
| 126 | "$title.min 0\n", |
||
| 127 | "$title.type GAUGE\n", |
||
| 128 | "$title.draw ", ($num) ? "STACK" : "AREA" , "\n", |
||
| 129 | ); |
||
| 130 | $num++; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | sub test_service {
|
||
| 136 | |||
| 137 | my $return = 1; |
||
| 138 | |||
| 139 | system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
|
||
| 140 | if ($? == 0) |
||
| 141 | {
|
||
| 142 | system ("$COMMAND >/dev/null 2>/dev/null");
|
||
| 143 | if ($? == 0) |
||
| 144 | {
|
||
| 145 | print "yes\n"; |
||
| 146 | $return = 0; |
||
| 147 | } |
||
| 148 | else |
||
| 149 | {
|
||
| 150 | print "no (could not connect to mysql)\n"; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | else |
||
| 154 | {
|
||
| 155 | print "no (mysqladmin not found)\n"; |
||
| 156 | } |
||
| 157 | exit $return; |
||
| 158 | } |
||
| 159 | |||
| 160 | sub getDBList {
|
||
| 161 | my @dbs; |
||
| 162 | foreach my $f (glob("/var/lib/mysql/*")) {
|
||
| 163 | 17f78427 | Lars Kruse | if (-d $f) {
|
| 164 | 970a09d1 | clorch | $f =~ s/\@002d/-/g; |
| 165 | cba9455c | Rodolphe Quiedeville/NB | $f =~ s!.*/!!; |
| 166 | @dbs[$#dbs+1]=$f }; |
||
| 167 | } |
||
| 168 | return @dbs; |
||
| 169 | } |
