创建博客 登录  
 关注
   显示下一条  |  关闭

Maple

×××××

 
 
 

日志

 
 

mysql replication出错自动检测的脚本  

2009-02-13 13:36:56|  分类: MySQL Replicatio |  标签: |字号 订阅

修改版:

#!/usr/local/bin/perl -w
#
# fix mysql replication if it encounters a problem
# Useage:perl fix_repl.pl ip1 ip2 ip3

$|=1;    # unbuffer stdout
use strict;
use DBIx::DWIW;
use Mail::Mailer;
my $ip;
foreach $ip (@ARGV) {
&fixdb($ip);
}

exit;

sub fixdb()
{
my $host = shift || 'localhost';
my $conn = DBIx::DWIW->;Connect(
DB =>; "test",
User =>; "sqlmon",
Pass =>; '!@#$%^&*()',
       Host =>; $host,
Port   =>; 3306) or die "Couldn't connect to database!";

print "checking $host ... \n";
my $info = $conn->;Hash("SHOW SLAVE STATUS") or die $@;
my @version = $conn->;Array("SHOW VARIABLES LIKE 'Version'");
my $fix_cmd;
my $start_cmd;
# slave not start
if ($info->;{Slave_IO_Running} eq 'No' and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START";
}
# 4.0.0 - 4.0.2
elsif ($version[1] =~ /^4\.0\.[012]/ and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START SQL_THREAD";
}
# 4.0.3 - 4.0.xx, 4.1.xx.   Don't know what 5.0 will be like.
elsif ($version[1] =~ /^4\.[01]\./ and $info->;{Slave_SQL_Running} eq 'No')
{
$fix_cmd = "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1";
$start_cmd = "SLAVE START SQL_THREAD";
}
# things are okay or unknown version?
else
{
print "GOOD\n";
}
if ($info->;{Slave_IO_Running} eq 'No'   or   $info->;{Slave_SQL_Running} eq 'No' ){
&mailtoadmin($info,$host);
print "FIXING ... ";
$conn->;Execute($fix_cmd);
$conn->;Execute($start_cmd);
print "DONE\n";
}

}

sub mailtoadmin(){
       my $body=shift;
my $host = shift;
       my @MailTo=("xxxx\@yyyy.com");
       my $subject = "Mysql Self fix replication on $host ";
my $mailer = Mail::Mailer->;new() or die $@;
$mailer->;open({ From =>; $host,
To    =>; \@MailTo,
Subject =>; $subject,
})or die "Can't open: $!\n";
# while ( my ($key, $value) = each(%$body) ) {
#        print $mailer "$key =>; $value\n";
# }
print $mailer <<MAILCONTENT;

*************************** show slave status***************************

Master_Host =>;   $body->;{'Master_Host'}
Master_User =>;   $body->;{'Master_User'}
Master_Port =>;   $body->;{'Master_Port'}
Connect_retry =>;   $body->;{'Connect_retry'}
Master_Log_File =>;   $body->;{'Master_Log_File'}
Read_Master_Log_Pos =>;$body->;{'Read_Master_Log_Pos'}
Relay_Log_File =>;   $body->;{'Relay_Log_File'}
Relay_Log_Pos =>;   $body->;{'Relay_Log_Pos'}
Relay_Master_Log_File =>;$body->;{'Relay_Master_Log_File'}
Slave_IO_Running =>;$body->;{'Slave_IO_Running'}
Slave_SQL_Running =>;$body->;{'Slave_SQL_Running'}
Replicate_do_db =>; $body->;{'Replicate_do_db'}
Replicate_ignore_db =>;$body->;{'Replicate_ignore_db'}
Last_errno =>; $body->;{'Last_errno'}
Last_error =>; $body->;{'Last_error'}
Skip_counter =>; $body->;{'Skip_counter'}
Exec_master_log_pos =>; $body->;{'Exec_master_log_pos'}
Relay_log_space =>; $body->;{'Relay_log_space'}
MAILCONTENT
$mailer->;close();
}

 

原版:

#!/usr/local/bin/perl -w
#
# fix mysql replication if it encounters a problem
# Useage:perl fix_repl.pl ip1 ip2 ip3

        $|=1;      # unbuffer stdout
        use strict;
        use DBIx::DWIW;
        use Mail::Mailer;
        my $ip;
foreach $ip (@ARGV) {
        &fixdb($ip);
}

exit;

sub fixdb()
{
        my $host = shift || 'localhost';
        my $conn = DBIx::DWIW->;Connect(
                DB   =>; "test",
                User =>; "sqlmon",
                Pass =>; '!@#$%^&*()',
            Host =>; $host,
                Port  =>; 3306) or die "Couldn't connect to database!";

        print "checking $host ... \n";
        my $info = $conn->;Hash("SHOW SLAVE STATUS") or die $@;
        my @version = $conn->;Array("SHOW VARIABLES LIKE 'Version'");
        my $fix_cmd;
        my $start_cmd;
# slave not start
        if ($info->;{Slave_IO_Running} eq 'No' and $info->;{Slave_SQL_Running} eq 'No')
        {
                $fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
                $start_cmd = "SLAVE START";
        }
# 4.0.0 - 4.0.2
        elsif ($version[1] =~ /^4\.0\.[012]/ and $info->;{Slave_SQL_Running} eq 'No')
        {
                $fix_cmd = "SET SQL_SLAVE_SKIP_COUNTER = 1";
                $start_cmd = "SLAVE START SQL_THREAD";
        }
# 4.0.3 - 4.0.xx, 4.1.xx.  Don't know what 5.0 will be like.
        elsif ($version[1] =~ /^4\.[01]\./ and $info->;{Slave_SQL_Running} eq 'No')
        {
                $fix_cmd = "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1";
                $start_cmd = "SLAVE START SQL_THREAD";
        }
# things are okay or unknown version?
        else
        {
                        print "GOOD\n";
        }
        if ($info->;{Slave_IO_Running} eq 'No'  or  $info->;{Slave_SQL_Running} eq 'No' ){
        &mailtoadmin($info,$host);
        print "FIXING ... ";
        $conn->;Execute($fix_cmd);
        $conn->;Execute($start_cmd);
        print "DONE\n";
        }

}

sub mailtoadmin(){
        my $body=shift;
                my $host = shift;
        my @MailTo=("xxxx\@yyyy.com");
        my $subject = "Mysql Self fix replication on $host ";
                my $mailer = Mail::Mailer->;new() or die $@;
                $mailer->;open({ From    =>; $host,
                To      =>; \@MailTo,
                Subject =>; $subject,
                })or die "Can't open: $!\n";
#    while ( my ($key, $value) = each(%$body) ) {
#        print $mailer "$key =>; $value\n";
#    }
        print $mailer <<MAILCONTENT;

        *************************** show slave status***************************

        Master_Host                        =>;  $body->;{'Master_Host'}
        Master_User                        =>;  $body->;{'Master_User'}
        Master_Port                        =>;  $body->;{'Master_Port'}
        Connect_retry                =>;  $body->;{'Connect_retry'}
        Master_Log_File        =>;  $body->;{'Master_Log_File'}
        Read_Master_Log_Pos =>;$body->;{'Read_Master_Log_Pos'}
        Relay_Log_File                =>;  $body->;{'Relay_Log_File'}
        Relay_Log_Pos                =>;  $body->;{'Relay_Log_Pos'}
        Relay_Master_Log_File =>;$body->;{'Relay_Master_Log_File'}
        Slave_IO_Running =>;$body->;{'Slave_IO_Running'}
        Slave_SQL_Running =>;$body->;{'Slave_SQL_Running'}
        Replicate_do_db        =>;   $body->;{'Replicate_do_db'}
        Replicate_ignore_db =>;$body->;{'Replicate_ignore_db'}
        Last_errno                                =>;    $body->;{'Last_errno'}
        Last_error                                =>;    $body->;{'Last_error'}
        Skip_counter                        =>;    $body->;{'Skip_counter'}
        Exec_master_log_pos =>; $body->;{'Exec_master_log_pos'}
        Relay_log_space        =>;    $body->;{'Relay_log_space'}
MAILCONTENT
        $mailer->;close();
}

  评论这张
转发至微博
转发至微博
0   分享到:        
阅读(287)| 评论(0)| 引用 (0) |举报
<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--相关文章--> <#--历史上的今天--> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2012