-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.py
57 lines (49 loc) · 1.73 KB
/
watch.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os,time,csv
now = time.strftime('%Y/%m/%d-%H:%M',time.localtime(time.time()))
today = time.strftime('%Y%m%d',time.localtime(time.time()))
idc = 'ja'
ip_list =[]
ips = open('/usr/home/sunran/csv/ip.list','r')
for i in ips:
ip_list.append(i.strip())
csvfile = '/usr/home/sunran/monitor/static/'+idc+'-'+today+'.csv'
try:
blacklist = open('/usr/home/sunran/csv/black.list','r')
except:
os.mknod('/usr/home/sunran/csv/black.list')
blacklist = open('/usr/home/sunran/csv/black.list','r')
blacklist = [i.split()[0].strip() for i in blacklist]
count = '10'
exclude = open('/usr/home/sunran/csv/exclude','r')
for i in exclude:
blacklist.append(i.strip())
print blacklist
write_line = [now]
for ip in ip_list:
data = os.popen('ping '+ip+' -c '+count+' -i 0.2').read()
loss = data.split('%')[0].split(' ')[-1]
try:
delay = data.split('min/avg/max/mdev =')[1].split('/')[1]
except:
delay = 'NONE'
write_line.append(loss)
print ip,loss
if int(loss) >= 30 and ip not in blacklist:
print os.popen('''/aaa/bin/nms-alert.py -m "IP:'''+ip+'''\n丢包率:'''+loss+'''%\n延时:'''+delay+''' ms" -r nms -R -s " " -a 'email,sms' ''').read()
os.system('echo '+ip+' auto >> /usr/home/sunran/csv/black.list')
print ip
try:
reader = csv.reader(file(csvfile,'rb'))
writer = csv.writer(file(csvfile,'ab'))
writer.writerow(write_line)
except:
writer = csv.writer(file(csvfile,'wb'))
ip_list.insert(0,'Time')
filedname = ip_list
writer.writerow(filedname)
writer = csv.writer(file(csvfile,'ab'))
writer.writerow(write_line)
if now.endswith('0'):
os.system('cat /dev/null > /usr/home/sunran/csv/black.list')