File size: 37,676 Bytes
17e971c | 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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | # coding: utf-8
# -------------------------------------------------------------------
# 宝塔Linux面板
# -------------------------------------------------------------------
# Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# -------------------------------------------------------------------
# Author: sww <sww@bt.cn>
# -------------------------------------------------------------------
import json
import os
# ------------------------------
# 进程模型
# ------------------------------
import sys
import time
import traceback
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
os.chdir("/www/server/panel")
import public
import psutil
from typing import Any
try:
from BTPanel import cache
except:
import cachelib
cache = cachelib.SimpleCache()
class RealProcess:
process_path = '/proc'
ps = json.loads(public.readFile('/www/server/panel/mod/base/process/process_ps.json'))
__isUfw = False
__isFirewalld = False
old_info = {}
new_info = {}
old_path = '/tmp/bt_task_old1.json'
__cpu_time = None
__process_net_list = {}
last_net_process = None
last_net_process_time = 0
old_net_path = '/tmp/bt_network_old1.json'
old_net_info = {}
new_net_info = {}
def __init__(self):
if os.path.exists('/usr/sbin/firewalld'): self.__isFirewalld = True
if os.path.exists('/usr/sbin/ufw'): self.__isUfw = True
def object_to_dict(self, obj):
result = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswith('__') and not callable(value) and not name.startswith('_'): result[name] = value
return result
def get_computers_use(self):
result = {}
cpu_usage = psutil.cpu_percent(interval=1, percpu=True)
result['cpu'] = round(sum(cpu_usage) / len(cpu_usage), 2)
memory = psutil.virtual_memory()
print(memory.total)
result['memory_usage'] = memory.percent
disk = psutil.disk_usage('/')
result['disk_usage'] = round(((disk.used / disk.total) * 100), 0)
network_io = psutil.net_io_counters()
result['network_io_bytes_sent'] = network_io.bytes_sent
result['network_io_bytes_recv'] = network_io.bytes_recv
return result
# ------------------------------ 获取进程列表 start ------------------------------
def get_process_list(self):
"""
获取进程列表
:return:
"""
try:
process_list = []
if type(self.new_info) != dict: self.new_info = {}
self.new_info['cpu_time'] = self.get_cpu_time()
self.new_info['time'] = time.time()
self.get_process_net_list()
for proc in psutil.process_iter(
['pid', 'ppid', 'name', 'username', 'create_time', 'memory_info', 'io_counters', 'num_threads', 'create_time', 'connections', 'open_files', 'status', 'cmdline']):
try:
proc_info = proc.as_dict(
attrs=['pid', 'ppid', 'name', 'username', 'create_time', 'memory_info', 'io_counters', 'num_threads', 'create_time', 'connections', 'open_files', 'status',
'cmdline'])
p_cpus = proc.cpu_times()
process_list.append({
'pid': proc_info['pid'],
'ppid': proc_info['ppid'],
'name': proc_info['name'],
'username': proc_info['username'],
'cpu_percent': self.get_cpu_percent(str(proc_info['pid']), p_cpus, self.new_info['cpu_time']),
'running_time': time.time() - proc_info['create_time'],
'memory_info': proc_info['memory_info'],
'io_info': proc_info['io_counters'],
'num_threads': proc_info['num_threads'],
'create_time': proc_info['create_time'],
'connections_info': proc_info['connections'],
'open_files': proc_info['open_files'],
'ps': self.get_process_ps(proc.name())['data'],
'status': proc_info['status'],
'cmdline': proc_info['cmdline'],
'net_info': self.get_process_network(proc_info['pid'])
})
cache.set(self.old_path, self.new_info, 600)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=process_list)
except Exception as e:
return public.returnResult(code=0, msg='获取进程列表失败' + str(e), status=False)
# ------------------------------ 获取进程列表 end ------------------------------
# ------------------------------ 获取进程信息 start ------------------------------
@staticmethod
def _format_connections(connects):
result = []
for i in connects:
r_addr = i.raddr
if not i.raddr:
r_addr = ('', 0)
l_addr = i.laddr
if not i.laddr:
l_addr = ('', 0)
result.append({
"fd": i.fd,
"family": i.family,
"local_addr": l_addr[0],
"local_port": l_addr[1],
"client_addr": r_addr[0],
"client_rport": r_addr[1],
"status": i.status
})
return result
@staticmethod
def get_connects(pid: str):
'''
@name 获取进程连接信息
@author hwliang<2021-08-09>
@param pid<int>
@return dict
'''
connects = 0
try:
if pid == 1:
return connects
tp = '/proc/' + str(pid) + '/fd/'
if not os.path.exists(tp):
return connects
for d in os.listdir(tp):
f_name = tp + d
if os.path.islink(f_name):
l = os.readlink(f_name)
if l.find('socket:') != -1:
connects += 1
except:
pass
return connects
def get_process_info_by_pid(self, pid: int) -> dict:
"""
获取进程信息
:param pid:
:return:
"""
try:
status_ps = {'sleeping': '睡眠', 'running': '活动'}
process = psutil.Process(int(pid))
if type(self.new_info) != dict: self.new_info = {}
self.new_info['cpu_time'] = self.get_cpu_time()
self.new_info['time'] = time.time()
self.get_process_net_list()
p_cpus = process.cpu_times()
# 获取连接信息
connections = process.connections()
p_mem = process.memory_full_info()
io_info = process.io_counters()
info = {
'pid': process.pid,
'ppid': process.ppid(),
'name': process.name(),
'threads': process.num_threads(),
'user': process.username(),
'username': process.username(),
'cpu_percent': self.get_cpu_percent(process.pid, p_cpus, self.new_info['cpu_time']),
'memory_info': self.object_to_dict(p_mem),
'memory_used': p_mem.uss,
'io_info': self.object_to_dict(io_info),
"io_write_bytes": io_info.write_bytes,
"io_read_bytes": io_info.read_bytes,
'connections': self._format_connections(connections),
"connects": self.get_connects(str(process.pid)),
'status': status_ps[process.status()] if process.status() in status_ps else process.status(),
'create_time': process.create_time(),
'running_time': process.cpu_times().user + process.cpu_times().system,
'cmdline': process.cmdline(),
'open_files': [self.object_to_dict(i) for i in process.open_files()],
'ps': self.get_process_ps(process.name())['data'],
'net_info': self.get_process_network(process.pid),
"exe": ' '.join(process.cmdline()),
}
cache.set(self.old_path, self.new_info, 600)
return public.returnResult(code=1, msg='success', status=True, data=info)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
# 通过name获取进程信息
def get_process_info_by_name(self, name: str) -> dict:
"""
通过name获取进程信息
:param name:
:return:
"""
try:
pids = [i.pid for i in psutil.process_iter(['pid', 'name', 'cmdline']) if i.name() == name]
infos = []
for pid in pids:
try:
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
# 通过启动命令获取进程信息
def get_process_info_by_exec(self, cli: str) -> dict:
"""
通过启动命令获取进程信息
:param cli:启动命令
:return:
"""
try:
pids = [i.pid for i in psutil.process_iter(['pid', 'cmdline']) if cli in ' '.join(i.cmdline())]
infos = []
for pid in pids:
try:
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
def get_process_info_by_port(self, port: int) -> dict:
"""
通过端口获取进程信息
:param port:
:return:
"""
try:
infos = []
for i in psutil.process_iter(['pid', 'connections']):
for conn in i.connections():
try:
if conn.laddr.port == int(port):
info = self.get_process_info_by_pid(i.pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
def get_process_info_by_ip(self, ip: str) -> dict:
"""
通过远程ip获取进程信息
:param ip:
:return:
"""
infos = []
try:
for i in psutil.process_iter(['pid', 'connections']):
for conn in i.connections():
try:
if conn.raddr:
if conn.raddr.ip == ip:
info = self.get_process_info_by_pid(i.pid)['data']
if info:
infos.append(info)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except:
return public.returnResult(code=0, msg='获取进程信息失败', status=False, data=infos)
def get_process_info_by_openfile(self, file_path: str) -> dict:
"""
通过打开文件获取进程信息
:param file_path:
:return:
"""
infos = []
try:
for i in psutil.process_iter(['pid', 'open_files']):
try:
for file in i.open_files():
if file.path == file_path:
info = self.get_process_info_by_pid(i.pid)['data']
if info:
infos.append(info)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except:
return public.returnResult(code=0, msg='获取进程信息失败', status=False, data=infos)
# ------------------------------ 获取进程信息 end ------------------------------
# ------------------------------ 获取进程ps start ------------------------------
def get_process_ps(self, name: str) -> dict:
"""
获取进程ps
:param name:
:return:
"""
return public.returnResult(code=1, msg='success', status=True, data=self.ps.get(name, '未知进程'))
# ------------------------------ 获取进程ps end ------------------------------
# ------------------------------ 获取进程树 start ------------------------------
def get_process_tree(self, pid: int) -> dict:
"""
获取进程树
:param pid:
:return:
"""
try:
pid = int(pid)
process = psutil.Process(pid)
process_tree = process.children(recursive=True)
infos = []
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
for prc in process_tree:
info = self.get_process_info_by_pid(prc.pid)
if info['status']:
infos.append(info['data'])
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程树失败' + str(e), status=False)
# ------------------------------ 获取进程树 end ------------------------------
# ------------------------------ 结束进程 start ------------------------------
# 结束进程pid
def kill_pid(self, pid: int) -> dict:
"""
通过关闭进程
:param pid:
:return:
"""
try:
os.kill(pid, 9)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
public.ExecShell('kill -9 ' + str(pid))
return public.returnResult(code=1, msg='结束进程失败' + str(e), status=True)
# 结束进程名
def kill_name(self, name: str) -> dict:
"""
通过name关闭进程
:param name:
:return:
"""
try:
os.system('killall ' + name)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='结束进程失败' + str(e), status=False)
# 结束进程树
def kill_tree(self, pid: int) -> dict:
"""
通过关闭进程树
:param pid:
:return:
"""
try:
p = psutil.Process(pid)
p.kill()
for i in p.children(recursive=True):
i.kill()
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
public.ExecShell('kill -9 ' + str(pid))
return public.returnResult(code=1, msg='success', status=True)
# 结束所有进程 pid,进程名,进程树
def kill_proc_all(self, pid: int) -> dict:
"""
结束所有进程
:return:
"""
try:
proc = psutil.Process(pid)
name = proc.name()
self.kill_pid(pid)
self.kill_name(name)
self.kill_tree(pid)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='结束进程失败' + str(e), status=False)
def kill_port(self, port: str) -> dict:
"""
结束端口进程
:param port:
:return:
"""
for process in psutil.process_iter(['pid', 'name', 'connections']):
try:
for conn in process.connections():
if conn.laddr.port == int(port):
self.kill_pid(process.pid)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data='')
# ------------------------------ 结束进程 end ------------------------------
# ------------------------------ 拉黑ip start ------------------------------
def add_black_ip(self, ips: list, ) -> dict:
"""
拉黑ip
:param ip:
:return:
"""
try:
if not public.get_firewall_status() == 1: return public.returnMsg(False, '当前系统防火墙未开启')
if [ip for ip in ips if ip in ['0.0.0.0', '127.0.0.0', "::1"]]: return {'status': False, 'msg': '禁止拉黑本机ip', 'data': ''}
for ip in ips:
if not public.check_ip(ip): continue
if public.M('firewall_ip').where("port=?", (ip,)).count() > 0: continue
if self.__isUfw:
if public.is_ipv6(ip):
public.ExecShell('ufw deny from ' + ip + ' to any')
else:
public.ExecShell('ufw insert 1 deny from ' + ip + ' to any')
else:
if self.__isFirewalld:
if public.is_ipv6(ip):
public.ExecShell('firewall-cmd --permanent --add-rich-rule=\'rule family=ipv6 source address="' + ip + '" drop\'')
else:
public.ExecShell('firewall-cmd --permanent --add-rich-rule=\'rule family=ipv4 source address="' + ip + '" drop\'')
else:
if public.is_ipv6(ip): return public.returnMsg(False, 'FIREWALL_IP_FORMAT')
public.ExecShell('iptables -I INPUT -s ' + ip + ' -j DROP')
addtime = time.strftime('%Y-%m-%d %X', time.localtime())
public.M('firewall_ip').add('address,addtime,types', (ip, addtime, 'drop'))
self.firewall_reload()
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='拉黑失败' + str(e), status=False)
# ------------------------------ 拉黑ip end ------------------------------
# ------------------------------ 取消拉黑ip start ------------------------------
# 删除IP屏蔽
def del_black_ip(self, ips: list) -> dict:
try:
if not public.get_firewall_status() == 1: return public.returnMsg(False, '当前系统防火墙未开启')
for ip in ips:
if not public.check_ip(ip): continue
if self.__isUfw:
public.ExecShell('ufw delete deny from ' + ip + ' to any')
else:
if self.__isFirewalld:
if public.is_ipv6(ip):
public.ExecShell('firewall-cmd --permanent --remove-rich-rule=\'rule family=ipv6 source address="' + ip + '" drop\'')
else:
public.ExecShell('firewall-cmd --permanent --remove-rich-rule=\'rule family=ipv4 source address="' + ip + '" drop\'')
else:
public.ExecShell('iptables -D INPUT -s ' + ip + ' -j DROP')
public.WriteLog("TYPE_FIREWALL", 'FIREWALL_ACCEPT_IP', (ip,))
public.M('firewall_ip').where("address=?", (ip,)).delete()
self.firewall_reload()
return public.returnResult(code=1, msg='success', status=True)
except Exception as e:
return public.returnResult(code=0, msg='删除失败' + str(e), status=False)
# 重载防火墙配置
def firewall_reload(self):
try:
if self.__isUfw:
public.ExecShell('/usr/sbin/ufw reload &')
return public.returnResult(code=1, msg='success', status=True)
if self.__isFirewalld:
public.ExecShell('firewall-cmd --reload &')
else:
public.ExecShell('/etc/init.d/iptables save &')
public.ExecShell('/etc/init.d/iptables restart &')
return public.returnResult(code=1, msg='success', status=True)
except:
return public.returnResult(code=0, msg='重载防火墙失败', status=False)
# ------------------------------ 取消拉黑ip end ------------------------------
# ------------------------------ 获取进程cpu start ------------------------------
# 获取cpu使用率
def get_cpu_percent(self, pid, cpu_times, cpu_time):
self.get_old()
percent = 0.00
process_cpu_time = self.get_process_cpu_time(cpu_times)
if not self.old_info: self.old_info = {}
if not pid in self.old_info:
self.new_info[pid] = {}
self.new_info[pid]['cpu_time'] = process_cpu_time
return percent
try:
percent = round(
100.00 * (process_cpu_time - self.old_info[pid]['cpu_time']) / (cpu_time - self.old_info['cpu_time']), 2)
except:
return 0
self.new_info[pid] = {}
self.new_info[pid]['cpu_time'] = process_cpu_time
if percent > 0: return percent
return 0.00
def get_process_cpu_time(self, cpu_times):
cpu_time = 0.00
for s in cpu_times: cpu_time += s
return cpu_time
def get_old(self):
if self.old_info: return True
data = cache.get(self.old_path)
if not data: return False
self.old_info = data
del (data)
return True
def get_cpu_time(self):
if self.__cpu_time: return self.__cpu_time
self.__cpu_time = 0.00
s = psutil.cpu_times()
self.__cpu_time = s.user + s.system + s.nice + s.idle
return self.__cpu_time
# ------------------------------ 获取进程cpu end ------------------------------
# ------------------------------ 获取进程net start ------------------------------
def get_process_network(self, pid):
'''
@name 获取进程网络流量
@author hwliang<2021-09-13>
@param pid<int> 进程ID
@return tuple
'''
if not self.__process_net_list:
self.get_process_net_list()
if not self.last_net_process_time: return 0, 0, 0, 0
if not pid in self.__process_net_list: return 0, 0, 0, 0
if not pid in self.last_net_process:
return self.__process_net_list[pid]['up'], self.__process_net_list[pid]['up_package'], \
self.__process_net_list[pid]['down'], self.__process_net_list[pid]['down_package']
up = int((self.__process_net_list[pid]['up'] - self.last_net_process[pid]['up']) / (
time.time() - self.last_net_process_time))
down = int((self.__process_net_list[pid]['down'] - self.last_net_process[pid]['down']) / (
time.time() - self.last_net_process_time))
up_package = int((self.__process_net_list[pid]['up_package'] - self.last_net_process[pid]['up_package']) / (
time.time() - self.last_net_process_time))
down_package = int(
(self.__process_net_list[pid]['down_package'] - self.last_net_process[pid]['down_package']) / (
time.time() - self.last_net_process_time))
return up, up_package, down, down_package
# 2026/02/05 使用中并不展示进程的网络, 返回空
def get_process_net_list(self):
return
w_file = '/dev/shm/bt_net_process'
if not os.path.exists(w_file): return
self.last_net_process = cache.get('net_process')
self.last_net_process_time = cache.get('last_net_process')
net_process_body = public.readFile(w_file)
if not net_process_body: return
net_process = net_process_body.split('\n')
for np in net_process:
if not np: continue
tmp = {}
np_list = np.split()
if len(np_list) < 5: continue
tmp['pid'] = int(np_list[0])
tmp['down'] = int(np_list[1])
tmp['up'] = int(np_list[2])
tmp['down_package'] = int(np_list[3])
tmp['up_package'] = int(np_list[4])
self.__process_net_list[tmp['pid']] = tmp
cache.set('net_process', self.__process_net_list, 600)
cache.set('last_net_process', time.time(), 600)
def get_network(self):
try:
self.get_net_old()
networkIo = psutil.net_io_counters()[:4]
self.new_net_info['upTotal'] = networkIo[0]
self.new_net_info['downTotal'] = networkIo[1]
self.new_net_info['upPackets'] = networkIo[2]
self.new_net_info['downPackets'] = networkIo[3]
self.new_net_info['time'] = time.time()
if not self.old_net_info: self.old_net_info = {}
if not 'upTotal' in self.old_net_info:
time.sleep(0.1)
networkIo = psutil.net_io_counters()[:4]
self.old_net_info['upTotal'] = networkIo[0]
self.old_net_info['downTotal'] = networkIo[1]
self.old_net_info['upPackets'] = networkIo[2]
self.old_net_info['downPackets'] = networkIo[3]
self.old_net_info['time'] = time.time()
s = self.new_net_info['time'] - self.old_net_info['time']
networkInfo = {}
networkInfo['upTotal'] = networkIo[0]
networkInfo['downTotal'] = networkIo[1]
networkInfo['up'] = round((float(networkIo[0]) - self.old_net_info['upTotal']) / s, 2)
networkInfo['down'] = round((float(networkIo[1]) - self.old_net_info['downTotal']) / s, 2)
networkInfo['downPackets'] = networkIo[3]
networkInfo['upPackets'] = networkIo[2]
networkInfo['downPackets_s'] = int((networkIo[3] - self.old_net_info['downPackets']) / s)
networkInfo['upPackets_s'] = int((networkIo[2] - self.old_net_info['upPackets']) / s)
cache.set(self.old_net_path, self.new_net_info, 600)
return networkInfo
except:
return None
def get_net_old(self):
if self.old_net_info: return True
data = cache.get(self.old_net_path)
if not data: return False
if not data: return False
self.old_net_info = data
del (data)
return True
# ------------------------------ 获取进程net end ------------------------------
# ------------------------------ 获取启动项列表 start ------------------------------
def get_run_list(self, search: str = ''):
runFile = ['/etc/rc.local', '/etc/profile', '/etc/inittab', '/etc/rc.sysinit']
runList = []
for rfile in runFile:
if not os.path.exists(rfile): continue
bodyR = self.clear_comments(public.readFile(rfile))
if not bodyR: continue
stat = os.stat(rfile)
accept = str(oct(stat.st_mode)[-3:])
if accept == '644': continue
tmp = {}
tmp['name'] = rfile
tmp['srcfile'] = rfile
tmp['size'] = os.path.getsize(rfile)
tmp['access'] = accept
tmp['ps'] = self.get_run_ps(rfile)
runList.append(tmp)
runlevel = self.get_my_runlevel()
runPath = ['/etc/init.d', '/etc/rc' + runlevel + '.d']
tmpAll = []
islevel = False
for rpath in runPath:
if not os.path.exists(rpath): continue
if runPath[1] == rpath: islevel = True
for f in os.listdir(rpath):
if f[:1] != 'S': continue
filename = rpath + '/' + f
if not os.path.exists(filename): continue
if os.path.isdir(filename): continue
if os.path.islink(filename):
flink = os.readlink(filename).replace('../', '/etc/')
if not os.path.exists(flink): continue
filename = flink
tmp = {}
tmp['name'] = f
if islevel: tmp['name'] = f[3:]
if tmp['name'] in tmpAll: continue
stat = os.stat(filename)
accept = str(oct(stat.st_mode)[-3:])
if accept == '644': continue
tmp['srcfile'] = filename
tmp['access'] = accept
tmp['size'] = os.path.getsize(filename)
tmp['ps'] = self.get_run_ps(tmp['name'])
runList.append(tmp)
tmpAll.append(tmp['name'])
data = {}
data['run_list'] = runList
data['run_level'] = runlevel
if search:
data['run_list'] = self.search_run(data['run_list'], search)
return public.returnResult(code=1, msg='success', status=True, data=data)
# 启动项查询
def search_run(self, data, search):
try:
ldata = []
for i in data:
if search in i['name'] or search in i['srcfile'] or search in i['ps']:
ldata.append(i)
return ldata
except:
return data
# 清除注释
def clear_comments(self, body):
bodyTmp = body.split("\n")
bodyR = ""
for tmp in bodyTmp:
if tmp.startswith('#'): continue
if tmp.strip() == '': continue
bodyR += tmp
return bodyR
# 服务注释
def get_run_ps(self, name):
runPs = {'netconsole': '网络控制台日志', 'network': '网络服务', 'jexec': 'JAVA', 'tomcat8': 'Apache Tomcat',
'tomcat7': 'Apache Tomcat', 'mariadb': 'Mariadb',
'tomcat9': 'Apache Tomcat', 'tomcat': 'Apache Tomcat', 'memcached': 'Memcached缓存器',
'php-fpm-53': 'PHP-5.3', 'php-fpm-52': 'PHP-5.2',
'php-fpm-54': 'PHP-5.4', 'php-fpm-55': 'PHP-5.5', 'php-fpm-56': 'PHP-5.6', 'php-fpm-70': 'PHP-7.0',
'php-fpm-71': 'PHP-7.1',
'php-fpm-72': 'PHP-7.2', 'rsync_inotify': 'rsync实时同步', 'pure-ftpd': 'FTP服务',
'mongodb': 'MongoDB', 'nginx': 'Web服务器(Nginx)',
'httpd': 'Web服务器(Apache)', 'bt': '宝塔面板', 'mysqld': 'MySQL数据库', 'rsynd': 'rsync主服务',
'php-fpm': 'PHP服务', 'systemd': '系统核心服务',
'/etc/rc.local': '用户自定义启动脚本', '/etc/profile': '全局用户环境变量',
'/etc/inittab': '用于自定义系统运行级别', '/etc/rc.sysinit': '系统初始化时调用的脚本',
'sshd': 'SSH服务', 'crond': '计划任务服务', 'udev-post': '设备管理系统', 'auditd': '审核守护进程',
'rsyslog': 'rsyslog服务', 'sendmail': '邮件发送服务', 'blk-availability': 'lvm2相关',
'local': '用户自定义启动脚本', 'netfs': '网络文件系统', 'lvm2-monitor': 'lvm2相关',
'xensystem': 'xen云平台相关', 'iptables': 'iptables防火墙', 'ip6tables': 'iptables防火墙 for IPv6',
'firewalld': 'firewall防火墙'}
if name in runPs: return runPs[name]
return name
# 获取当前运行级别
def get_my_runlevel(self):
try:
runlevel = public.ExecShell('runlevel')[0].split()[1]
except:
runlevel_dict = {"multi-user.target": '3', 'rescue.target': '1', 'poweroff.target': '0',
'graphical.target': '5', "reboot.target": '6'}
r_tmp = public.ExecShell('systemctl get-default')[0].strip()
if r_tmp in runlevel_dict:
runlevel = runlevel_dict[r_tmp]
else:
runlevel = '3'
return runlevel
# ------------------------------ 获取启动项列表 end ------------------------------
class Process(object):
process = RealProcess()
# 获取进程列表
def get_process_list(self):
return self.process.get_process_list()
# 获取进程信息->pid
def get_process_info_by_pid(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_pid(get.pid)
# 通过name获取进程信息
def get_process_info_by_name(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_name(get.name)
def get_process_info_by_exec(self, get: Any) -> dict:
if not hasattr(get, 'cli'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_exec(get.cli)
def get_process_info_by_port(self, get: Any) -> dict:
if not hasattr(get, 'port'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_port(get.port)
def get_process_info_by_ip(self, get: Any) -> dict:
if not hasattr(get, 'ip'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_ip(get.ip)
def get_process_info_by_openfile(self, get: Any) -> dict:
if not hasattr(get, 'file_path'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_openfile(get.file_path)
# 获取进程树
def get_process_tree(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_tree(get.pid)
# 结束进程pid
def kill_pid(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_pid(get.pid)
# 结束进程名
def kill_name(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_name(get.name)
# 结束进程树
def kill_tree(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_tree(get.pid)
# 结束所有进程 pid,进程名,进程树
def kill_proc_all(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_proc_all(get.pid)
def kill_port(self, get: Any) -> dict:
if not hasattr(get, 'port'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_port(get.port)
def add_black_ip(self, get: Any) -> dict:
if not hasattr(get, 'ips'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.add_black_ip(get.ips)
def del_black_ip(self, get: Any) -> dict:
if not hasattr(get, 'ips'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.del_black_ip(get.ips)
def get_process_ps(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_ps(get.name)
def get_run_list(self, get: Any) -> dict:
if not hasattr(get, 'search'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_run_list(get.search)
if __name__ == "__main__":
p = RealProcess()
print(p.get_computers_use())
# print('========================')
# print(p.get_process_list()['data'])
# print('========================')
# print(p.get_process_info_by_pid(1)['data'])
# print('========================')
# print(p.get_process_info_by_name('systemd'))
# print('========================')
# res = p.get_process_tree(1)
# print(res['data'][1])
# print('========================')
# print(p.kill_pid(1))
# print('========================')
# print(p.kill_name('systemd'))
# print('========================')
# print(p.kill_tree(1))
# print('========================')
# print(p.kill_proc_all(1))
# print('========================')
# print(p.get_process_info_by_exec('nginx'))
# print('========================')
# print(p.get_process_info_by_port(8888))
# print('========================')
# print(p.get_process_ps('nginx'))
# print('========================')
# print(p.get_process_info_by_ip('192.168.168.66'))
# print('========================')
# print(p.add_black_ip(['1.1.1.1']))
# print('========================')
# print(p.del_black_ip(['1.1.1.1']))
# print('========================')
|