Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mysql / mysql_size_all @ e641c802

Historique | Voir | Annoter | Télécharger (3,56 ko)

1
#!/usr/bin/perl
2
#
3
# Copyright (C) 2007 - Rodolphe Quiédeville <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 Quiédeville
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
use Munin::Plugin;
43

    
44

    
45
my $COMMAND;
46
my $MYSQLADMIN = $ENV{mysqladmin} || "mysql";
47

    
48
my $arg = shift();
49

    
50
if ($arg eq 'config') {
51
    print_config();
52
    exit();
53
} elsif ($arg eq 'autoconf') {
54
    unless (test_service() ) {
55
        print "yes\n";
56
    } else {
57
        print "no\n";
58
    }
59
    exit;
60
}
61

    
62
sub getDBList;
63
foreach my $db (getDBList()) {
64

    
65
  my (@infos,$info,$i_data,$i_index);
66

    
67
  $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | head -n 1";
68

    
69
  open(SERVICE, "$COMMAND |")
70
    or die("Could not execute '$COMMAND': $!");
71

    
72
  while (<SERVICE>) {
73
      (@infos)  = split;
74
  }
75
  close(SERVICE);
76

    
77
  my $i = 0;
78
  foreach $info (@infos) {
79
      $i++;
80
      if ($info eq 'Data_length') {
81
    $i_data = $i;
82
    next;
83
      }
84
      if ($info eq 'Index_length') {
85
    $i_index = $i;
86
    last;
87
      }
88
  }
89
  my $total_size = 0;
90
  if ($i_data>0 && $i_index>0) {
91
    $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | cut -f $i_data,$i_index | grep -v leng";
92

    
93
    open(SERVICE, "$COMMAND |")
94
      or die("Could not execute '$COMMAND': $!");
95

    
96
    while (<SERVICE>) {
97
        (m/(\d+).*?(\d+(?:\.\d+)?)/);
98
        $total_size += $1 + $2;
99
    }
100
    close(SERVICE);
101

    
102
  }
103
  my $fieldname = clean_fieldname($db);
104
  print("$fieldname.value $total_size\n");
105
}
106

    
107

    
108
sub print_config {
109

    
110
    my $num = 0;
111

    
112
    my @dbs = getDBList;
113

    
114
    print("graph_title MySQL databases size\n");
115
    print ('graph_args --base 1024 -l 0
116
graph_vlabel bytes
117
graph_category db
118
graph_info Plugin available at <a href="http://rodolphe.quiedeville.org/hack/munin/">http://rodolphe.quiedeville.org/hack/munin/</a>
119
');
120

    
121
    for my $db (@dbs) {
122
        my $fieldname = clean_fieldname($db);
123
        print("$fieldname.label $db\n",
124
              "$fieldname.min 0\n",
125
              "$fieldname.type GAUGE\n",
126
              "$fieldname.draw AREASTACK\n",
127
             );
128
        $num++;
129
    }
130
}
131

    
132

    
133
sub test_service {
134

    
135
    my $return = 1;
136

    
137
    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
138
    if ($? == 0)
139
    {
140
	system ("$COMMAND >/dev/null 2>/dev/null");
141
	if ($? == 0)
142
	{
143
	    print "yes\n";
144
	    $return = 0;
145
	}
146
	else
147
	{
148
	    print "no (could not connect to mysql)\n";
149
	}
150
    }
151
    else
152
    {
153
	print "no (mysqladmin not found)\n";
154
    }
155
    exit $return;
156
}
157

    
158
sub getDBList {
159
  my @dbs;
160
  foreach my $f (glob("/var/lib/mysql/*")) {
161
    if (-d $f) {
162
      $f =~ s/\@002d/-/g;
163
      $f =~ s/\@002e/./g;
164
      $f =~ s!.*/!!;
165
      @dbs[$#dbs+1]=$f };
166
  }
167
  return @dbs;
168
}