当前位置: 代码迷 >> MySQL >> mysql高可用之MHA(给养2)-邮件报警
  详细解决方案

mysql高可用之MHA(给养2)-邮件报警

热度:97   发布时间:2016-05-05 16:48:33.0
mysql高可用之MHA(补充2)--邮件报警

当mha进行failover 完成或由于错误停止时,我们可以使用send_report以邮件报警的方式来获得failover报告,以便我们及时了解现在的数据库状态。

首先需要修改脚本:

[[email protected] mha]# cat /usr/local/bin/send_report #!/usr/bin/perl#  Copyright (C) 2011 DeNA Co.,Ltd.##  This program is free software; you can redistribute it and/or modify#  it under the terms of the GNU General Public License as published by#  the Free Software Foundation; either version 2 of the License, or#  (at your option) any later version.##  This program is distributed in the hope that it will be useful,#  but WITHOUT ANY WARRANTY; without even the implied warranty of#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the#  GNU General Public License for more details.##  You should have received a copy of the GNU General Public License#   along with this program; if not, write to the Free Software#  Foundation, Inc.,#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA## Note: This is a sample script and is not complete. Modify the script based on your environment.use strict;use warnings FATAL => 'all';use Mail::Sender;use Getopt::Long;#new_master_host and new_slave_hosts are set only when recovering master succeededmy ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );my $smtp='smtp.163.com';my $mail_from='[email protected]';my $mail_user='[email protected]';my $mail_pass='password';#my $mail_to=['[email protected]','[email protected]'];my $mail_to='[email protected]';GetOptions(  'orig_master_host=s' => \$dead_master_host,  'new_master_host=s'  => \$new_master_host,  'new_slave_hosts=s'  => \$new_slave_hosts,  'subject=s'          => \$subject,  'body=s'             => \$body,);# Do whatever you want heremailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);sub mailToContacts {	my ($smtp, $mail_from, $mail_user, $mail_pass, $mail_to, $subject, $msg ) = @_;	open my $DEBUG, ">/var/log/masterha/app1/mail.log"		or die "Can't open the debug	file:$!\n";	my $sender = new Mail::Sender {		ctype		=> 'text/plain;charset=utf-8',		encoding	=> 'utf-8',		smtp		=> $smtp,		from		=> $mail_from,		auth		=> 'LOGIN',		TLS_allowed	=> '0',		authid		=> $mail_user,		authpwd		=> $mail_pass,		to		=> $mail_to,		subject		=> $subject,		debug		=> $DEBUG	};	$sender->MailMsg(		{			msg => $msg,			debug => $DEBUG		}	) or print $Mail::Sender::Error;	return 1;}exit 0;
然后修改配置文件,只需添加report_script即可

[server default]manager_log=/var/log/masterha/app1/manager.logmanager_workdir=/var/log/masterha/app1master_binlog_dir=/data/mysqlmaster_ip_failover_script=/usr/local/bin/master_ip_failovermaster_ip_online_change_script=/usr/local/bin/master_ip_online_changepassword=123456ping_interval=1remote_workdir=/tmprepl_password=123456repl_user=repreport_script=/usr/local/bin/send_reportssh_port=22ssh_user=rootuser=mha[server1]hostname=10.10.10.56port=3306[server2]hostname=10.10.10.57port=3306[server3]hostname=10.10.10.58port=3306
最后开启mha监控,停止master来触发failover,在最后我们可以看到生成了failover报告并send_report。

----- Failover Report -----app1: MySQL Master failover 10.10.10.57(10.10.10.57:3306) to 10.10.10.56(10.10.10.56:3306) succeededMaster 10.10.10.57(10.10.10.57:3306) is down!Check MHA Manager logs at rd-mysql-test4:/var/log/masterha/app1/manager.log for details.Started automated(non-interactive) failover.Invalidated master IP address on 10.10.10.57(10.10.10.57:3306)The latest slave 10.10.10.56(10.10.10.56:3306) has all relay logs for recovery.Selected 10.10.10.56(10.10.10.56:3306) as a new master.10.10.10.56(10.10.10.56:3306): OK: Applying all logs succeeded.10.10.10.56(10.10.10.56:3306): OK: Activated master IP address.10.10.10.58(10.10.10.58:3306): This host has the latest relay log events.Generating relay diff files from the latest slave succeeded.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)10.10.10.56(10.10.10.56:3306): Resetting slave info succeeded.Master failover to 10.10.10.56(10.10.10.56:3306) completed successfully.Thu Aug 13 11:27:36 2015 - [info] Sending mail..Unknown option: conf
我们收到的邮件如下:

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案