#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
from email.MIMEText import MIMEText
import smtplib
sys.setdefaultencoding('utf-8')
import socket, fcntl, struct
def send_mail(to_list,sub,content):
mail_host="smtp.163.com"
mail_user="XXXXX@163.com"
mail_pass="XXXXXXX"
mail_postfix="163.com"
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
def get_local_ip(ifname = 'eth0'):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
ret = socket.inet_ntoa(inet[20:24])
return ret
if sys.argv[1]!="master" and sys.argv[1]!="backup" and sys.argv[1]!="fault":
sys.exit()
else:
notify_type = sys.argv[1]
if __name__ == '__main__':
strcontent = get_local_ip()+ " " +notify_type+" State is activated, please make sure the HAProxy service running state!"
mailto_list = ['XXXXX@139.com, ]
for mailto in mailto_list:
send_mail(mailto, "HAProxy State switch alarm", strcontent.encode('utf-8'))
参考南非蚂蚁《高性能Linux服务器构建实战》