ensembl-hive  2.7.0
db-space.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 
17 
18 use strict;
19 use warnings;
20 
21 
22 use Getopt::Long;
23 my @options;
24 
25 # Get output immediately. It won't hurt performance.
26 use FileHandle;
27 autoflush STDERR;
28 autoflush STDOUT;
29 
30 my $debug = 0;
31 
32 my $pw;
33 push(@options, "password=s", \$pw);
34 
35 my $host = "localhost";
36 push(@options, "host=s", \$host);
37 
38 my $user = "localuser";
39 push(@options, "user=s", \$user);
40 
41 my $port = "3306";
42 push(@options, "port=s", \$port);
43 
44 die "Couldn't parse options" if !GetOptions(@options);
45 
46 my $cmd = mysql_cmd("show databases");
47 open(CMD, $cmd) or die "Couldn't $cmd: $!\n";
48 my @databases;
49 my $header = <CMD>;
50 while ( <CMD> ) {
51  s/[\r\n]$//g;
52  #print "$_\n";
53  push (@databases, $_);
54 }
55 close(CMD);
56 
57 #print "@databases";
58 
59 my %colmap = ( 'Data_length' => 6,
60  'Index_length' => 8,
61  'Engine' => 1,
62  'Comment' => 17 );
63 
64 my %size;
65 my %total_size;
66 my %engine_map;
67 my %top_tables;
68 
69 my $inno_db_free;
70 foreach my $db (@databases) {
71  print STDERR ".";
72  $cmd = mysql_cmd("use $db; show table status");
73 
74  open(CMD, $cmd) or die "Couldn't $cmd: $!\n";
75  my $header = <CMD>;
76  my $total_size = 0;
77  if (defined($header)) {
78  $header =~ s/[\r\n]$//g;
79  my @head = split("\t", $header);
80 
81  foreach my $col (keys %colmap) {
82  die "$db: Expected '$col', found '" . $head[$colmap{$col}] . "'"
83  if $head[$colmap{$col}] ne $col;
84  }
85 
86  while (<CMD>) {
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;
101 
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;
105  $inno_db_free = $1;
106  }
107  }
108  close(CMD);
109  }
110 }
111 print STDERR "\n";
112 
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);
116 
117 printf("%-40s ", "database");
118 foreach my $engine (sort keys(%engine_map)) {
119  printf "%7s ", $engine;
120 }
121 printf "%8s", "total";
122 print "\n";
123 
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);
130  }
131  printf("%8.1f\n", $total_size{$db} / 1024 / 1024);
132 }
133 
134 my $total_bytes;
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";
139 
140 my $count++;
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);
151  print("\n");
152  $count++;
153 }
154 
155 sub mysql_cmd {
156  my $mysql_cmd = shift;
157 
158  my $pw_args = $pw ? "-p$pw" : '';
159  return "mysql -uroot -h$host -u$user $pw_args -P$port -e '$mysql_cmd'|";
160 }
161 
map
public map()
mysql_cmd
public mysql_cmd()