centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)

centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)


#####################     sendmail.py  begin     #####################
#!/usr/bin/python -W
# -*- coding: UTF-8 -*-


from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE,formatdate
from email import encoders


import os


#server['name'], server['user'], server['passwd']
def send_mail(server, fro, to, subject, text, files=[]): 
    assert type(server) == dict 
    assert type(to) == list 
    assert type(files) == list 
 
    msg = MIMEMultipart() 
    msg['From'] = fro 
    msg['Subject'] = subject 
    msg['To'] = COMMASPACE.join(to) #COMMASPACE==', ' 
    msg['Date'] = formatdate(localtime=True) 
    msg.attach(MIMEText(text)) 
 
    for file in files: 
        part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data 
        part.set_payload(open(file, 'rb'.read())) 
        encoders.encode_base64(part) 
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) 
        msg.attach(part) 
 
    import smtplib 
    smtp = smtplib.SMTP(server['name']) 
    smtp.login(server['user'], server['passwd']) 
    smtp.sendmail(fro, to, msg.as_string()) 
    smtp.close()


send_mail({'name':'smtp.163.com','user':'erp2','passwd':'xxxxxxxxxx'}, 'erp2@163.com', ['99923309@qq.com'], '服务器故障转移', 'keepalived故障转移通知',[])  


#####################     sendmail.py  end     #####################


linux centos7.2实际测试可用,放在:/etc/keepalived/sendmail.py


keepalived配置如下:
在 /etc/keepalived/keepalived.conf 中增加:


vrrp_sync_group VG_1 {
    group {
        VI_1
    }
    notify_master /etc/keepalived/sendmail.py
}


重启keepalived服务即可

posted @ 2017-04-10 15:58  chinacloudy  阅读(245)  评论(0编辑  收藏  举报