设置Keepalived VIP切换邮件告警

修改keepalived.conf配置:

[root@server01 keepalived]# cat keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
saltstack@163.com
}
notification_email_from dba@dbserver.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_script vs_mysql_100 {
script "python /etc/keepalived/checkProxySQL.py -h localhost -P 6033"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 51
priority 150
advert_int 1
nopreempt authentication {
auth_type PASS
auth_pass 1111
}
track_script {
vs_mysql_100
}
virtual_ipaddress {
10.200.22.178
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}

通知 notify.sh:

#!/bin/bash

contact='root@localhost'

notify() {
local mailsubject="$(hostname) to be $1, vip floating"
local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
python /etc/keepalived/sendMail.py "$mailsubject" "$mailbody"
echo $mailbody >> /etc/keepalived/notify.log
} case $ in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit
;;
esac

发送邮件 sendMail.py:

 #!/usr/bin/python
# -*- coding: utf- -*-
# author xuaiqi
# date import os
import time
import shutil
import MySQLdb
import smtplib
#import requests
import datetime from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from datetime import datetime import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding) # email info
mailserver = "smtp.vcredit.com"
EMAIL_USERNAME = 'dbmonitor@vcredit.com'
EMAIL_PASSWORD = 'dbmonitor1234!!' title=sys.argv[]
contents=sys.argv[] #发送邮件
def send_mail(title, to_list):
me = "dbmonitor@vcredit.com" def _create_msg():
msg = MIMEMultipart('related')
msg['Subject'] = title
msg['From'] = me
msg['To'] = ';'.join(to_list)
msg['date'] = '2012-3-16' #解决邮件发送时间不对
msg.preamble = 'This is a multi-part message in MIME format.'
msg_text = MIMEText(contents, 'html','utf-8')
msg_alternative = MIMEMultipart('alternative')
msg_alternative.attach(msg_text)
msg.attach(msg_alternative)
#print msg
return msg
try:
server = smtplib.SMTP()
server.connect(mailserver)
server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
server.sendmail(me, to_list, _create_msg().as_string())
server.close()
print 'send mail Ok!'
except Exception, e:
print e print"--------------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Start sending mail --------------------"
if __name__ == '__main__':
user=['yujun02@vcredit.com','zhanglin04@vcredit.com','xuaiqi@vcredit.com']
print "发送的用户",user
print "内容",contents
send_mail(title, user)
print"---------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Sucessfull sending mail --------------------"

设置MHA切换邮件通知

报告脚本 send_report.py:

 #!/usr/bin/env python
#-*- encoding:utf- -*-
#-------------------------------------------------------------------------------
# Name: send_report.py
# Author: zhoujy
#----------------------------------------------
import os
import sys
import time
import datetime
import smtplib
import subprocess
import fileinput
import getopt
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.Utils import COMMASPACE, formatdate reload(sys)
sys.setdefaultencoding('utf8') def send_mail(to, subject, text, from_mail, server="localhost"):
message = MIMEMultipart()
message['From'] = from_mail
message['To'] = COMMASPACE.join(to)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message.attach(MIMEText(text,_charset='utf-8'))
smtp = smtplib.SMTP(server)
smtp.login(from_mail, 'dbmonitor1234!!')
smtp.sendmail(from_mail, to, message.as_string())
smtp.close() if __name__ == "__main__":
opts,args = getopt.getopt(sys.argv[:],"h",["orig_master_host=","new_master_host=","new_slave_hosts=","conf=","subject=","body=","app_vip=","new_master_ssh_port=","ssh_user="])
# print opts,args
for lines in opts:
key,values = lines
if key == '--orig_master_host':
orig_master_host = values
if key == '--new_master_host':
new_master_host = values
if key == '--new_slave_hosts':
new_slave_hosts = values
if key == '--subject':
subject = values
if key == '--body':
body = values
# text = sys.stdin.read()
mail_list = ['xuaiqi@vcredit.com','yujun02@vcredit.com','zhanglin04@vcredit.com']
send_mail(mail_list, subject.encode("utf8"), body, "dbmonitor@vcredit.com", server="smtp.vcredit.com")

Master基础配置:

 [server default]
user=root
password=iforgot
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=
#master_binlog_dir= /var/lib/mysql,/var/log/mysql
secondary_check_script=masterha_secondary_check -s 10.200.22.136 -s 10.200.22.138 -s 10.200.22.223
master_ip_failover_script="/etc/mha/scripts/master_ip_failover"
master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change"
report_script="/etc/mha/scripts/send_report.py"

测试验证

keepalived vip切换

mha切换

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)的更多相关文章

  1. MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(二)

    ProxySQL安装 yum/rpm安装 在github或官网上可以下载rpm包,wiki的Getting start章节有详细介绍. cat <<EOF | tee /etc/yum.r ...

  2. MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(一)

    准备服务器: docker network create --subnet=192.168.0.0/16 staticnetdocker run -d --privileged -v `pwd`/my ...

  3. MHA+ProxySQL实现读写分离高可用

    最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.c ...

  4. MHA+ProxySQL 读写分离高可用

    文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...

  5. MySQL for OPS 09:MHA + Atlas 实现读写分离高可用

    写在前面的话 前面做了 MHA 高可用,但是存在这样一个问题,我们花了 4 台机器,但是最终被利用起来的也就一台,主库.这样硬件利用率才 25%,这意味着除非发生故障,不然其他几台机器都是摆设.明显的 ...

  6. MySQL中间件之ProxySQL(10):读写分离方法论

    返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html 1.不同类型的读写分离 数据库中间件最基本的功能就是实现读写分离,Pr ...

  7. MySQL读写分离高可用集群及读操作负载均衡(Centos7)

    目录 概述 keepalived和heartbeat对比 一.环境 二.部署 部署lvs代理和keepalived MySQL+heartbeat+drbd的部署 MySQL主从复制 web服务器及a ...

  8. mysql读写分离[高可用]

    顾名思义, 在mysql负载均衡中有多种方式, 本人愚钝,只了解驱动中间件和mysql_proxy两种方式, 对于驱动,利用的是ReplicationDriver,具体请看远哥的这篇文章: MySQL ...

  9. Atlas读写分离[高可用]

    Atlas下载地址: https://github.com/Qihoo360/Atlas/releases Atlas是出于360的, 比mysql-proxy更稳定, 部署起来更方便. 环境: pr ...

随机推荐

  1. CSS盒子内容

    内边距 内边距(padding):内容与边框之间的距离 注意:padding 只能移动盒子的内容 padding属性联写: padding: 10px: 内边距的上下左右都移动 padding: 10 ...

  2. 元组tuple 可迭代对象

    1. 字符串.元组和列表相互转化 s = 'abcdef' li = ['a','b','c','d'] tu = (1,2,4,9) v = tuple(s) print('v = ',v) w = ...

  3. 将lvm逻辑卷分出一部分

    今天公司磁盘报警,查看磁盘发现挂载的/aa空间还很多,而/ee 空间不足,现将/aa上的lvm分出一部分空间.加到/ee上 注:首先要 df  -T  -h 开一下磁盘格式如果是xfs格式的lvm  ...

  4. 埃式筛法——求n以内素数

    素数筛法的关键就在一个“筛”字.算法从小到大枚举所有数,对每一个素数,筛去它的所有倍数,剩下的就都是素数了. 例如:求1-15中的所有素数. 1.  2是素数(唯一需要事先确定的),因此筛去2的所有倍 ...

  5. Netty 基本组件与线程模型

    Netty 的学习内容主要是围绕 TCP 和 Java NIO 这两个点展开的,由于 Netty 是基于 Java NIO 的 API 之上构建的网络通讯框架,Java NIO 中的几个组件,都能在 ...

  6. expdp用户10迁移到新环境11之正式实施

    expdp迁移源端数据库:cu    源端IP:   源端schema: xxx目标数据库:ora 目标IP:xxx操作流程:31日凌晨应用停,随后使用数据泵迁移,两套库迁移,迁移一套,迁移完毕应用确 ...

  7. vue打包上传oss

    今天把vue打包之后上传到oss,遇到了一点问题,现在解决了总结一下心得: OSS (Object Storage Service)名为对象存储,配合cdn使用达到静态文件托管加速,提升网站文件访问速 ...

  8. 更改手机系统的User-Agent & okhttp

    okhttp 和 volley 1. 之前用的是volley,其中一部分功能,比如User-Agent,是系统去处理的,改成okhttp库后,这部分功能需要浏览器自己处理 2. 具体区别可以参考: h ...

  9. memcached命令行、Memcached数据导出和导入、php连接memcache、php的session存储到memcached

    1.memcached命令行 telnet 127.0.0.1 11211set key2 0 30 2abSTOREDget key2VALUE key2 0 2abEND  如: set key3 ...

  10. 1.继承(extends)、超类(superClass)、子类(subClass)

    注意:继承主要使用的is-a关系 在子类中用一个新的方法来覆盖超类中的方法(override),需要注意的是如果子类之中的方法或者域  被覆盖时,仍然想访问superClass中的方法和域,此时必须使 ...