From aa4b2a8f10065accbf6a0334f4ab49d84fab8ef9 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 2 May 2025 21:58:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9monitor=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 61 ++++++++++++++++++++++++++++--------------------- proto/req.go | 7 ++++++ proto/status.go | 5 ++++ 3 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 proto/req.go diff --git a/handler/tool.go b/handler/tool.go index 677a3e3..820854a 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "io" + "log" "net/http" "os" "strconv" @@ -58,40 +59,48 @@ func SetUpToolGroup(router *gin.Engine) { func SetDeviceStatusV2(c *gin.Context) { // TODO var req SetDeviceStatusReq + var resp proto.GeneralResp if err := c.ShouldBind(&req); err != nil { - c.JSON(200, gin.H{"code": 400, "message": "参数错误"}) - return + resp.Code = proto.ParameterError + resp.Message = "参数解析失败:" + err.Error() } else { token := c.Request.Header.Get("token") if token == "" { - c.JSON(200, gin.H{"code": 401, "message": "token为空"}) - return - } - devices := worker.GetRedisSetMembers(token) - if len(devices) == 0 { - c.JSON(200, gin.H{"code": 402, "message": "设备为空"}) - return - } - for _, v := range devices { - if v == req.ID { - // 继续处理请求 - //是否是暂停之后第一次上线,如果是则发送邮件通知 - device_status := worker.GetRedis("monitor_" + req.ID) - isExist := worker.IsContainKey("monitor_" + req.ID) - if device_status == "2" || !isExist { - //发送邮件通知 - title := "设备上线" - content := "设备上线\n设备:" + req.ID + "\t状态:" + req.Status + "\t时间:" + time.Now().String() - go SendMail(title, content) + resp.Code = proto.TokenIsNull //token为空 + } else { + devices := worker.GetRedisSetMembers(token) + if len(devices) == 0 { + resp.Code = proto.MonitorServerIDIsNull + resp.Message = "服务器设备监控为空!" + } else { + isExit := false + for _, v := range devices { + if v == req.ID { + // 继续处理请求 + //是否是暂停之后第一次上线,如果是则发送邮件通知 + deviceStatus := worker.GetRedis("monitor_" + req.ID) + isExist := worker.IsContainKey("monitor_" + req.ID) + if deviceStatus == "2" || !isExist { + //发送邮件通知 + title := "设备上线" + content := "设备上线\n设备:" + req.ID + "\t状态:" + req.Status + "\t时间:" + time.Now().String() + go SendMail(title, content) + } + worker.SetRedisWithExpire("monitor_"+req.ID, "1", time.Second*300) + resp.Code = proto.SuccessCode + resp.Message = "success" + isExit = true + } + } + if isExit == false { + resp.Code = proto.MonitorServerIDNotFound + resp.Message = "设备不存在!" + log.Println("设备不存在,id:", req.ID, "\ttoken:", token, "\tdevices:", devices) } - worker.SetRedisWithExpire("monitor_"+req.ID, "1", time.Second*300) - c.JSON(200, gin.H{"code": 0, "message": "success"}) - return } } - c.JSON(200, gin.H{"code": 402, "message": "设备不存在"}) } - + c.JSON(http.StatusOK, resp) } func GetFileList(c *gin.Context) { diff --git a/proto/req.go b/proto/req.go new file mode 100644 index 0000000..699e44e --- /dev/null +++ b/proto/req.go @@ -0,0 +1,7 @@ +package proto + +type GeneralResp struct { + Code int `json:"code"` + Message string `json:"message"` + Data any `json:"data"` +} diff --git a/proto/status.go b/proto/status.go index a4e005a..3d9710b 100644 --- a/proto/status.go +++ b/proto/status.go @@ -73,4 +73,9 @@ const ( ShellUpdateFailed = 101 // 更新shell失败 ShellDeleteFailed = 102 // 删除shell失败 ShellSearchFailed = 103 // 获取shell失败 + + //monitor部分错误码 + MonitorServerIDIsNull = 110 // 监控服务器ID为空 + MonitorServerIDNotFound = 111 // 监控服务器ID不存在 + )