• 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服务即可

  • 相关阅读:
    Web开发较好用的几个chrome插件
    SQL注入专题
    内存泄露检测之ccmalloc
    ruby method lambda block proc 联系与区别 next break return
    c++构造函数详解
    VIM使用系列之一—配置VIM下编程和代码阅读环境
    一个项目经理的经验总结
    如何改正拖拉的习惯
    PHP开源软件《个人管理系统》希望大家一起来开发
    PHP 开源软件《个人管理系统》——完善登录模块
  • 原文地址:https://www.cnblogs.com/bdccloudy/p/7665220.html
Copyright © 2020-2023  润新知