2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
25 # Get output immediately. It won't hurt performance.
33 push(@options,
"password=s", \$pw);
35 my $host =
"localhost";
36 push(@options,
"host=s", \$host);
38 my $user =
"localuser";
39 push(@options,
"user=s", \$user);
42 push(@options,
"port=s", \$port);
44 die
"Couldn't parse options" if !GetOptions(@options);
47 open(CMD, $cmd) or die
"Couldn't $cmd: $!\n";
53 push (@databases, $_);
59 my %colmap = (
'Data_length' => 6,
70 foreach my $db (@databases) {
72 $cmd =
mysql_cmd(
"use $db; show table status");
74 open(CMD, $cmd) or die
"Couldn't $cmd: $!\n";
77 if (defined($header)) {
79 my @head = split(
"\t", $header);
81 foreach my $col (keys %colmap) {
82 die
"$db: Expected '$col', found '" . $head[$colmap{$col}] .
"'"
83 if $head[$colmap{$col}] ne $col;
87 my @data = split(
"\t");
88 print STDERR
"== $db\n" if ($debug);
89 my ($data_length, $index_length) = @data[6,8];
90 my ($engine, $comment) = @data[1,17];
91 my $tbl_name = $data[0];
92 $engine_map{$engine}++;
93 $index_length = 0
if (!defined($index_length) || $index_length eq
'NULL');
94 $data_length = 0
if (!defined($data_length) || $data_length eq
'NULL');
95 $engine =
'Unknown' if (!defined($engine) || $engine eq
'NULL');
96 $db =
'Unknown' if (!defined($db) || $db eq
'NULL');
97 $size{$db}{$engine} += $data_length + $index_length;
98 $total_size{$db} += $data_length + $index_length;
99 my $length = $data_length + $index_length;
100 $top_tables{$length}{$tbl_name}{$db} = 1;
102 if ( $comment =~ /InnoDB free: (\d+) kB/ ) {
103 warn
"Found two different inno DB free sized - $db - $tbl_name.\n"
104 if defined($inno_db_free) && $inno_db_free != $1;
113 print
"NOTE: All numbers are in megabytes (M).\n";
114 printf(
"Inno DB free: %.1f\n", $inno_db_free / 1024)
115 if defined($inno_db_free);
117 printf("%-40s ", "database");
118 foreach my $engine (sort keys(%engine_map)) {
119 printf
"%7s ", $engine;
121 printf
"%8s",
"total";
124 foreach my $db (sort {$total_size{$b} <=> $total_size{$a}} keys %total_size) {
125 printf(
"%-40s ", $db);
126 foreach my $engine (sort keys(%engine_map)) {
127 my $size= $size{$db}{$engine};
128 $size = 0
if !defined($size);
129 printf(
"%7.1f ", $size / 1024 / 1024);
131 printf(
"%8.1f\n", $total_size{$db} / 1024 / 1024);
135 map {$total_bytes+=$_} values %total_size;
136 print
"================================\n";
137 printf(
"TOTAL SPACE USED %7.1f\n", $total_bytes / 1024 / 1024);
138 print
"================================\n";
141 print
"==================\n";
142 print
"Top tables by size\n";
143 print
"==================\n";
144 foreach my $size (sort {$b<=>$a} keys %top_tables) {
145 last
if ($count > 5);
146 my @tbl_names = keys %{$top_tables{$size}};
147 my @dbs = keys %{$top_tables{$size}{$tbl_names[0]}};
148 printf(
"%-40s ", $dbs[0]);
149 printf(
"%-40s ", $tbl_names[0]);
150 printf(
"%7.1f ", $size / 1024 / 1024);
156 my $mysql_cmd = shift;
158 my $pw_args = $pw ?
"-p$pw" :
'';
159 return "mysql -uroot -h$host -u$user $pw_args -P$port -e '$mysql_cmd'|";