A (ugly) script for monitoring mysql replication
Mysql replication (if you can name “replication” a asynchronous stuff) it’s a nice thing…
But sometimeit have a problem ![]()
This is a simpe ugly script to monitoring slave status and sending a mail if slave stop in some way
I put this shi^H^H^script in a */10 cron.d file (but work also from command line)
—————————————————————————————————————
#!/usr/bin/perl -w
## On a slave server, check to see that the slave hasn’t stopped.
use strict;
use DBIx::DWIW;
use Mail::Mailer;
my $from_address = “SOMEALERTADRESS@YOUR.SITE”;
my $to_address = “ALERTREEPICIENTADDRESS@FOO.SITE”;
my $body;
my $subject;
open(LOG, “grep mysql /var/log/syslog|tail|”) || die “can’t fork tail: $!”;
while ( ) {
$body .= $_;
}
my $conn = DBIx::DWIW->Connect(
DB => “mysql”,
User => “ADMINUSER”,
Pass => “ADMINPASSWORD”,
Host => “localhost”,
) or exit;
my $info = $conn->Hash(”SHOW SLAVE STATUS”);
if (exists $info->{Slave_SQL_Running} and $info->{Slave_SQL_Running} and $info->{Slave_IO_Running} eq ‘Yes’)
{
# Se funziona esco dallo script
# If no prob exit from script
exit 0;
}
# Creo il body della mail
# Write mail body from logs
open(LOG, “grep mysql /var/log/syslog|tail|”) || die “can’t fork tail: $!”;
while ( ) {
$body .= $_;
}
## Testo i vari tipi di problema
## Check the prob and send a mail whit subject==problem type
## Mando una mail contentente il log e come soggetto l’errore
#
#
if (exists $info->{Slave_SQL_Running} and $info->{Slave_SQL_Running} eq ‘No’)
{
warn “slave SQL thread has stopped\n”;
$subject = “slave SQL thread has stopped\n”;
}
elsif (exists $info->{Slave_IO_Running} and $info->{Slave_IO_Running} eq ‘No’)
{
warn “slave IO thread has stopped\n”;
$subject = “slave IO thread has stopped\n”;
}
elsif (exists $info->{Slave_Running} and $info->{Slave_Running} eq ‘No’)
{
warn “slave has stopped\n”;
$subject = “slave has stopped\n”;}
my $mailer = Mail::Mailer->new();
$mailer->open({ From => $from_address,
To => $to_address,
Subject => $subject,
})
or die “Can’t open: $!\n”;
print $mailer $body;
$mailer->close();
—————————————————————————————————-
NB Sorry for my poor english
NBB Sorry for my poor perl