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

feat(script): 增强NTP服务检测逻辑并优化时间同步检查

- 新增对 chronyd/ntpd 服务运行状态的主动检查
- 修复本地时间获取逻辑,统一使用 UTC 时间戳避免时区偏差
- 更新时间差判定条件,确保服务运行状态下才判断同步状态
- 优化日志记录内容,明确显示本地UTC时间戳与差值
- 在PRD文档中标注NTP修复功能已实现
- 调整测试报告展示逻辑,支持错误信息滚动定位
- 增加.gitignore忽略新平台日志图片目录
上级 6a58107a
/预定系统/log/
/日志监测/error_log/
/预定配套件/中控门口屏/reports/
/预定配套件/中控门口屏/log/
\ No newline at end of file
/预定配套件/中控门口屏/log/
/新统一平台/log/imgs/
\ No newline at end of file
此差异已折叠。
......@@ -231,7 +231,7 @@
##### 13、定时任务查询(待实现):
##### 14、上传修复脚本(待实现):
##### 14、上传修复脚本(已实现NTP修复):
函数名称:Upload_the_repair_script
功能描述:函数只做上传脚本及脚本调用,上传当前目录下的issue_handler.sh脚本,将脚本上传到目标服务器,并且通过传入的修复函数,调用issue_handler对应的函数来修复。
......
......@@ -2254,6 +2254,18 @@ function Check-NTPService {
Write-Log -Level "INFO" -Message "[NTP] systemctl 输出: $([string]::Join(' ', $Result.Output))"
if ($Result.ExitCode -eq 0 -and $Result.Output -match "ntp|chronyd") {
Write-Log -Level "SUCCESS" -Message "[NTP] 已检测到 NTP/Chrony 服务"
# 新增:检查 chronyd/ntpd 是否处于 active(running)
$activeCmd = "if systemctl is-active chronyd >/dev/null 2>&1; then echo CHRONYD_ACTIVE; fi; if systemctl is-active ntpd >/dev/null 2>&1; then echo NTPD_ACTIVE; fi"
$activeRes = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $activeCmd
$chronydActive = ($activeRes.Output -match 'CHRONYD_ACTIVE')
$ntpdActive = ($activeRes.Output -match 'NTPD_ACTIVE')
if (-not $chronydActive -and -not $ntpdActive) {
Write-Log -Level "ERROR" -Message "[NTP] 检测到 chronyd/ntpd 未运行"
$summary.Status = '异常'; $summary.Detail = 'chronyd/ntpd 未运行'
$needRepair = $true
}
$StatusCommand = "timedatectl status"
$StatusResult = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command $StatusCommand
Write-Log -Level "INFO" -Message "[NTP] timedatectl 输出: $([string]::Join(' ', $StatusResult.Output))"
......@@ -2263,10 +2275,11 @@ function Check-NTPService {
Write-Log -Level "INFO" -Message "[NTP] 服务器时间戳: $([string]::Join(' ', $ServerTimeResult.Output))"
if ($ServerTimeResult.ExitCode -eq 0) {
$ServerTimeUTC = [int]($ServerTimeResult.Output | Select-Object -First 1).Trim()
$LocalTime = [int](Get-Date -UFormat %s)
# 本地改为 UTC 秒,避免时区偏差
$LocalTime = [int][DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$TimeDifference = [math]::Abs($ServerTimeUTC - $LocalTime)
Write-Log -Level "INFO" -Message "[NTP] 本地时间戳: $LocalTime, 差值: $TimeDifference 秒"
if ($TimeDifference -le 5) {
Write-Log -Level "INFO" -Message "[NTP] 本地UTC时间戳: $LocalTime, 差值: $TimeDifference 秒"
if ($TimeDifference -le 5 -and ($chronydActive -or $ntpdActive)) {
Write-Log -Level "SUCCESS" -Message "[NTP] 服务器时间与本地时间一致"
$summary.Status = '正常'
$summary.Detail = "时间差 ${TimeDifference}s"
......@@ -2306,7 +2319,8 @@ function Check-NTPService {
$postTime = Invoke-SSHCommand -HostName $ServerIP -User $Username -Pass $Password -Command "date +%s"
if ($postStatus.ExitCode -eq 0 -and $postTime.ExitCode -eq 0) {
$srvTs = [int]($postTime.Output | Select-Object -First 1).Trim()
$locTs = [int](Get-Date -UFormat %s)
# 本地用 UTC 秒
$locTs = [int][DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$diff = [math]::Abs($srvTs - $locTs)
Write-Log -Level "INFO" -Message "[NTP] 复检时间差: ${diff}s"
if ($diff -le 5) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论