From 42942e8876aa602c124d3c39bbbdac8e509c3253 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Thu, 7 Nov 2024 15:45:27 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E3=80=81=E8=AE=BE=E5=A4=87=E7=9B=91=E6=8E=A7=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E9=82=AE=E4=BB=B6=E9=80=9A=E7=9F=A5=EF=BC=9B?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=8F=A3=E5=8F=8A?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 5 ++- proto/conf.go | 2 ++ vp.conf | 2 ++ worker/myMail.go | 38 ++++++++++++++++++++++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 worker/myMail.go diff --git a/handler/tool.go b/handler/tool.go index 8cce9f6..217497f 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -7,9 +7,11 @@ import ( "net/http" "os" "strconv" + "time" "videoplayer/dao" "videoplayer/proto" "videoplayer/service" + "videoplayer/worker" ) type SetRedisReq struct { @@ -19,6 +21,11 @@ type SetRedisReq struct { 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) { toolGroup := router.Group("/tool") toolGroup.POST("/set_redis", SetRedis) @@ -29,6 +36,34 @@ func SetUpToolGroup(router *gin.Engine) { toolGroup.GET("/download", DownloadFile) //文件管理 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) { @@ -183,3 +218,50 @@ func GetRedis(c *gin.Context) { 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) + } +} diff --git a/main.go b/main.go index c42348e..1dd4629 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func main() { log.Fatal("添加定时任务失败: ", err) } 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() { // 创建cid的目录 @@ -153,4 +153,7 @@ func myTask() { // 定时任务 //redis中取出数据 handler.RunCron() + if proto.Config.MONITOR { + handler.ScanDeviceStatus() + } } diff --git a/proto/conf.go b/proto/conf.go index 15b5053..cc4fbdb 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -73,6 +73,8 @@ type ConfigStruct struct { TOKEN_SECRET string `json:"token_secret"` CID_BASE_DIR string `json:"cid_base_dir"` FILE_BASE_DIR string `json:"file_base_dir"` + MONITOR bool `json:"monitor"` // 状态监控及邮件通知 + SERVER_PORT string `json:"server_port"` // 服务端口 } // 读取配置文件 diff --git a/vp.conf b/vp.conf index a0c1ecb..33869ad 100644 --- a/vp.conf +++ b/vp.conf @@ -10,4 +10,6 @@ "token_secret":"mfjurnc_32ndj9dfhj", "cid_base_dir":"/home/lijun/cid/", "file_base_dir":"/home/lijun/file/", + "monitor":false, + "server_port":"8083" } \ No newline at end of file diff --git a/worker/myMail.go b/worker/myMail.go new file mode 100644 index 0000000..797516f --- /dev/null +++ b/worker/myMail.go @@ -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 +} From b83195da919246a9b2b22451f5ff16784a561ba0 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Thu, 7 Nov 2024 15:52:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=8D=E8=BF=9B=E8=A1=8C=E7=99=BB=E5=BD=95=E6=A3=80=E6=B5=8B?= =?UTF-8?q?,=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 4 ++++ proto/conf.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/handler/tool.go b/handler/tool.go index 217497f..1ddb1a0 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -48,6 +48,10 @@ func SetDeviceStatusV2(c *gin.Context) { return } else { token, _ := c.Get("token") + if token == nil { + c.JSON(200, gin.H{"code": 401, "message": "token为空"}) + return + } devices := worker.GetRedisSetMembers(token.(string)) if len(devices) == 0 { c.JSON(200, gin.H{"code": 402, "message": "设备为空"}) diff --git a/proto/conf.go b/proto/conf.go index cc4fbdb..97a207e 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -9,7 +9,7 @@ import ( var Config ConfigStruct var SigningKey = []byte{} -var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true} +var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true} const ( MYSQL_USER = "video_t2" From f15a1d605bcafb10404bf02ffde57f19c8b14364 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Thu, 7 Nov 2024 15:55:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proto/conf.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/proto/conf.go b/proto/conf.go index 97a207e..77e49a1 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -90,6 +90,10 @@ func ReadConfig(path string) error { err = decoder.Decode(&Config) if err != nil { fmt.Println("Error decoding config") + } else { + if Config.SERVER_PORT == "" { + Config.SERVER_PORT = "8083" // 默认端口 + } } SigningKey = []byte(Config.TOKEN_SECRET) return err From fec81d2beac50cb4a52e6e729ffb0f01e217750b Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 24 Nov 2024 15:04:19 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81token=E9=AA=8C=E8=AF=81url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proto/conf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proto/conf.go b/proto/conf.go index 15b5053..04b572c 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -9,7 +9,7 @@ import ( var Config ConfigStruct var SigningKey = []byte{} -var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true} +var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true} // 不需要token验证的url const ( MYSQL_USER = "video_t2" From b2053f14e481f11879fbd60d7bc7b2913688cac0 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 24 Nov 2024 15:19:37 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmonitor=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index 1ddb1a0..f9ec7d0 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -47,12 +47,12 @@ func SetDeviceStatusV2(c *gin.Context) { c.JSON(200, gin.H{"code": 400, "message": "参数错误"}) return } else { - token, _ := c.Get("token") - if token == nil { + token := c.Request.Header.Get("token") + if token == "" { c.JSON(200, gin.H{"code": 401, "message": "token为空"}) return } - devices := worker.GetRedisSetMembers(token.(string)) + devices := worker.GetRedisSetMembers(token) if len(devices) == 0 { c.JSON(200, gin.H{"code": 402, "message": "设备为空"}) return