#!/usr/bin/perl # vim: cink=0{,0},0),!^F,o,O,e autoindent ts=4 sw=4 cindent expandtab use strict; # I'm dyslexic so it's always a good idea to include this asap my $DEBUG = 1; # ============================================================================= # # $Id: mysqlrepchk,v 1.1 2005/02/16 13:09:01 xaphod Exp $ # # A quick and dirty script to check the replicaiton status of MySQL # # license: http://creativecommons.org/licenses/by-nc/2.0/ # # ============================================================================= ## SETTINGS my $master = { data_source => 'dbi:mysql:host=alpha;database=test', username => 'checker', password => 'some_password', }; my $slaves = { beta => { data_source => 'dbi:mysql:host=beta;database=test', username => 'checker', password => 'some_password', }, }; my %SQL; $SQL{INSERT} = q{ INSERT INTO _test_ (timestamp) VALUES ( ? ) }; $SQL{SELECT} = q{ SELECT COUNT(timestamp) FROM _test_ WHERE timestamp = ? }; $SQL{DELETE} = q{ DELETE FROM _test_ WHERE timestamp = ? }; # ============================================================================= use DBI; my @fail = (); my $count; # Open the master DB my $master_dbh = DBI->connect($master->{data_source}, $master->{username}, $master->{password}); # What's the time Mr Wolf? my $time = time; # Insert $count = $master_dbh->do($SQL{INSERT}, undef, $time); print STDERR "$count records added to master\n" if $DEBUG; # Check each slave in turn for my $slave (sort keys %{$slaves}) { my $cur = $slaves->{$slave}; print STDERR "checking $slave" if $DEBUG; my $slave_dbh = DBI->connect($cur->{data_source}, $cur->{username}, $cur->{password}); my $sth = $slave_dbh->prepare($SQL{SELECT}); $sth->execute($time); if (($sth->fetchrow_array)[0] == 1) { print STDERR ". Found! Replication OK." if $DEBUG; } else { print STDERR ". Not found! Replication if FUBAR." if $DEBUG; push @fail, $slave; } $sth->finish; $slave_dbh->disconnect; print STDERR "\n" if $DEBUG; } # Delete $count = $master_dbh->do($SQL{DELETE}, undef, $time); print STDERR "$count records deleted from master\n" if $DEBUG; # Disconnect from the DB $master_dbh->disconnect; # report if (scalar(@fail) > 0) { $count = scalar(@fail); print "REPLICATION FAILURE\n\n"; print "Replication has failed on $count hosts:\n"; foreach my $host (@fail) { print " $host\n"; } } __END__