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

refactor(MiddlewareCheck): 优化 MySQL 深度检测的 SQL 执行方式

- 将 docker exec 管道方式改为 -e 参数直接执行 SQL 语句
- 统一使用双引号保护密码中的特殊字符
- 分离多个 SQL 查询为独立命令以提高可靠性
- 为每个检测项添加无结果时的警告日志记录
- 修复主从复制状态检测中空输出的处理逻辑
- 优化查询失败时的错误信息记录机制
上级 76496f7c
......@@ -1259,14 +1259,13 @@ function Test-MySQLDeepCheck {
$actualContainer = (@($checkResult.Output)[0].ToString().Trim() -replace "`r","")
Write-Log -Level "INFO" -Message "[MySQL深度] 容器: $actualContainer"
# 辅助:通过 docker exec -i 管道方式执行 SQL 查询
# 密码用单引号保护特殊字符(如 & ),-i 允许通过 stdin 管道传入 SQL
$mysqlPipe = "docker exec -i $actualContainer mysql -uroot -p'$mysqlPassword' -N 2>/dev/null"
# 辅助:通过 docker exec mysql -e 方式执行 SQL 查询(与已有 MySQL 连接检测一致)
# 密码用双引号保护特殊字符(如 & ),使用 -e 直接传递 SQL 语句避免管道引号问题
$mysqlExec = "docker exec $actualContainer mysql -uroot -p`"$mysqlPassword`" -N"
# --- 1. 缓冲池命中率 ---
try {
$bpSql = "SHOW STATUS LIKE 'Innodb_buffer_pool_read%';"
$bpCmd = "echo ""$bpSql"" | $mysqlPipe"
$bpCmd = "$mysqlExec -e `"SHOW STATUS LIKE 'Innodb_buffer_pool_read%'`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $bpCmd"
$bpResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $bpCmd
......@@ -1292,6 +1291,8 @@ function Test-MySQLDeepCheck {
Value = "${hitRate}%"
Success = ($hitRate -ge 80)
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] 缓冲池命中率: 查询无结果 (ExitCode=$($bpResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] 缓冲池命中率检测异常: $($_.Exception.Message)"
......@@ -1299,16 +1300,22 @@ function Test-MySQLDeepCheck {
# --- 2. 慢查询状态 ---
try {
$slowSql = "SHOW VARIABLES LIKE 'slow_query_log'; SHOW STATUS LIKE 'Slow_queries';"
$slowCmd = "echo ""$slowSql"" | $mysqlPipe"
$slowCmd = "$mysqlExec -e `"SHOW VARIABLES LIKE 'slow_query_log'`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $slowCmd"
$slowResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $slowCmd
# 同时获取慢查询数量
$slowCountCmd = "$mysqlExec -e `"SHOW STATUS LIKE 'Slow_queries'`""
$slowCountResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $slowCountCmd
if ($slowResult.ExitCode -eq 0 -and $slowResult.Output) {
$slowOutput = $slowResult.Output -join "`n"
$slowEnabled = "OFF"; $slowCount = 0
if ($slowOutput -match 'slow_query_log\s+(ON|OFF)') { $slowEnabled = $matches[1] }
if ($slowOutput -match 'Slow_queries\s+(\d+)') { $slowCount = [long]$matches[1] }
if ($slowCountResult.ExitCode -eq 0 -and $slowCountResult.Output) {
$slowCountOutput = $slowCountResult.Output -join "`n"
if ($slowCountOutput -match 'Slow_queries\s+(\d+)') { $slowCount = [long]$matches[1] }
}
$slowStatus = if ($slowEnabled -eq "ON") { "正常" } else { "警告" }
$slowColor = if ($slowEnabled -eq "ON") { "SUCCESS" } else { "WARN" }
......@@ -1321,6 +1328,8 @@ function Test-MySQLDeepCheck {
Value = "$slowEnabled ($slowCount)"
Success = ($slowEnabled -eq "ON")
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] 慢查询状态: 查询无结果 (ExitCode=$($slowResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] 慢查询状态检测异常: $($_.Exception.Message)"
......@@ -1328,16 +1337,21 @@ function Test-MySQLDeepCheck {
# --- 3. 连接使用率 ---
try {
$connSql = "SHOW STATUS LIKE 'Threads_connected'; SHOW VARIABLES LIKE 'max_connections';"
$connCmd = "echo ""$connSql"" | $mysqlPipe"
$connCmd = "$mysqlExec -e `"SHOW STATUS LIKE 'Threads_connected'`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $connCmd"
$connResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $connCmd
$maxConnCmd = "$mysqlExec -e `"SHOW VARIABLES LIKE 'max_connections'`""
$maxConnResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $maxConnCmd
if ($connResult.ExitCode -eq 0 -and $connResult.Output) {
$connOutput = $connResult.Output -join "`n"
$currentConn = 0; $maxConn = 0
if ($connOutput -match 'Threads_connected\s+(\d+)') { $currentConn = [int]$matches[1] }
if ($connOutput -match 'max_connections\s+(\d+)') { $maxConn = [int]$matches[1] }
if ($maxConnResult.ExitCode -eq 0 -and $maxConnResult.Output) {
$maxConnOutput = $maxConnResult.Output -join "`n"
if ($maxConnOutput -match 'max_connections\s+(\d+)') { $maxConn = [int]$matches[1] }
}
$connRate = 0.0
if ($maxConn -gt 0) {
......@@ -1355,6 +1369,8 @@ function Test-MySQLDeepCheck {
Value = "${connRate}%"
Success = ($connRate -lt 80)
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] 连接使用率: 查询无结果 (ExitCode=$($connResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] 连接使用率检测异常: $($_.Exception.Message)"
......@@ -1362,13 +1378,13 @@ function Test-MySQLDeepCheck {
# --- 4. 主从复制状态 ---
try {
$replSql = "SHOW SLAVE STATUS\G"
$replCmd = "echo ""$replSql"" | $mysqlPipe"
# 使用 -E 参数获取竖向输出格式(等效于 \G)
$replCmd = "$mysqlExec -E -e `"SHOW SLAVE STATUS`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $replCmd"
$replResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $replCmd
if ($replResult.ExitCode -eq 0 -and $replResult.Output) {
$replOutput = $replResult.Output -join "`n"
if ($replResult.ExitCode -eq 0) {
$replOutput = if ($replResult.Output) { $replResult.Output -join "`n" } else { "" }
if ($replOutput -match '\S' -and $replOutput -notmatch 'Empty set') {
$ioRunning = ""; $sqlRunning = ""; $secondsBehind = ""
if ($replOutput -match 'Slave_IO_Running:\s*(\S+)') { $ioRunning = $matches[1] }
......@@ -1397,6 +1413,8 @@ function Test-MySQLDeepCheck {
Success = $true
}
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] 主从复制: 查询无结果 (ExitCode=$($replResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] 主从复制状态检测异常: $($_.Exception.Message)"
......@@ -1404,8 +1422,7 @@ function Test-MySQLDeepCheck {
# --- 5. TOP20 大表 ---
try {
$topTableSql = "SELECT table_name, ROUND(data_length/1024/1024,2) AS data_mb, ROUND(index_length/1024/1024,2) AS idx_mb, table_rows FROM information_schema.TABLES WHERE table_schema NOT IN ('mysql','information_schema','performance_schema','sys') ORDER BY data_length DESC LIMIT 20;"
$topTableCmd = "echo ""$topTableSql"" | $mysqlPipe"
$topTableCmd = "$mysqlExec -e `"SELECT table_name,ROUND(data_length/1024/1024,2) AS data_mb,ROUND(index_length/1024/1024,2) AS idx_mb,table_rows FROM information_schema.TABLES WHERE table_schema NOT IN ('mysql','information_schema','performance_schema','sys') ORDER BY data_length DESC LIMIT 20`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $topTableCmd"
$topTableResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $topTableCmd
......@@ -1422,6 +1439,8 @@ function Test-MySQLDeepCheck {
Value = "$($tableLines.Count)"
Success = $true
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] TOP20大表: 查询无结果 (ExitCode=$($topTableResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] TOP20大表检测异常: $($_.Exception.Message)"
......@@ -1429,16 +1448,21 @@ function Test-MySQLDeepCheck {
# --- 6. QPS/TPS 性能基线 ---
try {
$qpsSql = "SHOW STATUS LIKE 'Queries'; SHOW STATUS LIKE 'Uptime';"
$qpsCmd = "echo ""$qpsSql"" | $mysqlPipe"
$qpsCmd = "$mysqlExec -e `"SHOW STATUS LIKE 'Queries'`""
Write-Log -Level "INFO" -Message "[MySQL深度] 执行: $qpsCmd"
$qpsResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $qpsCmd
$uptimeCmd = "$mysqlExec -e `"SHOW STATUS LIKE 'Uptime'`""
$uptimeResult = Invoke-SSHCommand -HostName $Server.IP -User $Server.User -Pass $Server.Pass -Port $Server.Port -Command $uptimeCmd
if ($qpsResult.ExitCode -eq 0 -and $qpsResult.Output) {
$qpsOutput = $qpsResult.Output -join "`n"
$queries = 0; $uptime = 0
if ($qpsOutput -match 'Queries\s+(\d+)') { $queries = [long]$matches[1] }
if ($qpsOutput -match 'Uptime\s+(\d+)') { $uptime = [long]$matches[1] }
if ($uptimeResult.ExitCode -eq 0 -and $uptimeResult.Output) {
$uptimeOutput = $uptimeResult.Output -join "`n"
if ($uptimeOutput -match 'Uptime\s+(\d+)') { $uptime = [long]$matches[1] }
}
$qps = 0.0
if ($uptime -gt 0) {
......@@ -1453,6 +1477,8 @@ function Test-MySQLDeepCheck {
Value = "$qps"
Success = $true
}
} else {
Write-Log -Level "WARN" -Message "[MySQL深度] QPS: 查询无结果 (ExitCode=$($qpsResult.ExitCode))"
}
} catch {
Write-Log -Level "WARN" -Message "[MySQL深度] QPS检测异常: $($_.Exception.Message)"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论