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

fix(script): 修复SSL证书日期解析和堆内存检测问题

- 优化SSL证书日期解析逻辑,使用ParseExact方法提高准确性
- 添加多种日期格式支持,包括带GMT和不带GMT的格式
- 改进异常处理机制,避免日期解析失败导致脚本中断
- 修复堆内存设置检测,过滤错误信息并确保返回有效值
- 统一代码风格,增加注释说明日期格式解析规则
上级 82e213b2
......@@ -1432,28 +1432,39 @@ function Test-TimeSync {
if ($certCheck -match "notAfter=(.+)") {
$expiryDateStr = $Matches[1].Trim()
try {
# 尝试多种日期格式解析(兼容PowerShell 5.x)
# OpenSSL证书日期格式: "MMM dd HH:mm:ss yyyy GMT" 或 "MMM d HH:mm:ss yyyy GMT"
# 使用ParseExact + InvariantCulture解析(兼容中文系统)
$expiryDate = $null
$formats = @(
"MMM dd HH:mm:ss yyyy GMT",
"MMM d HH:mm:ss yyyy GMT",
"yyyy-MM-dd HH:mm:ss",
"yyyy/MM/dd HH:mm:ss"
)
foreach ($fmt in $formats) {
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$style = [System.Globalization.DateTimeStyles]::None
if ([DateTime]::TryParseExact($expiryDateStr, $fmt, $culture, $style, [ref]$expiryDate)) {
break
# 先尝试带GMT后缀的格式
try {
$expiryDate = [DateTime]::ParseExact($expiryDateStr, "MMM dd HH:mm:ss yyyy GMT", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 如果失败,尝试单数日期格式(双空格)
try {
$expiryDate = [DateTime]::ParseExact($expiryDateStr, "MMM d HH:mm:ss yyyy GMT", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 如果还是失败,尝试不带GMT的格式
$withoutGMT = $expiryDateStr -replace "\s+GMT$", ""
try {
$expiryDate = [DateTime]::ParseExact($withoutGMT, "MMM dd HH:mm:ss yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 最后尝试单数日期不带GMT
try {
$expiryDate = [DateTime]::ParseExact($withoutGMT, "MMM d HH:mm:ss yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
Write-Log "SSL证书日期解析失败: $expiryDateStr" "WARN"
}
}
}
# 如果上述格式都失败,尝试通用解析
if (-not $expiryDate) {
$expiryDate = [DateTime]::Parse($expiryDateStr)
}
if ($expiryDate) {
$daysLeft = ($expiryDate - [DateTime]::Now).Days
if ($daysLeft -lt 0) {
......@@ -1486,6 +1497,10 @@ function Test-TimeSync {
Add-Issue -Message "SSL证书${certStatusText}: $daysLeft 天" -Level $status
}
}
else {
Write-Log "SSL证书日期解析失败: $expiryDateStr" "WARN"
}
}
catch {
Write-Log "SSL证书日期解析失败: $expiryDateStr - $($_.Exception.Message)" "WARN"
}
......@@ -1499,28 +1514,39 @@ function Test-TimeSync {
if ($emqxCertCheck -match "notAfter=(.+)") {
$expiryDateStr = $Matches[1].Trim()
try {
# 尝试多种日期格式解析(兼容PowerShell 5.x)
# OpenSSL证书日期格式: "MMM dd HH:mm:ss yyyy GMT"
# 使用ParseExact + InvariantCulture解析(兼容中文系统)
$expiryDate = $null
$formats = @(
"MMM dd HH:mm:ss yyyy GMT",
"MMM d HH:mm:ss yyyy GMT",
"yyyy-MM-dd HH:mm:ss",
"yyyy/MM/dd HH:mm:ss"
)
foreach ($fmt in $formats) {
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$style = [System.Globalization.DateTimeStyles]::None
if ([DateTime]::TryParseExact($expiryDateStr, $fmt, $culture, $style, [ref]$expiryDate)) {
break
# 先尝试带GMT后缀的格式
try {
$expiryDate = [DateTime]::ParseExact($expiryDateStr, "MMM dd HH:mm:ss yyyy GMT", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 如果失败,尝试单数日期格式(双空格)
try {
$expiryDate = [DateTime]::ParseExact($expiryDateStr, "MMM d HH:mm:ss yyyy GMT", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 如果还是失败,尝试不带GMT的格式
$withoutGMT = $expiryDateStr -replace "\s+GMT$", ""
try {
$expiryDate = [DateTime]::ParseExact($withoutGMT, "MMM dd HH:mm:ss yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
# 最后尝试单数日期不带GMT
try {
$expiryDate = [DateTime]::ParseExact($withoutGMT, "MMM d HH:mm:ss yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
}
catch {
Write-Log "EMQX SSL证书日期解析失败: $expiryDateStr" "WARN"
}
}
}
# 如果上述格式都失败,尝试通用解析
if (-not $expiryDate) {
$expiryDate = [DateTime]::Parse($expiryDateStr)
}
if ($expiryDate) {
$daysLeft = ($expiryDate - [DateTime]::Now).Days
if ($daysLeft -lt 7) {
......@@ -1541,8 +1567,9 @@ function Test-TimeSync {
Message = "EMQX SSL证书有效期至 $expiryDateStr"
}
}
}
catch {
Write-Log "EMQX SSL证书日期解析失败: $expiryDateStr - $($_.Exception.Message)" "WARN"
Write-Log "EMQX SSL证书日期处理失败: $($_.Exception.Message)" "WARN"
}
}
}
......@@ -2440,11 +2467,12 @@ function Test-JavaApplication {
# 堆内存设置检测
$heapSettings = Invoke-SSHCommand "docker exec ujava2 ps aux | grep java | grep -oE '-Xm[sx][0-9A-Za-z]+' | head -3" -Timeout 10
if ($heapSettings) {
if ($heapSettings -and $heapSettings -notmatch "error|Error|ERROR") {
if ($heapSettings -is [array]) { $heapSettings = $heapSettings -join " " }
$heapSettingsString = "$heapSettings".Trim()
$results += [PSCustomObject]@{
Name = "堆内存设置"
Value = $heapSettings.Trim()
Value = $heapSettingsString
Threshold = "-"
Status = "正常"
Message = "JVM堆内存参数配置"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论