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

fix(server): 解决服务器自检脚本中的多个兼容性和查找问题

- 修复 Android 检查模块中 adb.exe 的查找逻辑,支持 window_adb 子目录
- 将配置变量声明为全局作用域以解决访问权限问题
- 改进 NTP 服务检测结果的格式兼容性,支持 hashtable 和 PSCustomObject 两种返回格式
- 扩展 issue_handler.sh 脚本的查找路径,支持多种候选目录位置
- 优化 MQTT 检测逻辑,修复工具存在性检查条件
- 更新内存使用统计命令,使用更可靠的 /proc/meminfo 读取方式
上级 0b8403ea
......@@ -173,18 +173,18 @@ $Global:UpythonVoicePorts = @(
# ================================
# 传统平台 ujava 容器内服务(只有nginx和meeting服务)
$UjavaOldPlatformContainerServices = @{
$Global:UjavaOldPlatformContainerServices = @{
"nginx" = "nginx: master process"
"meeting" = "ubains-meeting-inner-api-1.0-SNAPSHOT.jar"
}
# 传统平台 ujava 宿主机服务
$UjavaOldPlatformHostServices = @{
$Global:UjavaOldPlatformHostServices = @{
"extapi" = "ubains-meeting-api-1.0-SNAPSHOT.jar"
}
# 传统平台 upython 容器内需要监听的端口
$UpythonOldPlatformPorts = @(
$Global:UpythonOldPlatformPorts = @(
@{ Port = 8081; Process = "nginx"; Description = "Nginx 代理服务 (8081)" }
@{ Port = 8443; Process = "nginx"; Description = "Nginx HTTPS 服务 (8443)" }
@{ Port = 8000; Process = "uwsgi"; Description = "uWSGI 应用服务" }
......@@ -193,7 +193,7 @@ $UpythonOldPlatformPorts = @(
)
# DNS 测试域名列表
$DNSTestDomains = @(
$Global:DNSTestDomains = @(
"www.baidu.com"
"www.qq.com"
)
......@@ -203,7 +203,7 @@ $DNSTestDomains = @(
# ================================
# MQTT 主题列表(用于记录参考,实际检测依赖端口连通性)
$MQTTTopics = @(
$Global:MQTTTopics = @(
"/androidPanel/"
"/iot/v1/conference/service/request/"
"message/paperLessService/callService"
......@@ -214,7 +214,7 @@ $MQTTTopics = @(
)
# 中间件连接信息
$MiddlewareConfig = @{
$Global:MiddlewareConfig = @{
Redis = @{
ContainerName = "uredis"
Port = 6379
......@@ -247,7 +247,7 @@ $MiddlewareConfig = @{
# ================================
# 新统一平台日志路径配置
$NewPlatformLogs = @(
$Global:NewPlatformLogs = @(
@{ Name = "auth_log.out"; RemotePath = "/data/services/api/auth/auth-sso-auth/log.out" },
@{ Name = "gatway_log.out"; RemotePath = "/data/services/api/auth/auth-sso-gatway/log.out" },
@{ Name = "system_log.out"; RemotePath = "/data/services/api/auth/auth-sso-system/log.out" },
......@@ -259,13 +259,13 @@ $NewPlatformLogs = @(
)
# 传统平台 ujava 日志路径配置
$OldPlatformUjavaLogs = @(
$Global:OldPlatformUjavaLogs = @(
@{ Name = "对内后端_ubains-INFO-AND-ERROR.log"; RemotePath = "/var/www/java/api-java-meeting2.0/logs/ubains-INFO-AND-ERROR.log" }
@{ Name = "对外后端_ubains-INFO-AND-ERROR.log"; RemotePath = "/var/www/java/external-meeting-api/logs/ubains-INFO-AND-ERROR.log" }
)
# 传统平台 upython 日志路径配置
$OldPlatformUpythonLogs = @(
$Global:OldPlatformUpythonLogs = @(
@{ Name = "运维集控_error.log"; RemotePath = "/var/www/html/log/error.log" }
@{ Name = "运维集控_uinfo.log"; RemotePath = "/var/www/html/log/uinfo.log" }
@{ Name = "运维集控_uwsgi.log"; RemotePath = "/var/www/html/log/uwsgi.log" }
......@@ -2670,11 +2670,16 @@ function Main {
$ntpResults = Test-NTPService -ServerIP $server.IP -Username $server.User -Password $server.Pass -Port $server.Port
}
Write-Log -Level "INFO" -Message "NTP 服务检测完成."
# 输出 NTP 摘要
# 输出 NTP 摘要(兼容 hashtable 和 PSCustomObject 两种返回格式)
if ($ntpResults) {
$resultInfo = if ($ntpResults -is [array]) { $ntpResults[0] } else { $ntpResults }
$statusInfo = "状态=$($resultInfo.Status)"
# hashtable 用 Detail,PSCustomObject 用 TimeSync
if ($resultInfo -is [hashtable] -and $resultInfo.ContainsKey('Detail') -and $resultInfo.Detail) {
$statusInfo += " | $($resultInfo.Detail)"
} elseif (-not ($resultInfo -is [hashtable])) {
if ($resultInfo.TimeSync) { $statusInfo += " | 时间同步=$($resultInfo.TimeSync)" }
}
Write-Log -Level "INFO" -Message $statusInfo
}
Write-Log -Level "INFO" -Message "========== 结束检测NTP服务 =========="
......@@ -2710,9 +2715,7 @@ function Main {
$androidResults = Test-AndroidDeviceHealth -ScriptDir $SCRIPT_DIR
# 生成检测报告
# 转换NTPResults为hashtable格式
$ntpResultsHashtable = $null
# 转换NTPResults为hashtable格式
# 转换NTPResults为hashtable格式(兼容两种检测模式的返回格式)
$ntpResultsHashtable = $null
if ($ntpResults) {
# 确保ntpResults是数组
......@@ -2720,16 +2723,33 @@ function Main {
if ($ntpArray.Count -gt 0) {
$ntpFirst = $ntpArray[0]
# 将NTP状态转换为报告期望的状态格式
# 判断返回格式:Test-NTPService 返回 hashtable(中文Status+Detail)
# Test-NTPService-Shell 返回 PSCustomObject(英文Status+Daemon+TimeSync)
if ($ntpFirst -is [hashtable] -and $ntpFirst.ContainsKey('Detail')) {
# Test-NTPService 模式:直接透传中文状态
$ntpResultsHashtable = @{
Status = $ntpFirst.Status
Detail = $ntpFirst.Detail
}
} else {
# Test-NTPService-Shell 模式:转换英文状态为中文
$ntpStatus = switch ($ntpFirst.Status) {
"running" { "正常" }
"stopped" { "异常" }
"正常" { "正常" }
"异常" { "异常" }
"偏移" { "偏移" }
"未安装" { "未安装" }
default { "未知" }
}
$ntpDetail = ""
if ($ntpFirst.Daemon) { $ntpDetail += "$($ntpFirst.Daemon) - " }
if ($ntpFirst.TimeSync) { $ntpDetail += "$($ntpFirst.TimeSync)" }
if ($ntpFirst.Detail) { $ntpDetail = $ntpFirst.Detail }
$ntpResultsHashtable = @{
Status = $ntpStatus
Detail = "$($ntpFirst.Daemon) - $($ntpFirst.TimeSync)"
Detail = $ntpDetail
}
}
}
}
......
......@@ -10,10 +10,14 @@ function Resolve-AdbPath {
[string]$ScriptDir
)
# 1) 脚本目录
# 1) 脚本目录(直接)
if (-not [string]::IsNullOrEmpty($ScriptDir)) {
$localAdb = Join-Path $ScriptDir "adb.exe"
if (Test-Path $localAdb) { return $localAdb }
# 1b) 脚本目录下的 window_adb 子目录
$windowAdb = Join-Path $ScriptDir "window_adb\adb.exe"
if (Test-Path $windowAdb) { return $windowAdb }
}
# 2) 系统 PATH
......
......@@ -347,9 +347,36 @@ function Upload_the_repair_script {
}
# 2) 上传 issue_handler.sh 到远端目录
# 直接在当前工作目录下查找 issue_handler.sh
# 在多个候选路径中查找 issue_handler.sh
$localScript = $null
$searchPaths = @(
(Join-Path $PWD "issue_handler.sh"),
(Join-Path $PWD "lib\shell\issue_handler.sh"),
(Join-Path $PWD "问题处理\issue_handler.sh")
)
# 如果全局变量中有脚本目录信息,也加入搜索
if ($global:SCRIPT_DIR) {
$searchPaths += Join-Path $global:SCRIPT_DIR "issue_handler.sh"
$searchPaths += Join-Path $global:SCRIPT_DIR "lib\shell\issue_handler.sh"
}
# 如果 PSScriptRoot 可用(模块上下文),也搜索上级目录
if ($PSScriptRoot) {
$parentDir = Split-Path -Parent $PSScriptRoot
$searchPaths += Join-Path $parentDir "issue_handler.sh"
$searchPaths += Join-Path $parentDir "lib\shell\issue_handler.sh"
}
foreach ($candidate in $searchPaths) {
$fullPath = [System.IO.Path]::GetFullPath($candidate)
if (Test-Path -LiteralPath $fullPath) {
$localScript = $fullPath
break
}
}
if (-not $localScript) {
# 最后的兜底:使用 $PWD 下的路径(保持向后兼容)
$localScript = Join-Path $PWD "issue_handler.sh"
$localScript = [System.IO.Path]::GetFullPath($localScript)
}
Write-Log -Level "INFO" -Message "本地脚本: $localScript"
$scpOk = Copy-File-To-Remote -LocalPath $localScript -Server $Server -RemoteDir $RemoteDir
if (-not $scpOk) {
......
......@@ -203,6 +203,8 @@ function Test-MQTTConnection {
# 方法D:MQTT主题订阅检测(补充检测)
Write-Log -Level "INFO" -Message "[MQTT] ========== MQTT主题订阅检测 =========="
$topicSubResults = @()
$subscribedTopics = 0
$publishedTopics = 0
$mqttTopics = @(
"/androidPanel/",
"/iot/v1/conference/service/request/",
......@@ -347,16 +349,13 @@ function Test-MQTTConnection {
}
# 汇总结果
if ($isConnected) {
if ($mqttToolExists) {
$detailMsg += " | 主题订阅: $subscribedTopics/$($mqttTopics.Count) 成功"
if ($publishedTopics -gt 0) {
$detailMsg += " | 消息推送: 成功"
}
} else {
$detailMsg += " | 主题订阅: $subscribedTopics/$($mqttTopics.Count) 成功"
if ($publishedTopics -gt 0) {
$detailMsg += " | 消息推送: 成功"
}
$detailMsg += " | 主题订阅: 跳过(工具不存在)"
}
$results += @{
......
......@@ -192,26 +192,8 @@ function Test-ServerResources {
#region 4. 检测内存使用情况
Write-Log -Level "INFO" -Message "检测内存使用情况..."
$memCmd = @'
LC_ALL=C (
/usr/bin/free -m 2>/dev/null || /bin/free -m 2>/dev/null || free -m
) | awk -F"[[:space:]]+" '
$1 == "Mem:" {
total = $2;
used_field = $3;
free_field = $4;
buffcache = $6;
avail = $7;
if (avail == "" || avail == 0) {
used = used_field;
} else {
used = total - avail;
}
pct = 0;
if (total > 0) { pct = used * 100 / total; }
printf "%.2f,%.2f,%.1f\n", total/1024, used/1024, pct;
}'
'@
# 使用 awk 直接读取 /proc/meminfo(单行命令,兼葂dd plink SSH 传输)
$memCmd = 'awk "/MemTotal/{total=\$2} /MemAvailable/{avail=\$2} /MemFree/{free=\$2} END{if(avail==\"\"||avail==0){avail=free}; used=total-avail; pct=0; if(total>0){pct=used*100/total}; printf \"%.2f,%.2f,%.1f\", total/1048576, used/1048576, pct}" /proc/meminfo'
$memResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $memCmd
$memTotal = 0.0; $memUsed = 0.0; $memPercent = 0.0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论