添加服务器、设备监控功能,邮件通知;添加服务端口及监控配置文件更改功能

This commit is contained in:
junleea 2024-11-07 15:45:27 +08:00
parent 021c475720
commit 42942e8876
5 changed files with 128 additions and 1 deletions

View File

@ -7,9 +7,11 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"time"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
"videoplayer/service" "videoplayer/service"
"videoplayer/worker"
) )
type SetRedisReq struct { type SetRedisReq struct {
@ -19,6 +21,11 @@ type SetRedisReq struct {
Expire int `json:"expire" form:"expire"` Expire int `json:"expire" form:"expire"`
} }
type SetDeviceStatusReq struct {
ID string `json:"id" form:"id"` //设备编码
Status string `json:"status" form:"status"` //设备状态
}
func SetUpToolGroup(router *gin.Engine) { func SetUpToolGroup(router *gin.Engine) {
toolGroup := router.Group("/tool") toolGroup := router.Group("/tool")
toolGroup.POST("/set_redis", SetRedis) toolGroup.POST("/set_redis", SetRedis)
@ -29,6 +36,34 @@ func SetUpToolGroup(router *gin.Engine) {
toolGroup.GET("/download", DownloadFile) toolGroup.GET("/download", DownloadFile)
//文件管理 //文件管理
toolGroup.POST("/file_del", DelFile) toolGroup.POST("/file_del", DelFile)
//服务器、设备状态接口
toolGroup.POST("/monitor", SetDeviceStatusV2)
}
func SetDeviceStatusV2(c *gin.Context) {
// TODO
var req SetDeviceStatusReq
if err := c.ShouldBind(&req); err != nil {
c.JSON(200, gin.H{"code": 400, "message": "参数错误"})
return
} else {
token, _ := c.Get("token")
devices := worker.GetRedisSetMembers(token.(string))
if len(devices) == 0 {
c.JSON(200, gin.H{"code": 402, "message": "设备为空"})
return
}
for _, v := range devices {
if v == req.ID {
// 继续处理请求
worker.SetRedisWithExpire(req.ID, "1", 300)
c.JSON(200, gin.H{"code": 0, "message": "success"})
return
}
}
c.JSON(200, gin.H{"code": 402, "message": "设备不存在"})
}
} }
func DelFile(c *gin.Context) { func DelFile(c *gin.Context) {
@ -183,3 +218,50 @@ func GetRedis(c *gin.Context) {
return return
} }
} }
// 服务器、设备状态扫描
func ScanDeviceStatus() {
// TODO
// 检查设备状态
// 如果设备状态异常, 则发送邮件通知
devices := worker.GetRedisSetMembers("627gyf3488h")
offline := ""
for _, v := range devices {
res := worker.GetRedis(v)
if res == "" {
c := worker.IsContainKey("monitor_" + v)
if c == false {
worker.SetRedisWithExpire("monitor_"+v, "1", time.Hour*12)
offline += v + ","
}
}
}
if offline != "" {
title := "设备状态异常"
content := "设备状态异常\n设备: " + offline + "\t时间" + time.Now().String()
go SendMail(title, content)
}
}
func SendMail(title, content string) {
// TODO
// 发送邮件
// 邮件内容
// 邮件标题
// 收件人
// 发送邮件
// 发送邮件通知
// 发送邮件通知
var em worker.MyEmail
em.SmtpPassword = "nihzazdkmucnbhid"
em.SmtpHost = "pop.qq.com:587"
em.SmtpUserName = "354425203@qq.com"
em.SmtpPort = 587
em.ImapPort = 993
err := em.Send(title, content, []string{"3236990479@qq.com", "lijun@ljsea.top"})
if err != nil {
fmt.Println(err)
}
}

View File

@ -43,7 +43,7 @@ func main() {
log.Fatal("添加定时任务失败: ", err) log.Fatal("添加定时任务失败: ", err)
} }
c.Start() c.Start()
r.Run(":8083") // listen and serve on 0.0.0.0:8083 r.Run(":" + proto.Config.SERVER_PORT) // listen and serve on 0.0.0.0:8083
} }
func init() { func init() {
// 创建cid的目录 // 创建cid的目录
@ -153,4 +153,7 @@ func myTask() {
// 定时任务 // 定时任务
//redis中取出数据 //redis中取出数据
handler.RunCron() handler.RunCron()
if proto.Config.MONITOR {
handler.ScanDeviceStatus()
}
} }

View File

@ -73,6 +73,8 @@ type ConfigStruct struct {
TOKEN_SECRET string `json:"token_secret"` TOKEN_SECRET string `json:"token_secret"`
CID_BASE_DIR string `json:"cid_base_dir"` CID_BASE_DIR string `json:"cid_base_dir"`
FILE_BASE_DIR string `json:"file_base_dir"` FILE_BASE_DIR string `json:"file_base_dir"`
MONITOR bool `json:"monitor"` // 状态监控及邮件通知
SERVER_PORT string `json:"server_port"` // 服务端口
} }
// 读取配置文件 // 读取配置文件

View File

@ -10,4 +10,6 @@
"token_secret":"mfjurnc_32ndj9dfhj", "token_secret":"mfjurnc_32ndj9dfhj",
"cid_base_dir":"/home/lijun/cid/", "cid_base_dir":"/home/lijun/cid/",
"file_base_dir":"/home/lijun/file/", "file_base_dir":"/home/lijun/file/",
"monitor":false,
"server_port":"8083"
} }

38
worker/myMail.go Normal file
View File

@ -0,0 +1,38 @@
package worker
import (
"fmt"
"log"
"net/smtp"
)
type MyEmail struct {
SmtpPort int
ImapPort int
SmtpHost string
SmtpUserName string
SmtpPassword string
}
func (e *MyEmail) Send(title, content string, toEmail []string) error {
// 设置邮件头部
header := make(map[string]string)
header["From"] = e.SmtpUserName
header["To"] = toEmail[0]
header["Subject"] = title
// 组装邮件消息
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + content
// 发送邮件
err := smtp.SendMail(e.SmtpHost, smtp.PlainAuth("", e.SmtpUserName, e.SmtpPassword, "pop.qq.com"), e.SmtpUserName, toEmail, []byte(message))
if err != nil {
log.Fatalf("smtp error: %s", err)
}
return nil
}