python 用于keepalived的邮件通知脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于keepalived的邮件通知脚本相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# coding:utf-8

import smtplib
from email.mime.text import MIMEText
import os
import sys
import time

smtp_server = 'smtp.emoney.cn'
user = 'baoyifeng@emoney.cn'
password = '******'
mail_from = 'baoyifeng@emoney.cn'
display_from = 'keepalive_influx<{0}>'.format(mail_from)
mail_to = [
    "wangyuxin0623@emoney.cn",
    "lostsummer@gmail.com"
]
vip = '172.31.37.201'


def send_mail(mail_to, subject, content):
    msg = MIMEText(content, _subtype='plain', _charset='utf-8')
    msg['Subject'] = subject
    msg['From'] = display_from

    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtp_server)
        smtp.login(user, password)
        smtp.sendmail(mail_from, mail_to, msg.as_string())
        smtp.close()
        print('send ok')
        return True

    except Exception as e:
        senderr = str(e)
        print(senderr)
        return False


if __name__ == "__main__":
    if (len(sys.argv) < 2):
        print("less arg!")
        sys.exit(1)
    stat = sys.argv[1]
    if stat not in ["master", "backup", "fault"]:
        print("error arg!")
        sys.exit(2)

    hostip = 'localhost'
    timestamp = time.strftime('%Y-%m-%d %X', time.localtime())
    subject = "{0} is change to be {1}, {2} floating".format(
        hostip, stat, vip)
    content = "{0} :\nvrrp transition, {1} changed to be {2}".format(
        timestamp, hostip, stat)

    sys.exit(0) if send_mail(mail_to, subject, content) else sys.exit(3)

以上是关于python 用于keepalived的邮件通知脚本的主要内容,如果未能解决你的问题,请参考以下文章

keepalived双机热备实现故障时发送邮件通知

双KeepAlived+Mysql切换Vip时发邮件问题。

Keepalived通知脚本配置

keepalived 发送告警邮件到外部邮箱

keepalived 只启动2个进程,是啥情况

Rabbitmq+Nginx+keepalived高可用热备