Zero's Blog

keepalived切换通知脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
more changeState_mail.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import socket
import time
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email.Header import Header
import sys
#发送邮件的相关信息,根据实际情况填写
smtpHost = 'mail.xxx.com'
smtpPort = '25'
sslPort = '465'
fromMail = 'user@xxx.com'
toMail = 'user@xxx.com'
username = 'user@xxx.com'
password = 'passwd'
#解决中文问题
reload(sys)
sys.setdefaultencoding('utf8')
#邮件标题和内容
subject = socket.gethostname() + " HA status has changed"
body= (time.strftime("%Y-%m-%d %H:%M:%S")) + " vrrp transition, " + socket.gethostname() + " changed to be " + sys.argv[1]
#初始化邮件
encoding = 'utf-8'
mail = MIMEText(body.encode(encoding),'plain',encoding)
mail['Subject'] = Header(subject,encoding)
mail['From'] = fromMail
mail['To'] = toMail
mail['Date'] = formatdate()
try:
#连接smtp服务器,明文/SSL/TLS三种方式,根据你使用的SMTP支持情况选择一种
#普通方式,通信过程不加密
#smtp = smtplib.SMTP(smtpHost,smtpPort)
#smtp.ehlo()
#smtp.login(username,password)
#tls加密方式,通信过程加密,邮件数据安全,使用正常的smtp端口
#smtp = smtplib.SMTP(smtpHost,smtpPort)
#smtp.ehlo()
#smtp.starttls()
#smtp.ehlo()
#smtp.login(username,password)
#纯粹的ssl加密方式,通信过程加密,邮件数据安全
smtp = smtplib.SMTP_SSL(smtpHost,sslPort)
smtp.ehlo()
smtp.login(username,password)
#发送邮件
smtp.sendmail(fromMail,toMail,mail.as_string())
smtp.close()
print 'OK'
except Exception:
print 'Error: unable to send email'

changeState_mail.py