summaryrefslogtreecommitdiff
path: root/files/munin/mysql_size_all
blob: f5954adca8c9df51d2c8c34e3079b8315f414362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/perl
#
# Copyright (C) 2007 - Rodolphe Quiedeville <rodolphe@quiedeville.org>
# Copyright (C) 2003-2004 - Andreas Buer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# $Log$
# Revision 1.1  2007/01/17 10:41:01  rodo
# Change incorrect family
#
# Revision 1.0  2007/01/16 15:57:01  rodo
# Created by Rodolphe Quiedeville
#
# Parameters:
#
#   config
#   autoconf
#
# Configuration variables
#
#   mysqlopts     - Options to pass to mysql
#   mysqladmin    - Override location of mysqladmin
#
#%# family=manual
#%# capabilities=autoconf

use strict;

# unless ($0 =~ /mysql_size(?:_([^_]+)|)_(.+)\s*$/)
# {
#     die "Could not parse name $0.\n";
# }
# my $db = $2;

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

my %WANTED = ( "Index"  => "index", 
               "Datas" => "datas",
             );

my $arg = shift();

if ($arg eq 'config') {
    print_config();
    exit();
} elsif ($arg eq 'autoconf') {
    unless (test_service() ) {
        print "yes\n";
    } else {
        print "no\n";
    }
    exit;
}

sub getDBList;
foreach my $db (getDBList()) {

  my $datas = 0;
  my $indexes = 0;
  my (@infos,$info,$i_data,$i_index);

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

  open(SERVICE, "$COMMAND |")
    or die("Coult not execute '$COMMAND': $!");

  while (<SERVICE>) {
      (@infos)  = split;
  }
  close(SERVICE);

  my $i = 0;
  foreach $info (@infos) {
      $i++;
      if ($info eq 'Data_length') {
    $i_data = $i;
    next;
      }
      if ($info eq 'Index_length') {
    $i_index = $i;
    last;
      }
  }
  my $total_size = 0;
  if ($i_data>0 && $i_index>0) {
    $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} $db -e 'show table status;' | cut -f $i_data,$i_index | grep -v leng";

    open(SERVICE, "$COMMAND |")
      or die("Coult not execute '$COMMAND': $!");

    while (<SERVICE>) {
        (m/(\d+).*?(\d+(?:\.\d+)?)/);
        $datas += $1;
        $indexes += $2;
    }
    close(SERVICE);

    $total_size = $datas+$indexes;
  }
  print("$db.value $total_size\n");
#   print("datas.value $datas\n");
#   print("index.value $indexes\n");
}


sub print_config {

    my $num = 0;

    my @dbs = getDBList;

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

    for my $db (@dbs) {
        my $title = "$db";
        print("$title.label ${title}\n",
              "$title.min 0\n",
              "$title.type GAUGE\n",
              "$title.draw ", ($num) ? "STACK" : "AREA" ,  "\n",
             );
        $num++;
    }
}


sub test_service {

    my $return = 1;

    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
    if ($? == 0)
    {
	system ("$COMMAND >/dev/null 2>/dev/null");
	if ($? == 0)
	{
	    print "yes\n";
	    $return = 0;
	}
	else
	{
	    print "no (could not connect to mysql)\n";
	}
    }
    else
    {
	print "no (mysqladmin not found)\n";
    }
    exit $return;
}

sub getDBList {
  my @dbs;
  foreach my $f (glob("/var/lib/mysql/*")) {
    if (-d $f) { 
      $f =~ s!.*/!!;
      @dbs[$#dbs+1]=$f };
  }
  return @dbs;
}