修改monitor监控设备状态部分
This commit is contained in:
parent
770a9df5d7
commit
aa4b2a8f10
|
|
@ -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
|
||||
}
|
||||
resp.Code = proto.TokenIsNull //token为空
|
||||
} else {
|
||||
devices := worker.GetRedisSetMembers(token)
|
||||
if len(devices) == 0 {
|
||||
c.JSON(200, gin.H{"code": 402, "message": "设备为空"})
|
||||
return
|
||||
}
|
||||
resp.Code = proto.MonitorServerIDIsNull
|
||||
resp.Message = "服务器设备监控为空!"
|
||||
} else {
|
||||
isExit := false
|
||||
for _, v := range devices {
|
||||
if v == req.ID {
|
||||
// 继续处理请求
|
||||
//是否是暂停之后第一次上线,如果是则发送邮件通知
|
||||
device_status := worker.GetRedis("monitor_" + req.ID)
|
||||
deviceStatus := worker.GetRedis("monitor_" + req.ID)
|
||||
isExist := worker.IsContainKey("monitor_" + req.ID)
|
||||
if device_status == "2" || !isExist {
|
||||
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)
|
||||
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
||||
return
|
||||
resp.Code = proto.SuccessCode
|
||||
resp.Message = "success"
|
||||
isExit = true
|
||||
}
|
||||
}
|
||||
c.JSON(200, gin.H{"code": 402, "message": "设备不存在"})
|
||||
if isExit == false {
|
||||
resp.Code = proto.MonitorServerIDNotFound
|
||||
resp.Message = "设备不存在!"
|
||||
log.Println("设备不存在,id:", req.ID, "\ttoken:", token, "\tdevices:", devices)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func GetFileList(c *gin.Context) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package proto
|
||||
|
||||
type GeneralResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
|
@ -73,4 +73,9 @@ const (
|
|||
ShellUpdateFailed = 101 // 更新shell失败
|
||||
ShellDeleteFailed = 102 // 删除shell失败
|
||||
ShellSearchFailed = 103 // 获取shell失败
|
||||
|
||||
//monitor部分错误码
|
||||
MonitorServerIDIsNull = 110 // 监控服务器ID为空
|
||||
MonitorServerIDNotFound = 111 // 监控服务器ID不存在
|
||||
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue