W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在sentinel中,如果出現(xiàn)warning以上級(jí)別的事件發(fā)生, 是可以通過如下配置進(jìn)行腳本調(diào)用的(對(duì)于該腳本redis啟動(dòng)用戶需要有執(zhí)行權(quán)限):
sentinel notification-script mymaster /redis/script/notify.py
比如說,我們希望在發(fā)生這些事件的時(shí)候進(jìn)行郵件通知,那么,notify.py就是一個(gè)觸發(fā)郵件調(diào)用的東東,傳入第一個(gè)參數(shù)為事件類型,第二個(gè)參數(shù)為事件信息:
#!/bin/python
from sendmail import send_mail
import sys
event_type = sys.argv[1]
event_desc = sys.argv[2]
mail_content = event_type + ":" + event_desc
send_mail("xxxx@qq.com",
["xxxxx@cmbc.com.cn","xxxx@gmail.com"],
"Redis Sentinel Event Notification Mail",
mail_content,
cc=["xxx@gmail.com","xxx@139.com"],
bcc=["xxxx@qq.com"]
)
有兩個(gè)注意事項(xiàng): 1) 這個(gè)時(shí)候如果集群發(fā)生了切換會(huì)產(chǎn)生很多事件,此腳本是在每一個(gè)事件發(fā)生時(shí)調(diào)用一次,那么你將短時(shí)間收到很多封郵件,加上很多的郵件網(wǎng)關(guān)是不允許在一個(gè)短時(shí)間內(nèi)發(fā)送太多的郵件的,因此這個(gè)僅僅是一個(gè)示例,并不具備實(shí)際上的作用。 2) 一般我們會(huì)采用多個(gè)sentinel,只需在一個(gè)sentinel上配置即可,否則將同一個(gè)消息會(huì)被多個(gè)sentinel多次處理。
附sendmail模塊代碼:
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import Encoders
from email.message import Message
import datetime
def send_mail(fromPerson,toPerson, subject="", text="",files=[], cc=[], bcc=[]):
server = "smtp.qq.com"
assert type(toPerson)==list
assert type(files)==list
assert type(cc)==list
assert type(bcc)==list
message = MIMEMultipart()
message['From'] = fromPerson
message['To'] = ', '.join(toPerson)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message['Cc'] = ','.join(cc)
message['Bcc'] = ','.join(bcc)
message.attach(MIMEText(text))
for f in files:
part = MIMEApplication(open(f,"rb").read())
part.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(part)
addresses = []
for x in toPerson:
addresses.append(x)
for x in cc:
addresses.append(x)
for x in bcc:
addresses.append(x)
smtp = smtplib.SMTP_SSL(server)
smtp.login("xxxx@qq.com","xxxx")
smtp.sendmail(message['From'],addresses,message.as_string())
smtp.close()
最佳實(shí)踐:采用ELK(Elastic+Logstash+Kibana)進(jìn)行日志收集告警(ElastAlert用起來不錯(cuò)),不啟用這個(gè)事件通知功能。如果你的環(huán)境中沒有ELK,或者啟動(dòng)一個(gè)Tcp Server進(jìn)程,notify腳本將事件通過tcp方式吐給這個(gè)server,該Server收集一批事件后再做諸如發(fā)郵件的處理。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: