提交 0f2d1ee8 authored 作者: 陈泽健's avatar 陈泽健

feat(系统服务进程监测):增强远程进程状态检测功能

- 添加针对 JAR 进程的精确检测逻辑
-优化普通进程检测方法,提高匹配准确性
- 新增进程类型检测,支持 Java 进程
- 重构代码结构,提高可读性和可维护性
上级 cb30e995
......@@ -388,6 +388,22 @@ class ServerProcessMonitor:
return None
def _get_remote_process_status(self, server_name, process_name):
"""
获取远程服务器进程状态 - 增强版JAR进程检测
参数:
server_name: 服务器名称
process_name: 进程/容器名称
返回:
dict: 包含进程状态的字典,包含:
- is_container (bool): 是否为容器
- running (bool): 是否在运行
- status (str): 进程状态
- pid (str): 进程ID
- command (str): 完整命令
- process_type (str): 进程类型
"""
ssh = self.ssh_clients.get(server_name)
if not ssh:
self.logger.warning(f"{Fore.YELLOW}未找到服务器 {server_name} 的SSH连接{Style.RESET_ALL}")
......@@ -396,38 +412,61 @@ class ServerProcessMonitor:
try:
self.logger.info(f"正在检查进程: {process_name}...")
# 1. 首先检查是否是容器
# 1. 容器检查逻辑保持不变
container_status = self._check_container_status(ssh, process_name)
if container_status:
status_msg = "容器" if container_status['is_container'] else "进程"
self.logger.info(f"{status_msg} {process_name} 状态: {container_status['status']}")
return container_status
# 2. 通用进程检测逻辑
# 使用更精确的进程检测方式,避免误匹配
# 2. 智能进程检测
if process_name.endswith('.jar'):
# 2.1 JAR进程精确检测
cmd = (
f"ps -ef | "
f"grep '{process_name}' | " # 匹配完整JAR路径
f"grep -v grep | " # 排除grep进程自身
f"head -1" # 只取第一个匹配结果
)
stdin, stdout, stderr = ssh.exec_command(cmd)
output = stdout.read().decode().strip()
if output:
parts = output.split()
return {
'is_container': False,
'running': True,
'pid': parts[1], # PID
'status': parts[7], # 状态
'command': ' '.join(parts[7:]), # 完整命令
'process_type': 'java'
}
else:
# 2.2 普通进程检测
stdin, stdout, stderr = ssh.exec_command(
f"ps aux | grep -v grep | grep -i '{process_name}'"
f"pgrep -f '{process_name}' || echo ''" # 使用pgrep -f进行全命令匹配
)
pid = stdout.read().decode().strip()
if pid:
# 获取完整进程信息
stdin, stdout, stderr = ssh.exec_command(
f"ps -p {pid} -o pid,stat,cmd"
)
process_info = stdout.read().decode().strip()
if process_info:
# 解析进程信息
process_lines = process_info.split('\n')
first_process = process_lines[0].split()
pid = first_process[1]
status = first_process[7]
cmd = ' '.join(first_process[10:])
self.logger.info(f"{Fore.GREEN}进程运行中 - PID: {pid}, 状态: {status}, 命令: {cmd}{Style.RESET_ALL}")
lines = process_info.split('\n')
if len(lines) > 1:
parts = lines[1].split()
return {
'is_container': False,
'running': True,
'pid': pid,
'status': status,
'command': cmd,
'process_type': self._detect_process_type(cmd) # 新增进程类型检测
'pid': parts[0], # PID
'status': parts[1], # 状态
'command': ' '.join(parts[2:]), # 命令
'process_type': self._detect_process_type(' '.join(parts[2:]))
}
# 3. 进程不存在的情况
self.logger.warning(f"{Fore.YELLOW}进程 {process_name} 未运行{Style.RESET_ALL}")
return {
'is_container': False,
......@@ -687,7 +726,9 @@ if __name__ == "__main__":
'process_names': [
'ungrok', 'umysql', 'uredis',
'upython', 'ujava2', 'paperless',
'cardtable', 'ustorage', 'utracker'
'cardtable', 'ustorage', 'utracker',
'ubains-meeting-inner-api-1.0-SNAPSHOT.jar',
'ubains-meeting-api-1.0-SNAPSHOT.jar'
]
},
{ # 服务器2(对外云端)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论