fix: agent_version 为空 & 恢复批量卸载

- 后端上报时若 agent_version 为空则不覆盖已有版本(防止旧Agent清空版本号)
- 安装/升级后写入版本号 v0.1.0(而非 installed/latest)
- DB 存量 online 主机 agent_version 补填 v0.1.0
- 前端恢复批量卸载 Agent 按钮
This commit is contained in:
attacker committed 2026-06-19 01:51:16 +00:00
1 parent 502674d73a
commit 4dd3e3238d
3 files changed
+43 -11

No files matched your search

+14 -3
View File
@@ -96,19 +96,30 @@ func (h *AgentHandler) ReceiveReport(c *gin.Context) {
}
// 直接 SQL 更新 asset_servers 监控字段
// 若 AgentVersion 非空则同步写入,否则保留数据库中已有版本号(防止旧版Agent空字段覆盖已设置的版本)
updateSQL := `UPDATE asset_servers SET
cpu_usage = ?, memory_usage = ?, disk_usage = ?,
cpu = ?, memory = ?, disk = ?,
status = ?, agent_status = ?, agent_version = ?,
status = ?, agent_status = ?` + func() string {
if req.AgentVersion != "" {
return `, agent_version = ?`
}
return ``
}() + `,
hostname = CASE WHEN hostname != ? AND ip = ? THEN ? ELSE hostname END,
updated_at = NOW()`
args := []interface{}{
req.CPU.UsagePercent, req.Memory.UsagePercent, diskAvg,
req.CPU.CoreCount, memMB, diskGB,
"running", "online", req.AgentVersion,
"running", "online",
}
if req.AgentVersion != "" {
args = append(args, req.AgentVersion)
}
args = append(args,
// CASE WHEN hostname != ? AND ip = ? THEN ? ELSE hostname END
req.Hostname, matchIP, req.Hostname,
}
)
if req.System != nil {
if req.System.OSVersion != "" {
updateSQL += ", os = ?"
@@ -171,10 +171,10 @@ func (h *AgentInstallHandler) InstallAgent(c *gin.Context) {
installed := contains(verifyOut, "active") || contains(verifyOut, "running")
if installed {
// 更新服务器 Agent 状态
// 更新服务器 Agent 状态(版本号:安装成功后由 Agent 下一次上报自动更新)
h.db.Model(&server).Updates(map[string]interface{}{
"agent_status": "online",
"agent_version": "installed",
"agent_version": "v0.1.0",
"last_check_at": time.Now(),
})
}
@@ -224,7 +224,7 @@ func (h *AgentInstallHandler) installOne(server models.Server, serverURL, token
if installed {
h.db.Model(&server).Updates(map[string]interface{}{
"agent_status": "online",
"agent_version": "installed",
"agent_version": "v0.1.0",
"last_check_at": time.Now(),
})
}
@@ -433,7 +433,7 @@ func (h *AgentInstallHandler) BatchUpgradeAgent(c *gin.Context) {
// 更新版本号
h.db.Model(&server).Updates(map[string]interface{}{
"agent_version": "latest",
"agent_version": "v0.1.0",
"last_check_at": time.Now(),
})
log.Printf("[BatchUpgrade] %s Agent 升级成功", server.Hostname)