-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverse.py
171 lines (150 loc) · 4.59 KB
/
traverse.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#-*- coding: UTF-8 -*-
import poplib
from email import parser
import pysqlite2.dbapi2 as sqlite
import string
import datetime
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import urllib
import time
def time_format(TIME):
ISOTIMEFORMAT = '%Y-%m-%d %X'
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800'
a = datetime.datetime.strptime(TIME,GMT_FORMAT)
return a.strftime(ISOTIMEFORMAT)
def retr_mail(username,password):
"""Login"""
server = 'mail.a.com.cn'
pop = poplib.POP3_SSL(server)
pop.set_debuglevel(1)
pop.user(username)
pop.pass_(password)
"""SQL"""
cx = sqlite.connect('/us/monitor/watchtower.db')
cu = cx.cursor()
"""clear all data before retr mail"""
cu.execute("delete from root_mail")
#cu.execute("select message_id from root_mail order by id desc limit 1")
#last = cu.fetchone()[0]
#print 'last'+last
ret = pop.stat()
print ret
x = range(1,ret[0]+1)
x.reverse()
for i in x:
m = pop.retr(i)
pop.dele(i)
mail_list=[m]
mail_list = ['\n'.join(i[1]) for i in mail_list]
ms = [parser.Parser().parsestr(i) for i in mail_list]
m = ms[0]
message_id = m.get('message-id')
subject = m.get('subject')
date = time_format(m.get('date'))
k = string.find(subject,'Traverse')
if k > 0:
try:
tup = (message_id,date,subject)
cu.execute("insert into root_mail(message_id,date,subject) values (?,?,?)",tup)
print 'insert ok'
except Exception,e:
print e
break
else:
pass
#print subject
print 'finish'
pop.quit()
cx.commit()
def classify():
loss={}
status={}
traffic={}
error={}
cpu={}
'''get all data'''
cx = sqlite.connect('/usr/home/nitor/watchtower.db')
cu = cx.cursor()
cu.execute('delete from root_count')
cu.execute('select subject from root_mail')
data = cu.fetchall()
for i in data:
l = string.find(i[0],'Loss')
s = string.find(i[0],'Status')
t = string.find(i[0],'Traffic')
e = string.find(i[0],'Error')
c = string.find(i[0],'CPU')
if l > 0:
if loss.has_key(i):loss[i]+=1
else: loss[i]=1
if s > 0:
if status.has_key(i):status[i]+=1
else: status[i]=1
if t > 0:
if traffic.has_key(i):traffic[i]+=1
else: traffic[i]=1
if e > 0:
if error.has_key(i):error[i]+=1
else: error[i]=1
if c > 0:
if cpu.has_key(i):cpu[i]+=1
else: cpu[i]=1
for i in loss:
try:
tup = ('loss',i[0],loss[i])
cu.execute("insert into root_count(type,subject,num) values (?,?,?)",tup)
#print 'loss'
except Exception,e:
print e
for i in status:
try:
tup = ('status',i[0],status[i])
cu.execute("insert into root_count(type,subject,num) values (?,?,?)",tup)
#print 'status'
except Exception,e:
pass
for i in traffic:
try:
tup = ('traffic',i[0],traffic[i])
cu.execute("insert into root_count(type,subject,num) values (?,?,?)",tup)
#print 'traffic'
except Exception,e:
pass
for i in error:
try:
tup = ('error',i[0],error[i])
cu.execute("insert into root_count(type,subject,num) values (?,?,?)",tup)
#print 'error'
except Exception,e:
pass
for i in cpu:
try:
tup = ('CPU',i[0],cpu[i])
cu.execute("insert into root_count(type,subject,num) values (?,?,?)",tup)
#print 'error'
except Exception,e:
pass
cx.commit()
def sendmail(user,passwd):
content = '<p>更多请访问 <a href="http://172.1.8.10/traverse/">http://12.6.18.10/traverse/</a><p>'+urllib.urlopen('http://172.16.118.110/traverse/').read().split('<!--content-->')[1]
mail_from='[email protected]'
#mail_to=['[email protected]']
msg=MIMEMultipart('alternative')
msg['subject']=u'[Traverse Alert Summary 每日报警汇总]-'+time.strftime('%Y/%m/%d')
msg['from']='[email protected]'
msg['to']=';'.join(mail_to)
msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')
html=MIMEText(content,'html',_charset='utf8')
msg.attach(html)
smtp=smtplib.SMTP()
smtp.connect('mail.sina.com.cn')
smtp.login(user,passwd)
smtp.sendmail(mail_from,mail_to,msg.as_string())
smtp.quit()
retr_mail('[email protected]','pass')
classify()
sendmail('ab','pass')