keepalived 发送告警邮件到外部邮箱
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived 发送告警邮件到外部邮箱相关的知识,希望对你有一定的参考价值。
邮件脚本:
keepalived_notify.py
#!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header import sys, time, subprocess # 第三方 SMTP 服务 mail_host="smtp.exmail.qq.com" #设置服务器 mail_user="xxx" #用户名 mail_pass="xxx" #口令 sender = ‘[email protected]‘ # 邮件发送者 receivers = [‘[email protected]‘, ‘[email protected]‘] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 p = subprocess.Popen(‘hostname‘, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) hostname = p.stdout.readline().split(‘\n‘)[0] message_to = ‘‘ for i in receivers: message_to += i + ‘;‘ def print_help(): note = ‘‘‘python script.py role ip vip ‘‘‘ print(note) exit(1) time_stamp = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time())) if len(sys.argv) != 4: print_help() elif sys.argv[1] == ‘master‘: message_content = ‘%s server: %s(%s) change to Master, vIP: %s‘ %(time_stamp, sys.argv[2], hostname, sys.argv[3]) subject = ‘%s change to Master -- keepalived notify‘ %(sys.argv[2]) elif sys.argv[1] == ‘backup‘: message_content = ‘%s server: %s(%s) change to Backup, vIP: %s‘ %(time_stamp, sys.argv[2], hostname, sys.argv[3]) subject = ‘%s change to Backup -- keepalived notify‘ %(sys.argv[2]) else: print_help() message = MIMEText(message_content, ‘plain‘, ‘utf-8‘) message[‘From‘] = Header(sender, ‘utf-8‘) message[‘To‘] = Header(message_to, ‘utf-8‘) message[‘Subject‘] = Header(subject, ‘utf-8‘) try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print("Error: 无法发送邮件") print(e)
使用方法:
python script.py{脚本名} role{master|backup} ip{本keepalived服务器IP} vip{虚拟IP}
master keepalived:
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } ## 上面的配置邮件只能发送到本机,mail 可查看 vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/80" interval 2 weight -10 } vrrp_instance VI_1 { state BACKUP ############ MASTER|BACKUP interface ens160 virtual_router_id 11 mcast_src_ip 192.168.1.178 priority 99 ########### 权值要比 back 高 advert_int 2 authentication { auth_type PASS auth_pass 8u90u3fhE3FQ } track_script { chk_http_port ### 执行监控的服务 } virtual_ipaddress { 192.168.1.96 } notify_master "/bin/python /script/keepalived_notify.py master 192.168.1.178 192.168.1.96" notify_backup "/bin/python /script/keepalived_notify.py backup 192.168.1.178 192.168.1.96" }
backup keepalived:
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } ## 上面的配置邮件只能发送到本机,mail 可查看 vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/80" interval 2 weight -10 } vrrp_instance VI_1 { state BACKUP ############ MASTER|BACKUP interface ens160 virtual_router_id 11 mcast_src_ip 192.168.1.174 priority 100 ########### 权值要比 back 高 advert_int 2 authentication { auth_type PASS auth_pass 8u90u3fhE3FQ } track_script { chk_http_port ### 执行监控的服务 } virtual_ipaddress { 192.168.1.96 } notify_master "/bin/python /script/keepalived_notify.py master 192.168.1.174 192.168.1.96" notify_backup "/bin/python /script/keepalived_notify.py backup 192.168.1.174 192.168.1.96" }
以上是关于keepalived 发送告警邮件到外部邮箱的主要内容,如果未能解决你的问题,请参考以下文章