ensembl-hive  2.7.0
search_dbs.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 # this script will lokk through all listed databases for
19 # database names which match given regexp.
20 # This version should be ok if one or more of the given database
21 # servers is not available.
22 
23 
24 use strict;
25 use warnings;
26 
27 use DBI;
28 
29 # seconds before we give up on database
30 my $timeout = 5;
31 
32 my @all_locators = qw {
33  genebuild1:3306:ensro::
34  genebuild2:3306:ensro::
35  genebuild3:3306:ensro::
36  genebuild4:3306:ensro::
37  genebuild5:3306:ensro::
38  genebuild6:3306:ensro::
39  genebuild6:3307:ensro::
40 
41  compara1:3306:ensro::
42  compara2:3306:ensro::
43  compara3:3306:ensro::
44 
45  mart1:3306:ensro::
46  mart2:3306:ensro::
47 
48  ens-genomics1:3306:ensro::
49  ens-genomics2:3306:ensro::
50 
51  ens-research:3306:ensro::
52  ens-research:3309:ensro::
53 
54  ensdb-1-11:3319:ensro::
55  ensdb-1-11:3317:ensro::
56 
57  ens-staging:3306:ensro::
58  ens-livemirror:3306:ensro::
59 
60  };
61 
62 
63 my $pattern=shift;
64 my $size = 0;
65 my %sizes;
66 
67 if( @ARGV ) {
68  $size = 1;
69 }
70 
71 if( !$pattern ) {
72  printf( "You need to supply a regexp as argument.\n" );
73  printf( "Use -list if you want to see which databases are configured.\n" );
74  printf( "If you have any parameters after the regexp, the program will print\n" );
75  printf( " database sizes as well.\n" );
76  printf( "\nExample: search_dbs.pl \"^stabenau\" -sizes | sort -knr3\n" );
77  exit;
78 }
79 
80 my $list = 0;
81 
82 if( $pattern eq "-list" ) {
83  $list = 1;
84 }
85 
86 
87 for my $loc ( @all_locators ) {
88  my @elems = split( ":", $loc );
89  my @dbnames = ();
90  %sizes = ();
91  my $dsn = sprintf( "DBI:mysql:host=%s;port=%s", $elems[0], $elems[1] );
92 
93  $SIG{ALRM} = sub{die("timeout");};
94 
95  eval {
96  alarm $timeout;
97  my $db = DBI->connect( $dsn, $elems[2], $elems[3], { RaiseError => 1 } );
98  my $res = $db->selectall_arrayref( "show databases" );
99  @dbnames = map { $_->[0] } @$res;
100  $db->disconnect();
101  alarm 0;
102  };
103 
104  if( !@dbnames ) {
105  print STDERR "$loc NOT OK\n";
106  } else {
107  if( $size ) {
108  for my $dbname ( @dbnames ) {
109  if( $dbname =~ /$pattern/ ) {
110  eval {
111  alarm $timeout;
112  my $db = DBI->connect( $dsn, $elems[2], $elems[3], { RaiseError => 1, PrintError=> 0 } );
113  $db->do( "use $dbname" );
114  my $t_status = $db->selectall_arrayref( "show table status" );
115  my $size = 0;
116  map { $size += $_->[6]; $size += $_->[8] } @$t_status;
117  print "$loc $dbname $size\n";
118  $db->disconnect();
119  alarm 0;
120  };
121  if( $@ ) {
122  print( "Problem on $loc $dbname.\n ", $@ );
123  }
124  }
125  }
126  } else {
127  if( $list ) {
128  print STDERR "$loc ok\n";
129  } else {
130  for my $dbname ( @dbnames ) {
131  if( $dbname =~ /$pattern/ ) {
132  print "$loc $dbname\n";
133  }
134  }
135  }
136  }
137  }
138 }
map
public map()