-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWatch_Dogs-Client.py
475 lines (374 loc) · 13.5 KB
/
Watch_Dogs-Client.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env python
# encoding:utf-8
"""
Watch_Dogs
基于Flask的远程监控客户端
- 来源验证
- 异步响应
"""
import getpass
import functools
import traceback
from tornado.ioloop import IOLoop
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from flask import Flask, request, jsonify
from setting import Setting
from Core.sys_monitor import SysMonitor
from Core.process_manage import ProcManager
from Core.process_monitor import ProcMonitor
from Core.prcess_exception import NoWatchedProcess
app = Flask("Watch_Dogs-Client")
# 全局资源及变量
setting = Setting()
logger = setting.logger
ALLOWED_REQUEST_ADDR = setting.ALLOWED_REQUEST_ADDR_LIST
LINUX_USER = getpass.getuser()
system_monitor = SysMonitor()
process_monitor = ProcMonitor(net_monitor=Setting.NET_MONITOR)
process_manager = ProcManager()
# 初始化监控数据
system_monitor.calc_cpu_percent()
system_monitor.calc_cpu_percent_by_cores()
system_monitor.calc_net_speed()
system_monitor.calc_io_speed()
# log
logger.info("Watch_Dogs-Clinet @ " + str(system_monitor.get_intranet_ip()) + " start at " + setting.get_local_time())
START_TIME = setting.get_local_time()
# 在flask使用装饰器是,需要使用functools.wraps({func_name})以使函数的属性顺利传递给外层的@app.route()
# reference : https://segmentfault.com/a/1190000006658289
# reference : https://stackoverflow.com/questions/31900579/view-function-mapping-is-overwriting-an-
# existing-endpoint-function-when-using-a
def request_source_check(func):
"""装饰器 - 请求地址识别"""
global ALLOWED_REQUEST_ADDR
@functools.wraps(func)
def wrapper(*args, **kw):
# 验证请求地址
if request.remote_addr not in ALLOWED_REQUEST_ADDR and "0.0.0.0" not in ALLOWED_REQUEST_ADDR:
logger.error("Unknown request addr - " + str(request.remote_addr))
return jsonify({"Error": "Unknown request addr - " + str(request.remote_addr)}), 403
try:
res = func(*args, **kw)
except Exception as e:
logger.error("Error " + str(e.__class__) + " | " + e.message)
logger.error("Error details : " + traceback.format_exc())
return jsonify(
{"Error": e.message, "Error type": str(e.__class__), "Error detail": traceback.format_exc()}), 501
return res
return wrapper
# -----index-----
@app.route("/")
@request_source_check
def index():
global LINUX_USER
res = {
"user": LINUX_USER,
"time": setting.get_local_time(),
"nethogs env": process_monitor.is_libnethogs_install(),
"nethogs status": process_monitor.nethogs_running_status,
"start time": START_TIME
}
return jsonify(res)
# -----sys------
@app.route("/sys/info")
@request_source_check
def sys_info():
global system_monitor
logger.info("collect sys info.")
return jsonify(system_monitor.get_sys_info())
@app.route("/sys/loadavg")
@request_source_check
def sys_loadavg():
global system_monitor
logger.info("collect sys loadavg info.")
return jsonify(system_monitor.get_sys_loadavg())
@app.route("/sys/uptime")
@request_source_check
def sys_uptime():
global system_monitor
return jsonify(system_monitor.get_sys_uptime())
@app.route("/sys/cpu/info")
@request_source_check
def sys_cpu_info():
global system_monitor
return jsonify(system_monitor.get_cpu_info())
@app.route("/sys/cpu/percent")
@request_source_check
def sys_cpu_percent():
global system_monitor
return str(system_monitor.calc_cpu_percent())
@app.route("/sys/cpu/percents")
@request_source_check
def sys_cpu_percents():
global system_monitor
return jsonify(system_monitor.calc_cpu_percent_by_cores())
@app.route("/sys/mem/info")
@request_source_check
def sys_mem_info():
global system_monitor
return jsonify(system_monitor.get_mem_info())
@app.route("/sys/mem/size")
@request_source_check
def sys_mem_size():
global system_monitor
return str(system_monitor.get_sys_total_mem())
@app.route("/sys/mem/percent")
@request_source_check
def sys_mem_percent():
global system_monitor
return str(system_monitor.calc_mem_percent())
@app.route("/sys/net")
@request_source_check
def sys_net_percent():
global system_monitor
return jsonify(system_monitor.calc_net_speed())
@app.route("/sys/net/devices")
@request_source_check
def sys_net_devices():
global system_monitor
return jsonify(system_monitor.get_all_net_device())
@app.route("/sys/net/default_device")
@request_source_check
def sys_net_defaultdevice():
global system_monitor
return system_monitor.get_default_net_device()
@app.route("/sys/net/ip")
@request_source_check
def sys_net_ip():
global system_monitor
res = {
"intranet_ip": system_monitor.get_intranet_ip(),
"extranet_ip": system_monitor.get_extranet_ip()
}
return jsonify(res)
@app.route("/sys/io")
@request_source_check
def sys_io():
global system_monitor
return jsonify(system_monitor.calc_io_speed())
@app.route("/sys/disk/stat")
@request_source_check
def sys_disk_stat():
global system_monitor
return jsonify(system_monitor.get_disk_stat())
# -----manage-----
@app.route("/proc/search/<string:key_word>")
@request_source_check
def proc_search(key_word):
global process_manager
search_type = "contain"
if request.args.has_key("type"):
search_type = request.args.get("type")
return jsonify(process_manager.search_pid_by_keyword(key_word, search_type))
@app.route("/proc/kill/<int:pid>")
@request_source_check
def proc_kill(pid):
global process_manager
kill_child = False
kill_process_gourp = False
if request.args.has_key("kill_child"):
kill_child = request.args.get("kill_child")
if request.args.has_key("kill_process_gourp"):
kill_process_gourp = request.args.get("kill_process_gourp")
if not kill_child and kill_process_gourp:
process_manager.kill_process(pid)
else:
process_manager.kill_all_process(pid, kill_child, kill_process_gourp)
@app.route("/proc/start/<string:execute_file_full_path>")
@request_source_check
def proc_start(execute_file_full_path):
global process_manager
pid = process_manager.start_process(execute_file_full_path)
return str(pid)
# -----log-----
@app.route("/log/exist")
@request_source_check
def log_exist():
global process_manager
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
return jsonify(process_manager.is_log_exist(path))
return jsonify({"ERROR": "NO PATH"})
@app.route("/log/size")
@request_source_check
def log_size():
global process_manager
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
return jsonify(process_manager.get_log_size(path))
return jsonify({"ERROR": "NO PATH"})
@app.route("/log/head")
@request_source_check
def log_head():
global process_manager
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
n = 100
if request.args.has_key("n"):
n = int(request.args.get("n").encode('utf-8'))
return jsonify(process_manager.get_log_head(path, n))
return jsonify({"ERROR": "NO PATH"})
@app.route("/log/tail")
@request_source_check
def log_tail():
global process_manager
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
n = 100
if request.args.has_key("n"):
n = int(request.args.get("n").encode('utf-8'))
return jsonify(process_manager.get_log_tail(path, n))
return jsonify({"ERROR": "NO PATH"})
@app.route("/log/last_update_time")
@request_source_check
def log_last_update_time():
global process_manager
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
return str(process_manager.get_log_last_update_time(path))
return jsonify({"ERROR": "NO PATH"})
@app.route("/log/keyword_lines")
@request_source_check
def log_keyword_lines():
global process_manager
if request.args.has_key("path") and request.args.has_key("key_word"):
path = request.args.get("path").encode('utf-8')
kw = request.args.get("key_word").encode('utf-8')
return jsonify(process_manager.get_log_keyword_lines(path, kw))
return jsonify({"ERROR": "NO path & key_word"})
# -----process all-----
@app.route("/proc/<int:pid>/")
@request_source_check
def process_all_info(pid):
"""进程所有信息汇总"""
global process_monitor
if not process_monitor.is_process_watched(pid):
# import! : 如果未被进程初始化, 则初始化之后在进行计算进程数据
process_monitor.watch_process(pid)
# init process data
process_monitor.calc_process_cpu_percent(pid)
process_monitor.calc_process_io_speed(pid)
if process_monitor.net_monitor_ability:
process_monitor.calc_process_net_speed(pid)
logger.info("add process watch pid = {}".format(str(pid)))
logger.info("now watched process list :" + str(process_monitor.get_all_watched_pid()))
res = process_monitor.get_process_info(pid)
res["cpu"] = process_monitor.calc_process_cpu_percent(pid)
res["io"] = process_monitor.calc_process_io_speed(pid)
res["mem"] = process_monitor.get_process_mem(pid)
if process_monitor.nethogs_running_status:
res["net_recent"] = process_monitor.calc_process_net_speed(pid, speed_type="recent")
res["net"] = process_monitor.calc_process_net_speed(pid, speed_type="long")
else: # nethogs error
res["net_recent"] = [-0.1, -0.1]
res["net"] = [-0.1, -0.1]
logger.info("collect process({}) info.".format(str(pid)))
return jsonify(res)
@app.route("/proc/all_pid/")
@request_source_check
def proc_all_pid():
global process_manager
return jsonify(process_manager.get_all_pid())
@app.route("/proc/all_pid_name/")
@request_source_check
def proc_all_pid_name():
global process_manager
return jsonify(process_manager.get_all_pid_name())
# -----watch-----
@app.route("/proc/watch/all")
@request_source_check
def proc_watch_all():
global process_monitor
return jsonify(list(process_monitor.get_all_watched_pid()))
@app.route("/proc/watch/is/<int:pid>")
@request_source_check
def proc_watch_is_pid(pid):
global process_monitor
return str(process_monitor.is_process_watched(pid))
@app.route("/proc/watch/add/<int:pid>", )
@request_source_check
def proc_watch_add_pid(pid):
global process_monitor
if not process_monitor.is_process_watched(pid):
process_monitor.watch_process(pid)
# init process data
process_monitor.calc_process_cpu_percent(pid)
process_monitor.calc_process_io_speed(pid)
if process_monitor.net_monitor_ability:
process_monitor.calc_process_net_speed(pid)
logger.info("add process watch pid = {}".format(str(pid)))
logger.info("now watched process list :" + str(process_monitor.get_all_watched_pid()))
return str(process_monitor.is_process_watched(pid))
@app.route("/proc/watch/remove/<int:pid>")
@request_source_check
def proc_watch_remove_pid(pid):
global process_monitor
process_monitor.remove_watched_process(pid)
logger.info("pid = {} process removed".format(str(pid)))
logger.info("now watched process list :" + str(process_monitor.get_all_watched_pid()))
return str(process_monitor.is_process_watched(pid))
# -----process-----
@app.route("/proc/<int:pid>/info")
@request_source_check
def proc_pid_info(pid):
global process_monitor
return jsonify(process_monitor.get_process_info(pid))
@app.route("/proc/<int:pid>/cpu")
@request_source_check
def proc_pid_cpu(pid):
global process_monitor
return jsonify(process_monitor.calc_process_cpu_percent(pid))
@app.route("/proc/<int:pid>/io")
@request_source_check
def proc_pid_io(pid):
global process_monitor
return jsonify(process_monitor.calc_process_io_speed(pid, style="M"))
@app.route("/proc/<int:pid>/net")
@request_source_check
def proc_pid_net(pid):
global process_monitor
return jsonify(process_monitor.calc_process_net_speed(pid))
@app.route("/proc/<int:pid>/mem")
@request_source_check
def proc_pid_mem(pid):
global process_monitor
return jsonify(process_monitor.get_process_mem(pid))
@app.route("/path/size/total")
@request_source_check
def path_size_total():
global process_monitor
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
return str(process_monitor.get_path_total_size(path))
else:
return jsonify({"ERROR": "NO path"})
@app.route("/path/size/avail")
@request_source_check
def path_size_avail():
global process_monitor
if request.args.has_key("path"):
path = request.args.get("path").encode('utf-8')
return jsonify(process_monitor.get_path_avail_size(path))
else:
return jsonify({"ERROR": "NO path"})
# -----404-----
@app.errorhandler(404)
def api_no_found(e):
return jsonify(
{"ERROR": str(request.url) + " no found! please click https://github.com/Watch-Dogs-HIT/Watch_Dogs-Client"}
), 404
# todo : 直接改用tornado部署Watch_Dogs-Client
if __name__ == "__main__":
# 利用tornado部署flask应用
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(setting.PORT, )
IOLoop.instance().start()
# flask demo
# app.run(
# host="0.0.0.0",
# port=setting.PORT,
# debug=False,
# threaded=True
# )
# bug :一段时间后检测进程占用内存不断上升? 50m -> 600m