This repository was archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.py
254 lines (222 loc) · 6.95 KB
/
send.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python
# encoding:utf-8
import urllib
import urllib2
import time
import xml.dom.minidom
import logging
import logging.config
import json
import os
import sys
import hashlib
import re
reload(sys)
sys.setdefaultencoding('utf8')
os.chdir(sys.path[0])
error = 0
logging.config.fileConfig("logging.conf") # 采用配置文件
logger = logging.getLogger()
# 自定义的异常类
class NetworkError(Exception):
tp = 0
name = ""
def __init__(self, arg1, arg2):
self.tp = arg1
self.name = arg2
ErrorType = {0:'Database Connection Error',1:'Database Query Error',2:'Database Modification Error',
3:'File Deletion Error',4:'File Modification Error',5:'IP Conflict Error',6:'Invalid FIB Error',7:'Invalid Info File Error'}
# 显示提示信息的函数
def warning(errortype):
global error
logger.error("Configuration Error: Parameter " + errortype + "is not set or is not set properly.")
error += 1
# 获得各项设置的函数
def get(type):
global conf,error
tmp = conf.getElementsByTagName(type)
if len(tmp) <= 0 or len(tmp[0].childNodes) <= 0:
warning(type)
return ""
else:
return tmp[0].childNodes[0].data
# **************获取配置参数*******************
def getconf():
global DOMTree,conf,location,ip,longitude,latitude,monitor,receive_FIB_path,send_FIB_path,remark,password
# 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("conf.xml")
conf = DOMTree.documentElement
# 获取Location
location = get("Location")
# 获取IP
ip = get("IP")
# 获取经度
longitude = get("Longitude")
# 获取纬度
latitude = get("Latitude")
# 获取监测站点的url
monitor = "http://" + get("Monitor_Host")
# 获取Receive_FIB_path
receive_FIB_path = get("Receive_FIB_path")
# 获取Send_FIB_path
send_FIB_path = get("Send_FIB_path")
# 获取Remark
remark = get("Remark")
# 获取Password
m = hashlib.md5()
m.update(get("Password"))
password = m.hexdigest()
# **************获取配置参数*******************
# **************获取系统参数*******************
def getsystem():
global mem, cpu, net, uptime, hd
# Memory
mem = {}
f = open("/proc/meminfo")
lines = f.readlines()
f.close()
for line in lines:
if len(line) < 2: continue
name = line.split(':')[0]
var = line.split(':')[1].split()[0]
mem[name] = long(var) * 1024.0
mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']
# CPU
cpu = []
cpuinfo = {}
f = open("/proc/cpuinfo")
lines = f.readlines()
f.close()
for line in lines:
if line == '\n':
cpu.append(cpuinfo)
cpuinfo = {}
if len(line) < 2: continue
name = line.split(':')[0].rstrip()
var = line.split(':')[1]
cpuinfo[name] = var
# uptime
uptime = {}
f = open("/proc/uptime")
con = f.read().split()
f.close()
all_sec = float(con[0])
MINUTE,HOUR,DAY = 60,3600,86400
uptime['day'] = int(all_sec / DAY )
uptime['hour'] = int((all_sec % DAY) / HOUR)
uptime['minute'] = int((all_sec % HOUR) / MINUTE)
uptime['second'] = int(all_sec % MINUTE)
uptime['Free rate'] = float(con[1]) / float(con[0])
# network
net = []
f = open("/proc/net/dev")
lines = f.readlines()
f.close()
for line in lines[2:]:
con = line.split()
"""
intf = {}
intf['interface'] = con[0].lstrip(":")
intf['ReceiveBytes'] = int(con[1])
intf['ReceivePackets'] = int(con[2])
intf['ReceiveErrs'] = int(con[3])
intf['ReceiveDrop'] = int(con[4])
intf['ReceiveFifo'] = int(con[5])
intf['ReceiveFrames'] = int(con[6])
intf['ReceiveCompressed'] = int(con[7])
intf['ReceiveMulticast'] = int(con[8])
intf['TransmitBytes'] = int(con[9])
intf['TransmitPackets'] = int(con[10])
intf['TransmitErrs'] = int(con[11])
intf['TransmitDrop'] = int(con[12])
intf['TransmitFifo'] = int(con[13])
intf['TransmitFrames'] = int(con[14])
intf['TransmitCompressed'] = int(con[15])
intf['TransmitMulticast'] = int(con[16])
"""
intf = dict(
zip(
( 'interface','ReceiveBytes','ReceivePackets',
'ReceiveErrs','ReceiveDrop','ReceiveFifo',
'ReceiveFrames','ReceiveCompressed','ReceiveMulticast',
'TransmitBytes','TransmitPackets','TransmitErrs',
'TransmitDrop', 'TransmitFifo','TransmitFrames',
'TransmitCompressed','TransmitMulticast' ),
( con[0].rstrip(":"),int(con[1]),int(con[2]),
int(con[3]),int(con[4]),int(con[5]),
int(con[6]),int(con[7]),int(con[8]),
int(con[9]),int(con[10]),int(con[11]),
int(con[12]),int(con[13]),int(con[14]),
int(con[15]),int(con[16]), )
)
)
net.append(intf)
# Disk
hd = {}
disk = os.statvfs("/")
hd['available'] = disk.f_bsize * disk.f_bavail
hd['capacity'] = disk.f_bsize * disk.f_blocks
hd['used'] = disk.f_bsize * disk.f_bfree
# **************获取系统参数*******************
def sendpost(url, data, save_location=''):
data_urlencode = urllib.urlencode(data)
req = urllib2.Request(url=url, data=data_urlencode)
# print req # 若不想打印信息可以注释此行
res_data = urllib2.urlopen(req)
res = res_data.read()
if save_location != '':
with open(save_location, "wb") as code:
code.write(res)
else:
fail=json.loads(res)
if fail['Fail'] != 0:
raise NetworkError(fail['Type'],os.path.basename(url))
while True:
time.sleep(2) # time
try:
getconf()
if error == 0:
# 发给alive的部分
alive_data = {'event': 'sendalive'}
alive_data['ip'] = ip # 发给alive.php的IP地址
alive_data['longitude'] = longitude
alive_data['latitude'] = latitude
alive_data['location'] = location # 发给alive.php的Location
alive_data['remark'] = remark
alive_data['password'] = password
getsystem()
alive_data['system'] = str(cpu)+'\n'+str(mem)+'\n'+str(hd)+'\n'+str(net)+'\n'+str(uptime)+'\n'
alive_url = monitor + "/alive.php" # alive_url里存的是alive.php的URL
sendpost(alive_url, alive_data)
# 发给fibdown的部分
fibdown_data = {'event': 'sendfibdown'}
fibdown_data['ip'] = ip # 发给fibdown.php的IP地址
fibdown_url = monitor + "/FIBdown.php" # fibdown_url里存的是FIBdown.php的URL
save_location = receive_FIB_path # save_location里存的是接收到的FIB表的存放路径和存放文件名
sendpost(fibdown_url, fibdown_data, save_location)
# 发给fibup的部分
fib_location = send_FIB_path # fib_location里存的是要上传的FIB表的位置
fibup_url = monitor + "/FIBup.php" # fib_url里存的是FIBup.php的URL
with open(fib_location) as fib:
fibup_data = {'event': 'sendfibup', 'file': fib.read(), 'ip': ip}
sendpost(fibup_url, fibup_data)
except NetworkError, e:
logger.error('PHP Feedback Error: In '+str(e.name)+": "+ErrorType[e.tp])
continue
except urllib2.HTTPError, e:
logger.error('HTTP Error[%s]' % str(e.code))
continue
except urllib2.URLError, e:
logger.error('URL Error: '+str(e.reason))
continue
except xml.dom.DOMException, e:
logger.error('XML Error:'+str(e))
continue
except IOError, e:
logger.error('File Error:'+str(e))
continue
except KeyboardInterrupt:
break
except Exception,e:
logger.error('Other Error: '+str(e))
continue