修改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"
"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) {

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失败
ShellDeleteFailed = 102 // 删除shell失败
ShellSearchFailed = 103 // 获取shell失败
//monitor部分错误码
MonitorServerIDIsNull = 110 // 监控服务器ID为空
MonitorServerIDNotFound = 111 // 监控服务器ID不存在
)