mysql高可用之MHA--邮件报警

 当mha进行failover 完成或由于错误停止时,我们可以使用send_report以邮件报警的方式来获得failover报告,以便我们及时了解现在的数据库状态。
首先需要修改脚本:
[html] view plain
  1. [root@rd-mysql-test4 mha]# cat /usr/local/bin/send_report   
  2. #!/usr/bin/perl  
  3.   
  4. #  Copyright (C) 2011 DeNA Co.,Ltd.  
  5. #  
  6. #  This program is free software; you can redistribute it and/or modify  
  7. #  it under the terms of the GNU General Public License as published by  
  8. #  the Free Software Foundation; either version 2 of the License, or  
  9. #  (at your option) any later version.  
  10. #  
  11. #  This program is distributed in the hope that it will be useful,  
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of  
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
  14. #  GNU General Public License for more details.  
  15. #  
  16. #  You should have received a copy of the GNU General Public License  
  17. #   along with this program; if not, write to the Free Software  
  18. #  Foundation, Inc.,  
  19. #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA  
  20.   
  21. ## Note: This is a sample script and is not complete. Modify the script based on your environment.  
  22.   
  23. use strict;  
  24. use warnings FATAL => 'all';  
  25. use Mail::Sender;  
  26. use Getopt::Long;  
  27.   
  28. #new_master_host and new_slave_hosts are set only when recovering master succeeded  
  29. my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );  
  30.   
  31. my $smtp='smtp.163.com';  
  32. my $mail_from='from@163.com';  
  33. my $mail_user='from@163.com';  
  34. my $mail_pass='password';  
  35. #my $mail_to=['to1@qq.com','to2@qq.com'];  
  36. my $mail_to='to@qq.com';  
  37.   
  38. GetOptions(  
  39.   'orig_master_host=s' => \$dead_master_host,  
  40.   'new_master_host=s'  => \$new_master_host,  
  41.   'new_slave_hosts=s'  => \$new_slave_hosts,  
  42.   'subject=s'          => \$subject,  
  43.   'body=s'             => \$body,  
  44. );  
  45.   
  46. # Do whatever you want here  
  47. mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);  
  48.   
  49. sub mailToContacts {  
  50.     my ($smtp, $mail_from, $mail_user, $mail_pass, $mail_to, $subject, $msg ) = @_;  
  51.     open my $DEBUG, ">/var/log/masterha/app1/mail.log"  
  52.         or die "Can't open the debug    file:$!\n";  
  53.     my $sender = new Mail::Sender {  
  54.         ctype       => 'text/plain;charset=utf-8',  
  55.         encoding    => 'utf-8',  
  56.         smtp        => $smtp,  
  57.         from        => $mail_from,  
  58.         auth        => 'LOGIN',  
  59.         TLS_allowed => '0',  
  60.         authid      => $mail_user,  
  61.         authpwd     => $mail_pass,  
  62.         to      => $mail_to,  
  63.         subject     => $subject,  
  64.         debug       => $DEBUG  
  65.     };  
  66.     $sender->MailMsg(  
  67.         {  
  68.             msg => $msg,  
  69.             debug => $DEBUG  
  70.         }  
  71.     ) or print $Mail::Sender::Error;  
  72.     return 1;  
  73. }  
  74.   
  75. exit 0;  
然后修改配置文件,只需添加report_script即可
[html] view plain
  1. [server default]  
  2. manager_log=/var/log/masterha/app1/manager.log  
  3. manager_workdir=/var/log/masterha/app1  
  4. master_binlog_dir=/data/mysql  
  5. master_ip_failover_script=/usr/local/bin/master_ip_failover  
  6. master_ip_online_change_script=/usr/local/bin/master_ip_online_change  
  7. password=123456  
  8. ping_interval=1  
  9. remote_workdir=/tmp  
  10. repl_password=123456  
  11. repl_user=rep  
  12. report_script=/usr/local/bin/send_report  
  13. ssh_port=22  
  14. ssh_user=root  
  15. user=mha  
  16.   
  17. [server1]  
  18. hostname=10.10.10.56  
  19. port=3306  
  20.   
  21. [server2]  
  22. hostname=10.10.10.57  
  23. port=3306  
  24.   
  25. [server3]  
  26. hostname=10.10.10.58  
  27. port=3306  
最后开启mha监控,停止master来触发failover,在最后我们可以看到生成了failover报告并send_report。
[html] view plain
  1. ----- Failover Report -----  
  2.   
  3. app1: MySQL Master failover 10.10.10.57(10.10.10.57:3306) to 10.10.10.56(10.10.10.56:3306) succeeded  
  4.   
  5. Master 10.10.10.57(10.10.10.57:3306) is down!  
  6.   
  7. Check MHA Manager logs at rd-mysql-test4:/var/log/masterha/app1/manager.log for details.  
  8.   
  9. Started automated(non-interactive) failover.  
  10. Invalidated master IP address on 10.10.10.57(10.10.10.57:3306)  
  11. The latest slave 10.10.10.56(10.10.10.56:3306) has all relay logs for recovery.  
  12. Selected 10.10.10.56(10.10.10.56:3306) as a new master.  
  13. 10.10.10.56(10.10.10.56:3306): OK: Applying all logs succeeded.  
  14. 10.10.10.56(10.10.10.56:3306): OK: Activated master IP address.  
  15. 10.10.10.58(10.10.10.58:3306): This host has the latest relay log events.  
  16. Generating relay diff files from the latest slave succeeded.  
  17. 10.10.10.58(10.10.10.58:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.10.10.56(10.10.10.56:3306)  
  18. 10.10.10.56(10.10.10.56:3306): Resetting slave info succeeded.  
  19. Master failover to 10.10.10.56(10.10.10.56:3306) completed successfully.  
  20. Thu Aug 13 11:27:36 2015 - [info] Sending mail..  
  21. Unknown option: conf  
我们收到的邮件如下:

 

posted @ 2017-11-11 12:15  John_2011  阅读(1775)  评论(0编辑  收藏  举报