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

feat(server-check): 新增NTP服务检测功能

- 添加 Check-NTPService 函数用于检测目标服务器的 NTP 或 Chrony 服务
- 检测 NTP 服务状态及服务器时间与北京时间的一致性
- 在主流程中集成 NTP 服务检测逻辑
- 优化 IP 地址检测逻辑,区分并展示授权与未授权 IP
- 更新配置文件检测范围,新增 config.js 和 unified*.conf 文件类型
- 将 AllowedIPs 列表动态绑定目标服务器 IP 地址
- 文档同步更新检测说明与注意事项
上级 a21d4d74
......@@ -161,12 +161,12 @@
中间件配置文件检测:
1、检测/data/middleware/nginx/config目录下的conf格式文件中的IP地址
注意:如果遇到有172.17.0.1、127.0.0.1的IP地址以及和目标服务器IP地址一致的需判断为正确!检测日志需要打印出配置文件的路径,不要进行修改IP的操作!日志需会更简洁明了!
注意:如果遇到有172.17.0.1、127.0.0.1的IP地址以及和目标服务器IP地址一致的需判断为正确!检测日志需要打印出配置文件的路径,不要进行修改IP的操作!将单个配置文件内的IP地址检测收集结果后再标注打印,并增加配置项地址。日志需要更简洁明了!
##### 服务器NTP服务检测(❌ 待实现):
检测目标服务器上的ntp服务是否开启并能够正确进行同步时间操作,ntp服务应该分为:ntp和chronyd两种,并且需要检测目标服务器时间和北京时间是否一致。
注意:不要做ntp配置文件的修改操作,此脚本只做检测!
注意:此检测步骤需要在日志导出之前执行。不要做ntp配置文件的修改操作,此脚本只做检测!
##### 服务自检报告输出
将服务自检的所有操作步骤与结果输出到日志文件中!
\ No newline at end of file
......@@ -1589,7 +1589,7 @@ function Show-HealthReport {
}
# upython 服务统计
if ($UpythonResults -and $UpythonResults.Count -gt 0) {
if ($UpythonResults -and $UpythonResults.Count -gt 0) {
Write-Host "【upython 容器服务状态】" -ForegroundColor Yellow
foreach ($r in $UpythonResults) {
$totalServices++
......@@ -1683,6 +1683,8 @@ function Show-HealthReport {
Write-Host ""
}
# 服务器NTP服务分析结果
# 总结
Write-Host "==================================================================" -ForegroundColor Cyan
Write-Host "【检测总结】" -ForegroundColor Yellow
......@@ -1824,10 +1826,10 @@ function Check-NewPlatformIPs {
)
# 定义允许的IP地址
$AllowedIPs = @("172.17.0.1", "127.0.0.1")
$AllowedIPs = @("172.17.0.1", "127.0.0.1", $ServerIP)
foreach ($Directory in $Directories) {
$Command = "find $Directory -type f \( -name '*.yml' -o -name '*.properties' -o -name 'config.json' -o -name '*.conf' \)"
$Command = "find $Directory -type f \( -name '*.yml' -o -name '*.properties' -o -name 'config.js' -o -name 'config.json' -o -name 'unified*.conf' \)"
$Result = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $Command
if ($Result.ExitCode -eq 0) {
......@@ -1843,13 +1845,24 @@ function Check-NewPlatformIPs {
if ($IPResult.ExitCode -eq 0) {
$IPs = $IPResult.Output -split "`n" | Where-Object { $_ -ne "" }
$UnauthorizedIPs = @()
$AuthorizedIPs = @()
foreach ($IP in $IPs) {
if ($AllowedIPs -notcontains $IP) {
Write-Warning "检测到未授权的IP地址: $IP 在文件: $File"
$UnauthorizedIPs += $IP
} else {
Write-Host "IP地址 $IP 在文件 $File 是合法的" -ForegroundColor Green
$AuthorizedIPs += $IP
}
}
if ($UnauthorizedIPs.Count -gt 0) {
Write-Warning "文件: $File 检测到未授权的IP地址: $($UnauthorizedIPs -join ", ")"
}
if ($AuthorizedIPs.Count -gt 0) {
Write-Host "文件: $File 合法的IP地址: $($AuthorizedIPs -join ", ")" -ForegroundColor Green
}
} else {
Write-Warning "未找到任何IP地址: $File"
}
......@@ -1862,6 +1875,64 @@ function Check-NewPlatformIPs {
Write-Host "新统一平台配置文件IP检测完成." -ForegroundColor Green
}
# ================================
# 检测 NTP 服务
# ================================
function Check-NTPService {
param (
[string]$ServerIP,
[string]$Username,
[string]$Password
)
Write-Host "开始检测目标服务器的NTP服务..." -ForegroundColor Yellow
# 检测是否安装了 NTP 或 Chrony 服务
$Command = "systemctl list-units --type=service | grep -E 'ntp|chronyd'"
$Result = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $Command
if ($Result.ExitCode -eq 0 -and $Result.Output -match "ntp|chronyd") {
Write-Host "目标服务器已安装 NTP 或 Chrony 服务." -ForegroundColor Green
# 检测 NTP 服务状态
$StatusCommand = "timedatectl status"
$StatusResult = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $StatusCommand
if ($StatusResult.ExitCode -eq 0) {
Write-Host "NTP 服务状态: " -ForegroundColor Cyan
Write-Host $StatusResult.Output
# 检测服务器时间与北京时间是否一致
$TimeCommand = "date +%s"
$ServerTimeResult = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $TimeCommand
if ($ServerTimeResult.ExitCode -eq 0) {
$ServerTimeUTC = [int]$ServerTimeResult.Output.Trim()
# Adjust server time to local timezone (Asia/Shanghai)
$ServerTime = $ServerTimeUTC + (8 * 3600) # Add 8 hours for CST
$LocalTime = [int](Get-Date -UFormat %s)
$TimeDifference = [math]::Abs($ServerTime - $LocalTime)
if ($TimeDifference -le 5) {
Write-Host "服务器时间与北京时间一致." -ForegroundColor Green
} else {
Write-Warning "服务器时间与北京时间存在偏差: $TimeDifference 秒"
}
} else {
Write-Warning "无法获取目标服务器时间."
}
} else {
Write-Warning "无法获取 NTP 服务状态."
}
} else {
Write-Warning "目标服务器未安装 NTP 或 Chrony 服务."
}
Write-Host "NTP 服务检测完成." -ForegroundColor Green
}
# ================================
# 主函数
# ================================
......@@ -1978,6 +2049,11 @@ function Main {
Check-TraditionalPlatformIPs -ServerIP $server.IP -Username $server.User -Password $server.Pass
}
# 检测 NTP 服务
Write-Log -Level "INFO" -Message "开始检测 NTP 服务..."
Check-NTPService -ServerIP $server.IP -Username $server.User -Password $server.Pass
Write-Log -Level "INFO" -Message "NTP 服务检测完成."
# 询问是否导出日志
Write-Host ""
Write-Host "==================================================================" -ForegroundColor Cyan
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论