-
Notifications
You must be signed in to change notification settings - Fork 0
/
kk47mail.py
executable file
·146 lines (129 loc) · 4.09 KB
/
kk47mail.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# use dayu count to send mail
import smtplib
import getopt
import sys
import os
import datetime
def dayumail(para, mailto=[]):
options = para.strip('\'').split('|||')
if len(options) != 4:
msg = 'Invalid alert format'
return False, msg
if "@]" in options[1]:
options[1] = options[1].split(']',1)[0] + eenvid + options[1].split(']',1)[1]
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
header = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (mailuser, ','.join(mailto), options[1].strip()))
body = ("Subject: %s\r\nLevel: %s\r\nDate: %s\r\n" % (options[1].strip(), options[0].strip(), date))
body += ("Action: %s\r\nDescription: \r\n%s\r\n" % (options[2].strip(), options[3].strip()))
# write to alert log
with open(alertlog, 'ab') as f:
f.write('\n\n')
f.write(header)
f.write(body)
f.write('\n\n')
if alertmail.lower() == 'false':
msg = "alert mail is disable, do nothing"
return False, msg
if len(mailto) == 0:
msg = "none mail recipients given"
return False, msg
try:
server = smtplib.SMTP(mailhub)
server.login(mailuser, mailpwd)
server.sendmail(mailuser, mailto, header + body)
server.quit()
except Exception, e:
msg = 'Send alert email:%s' % str(e.message)
return False, msg
return True, ''
def usage():
u = '''
Name:
%s - dayu email send scripts, act as a command.
Synopsis:
%s [-h]
Description:
Arguments are as following:
-h print the help message
-p content of the mail to send
-m email address to send mail
Example:
python %s -p "INFO|||Smoething|||Nothing|||Just a test" -m "[email protected] [email protected]"
'''
prog = os.path.basename(sys.argv[0])
print u % (prog, prog, prog)
if __name__ == "__main__":
try:
opts,args = getopt.getopt(sys.argv[1:], "hp:m:")
except getopt.GetoptError, msg:
print msg
sys.exit(1)
para = ''
mailto = []
alertmail = 'false'
alertlog = '/var/log/dayu/alert.log'
nodecfg = '/var/local/dayu/nodes.cfg'
try:
f = open(nodecfg, 'r+')
lines = f.readlines()
f.close()
except IOError:
sys.exit(1)
for line in lines:
if line.startswith('EMAIL_ALERT'):
vec = line.split('=')
if len(vec) != 2 :
continue
alertmail = vec[1].strip()
continue
if line.startswith('eenvid'):
vec = line.split('=')
if len(vec) != 2 :
continue
eenvid = vec[1].strip()
continue
if line.startswith('SEND_ALERT_TO'):
vec = line.split('=')
if len(vec) != 2 :
continue
mailto = vec[1].strip().split()
continue
if line.startswith("EMAIL_HUB"):
vec = line.split('=')
if len(vec) != 2 :
continue
mailhub = vec[1].strip()
continue
if line.startswith('EMAIL_USER'):
vec = line.split('=')
if len(vec) != 2 :
continue
mailuser = vec[1].strip()
continue
if line.startswith('EMAIL_PWD'):
vec = line.split('=')
if len(vec) != 2 :
continue
mailpwd = vec[1].strip()
continue
for opt, value in opts:
if opt == "-h":
usage()
sys.exit(0)
elif opt == "-p":
para = value
elif opt == "-m":
if len(value.strip()) != 0:
mailto = value.strip().split()
else:
print "Wrong options!"
sys.exit(1)
if para != "" and mailuser != "" and mailhub != "" and mailpwd != "":
res, msg = dayumail(para, mailto)
if not res:
print msg
sys.exit(1)
else:
print "Wrong parameter"
sys.exit(1)