- #!/usr/bin/perl -w
-
# Use mandatory external modules
-
use strict;
-
use DBI;
-
use Net::SMTP_auth;
-
use Time::HiRes;
-
use POSIX "strftime";
-
-
my $max_behind_m = 120;
-
my $check_log = '/var/log/mysql_check_log';
-
-
my $mailhost = 'smtp.163.com';
-
my $mailfrom = 'xxxxxx@163.com';
-
my @mailto = '159xxxxxx@139.com';
-
my $subject = "mysql_repl_error::";
-
my $text;
-
my $mailuser = 'xxxxx';
-
my $mailpasswd = 'xxxxxx';
-
-
# open log--file
-
open FH, ">> $check_log" or die $!;
-
-
my @aa = (
-
[ '192.168.1.203', '3306', 'myrepl', '123456' ],
-
[ '192.168.1.240', '3306', 'myrepl', '123456' ],
-
);
-
-
foreach my $a (@aa) {
-
&account(@$a);
-
}
-
-
# connect to servers (this)
-
sub account {
-
my ( $host, $port, $user, $pass ) = @_;
-
my $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
-
print FH "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!"
-
if ( !$this_dbh );
-
my $slave_status = &MysqlQuery( $this_dbh, "SHOW SLAVE STATUS" );
-
print FH "ERROR: SQL Query Error: " . $this_dbh->errstr
-
unless ($slave_status);
-
-
my $Slave_IO = $slave_status->{Slave_IO_Running};
-
my $Slave_SQL = $slave_status->{Slave_SQL_Running};
-
my $Seconds_Behind_Master = $slave_status->{Seconds_Behind_Master};
-
-
print "IO:\t\t $Slave_IO\n";
-
print "SQL:\t\t $Slave_SQL\n";
-
print "Behind_Master:\t $Seconds_Behind_Master\n";
-
-
# Send to Mail
-
if ( ( $Slave_IO eq 'Yes' )
-
and ( $Slave_SQL eq 'Yes' )
-
and ( $Seconds_Behind_Master < $max_behind_m ) )
-
{
-
print "$host mysql replicate is OK" . "\n";
-
}
-
else {
-
print FH "-" x 80 . ">", "\n";
-
my $start_time = ¤t_time();
-
print FH "start at $start_time\n";
-
-
print FH "mysql replicate is Fail!!!!!!!!!!!!" . "\n";
-
print FH "IO:\t\t $Slave_IO\n";
-
print FH "SQL:\t\t $Slave_SQL\n";
-
print FH "Behind_Master:\t $Seconds_Behind_Master\n";
-
-
my $end_time = ¤t_time();
-
print FH "end at $end_time\n";
-
close(FH);
-
-
$text = "$Slave_IO, $Slave_SQL, $Seconds_Behind_Master";
-
-
# &SendMail();
-
}
-
}
-
-
#-----------------------------------------------------------------
-
sub MysqlConnect {
-
my ( $host, $port, $user, $pass ) = @_;
-
my $dsn = "DBI:mysql:host=$host;port=$port";
-
return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
-
}
-
-
#-----------------------------------------------------------------
-
sub MysqlQuery {
-
my ( $dbh, $query ) = @_;
-
my $sth = $dbh->prepare($query);
-
my $res = $sth->execute;
-
return undef unless ($res);
-
my $row = $sth->fetchrow_hashref;
-
$sth->finish;
-
return $row;
-
}
-
-
#-----------------------------------------------------------------
-
sub current_time() {
-
my $time_now = POSIX::strftime( "[%Y-%m-%d %H:%M:%S]", localtime );
-
return $time_now;
-
}
-
-
#-----------------------------------------------------------------
-
sub SendMail() {
-
-
my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
-
or die "Error.\n";
-
$smtp->auth( 'LOGIN', $mailuser, $mailpasswd );
-
-
foreach my $mailto (@mailto) {
-
$smtp->mail($mailfrom);
-
$smtp->to($mailto);
-
$smtp->data();
-
$smtp->datasend("To: $mailto\n");
-
$smtp->datasend("From:$mailfrom\n");
-
$smtp->datasend("Subject: $subject\n");
-
$smtp->datasend("\n");
-
$smtp->datasend("$text\n");
-
$smtp->dataend();
-
}
-
-
$smtp->quit;
-
}
注:新添加只要复制 @aa数组[ ] 之间
阅读(1729) | 评论(0) | 转发(0) |