修改monitor监控设备状态部分

This commit is contained in:
junleea 2025-05-02 21:58:40 +08:00
parent 770a9df5d7
commit aa4b2a8f10
3 changed files with 47 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -58,40 +59,48 @@ func SetUpToolGroup(router *gin.Engine) {
func SetDeviceStatusV2(c *gin.Context) { func SetDeviceStatusV2(c *gin.Context) {
// TODO // TODO
var req SetDeviceStatusReq var req SetDeviceStatusReq
var resp proto.GeneralResp
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
c.JSON(200, gin.H{"code": 400, "message": "参数错误"}) resp.Code = proto.ParameterError
return resp.Message = "参数解析失败:" + err.Error()
} else { } else {
token := c.Request.Header.Get("token") token := c.Request.Header.Get("token")
if token == "" { if token == "" {
c.JSON(200, gin.H{"code": 401, "message": "token为空"}) resp.Code = proto.TokenIsNull //token为空
return } else {
} devices := worker.GetRedisSetMembers(token)
devices := worker.GetRedisSetMembers(token) if len(devices) == 0 {
if len(devices) == 0 { resp.Code = proto.MonitorServerIDIsNull
c.JSON(200, gin.H{"code": 402, "message": "设备为空"}) resp.Message = "服务器设备监控为空!"
return } else {
} isExit := false
for _, v := range devices { for _, v := range devices {
if v == req.ID { if v == req.ID {
// 继续处理请求 // 继续处理请求
//是否是暂停之后第一次上线,如果是则发送邮件通知 //是否是暂停之后第一次上线,如果是则发送邮件通知
device_status := worker.GetRedis("monitor_" + req.ID) deviceStatus := worker.GetRedis("monitor_" + req.ID)
isExist := worker.IsContainKey("monitor_" + req.ID) isExist := worker.IsContainKey("monitor_" + req.ID)
if device_status == "2" || !isExist { if deviceStatus == "2" || !isExist {
//发送邮件通知 //发送邮件通知
title := "设备上线" title := "设备上线"
content := "设备上线\n设备:" + req.ID + "\t状态:" + req.Status + "\t时间" + time.Now().String() content := "设备上线\n设备:" + req.ID + "\t状态:" + req.Status + "\t时间" + time.Now().String()
go SendMail(title, content) 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) { func GetFileList(c *gin.Context) {

7
proto/req.go Normal file
View File

@ -0,0 +1,7 @@
package proto
type GeneralResp struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
}

View File

@ -73,4 +73,9 @@ const (
ShellUpdateFailed = 101 // 更新shell失败 ShellUpdateFailed = 101 // 更新shell失败
ShellDeleteFailed = 102 // 删除shell失败 ShellDeleteFailed = 102 // 删除shell失败
ShellSearchFailed = 103 // 获取shell失败 ShellSearchFailed = 103 // 获取shell失败
//monitor部分错误码
MonitorServerIDIsNull = 110 // 监控服务器ID为空
MonitorServerIDNotFound = 111 // 监控服务器ID不存在
) )