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

feat(scripts): 增强服务器健康检查和问题处理功能

- 规范化容器端口和网络信息展示,支持数组和字符串格式
- 优化备份压缩逻辑,增加错误匹配检测
- 完善问题处理脚本的交互式菜单和子操作选项
- 统一shell脚本解释器为#!/usr/bin/env bash
- 添加.gitattributes文件统一换行符处理
- 重构主函数入口逻辑提高代码可维护性
上级 92674712
# Normalize line endings for cross-platform compatibility
* text=auto
# Always use LF for shell scripts (prevents /bin/bash^M errors on Linux)
*.sh text eol=lf
#!/bin/bash #!/usr/bin/env bash
# 修复中文换行问题的专用工具脚本 # 修复中文换行问题的专用工具脚本
......
#!/bin/bash #!/usr/bin/env bash
# 设置标准环境变量,避免"参数列表过长"问题 # 设置标准环境变量,避免"参数列表过长"问题
export LANG=C export LANG=C
...@@ -2046,19 +2046,80 @@ show_submenu() { ...@@ -2046,19 +2046,80 @@ show_submenu() {
update_frontend "$platform" update_frontend "$platform"
update_backend "$platform" update_backend "$platform"
update_ops_backend "$platform" update_ops_backend "$platform"
#!/bin/bash update_ops_frontend "$platform"
;;
# 日志函数 *)
log_info() { log_error "无效的选择: $sub_choice"
echo -e "\033[32m[INFO] $1\033[0m" ;;
} esac
;;
2)
sub_choice=$(get_user_choice "请选择子操作 [2.1-2.3, 或按回车全选]: " "2.1,2.2,2.3")
log_info "用户选择子操作: $sub_choice"
log_warn() { case $sub_choice in
echo -e "\033[33m[WARN] $1\033[0m" 2.1)
} log_info "执行修复普通文件权限"
fix_permissions "$platform"
;;
2.2)
log_info "执行修复数据库用户权限"
fix_db_permissions
;;
2.3)
log_info "执行修复nginx用户权限"
fix_nginx_user
;;
"")
log_info "执行修复所有权限问题"
fix_permissions "$platform"
fix_db_permissions
fix_nginx_user
;;
*)
log_error "无效的选择: $sub_choice"
;;
esac
;;
3)
log_info "执行修复配置文件IP不对问题"
fix_ip_configurations "$platform"
;;
4)
sub_choice=$(get_user_choice "请选择子操作 [4.1-4.4]: " "4.1,4.2,4.3,4.4")
log_info "用户选择子操作: $sub_choice"
log_error() { case $sub_choice in
echo -e "\033[31m[ERROR] $1\033[0m" 4.1)
log_info "执行清理已删除文件"
clean_deleted_files
;;
4.2)
log_info "执行轮转日志文件"
rotate_logs "$platform"
;;
4.3)
log_info "执行清理数据库system_log表"
cleanup_system_log_table
;;
4.4)
log_info "显示磁盘分区调整参考信息"
show_disk_partition_info
;;
*)
log_error "无效的选择: $sub_choice"
;;
esac
;;
*)
# 对于不需要子菜单的选项,直接执行
case $main_choice in
*)
log_error "无效的选择: $main_choice"
;;
esac
;;
esac
} }
# 交互式模式函数 # 交互式模式函数
...@@ -2169,129 +2230,7 @@ interactive_mode() { ...@@ -2169,129 +2230,7 @@ interactive_mode() {
# 脚本入口 # 脚本入口
main "$@" main "$@"
# 主函数 # 脚本入口
main() {
# 记录脚本启动日志
log_info "脚本开始执行,参数: $*"
# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]; then
log_warn "建议以root权限运行此脚本以确保所有操作都能正常执行"
fi
# 如果提供了命令行参数,则使用命令行模式
if [ $# -gt 0 ]; then
local platform=$(detect_platform)
if [ "$platform" = "unknown" ]; then
log_error "无法检测到有效的平台类型"
exit 1
fi
log_info "检测到平台类型: $platform"
case $1 in
--action)
case $2 in
update_packages)
log_info "执行操作: update_packages"
update_inner_backend_jar "$platform"
update_external_backend_jar "$platform"
update_frontend "$platform"
update_backend "$platform"
update_ops_backend "$platform"
update_ops_frontend "$platform"
;;
fix_permissions)
log_info "执行操作: fix_permissions"
fix_permissions "$platform"
;;
fix_ip_configs)
log_info "执行操作: fix_ip_configs"
fix_ip_configurations "$platform"
;;
clean_deleted_files)
log_info "执行操作: clean_deleted_files"
clean_deleted_files
;;
clean_deleted_files_cron)
log_info "执行操作: clean_deleted_files_cron"
clean_deleted_files_cron
;;
rotate_logs)
log_info "执行操作: rotate_logs"
rotate_logs "$platform"
;;
export_logs)
log_info "执行操作: export_logs"
export_and_compress_logs "$platform"
;;
fix_external_service)
log_info "执行操作: fix_external_service"
fix_external_service_disconnect "$platform"
;;
fix_ntp)
log_info "执行操作: fix_ntp"
fix_ntp_config
;;
fix_ports)
log_info "执行操作: fix_ports"
fix_port_access "$platform"
;;
fix_dns)
log_info "执行操作: fix_dns"
fix_dns_config
;;
backup_environment_data)
log_info "执行操作: backup_environment_data"
backup_environment_data "$platform"
;;
*)
log_error "未知操作: $2"
exit 1
;;
esac
;;
*)
log_error "未知参数: $1"
exit 1
;;
esac
else
# 否则进入交互式模式
log_info "进入交互式模式"
interactive_mode
fi
log_info "脚本执行完毕"
}
}
# 显示帮助信息
show_help() {
echo "Usage: $0 [OPTIONS] [COMMAND]"
echo ""
echo "OPTIONS:"
echo " -h, --help 显示此帮助信息"
echo " -v, --version 显示版本信息"
echo ""
echo "COMMANDS:"
echo " repair 尝试自动修复常见问题"
echo " check 检查系统状态"
echo " clean 清理临时文件"
echo ""
}
# 显示版本信息
show_version() {
echo "Issue Handler Tool v1.0"
}
```
ubains-module-test/辅助工具/脚本工具/常见问题处理/issue_handler.sh
```bash
<<<<<<< SEARCH
main "$@"
main() { main() {
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
case "$1" in case "$1" in
...@@ -2334,9 +2273,3 @@ main() { ...@@ -2334,9 +2273,3 @@ main() {
log_info "脚本执行完毕" log_info "脚本执行完毕"
} }
# 脚本入口
main "$@"
# 脚本入口
main "$@"
...@@ -1993,6 +1993,27 @@ function Show-HealthReport { ...@@ -1993,6 +1993,27 @@ function Show-HealthReport {
$szParts = @(); if ($c.SizeRw -ne $null) { $szParts += ("rw={0}" -f $c.SizeRw) }; if ($c.SizeRootFs -ne $null) { $szParts += ("root={0}" -f $c.SizeRootFs) } $szParts = @(); if ($c.SizeRw -ne $null) { $szParts += ("rw={0}" -f $c.SizeRw) }; if ($c.SizeRootFs -ne $null) { $szParts += ("root={0}" -f $c.SizeRootFs) }
$szStr = if ($szParts.Count -gt 0) { ($szParts -join ', ') } else { '-' } $szStr = if ($szParts.Count -gt 0) { ($szParts -join ', ') } else { '-' }
$md += "- $statusIcon 名称: $($c.Name) | 镜像: $($c.Image) | 状态: $($c.Status) | 健康: $health | 重启: $rp/$rc | IP: $ip" $md += "- $statusIcon 名称: $($c.Name) | 镜像: $($c.Image) | 状态: $($c.Status) | 健康: $health | 重启: $rp/$rc | IP: $ip"
# 规范化端口/网络字符串,兼容数组或字符串
$portsStr = '-'
if ($null -ne $showPorts) {
if ($showPorts -is [string]) {
$portsStr = $showPorts
}
else {
$tmp = @($showPorts)
if ($tmp.Count -gt 0) { $portsStr = ($tmp -join ', ') }
}
}
$netsStr = '-'
if ($null -ne $showNets) {
if ($showNets -is [string]) {
$netsStr = $showNets
}
else {
$tmpn = @($showNets)
if ($tmpn.Count -gt 0) { $netsStr = ($tmpn -join ', ') }
}
}
$md += " - 端口: $portsStr" $md += " - 端口: $portsStr"
$md += " - 网络: $netsStr" $md += " - 网络: $netsStr"
if ($mountsStr -ne '-') { $md += " - 挂载: $mountsStr" } if ($mountsStr -ne '-') { $md += " - 挂载: $mountsStr" }
...@@ -2413,7 +2434,7 @@ function DataBakup { ...@@ -2413,7 +2434,7 @@ function DataBakup {
$res2 = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $packCmd $res2 = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $packCmd
$packOutput = if ($res2.Output) { [string]::Join(' ', $res2.Output) } else { '' } $packOutput = if ($res2.Output) { [string]::Join(' ', $res2.Output) } else { '' }
Write-Log -Level "INFO" -Message "[BAK] 压缩输出: $packOutput" Write-Log -Level "INFO" -Message "[BAK] 压缩输出: $packOutput"
if ($res2.ExitCode -ne 0) { Write-Log -Level "ERROR" -Message "[BAK] 压缩备份失败"; return @{ Summary = "失败" } } if ($res2.ExitCode -ne 0 -or $packOutput -match "error") { Write-Log -Level "ERROR" -Message "[BAK] 压缩备份失败"; return @{ Summary = "失败" } }
if (-not $script:PSCP_PATH) { if (-not $script:PSCP_PATH) {
Write-Log -Level "ERROR" -Message "[BAK] 未找到 pscp.exe,无法下载备份文件"; $downloadSummary = "未下载" Write-Log -Level "ERROR" -Message "[BAK] 未找到 pscp.exe,无法下载备份文件"; $downloadSummary = "未下载"
...@@ -2575,22 +2596,8 @@ function Main { ...@@ -2575,22 +2596,8 @@ function Main {
Write-Log -Level "INFO" -Message "========== 容器信息(报告) ==========" Write-Log -Level "INFO" -Message "========== 容器信息(报告) =========="
$containerInfo = Test-ContainerInformation -Server $server $containerInfo = Test-ContainerInformation -Server $server
if (-not $containerInfo -or $containerInfo.Count -eq 0) { if (-not $containerInfo -or $containerInfo.Count -eq 0) {
Write-Log -Level "INFO" -Message "未发现容器或 Docker 不可用"
} else {
# 输出 Markdown 友好日志
Write-Log -Level "INFO" -Message "容器数量: $($containerInfo.Count)"
foreach ($c in $containerInfo) {
$statusIcon = if ($c.Running) { "✅" } else { "⛔" }
$ip = if ($c.IPAddress) { $c.IPAddress } else { "-" }
$ports = if ($c.Ports -and $c.Ports.Count -gt 0) { ($c.Ports -join ', ') } else { '-' }
$mounts = if ($c.Mounts -and $c.Mounts.Count -gt 0) { ($c.Mounts -join '; ') } else { '-' }
$szParts = @(); if ($c.SizeRw -ne $null) { $szParts += ("rw={0}" -f $c.SizeRw) }; if ($c.SizeRootFs -ne $null) { $szParts += ("root={0}" -f $c.SizeRootFs) }
$szStr = if ($szParts.Count -gt 0) { ($szParts -join ', ') } else { '-' }
Write-Log -Level "INFO" -Message "- $statusIcon 名称: $($c.Name) | 镜像: $($c.Image) | 状态: $($c.Status) | IP: $ip"
Write-Log -Level "INFO" -Message " 端口: $ports"
Write-Log -Level "INFO" -Message " 网络: $mounts | 大小: $szStr"
}
} }
$md += ""
# 检测配置文件中的IP地址 # 检测配置文件中的IP地址
Write-Host "" Write-Host ""
......
#!/bin/bash #!/usr/bin/env bash
#====================================================================== #======================================================================
# 自动化部署打包上传脚本 # 自动化部署打包上传脚本
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论