diff --git a/handler/tool.go b/handler/tool.go index 8cce9f6..f9ec7d0 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,38 @@ 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.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 { + // 继续处理请求 + 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 +222,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..583c28c 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" @@ -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"` // 服务端口 } // 读取配置文件 @@ -88,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 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 +}