-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.py
396 lines (347 loc) · 12.5 KB
/
loop.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# encoding: utf-8
from autobahn.twisted.websocket import WebSocketClientProtocol,WebSocketClientFactory,connectWS
from twisted.internet import reactor,threads
from twisted.internet.protocol import ReconnectingClientFactory
# from twisted.internet.utils import getProcessOutput
from tasks import CmdTask,tracerouteToDest
import os
import json
import psutil
import config
from config import *
#from snmp import hostSysInfo
import time
from utils import *
from arp import getArpInfo
from task_process import *
from threading import Thread
#import msghandler as gitutil
from msghandler import gitutil
import subprocess
import constants
import datetime
HEARTBEAT_INTERVAL = 60
runningTask={}
arpNeighbors = getArpInfo()
lastNetIo = {"sent":0,"recv":0}
PIPE = None
global facotry
facotry = None
try :
version = json.load(open('version.json','r'))['version']
except Exception,e :
version = constants.SYS_VERSION
def sendInfoToPipe(info):
if PIPE :
PIPE.send(info)
else:
print 'no pipe'
def executeTask(task):
#print 'executeTask',task
taskHandler = CmdTask(os.path.join(os.path.dirname(__file__),'optypes.json'))
if task['taskRecordId'] not in runningTask:
runningTask[task['taskRecordId']] = {}
runningTask[task['taskRecordId']][task['taskId']]=taskHandler
else:
runningTask[task['taskRecordId']][task['taskId']]=taskHandler
# print runningTask
optype = task['optype']
params = task
if task.get('timeout'):
timeout = task['timeout']
else:
timeout = 20
result = taskHandler.run(optype,params,timeout)
if task['optype'] == 'ping':
rawresult,traceresult = tracerouteToDest(task['destination'])
result['endTime'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if traceresult:
result['trace_result'] = traceresult
result['trace_rawresult'] = rawresult
return result
def executeTraceroute(task):
result = {}
result['sched_type'] = task['sched_type']
result['optype'] = task['optype']
result['taskId'] = task['taskId']
result['clientId'] = task['clientId']
result['destination'] = task['destination']
result['createTime'] = task['createTime']
result['startTime'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
result['taskRecordId'] = task.get('taskRecordId')
result['jobId'] = task.get('jobId')
result['interval'] = task.get('interval')
result['type'] = task.get('type')
rawresult,traceresult = tracerouteToDest(task['destination'])
result['endTime'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if traceresult:
result['status'] = 'success'
result['trace_result'] = traceresult
result['trace_rawresult'] = rawresult
else:
result['status'] = 'failed'
result['trace_result'] = []
result['trace_rawresult'] = ''
return result
def arpUpdate(data=None):
if data :
arpNeighbors = data
return
d = threads.deferToThread(getArpInfo)
d.addCallback(arpUpdate)
# reactor.callLater(10*60,arpUpdate)
def getSysInfo(id):
# hostInfo=None
# if hostAddr:
# hostInfo = hostSysInfo(self.hostAddr)
global version
cpu_percent = psutil.cpu_percent(interval=None)
mem_usage = psutil.virtual_memory().percent
license_key = os.getenv('license_key')
if NETWORK_INTERFACE in psutil.net_if_addrs():
ip = psutil.net_if_addrs()[NETWORK_INTERFACE][0].address
mask = psutil.net_if_addrs()[NETWORK_INTERFACE][0].netmask
mac = psutil.net_if_addrs()[NETWORK_INTERFACE][getMacAddressIndex()].address
ipv6address = getIpv6Address()
else:
ip = None
mask = None
mac = None
ipv6address = []
bytes_sent = psutil.net_io_counters().bytes_sent
bytes_recv = psutil.net_io_counters().bytes_recv
bytes_sent_avg = round(float(bytes_sent-lastNetIo["sent"])/HEARTBEAT_INTERVAL/1024,2) if bytes_sent-lastNetIo["sent"] > 0 else 0 #Kbytes/s
bytes_recv_avg = round(float(bytes_recv-lastNetIo["recv"])/HEARTBEAT_INTERVAL/1024,2) if bytes_recv-lastNetIo["recv"] > 0 else 0 #Kbytes/s
lastNetIo["sent"] = bytes_sent
lastNetIo["recv"] = bytes_recv
task_processes = getTaskProcessInfo()
topTotal,processs = getProgress()
probeInfo = dict(cpu = cpu_percent, memory = mem_usage,ipaddress=ip,device_type = 'soft',ipv6address=ipv6address,toptotal=topTotal,topprocess=processs,license_key=license_key,netmask=mask,macaddress=mac,arpNeighbors=arpNeighbors,bytesSent=bytes_sent_avg,bytesRecv=bytes_recv_avg,task_processes=task_processes)
result = dict(clientId=id,type="heartbeat",time=time.time(),content=probeInfo)
#result['currentVersion'] = gitutil.getCurrentVersion(config.APPLICATION_PATH).__str__()
result['currentVersion'] = version
return result
def getIpv6Address():
ipv6 = []
for index in range(len(psutil.net_if_addrs()[NETWORK_INTERFACE])):
address = psutil.net_if_addrs()[NETWORK_INTERFACE][index].address
if index == 0 or 'fe80::' in address or len(address) == 17:
pass
else:
ipv6.append(address)
return ipv6
def getProgress():
return '',''
cmd = 'top -bn 1'
cmd = shlex.split(cmd.strip())
pipe = subprocess.Popen(cmd,stdin = subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
i = 0
while i < 10:
if pipe.poll() is None:
time.sleep(5)
i += 1
else:
stdoutdata,stderrordata = pipe.communicate()
break
else:
return '','{}'
return sortAndGetMaxCpu(stdoutdata)
#按照cpu排序,如果剩余的cpu为0.0,则继续按照内存排序,返回CPU内存利用率前15名
def sortAndGetMaxCpu(out):
lines = out.split('\n')
topTotal = ''
processs = []
for i in range(5):
topTotal +=lines[i]+'\n'
lines = lines[7::]
#去除cpu 内存利用率都为0.0的进程
for line in lines:
process = line.split(' ')
process = [one for one in process if one !='']
if len(line)!=0 and (process[8]!='0.0' or process[9]!='0.0'):
processs.append(process)
#排序
result = []
sort = 8
nextsort = 9
for i in range(15):
max = processs[0][sort]
maxprocess = processs[0]
for process in processs:
if process[sort]>max:
max = process[sort]
maxprocess = process
processs.remove(maxprocess)
if max == '0.0':#当剩余的cpu利用率都为0.0则按照内存排序
sort = nextsort
#取有用的参数
process = {}
process['user'] = maxprocess[1]
process['cpu_percent'] = maxprocess[8]
process['mem_usage'] = maxprocess[9]
process['process'] = maxprocess[11]
result.append(process)
return topTotal,result
class ProbeWebsocketClientProtocol(WebSocketClientProtocol):
needReconnection = False
def onConnect(self,response):
self.hostAddr=''
self.id=''
psutil.cpu_percent(interval=None)
# pass
def onMessage(self,payload,isBinary):
task = json.loads(payload)
if task.get('type') == 'yourId':
if task['content'] :
self.id = task['content']['id']
self.hostAddr=task['content']['hostAddr']
if self.id == '':
self.sendMessage(json.dumps(getSysInfo(self.id)))
else:
time.sleep(1)
self.sendHeartBeat()
#elif task.get('type') == 'version' :
#print 'send info to pipe '
#sendInfoToPipe(task)
elif task.get('type') == 'update' :
gitutil.updateToLatestVersion()
elif task.get('type') == 'reboot' :
print 'reboot now'
#sendInfoToPipe(task)
gitutil.reboot()
elif 'optype' in task:
d = threads.deferToThread(executeTask,task)
# d = defer.execute(executeTask,task)
d.addCallback(self.callBack)
#if task['optype'] == 'ping':
#print task
#d = threads.deferToThread(executeTraceroute,task)
#d.addCallback(self.callBack)
# d.addErrback(self.errorBack)
elif task.get('type') == 'stop':
if task.get('taskRecordId'):
# print task['taskRecordId']
self.terminate(task['taskRecordId'])
elif task.get('type') == 'reverseSSH' :
print 'reverseSSH'
gitutil.reverseSSH(config.REVERSE_SSH_PORT,config.REMOTE_SERVER_IP,config.REMOTE_KEY_PATH)
elif task.get('type') == 'killreverseSSH':
gitutil.killReverseSSH()
def onOpen(self):
self.needReconnection = False
def onClose(self,wasClean,code,reason):
print 'connection close'
def callBack(self,result):
if result['taskRecordId'] in runningTask:
if result['taskId'] in runningTask[result['taskRecordId']]:
del runningTask[result['taskRecordId']][result['taskId']]
if not runningTask[result['taskRecordId']]:
del runningTask[result['taskRecordId']]
print result
# self.sendMessage(json.dumps(result))
self._whether_error_exists(result)
def sendHeartBeat(self):
# d = threads.deferToThread(getSysInfo,self.id,self.hostAddr)
# d.addCallback(self.callBack)
result = getSysInfo(self.id)
#print result
self.sendMessage(json.dumps(result))
reactor.callLater(HEARTBEAT_INTERVAL,self.sendHeartBeat)
def sendHostInfo(self):
# hostInfo = hostSysInfo(self.hostAddr)
# result = dict(clientId = self.id, type = "hostInfo", content = hostInfo,time=time.time())
# print result
# self.sendMessage(json.dumps(result))
# reactor.callLater(2*60,self.sendHostInfo)
pass
def terminate(self,taskRecordId):
if taskRecordId in runningTask:
for taskHandler in runningTask[taskRecordId].values():
taskHandler.terminate = True
else:
print "the task was already terminated"
# self.sendMessage(json.dumps(dict(status="fail",clientId=self.id,taskId=taskId,content="the task was already terminated")))
def _whether_error_exists(self,result):
is_error_exists = False
if not result.get('optype') or result['optype'] == 'iperf-s' or result['optype'] == 'iperf-close':
self.sendMessage(json.dumps(result))
return
if result['status'] == 'fail' :
is_error_exists = True
elif result['type'] == 'ttl' :
if not result['result']['rtt'] or (float(result['result']['rtt']) >= result['threshold'] ):
is_error_exists = True
elif result['type'] == 'loss' :
if not result['result']['packet loss'] or (float(result['result']['rtt']) >= result['threshold']) :
is_error_exists = True
elif result['type'] == 'bandwidth' :
if not result['result']['bandwidth'] :
is_error_exists = True
else :
bandwidth = result['result']['bandwidth'].split(' ')[0]
if float(bandwidth) < result['threshold'] :
is_error_exists = True
# if is_error_exists :
# if not result['status'] == 'fail' :
# result['status'] = 'abnormal'
# d = threads.deferToThread(executeTraceroute,result)
# d.addCallback(lambda x : self.sendMessage(json.dumps(x)))
# else :
self.sendMessage(json.dumps(result))
class MyWebSocketClientFactory(WebSocketClientFactory,ReconnectingClientFactory):
protocolInstance = None
def buildProtocol(self,address):
proto = WebSocketClientFactory.buildProtocol(self,address)
self.protocolInstance = proto
return proto
def clientConnectionLost(self,connector,reason):
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self,connector,reason):
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def sendMessage(self,message):
if self.protocolInstance :
if isinstance(message,dict):
self.protocolInstance.sendMessage(json.dumps(message,ensure_ascii=False))
elif isinstance(message,str) or isinstance(message,unicode) :
self.protocolInstance.sendMessage(message)
#facotry = WebSocketClientFactory("ws://210.25.137.240:8088/worker")
def handlerMessage():
while True :
msg = PIPE.recv()
trigger = msg.get('trigger')
if not facotry :
return
if trigger['type'] == 'version' :
reply = {}
reply['type'] = trigger['type']
reply['op'] = trigger['op']
reply['currentVersion'] = msg.get('extra').get('currentVersion')
if msg.get('type') == 'op_status' :
reply['status'] = msg.get('result')
else :
data = msg.get('value')
if data :
reply['status'] = 'success'
reply['data'] = data
else :
reply['data'] = None
reply['status'] = False
facotry.sendMessage(reply)
def operate_conn(serverAddr,pipe=None):
'''
operate the conmunicate with server
'''
globals()['PIPE'] = pipe # change the global variable PIPE
print 'pipe is ',pipe
if pipe :
print 'monitor start'
monitor = Thread(target = handlerMessage)
monitor.daemon = True
monitor.start()
facotry = MyWebSocketClientFactory(serverAddr)
facotry.protocol = ProbeWebsocketClientProtocol
connectWS(facotry)
arpUpdate()
reactor.run()
if __name__ == '__main__' :
operate_conn("ws://210.25.137.240:8088/worker")