Ruslan Bondar Site: RuslanBondar/projects/scripts/mysqlslave ...

Ruslan Bondar | Page Index | Recently Commented | Users | Registration | Вход:  Пароль:  

Mysql Replication Slave Cheacker


Attension This Script modify /etc/hosts
Remember to make backup of /etc/hosts before run
Use it on you own risk


1. All slave must be marked by add at the end of slave line in /etc/hosts ##SLAVE
2. Move master host after slave hosts in /etc/hosts
3. Add slave host to master host line in /etc/hosts


Example


1.2.3.4 slave ##SLAVE
1.2.3.5 master slave


ping slave get 1.2.3.4
if slave .4 down script will add coment line lik this:


#1.2.3.4 slave ##SLAVE
1.2.3.5 master slave


and ping slave get 1.2.3.5


#!/usr/bin/perl -w
use strict;
use DBI;

my $user = 'root';
my $pass = '*****';
my $error = 'none';

sub check_slave {
    my $dbh = DBI->connect("dbi:mysql:host=".shift, $user, $pass) or return 0;
    my $i = $dbh->selectrow_hashref("SHOW SLAVE STATUS");
    $error = 'Slave SQL not running';
    return 0 if (exists $i->{Slave_SQL_Running} and $i->{Slave_SQL_Running} eq 'No');
    $error = 'Slave IO not running';
    return 0 if (exists $i->{Slave_IO_Running} and $i->{Slave_IO_Running} eq 'No');
    $error = 'Unknown error';
    if (exists $i->{Seconds_Behind_Master} and defined($i->{Seconds_Behind_Master})){
            return 1 if ($i->{Seconds_Behind_Master}<10);
            $error = 'Slave to far from master['.$i->{Seconds_Behind_Master}.']';
    }
    return 0;
}

sub trim {
    my $s = shift;
    for ($s) { s/^\s+//; s/\s+$//; }
    return $s;
}

open HOSTS_IN, "/etc/hosts";
open HOSTS_OUT, ">/etc/hosts_new";
while (){
    if (/#?([0-9.]+)[\t ]+([a-zA-Z0-9].+) *##SLAVE.*/){
        my $ip    = trim($1);
        my $hosts = trim($2);
        if (!check_slave($ip)){
            print HOSTS_OUT "#";
            print "$ip\t$hosts\t$error\n";
        }
        print HOSTS_OUT "$ip\t\t$hosts\t##SLAVE\n";
    } else {
        print HOSTS_OUT $_;
    }
}
close HOSTS_OUT;
close HOSTS_IN;

`mv /etc/hosts_new /etc/hostss`


When you ready change last line to mv to /etc/hosts

Комментариев нет. [Показать комментарии/форму]