Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111733
  • 博文数量: 25
  • 博客积分: 1094
  • 博客等级: 少尉
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 16:36
文章分类

全部博文(25)

文章存档

2011年(14)

2010年(11)

分类: Python/Ruby

2011-05-23 10:33:53

  1. #!/usr/bin/perl -w
  2. # Use mandatory external modules
  3. use strict;
  4. use DBI;
  5. use Net::SMTP_auth;
  6. use Time::HiRes;
  7. use POSIX "strftime";

  8. my $max_behind_m = 120;
  9. my $check_log = '/var/log/mysql_check_log';

  10. my $mailhost = 'smtp.163.com';
  11. my $mailfrom = 'xxxxxx@163.com';
  12. my @mailto = '159xxxxxx@139.com';
  13. my $subject = "mysql_repl_error::";
  14. my $text;
  15. my $mailuser = 'xxxxx';
  16. my $mailpasswd = 'xxxxxx';

  17. # open log--file
  18. open FH, ">> $check_log" or die $!;

  19. my @aa = (
  20.     [ '192.168.1.203', '3306', 'myrepl', '123456' ],
  21.     [ '192.168.1.240', '3306', 'myrepl', '123456' ],
  22. );

  23. foreach my $a (@aa) {
  24.     &account(@$a);
  25. }

  26. # connect to servers (this)
  27. sub account {
  28.     my ( $host, $port, $user, $pass ) = @_;
  29.     my $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
  30.     print FH "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!"
  31.       if ( !$this_dbh );
  32.     my $slave_status = &MysqlQuery( $this_dbh, "SHOW SLAVE STATUS" );
  33.     print FH "ERROR: SQL Query Error: " . $this_dbh->errstr
  34.       unless ($slave_status);

  35.     my $Slave_IO = $slave_status->{Slave_IO_Running};
  36.     my $Slave_SQL = $slave_status->{Slave_SQL_Running};
  37.     my $Seconds_Behind_Master = $slave_status->{Seconds_Behind_Master};

  38.     print "IO:\t\t $Slave_IO\n";
  39.     print "SQL:\t\t $Slave_SQL\n";
  40.     print "Behind_Master:\t $Seconds_Behind_Master\n";

  41.     # Send to Mail
  42.     if ( ( $Slave_IO eq 'Yes' )
  43.         and ( $Slave_SQL eq 'Yes' )
  44.         and ( $Seconds_Behind_Master < $max_behind_m ) )
  45.     {
  46.         print "$host mysql replicate is OK" . "\n";
  47.     }
  48.     else {
  49.         print FH "-" x 80 . ">", "\n";
  50.         my $start_time = &current_time();
  51.         print FH "start at $start_time\n";

  52.         print FH "mysql replicate is Fail!!!!!!!!!!!!" . "\n";
  53.         print FH "IO:\t\t $Slave_IO\n";
  54.         print FH "SQL:\t\t $Slave_SQL\n";
  55.         print FH "Behind_Master:\t $Seconds_Behind_Master\n";

  56.         my $end_time = &current_time();
  57.         print FH "end at $end_time\n";
  58.         close(FH);

  59.         $text = "$Slave_IO, $Slave_SQL, $Seconds_Behind_Master";

  60.         # &SendMail();
  61.     }
  62. }

  63. #-----------------------------------------------------------------
  64. sub MysqlConnect {
  65.     my ( $host, $port, $user, $pass ) = @_;
  66.     my $dsn = "DBI:mysql:host=$host;port=$port";
  67.     return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
  68. }

  69. #-----------------------------------------------------------------
  70. sub MysqlQuery {
  71.     my ( $dbh, $query ) = @_;
  72.     my $sth = $dbh->prepare($query);
  73.     my $res = $sth->execute;
  74.     return undef unless ($res);
  75.     my $row = $sth->fetchrow_hashref;
  76.     $sth->finish;
  77.     return $row;
  78. }

  79. #-----------------------------------------------------------------
  80. sub current_time() {
  81.     my $time_now = POSIX::strftime( "[%Y-%m-%d %H:%M:%S]", localtime );
  82.     return $time_now;
  83. }

  84. #-----------------------------------------------------------------
  85. sub SendMail() {

  86.     my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
  87.       or die "Error.\n";
  88.     $smtp->auth( 'LOGIN', $mailuser, $mailpasswd );

  89.     foreach my $mailto (@mailto) {
  90.         $smtp->mail($mailfrom);
  91.         $smtp->to($mailto);
  92.         $smtp->data();
  93.         $smtp->datasend("To: $mailto\n");
  94.         $smtp->datasend("From:$mailfrom\n");
  95.         $smtp->datasend("Subject: $subject\n");
  96.         $smtp->datasend("\n");
  97.         $smtp->datasend("$text\n");
  98.         $smtp->dataend();
  99.     }

  100.     $smtp->quit;
  101. }

注:新添加只要复制 @aa数组[ ] 之间
阅读(1729) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Perl 备份Svn,利用7z 打包并且加密,上传至FTP

给主人留下些什么吧!~~