Mysql High availability MHA (Supplement 2)-email alarm
When the mha failover is completed or stopped due to an error, we can use send_report to get the failover report by email alarm, so that we can keep abreast of the current database status.
First you need to modify the script:
[root@rd-mysql-test4 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 succeeded
My ($dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body)
My $smtp='smtp.163.com'
My $mail_from='from@163.com'
My $mail_user='from@163.com'
My $mail_pass='password'
# my $mail_to= ['to1@qq.com','to2@qq.com']
My $mail_to='to@qq.com'
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 here
MailToContacts ($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 fileParticipation!\ 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
Then modify the configuration file by adding report_script
[server default]
Manager_log=/var/log/masterha/app1/manager.log
Manager_workdir=/var/log/masterha/app1
Master_binlog_dir=/data/mysql
Master_ip_failover_script=/usr/local/bin/master_ip_failover
Master_ip_online_change_script=/usr/local/bin/master_ip_online_change
Password=123456
Ping_interval=1
Remote_workdir=/tmp
Repl_password=123456
Repl_user=rep
Report_script=/usr/local/bin/send_report
Ssh_port=22
Ssh_user=root
User=mha
[server1]
Hostname=10.10.10.56
Port=3306
[server2]
Hostname=10.10.10.57
Port=3306
[server3]
Hostname=10.10.10.58
Port=3306
Finally, turn on mha monitoring, stop master to trigger failover, and finally we can see that the failover report is generated and send_report.
-Failover Report-
App1: MySQL Master failover 10.10.10.57 (10.10.10.57 to 3306) to 10.10.56 (10.10.10.56 virtual 3306) succeeded
Master 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 purl 3306)
The latest slave 10.10.10.56 (10.10.10.56) has all relay logs for recovery.
Selected 10.10.10.56 (10.10.10.56) as a new master.
10.10.10.56 (10.10.10.56 OK 3306): Applying all logs succeeded.
10.10.10.56 (10.10.10.56 OK 3306): Activated master IP address.
10.10.10.58 (10.10.10.58): 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 OK 3306): Applying all logs succeeded. Slave started, replicating from 10.10.10.56 (10.10.10.56 virtual 3306)
10.10.10.56 (10.10.10.56): Resetting slave info succeeded.
Master failover to 10.10.10.56 (10.10.10.56) completed successfully.
Thu Aug 13 11:27:36 2015-[info] Sending mail..
Unknown option: conf