galera cluster,mysql配置wsrep_notify_cmd参数,增加邮件告警

vi /usr/local/sunlight/wsrep_notify_cmd.sh

chown mysql:mysql  /usr/local/sunlight/wsrep_notify_cmd.sh

chmod 700 /usr/local/sunlight/wsrep_notify_cmd.sh

 

内容如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script to send email notifications when a change in Galera cluster membership
# occurs.
#
# Complies with http://www.codership.com/wiki/doku.php?id=notification_command
#
# Author: Gabe Guillen <gguillen@gesa.com>
# Version: 1.3
# Release: 10/03/2013
# Use at your own risk. No warranties expressed or implied.
#

import os
import sys
import getopt

import smtplib

try: from email.mime.text import MIMEText
except ImportError:
# Python 2.4 (CentOS 5.x)
from email.MIMEText import MIMEText

import socket
from email.utils import formataddr
# Change this to some value if you don't want your server hostname to show in
# the notification emails
SP_NAME="深圳广信"
THIS_SERVER = socket.gethostname()

# Server hostname or IP address
SMTP_SERVER = 'smtp.exmail.qq.com'
SMTP_PORT = 25

# Set to True if you need SMTP over SSL
SMTP_SSL = False

# Set to True if you need to authenticate to your SMTP server
SMTP_AUTH = False
# Fill in authorization information here if True above
SMTP_USERNAME = 'SL-zabbix@sunlight-tech.com'
SMTP_PASSWORD = 'SLzAbbix@2016'

# Takes a single sender
MAIL_FROM = 'SL-zabbix@sunlight-tech.com'
# Takes a list of recipients
MAIL_TO = ['chao.dong@sunlight-tech.com']


MAIL_TITLE = "深圳广信 - 数据库集群-监控报警"
# Edit below at your own risk
################################################################################
def main(argv):

str_status = ''
str_uuid = ''
str_primary = ''
str_members = ''
str_index = ''
message = ''

usage = "Usage: " + os.path.basename(sys.argv[0]) + " --status <status str>"
usage += " --uuid <state UUID> --primary <yes/no> --members <comma-seperated"
usage += " list of the component member UUIDs> --index <n>"

try:
opts, args = getopt.getopt(argv, "h", ["status=","uuid=",'primary=','members=','index='])
except getopt.GetoptError:
print usage
sys.exit(2)

if(len(opts) > 0):
message_obj = GaleraStatus(SP_NAME,THIS_SERVER)

for opt, arg in opts:
if opt == '-h':
print usage
sys.exit()
elif opt in ("--status"):
message_obj.set_status(arg)
elif opt in ("--uuid"):
message_obj.set_uuid(arg)
elif opt in ("--primary"):
message_obj.set_primary(arg)
elif opt in ("--members"):
message_obj.set_members(arg)
elif opt in ("--index"):
message_obj.set_index(arg)
try:
send_notice_email(message_obj)
except Exception, e:
print "Unable to send notification: %s" % e
sys.exit(1)
else:
print usage
sys.exit(2)

sys.exit(0)

def send_notice_email(message):
global SMTP_USERNAME,SMTP_PASSWORD,MAIL_FROM,MAIL_TO
msg = MIMEText(str(message), 'plain', 'utf-8')
msg['From'] = formataddr(["盛阳监控管理员",MAIL_FROM])
msg['To'] = formataddr(["Chao",MAIL_TO])
msg['Subject'] = "盛阳科技 - 数据库集群监控报警"
try:
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.login(SMTP_USERNAME,SMTP_PASSWORD)
server.sendmail(SMTP_USERNAME, MAIL_TO, msg.as_string())
print u"邮件发送成功!"
server.quit()
except smtplib.SMTPException:
print u"Error: 无法发送邮件"


class GaleraStatus:
def __init__(self, sp,server):
self._sp = sp
self._server = server
self._status = ""
self._uuid = ""
self._primary = ""
self._members = ""
self._index = ""
self._count = 0

def set_status(self, status):
self._status = status
self._count += 1

def set_uuid(self, uuid):
self._uuid = uuid
self._count += 1

def set_primary(self, primary):
self._primary = primary.capitalize()
self._count += 1

def set_members(self, members):
self._members = members.split(',')
self._count += 1

def set_index(self, index):
self._index = index
self._count += 1

def __str__(self):
message = "运营商名称: " + self._sp + "\n\n"
message += "节点Hostname: " + self._server + "\n\n"
message += "触发规则: " + "数据库集群-节点状态-发生变化"

if(self._count > 1):
message += "s"

message += "\n\n"
message += "--------------------------------------------------------\n"
if(self._status):
message += "Status of this node: " + self._status + "\n\n"

if(self._uuid):
message += "Cluster state UUID: " + self._uuid + "\n\n"

if(self._primary):
message += "Current cluster component is primary: " + self._primary + "\n\n"

if(self._members):
message += "Current members of the component:\n"

if(self._index):
for i in range(len(self._members)):
if(i == int(self._index)):
message += "-> "
else:
message += "-- "

message += self._members[i] + "\n"
else:
message += "\n".join((" " + str(x)) for x in self._members)

message += "\n"

if(self._index):
message += "Index of this node in the member list: " + self._index + "\n"
message += "--------------------------------------------------------\n"
message += "\n"
message += "收到这封邮件,说明集群节点角色或状态发生变化,请登录服务器进行检查!"
return message

if __name__ == "__main__":
main(sys.argv[1:])

posted on 2017-12-25 16:17  天涯飞鸿  阅读(863)  评论(0编辑  收藏  举报

导航