#!/usr/bin/perl ############################################################### # Dayana MySQL Check v1.0 # Massive MySQL Databases Check Utility # Copyright(c) 1998-2006 Dayana Networks Ltd. # All rights reserved. ############################################################### ## Configuration # A temporary folder for doing some read and write, /tmp is just fine. $TempFolder = "/tmp"; # Type of check according to MySQL # Possible values are: # QUICK [Do not scan the rows to check for incorrect links.] # FAST [Check only tables that have not been closed properly.] # CHANGED [Check only tables that have been changed since the last # check or that have not been closed properly.] # MEDIUM [Scan rows to verify that deleted links are valid. This # also calculates a key checksum for the rows and verifies # this with a calculated checksum for the keys.] # EXTENDED [Do a full key lookup for all keys for each row. This # ensures that the table is 100% consistent, but takes a # long time.] $CheckType = "QUICK"; ### Do not edit below this line ### $sql_rep_rn = int(rand(8999)+1000); $rep_name = $TempFolder."/sql_rep_".$sql_rep_rn; setpriority(0, 0, 19); print "Dayana MySQL Check\n"; print "Dayana MySQL Check v1.0\n"; print "Massive MySQL Databases Check Utility\n"; print "Copyright(c) 1998-2006 Dayana Networks Ltd.\n\n"; @RES = `mysql -B -s -e "SHOW DATABASES;"`; my @DATABASES; foreach my $res (@RES) { $res =~ s/\n//; $res =~ s/\r//; push(@DATABASES,$res); } foreach my $db (@DATABASES) { print "\nChecking tables of $db:\n"; CheckDatabase($db); } print "\n\n"; sub CreateSqlRep { local ($STM,$FN) = @_; open(SQL,">$FN"); print SQL $STM; close(SQL); } sub RemoveSqlRep { local ($FN) = @_; unlink($FN); } sub CheckDatabase { local($DB) = @_; my @DBRES = `mysql -B -s -e "SHOW TABLES FROM $DB;"`; my @TABLES; foreach my $dbres (@DBRES) { $dbres =~ s/\n//; $dbres =~ s/\r//; push(@TABLES,$dbres); } my $db_error_count = 0; my $db_count = 0; foreach my $table (@TABLES) { $db_count++; $db_error_count += CheckTable($DB,$table); } if($db_error_count==0) { print " $db_count tables checked. All OK.\n"; } else { print " $db_count tables checked. $db_error_count table(s) having problems.\n"; } } sub CheckTable { local ($db,$table) = @_; my $dbin = chr(96).$db.chr(96); my $tbin = chr(96).$table.chr(96); CreateSqlRep("USE $dbin; CHECK TABLE $tbin $CheckType;",$rep_name); my $chres = qx{mysql -B -s < $rep_name}; RemoveSqlRep($rep_name); $chres =~ s/\n//; $chres =~ s/\r//; if($chres =~ /Table is already up to date/i || $chres =~ /ok/i) { return(0); } else { print "==> Table $table has some problems:\n $chres\n"; return(1); } }