root / plugins / mysql / mysql_size_all @ 17f78427
Historique | Voir | Annoter | Télécharger (3,81 ko)
| 1 |
#!/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 %WANTED = ( "Index" => "index", |
| 52 |
"Datas" => "datas", |
| 53 |
); |
| 54 |
|
| 55 |
my $arg = shift(); |
| 56 |
|
| 57 |
if ($arg eq 'config') {
|
| 58 |
print_config(); |
| 59 |
exit(); |
| 60 |
} elsif ($arg eq 'autoconf') {
|
| 61 |
unless (test_service() ) {
|
| 62 |
print "yes\n"; |
| 63 |
} else {
|
| 64 |
print "no\n"; |
| 65 |
} |
| 66 |
exit; |
| 67 |
} |
| 68 |
|
| 69 |
sub getDBList; |
| 70 |
foreach my $db (getDBList()) {
|
| 71 |
|
| 72 |
my $datas = 0; |
| 73 |
my $indexes = 0; |
| 74 |
my (@infos,$info,$i_data,$i_index); |
| 75 |
|
| 76 |
$COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | head -n 1";
|
| 77 |
|
| 78 |
open(SERVICE, "$COMMAND |") |
| 79 |
or die("Coult not execute '$COMMAND': $!");
|
| 80 |
|
| 81 |
while (<SERVICE>) {
|
| 82 |
(@infos) = split; |
| 83 |
} |
| 84 |
close(SERVICE); |
| 85 |
|
| 86 |
my $i = 0; |
| 87 |
foreach $info (@infos) {
|
| 88 |
$i++; |
| 89 |
if ($info eq 'Data_length') {
|
| 90 |
$i_data = $i; |
| 91 |
next; |
| 92 |
} |
| 93 |
if ($info eq 'Index_length') {
|
| 94 |
$i_index = $i; |
| 95 |
last; |
| 96 |
} |
| 97 |
} |
| 98 |
my $total_size = 0; |
| 99 |
if ($i_data>0 && $i_index>0) {
|
| 100 |
$COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | cut -f $i_data,$i_index | grep -v leng";
|
| 101 |
|
| 102 |
open(SERVICE, "$COMMAND |") |
| 103 |
or die("Coult not execute '$COMMAND': $!");
|
| 104 |
|
| 105 |
while (<SERVICE>) {
|
| 106 |
(m/(\d+).*?(\d+(?:\.\d+)?)/); |
| 107 |
$datas += $1; |
| 108 |
$indexes += $2; |
| 109 |
} |
| 110 |
close(SERVICE); |
| 111 |
|
| 112 |
$total_size = $datas+$indexes; |
| 113 |
} |
| 114 |
print("$db.value $total_size\n");
|
| 115 |
# print("datas.value $datas\n");
|
| 116 |
# print("index.value $indexes\n");
|
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
sub print_config {
|
| 121 |
|
| 122 |
my $num = 0; |
| 123 |
|
| 124 |
my @dbs = getDBList; |
| 125 |
|
| 126 |
print("graph_title MySQL databases size\n");
|
| 127 |
print ('graph_args --base 1024 -l 0
|
| 128 |
graph_vlabel bytes |
| 129 |
graph_category db |
| 130 |
graph_info Plugin available at <a href="http://rodolphe.quiedeville.org/hack/munin/">http://rodolphe.quiedeville.org/hack/munin/</a> |
| 131 |
'); |
| 132 |
|
| 133 |
for my $db (@dbs) {
|
| 134 |
my $title = "$db"; |
| 135 |
print("$title.label ${title}\n",
|
| 136 |
"$title.min 0\n", |
| 137 |
"$title.type GAUGE\n", |
| 138 |
"$title.draw ", ($num) ? "STACK" : "AREA" , "\n", |
| 139 |
); |
| 140 |
$num++; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
sub test_service {
|
| 146 |
|
| 147 |
my $return = 1; |
| 148 |
|
| 149 |
system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
|
| 150 |
if ($? == 0) |
| 151 |
{
|
| 152 |
system ("$COMMAND >/dev/null 2>/dev/null");
|
| 153 |
if ($? == 0) |
| 154 |
{
|
| 155 |
print "yes\n"; |
| 156 |
$return = 0; |
| 157 |
} |
| 158 |
else |
| 159 |
{
|
| 160 |
print "no (could not connect to mysql)\n"; |
| 161 |
} |
| 162 |
} |
| 163 |
else |
| 164 |
{
|
| 165 |
print "no (mysqladmin not found)\n"; |
| 166 |
} |
| 167 |
exit $return; |
| 168 |
} |
| 169 |
|
| 170 |
sub getDBList {
|
| 171 |
my @dbs; |
| 172 |
foreach my $f (glob("/var/lib/mysql/*")) {
|
| 173 |
if (-d $f) {
|
| 174 |
$f =~ s/\@002d/-/g; |
| 175 |
$f =~ s!.*/!!; |
| 176 |
@dbs[$#dbs+1]=$f }; |
| 177 |
} |
| 178 |
return @dbs; |
| 179 |
} |
