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

feat(server): 添加MySQL深度检测和优化EMQX Dashboard检测

- 添加QPS统计功能,监控每秒查询数
- 添加TPS统计功能,监控每秒事务数
- 添加死锁检测功能,监控InnoDB死锁数量
- 添加Buffer Pool命中率检测,监控缓冲池性能
- 添加表缓存命中率检测,监控表打开缓存性能
- 优化EMQX Dashboard检测逻辑,支持多种HTTP状态码判断
- 改进EMQX Dashboard认证检测,区分正常访问和认证需求
- 添加详细的MySQL
上级 73232d18
......@@ -1974,7 +1974,122 @@ function Test-MySQLStatus {
}
}
# 数据库列表
# ========== MySQL深度检测 ==========
# QPS统计 (每秒查询数)
$qpsQuery = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW GLOBAL STATUS LIKE \"Questions\";' 2>&1" -Timeout 10
if ($qpsQuery -match "Questions\s+(\d+)") {
$questions = [long]$Matches[1]
$uptimeSec = $uptimeSeconds
if ($uptimeSec -gt 0) {
$qps = [Math]::Round($questions / $uptimeSec, 2)
$results += [PSCustomObject]@{
Name = "MySQL QPS"
Value = "$qps"
Threshold = "-"
Status = "正常"
Message = "每秒查询数 (Questions / Uptime)"
}
}
}
# TPS统计 (每秒事务数)
$tpsCommit = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW GLOBAL STATUS LIKE \"Com_commit\";' 2>&1" -Timeout 10
$tpsRollback = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW GLOBAL STATUS LIKE \"Com_rollback\";' 2>&1" -Timeout 10
if ($tpsCommit -match "Com_commit\s+(\d+)" -and $tpsRollback -match "Com_rollback\s+(\d+)") {
$commits = [long]$Matches[1]
$rollbacks = [long]$Matches[2]
$totalTrans = $commits + $rollbacks
if ($uptimeSec -gt 0) {
$tps = [Math]::Round($totalTrans / $uptimeSec, 2)
$results += [PSCustomObject]@{
Name = "MySQL TPS"
Value = "$tps"
Threshold = "-"
Status = "正常"
Message = "每秒事务数 ((Com_commit + Com_rollback) / Uptime)"
}
}
}
# 死锁检测
$deadlockQuery = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Innodb_deadlocks\";' 2>&1" -Timeout 10
if ($deadlockQuery -match "Innodb_deadlocks\s+(\d+)") {
$deadlockCount = [int]$Matches[1]
$deadlockStatus = Get-StatusByThreshold -Value "$deadlockCount" -WarningThreshold "1" -CriticalThreshold "10" -HigherIsWorse $true
$result = [PSCustomObject]@{
Name = "InnoDB死锁"
Value = "$deadlockCount 个"
Threshold = ">1警告,>10严重"
Status = $deadlockStatus
Message = "InnoDB累计死锁数量"
}
$results += $result
if ($deadlockStatus -ne "正常") {
Add-Issue -Message "MySQL检测到死锁: $deadlockCount 个" -Level $deadlockStatus
}
}
# Buffer Pool命中率
$bpReads = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Innodb_buffer_pool_read_requests\";' 2>&1" -Timeout 10
$bpMisses = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Innodb_buffer_pool_reads\";' 2>&1" -Timeout 10
if ($bpReads -match "Innodb_buffer_pool_read_requests\s+(\d+)" -and $bpMisses -match "Innodb_buffer_pool_reads\s+(\d+)") {
$readRequests = [double]$Matches[1]
$readMisses = [double]$Matches[2]
if ($readRequests -gt 0) {
$hitRate = [Math]::Round((($readRequests - $readMisses) / $readRequests) * 100, 2)
$bpStatus = Get-StatusByThreshold -Value "$hitRate" -WarningThreshold "90" -CriticalThreshold "95" -HigherIsWorse $false
$result = [PSCustomObject]@{
Name = "Buffer Pool命中率"
Value = "$hitRate%"
Threshold = ">95%警告,<90%严重"
Status = $bpStatus
Message = "缓冲池命中率"
}
$results += $result
if ($bpStatus -ne "正常") {
Add-Issue -Message "Buffer Pool命中率过低: $hitRate%" -Level $bpStatus
}
}
}
# 表缓存命中率
$tableOpens = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Opened_tables\";' 2>&1" -Timeout 10
$tableOpenCacheHits = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Table_open_cache_hits\";' 2>&1" -Timeout 10
$tableOpenCacheMisses = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW STATUS LIKE \"Table_open_cache_misses\";' 2>&1" -Timeout 10
if ($tableOpenCacheHits -match "Table_open_cache_hits\s+(\d+)" -and $tableOpenCacheMisses -match "Table_open_cache_misses\s+(\d+)") {
$cacheHits = [double]$Matches[1]
$cacheMisses = [double]$Matches[2]
$totalCacheAccess = $cacheHits + $cacheMisses
if ($totalCacheAccess -gt 0) {
$cacheHitRate = [Math]::Round(($cacheHits / $totalCacheAccess) * 100, 2)
$results += [PSCustomObject]@{
Name = "表缓存命中率"
Value = "$cacheHitRate%"
Threshold = "-"
Status = "正常"
Message = "表打开缓存命中率"
}
}
} elseif ($tableOpens -match "Opened_tables\s+(\d+)") {
# 兼容旧版本MySQL,显示表打开次数
$openedTables = [int]$Matches[1]
$results += [PSCustomObject]@{
Name = "表打开次数"
Value = "$openedTables"
Threshold = "-"
Status = "正常"
Message = "表缓存未命中的打开次数"
}
}
# ========== 数据库列表 ==========
$databases = Invoke-SSHCommand "docker exec umysql mysql -uroot -p'$MYSQL_PASSWORD' -e 'SHOW DATABASES;' 2>&1" -Timeout 10
if ($databases) {
$dbList = ($databases -split "`n" | Where-Object { $_ -notmatch "Database|^\+" } | Where-Object { $_ -ne "information_schema" -and $_ -ne "performance_schema" -and $_ -ne "mysql" }) -join ", "
......@@ -2201,24 +2316,39 @@ function Test-EMQXStatus {
}
}
# EMQX Dashboard API检测(新增)
$dashboardStatus = Invoke-SSHCommand "docker exec uemqx curl -s http://localhost:18083/api/v5/status 2>&1" -Timeout 10
if ($dashboardStatus -match "200|status|ok") {
# EMQX Dashboard API检测(需要认证)
# 使用默认admin/admin密码或跳过认证检测端口是否开放
$dashboardCheck = Invoke-SSHCommand "docker exec uemqx curl -s -o /dev/null -w '%{http_code}' http://localhost:18083 2>&1" -Timeout 10
Write-Log "Dashboard HTTP状态码: $dashboardCheck" "DEBUG"
if ($dashboardCheck -match "200|302|401") {
# 200=正常, 302=重定向到登录页, 401=需要认证(说明服务正常)
$results += [PSCustomObject]@{
Name = "EMQX Dashboard"
Value = "正常"
Threshold = "-"
Status = "正常"
Message = "EMQX管理界面可访问"
Message = "EMQX管理界面可访问 (HTTP $dashboardCheck)"
}
}
else {
elseif ($dashboardCheck -match "000|404|502|503") {
# 000=连接失败, 404=页面不存在, 502/503=服务异常
$results += [PSCustomObject]@{
Name = "EMQX Dashboard"
Value = "异常"
Threshold = "-"
Status = "警告"
Message = "EMQX管理界面无法访问"
Message = "EMQX管理界面无法访问 (HTTP $dashboardCheck)"
}
}
else {
# 未知状态码
$results += [PSCustomObject]@{
Name = "EMQX Dashboard"
Value = "未知"
Threshold = "-"
Status = "正常"
Message = "EMQX Dashboard状态未知 (HTTP $dashboardCheck)"
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论