dp.cx blog

Posted on

Filed under perl, mysql, monitoring, and replication

A commenter recently asked if I could publish the script that I wrote to handle monitoring master-master replication. It's been almost two years since I wrote the code, and I no longer have access to the original, but I've reproduced the general code here.

 
#!/usr/bin/perl
 
use warnings;
use strict;
 
use Date::Manip;
use DBI;
 
my $server_id = 2; # value of the "other" server
my $dbh = DBI::connect("dbd:mysql:replication_tracking:localhost:3306", "replication_user", "replication_password");
die "Can't connect to localhost" unless $dbh;
 
my $sth = $dbh->prepare("SELECT ts FROM heartbeat WHERE server_id = $server_id");
$sth->execute();
my ($ts) = $sth->fetchrow_array();
$sth->finish();
$dbh->finish();
 
$ts = ParseDateTime($ts);
my $current_time = ParseDateTime("now");
 
my $delta = DateCalc($current_time, $ts);
my $diff = abs(Delta_Format($delta, "%st"));
 
if ($diff >= 2) {
    # msg the admin
}