Compare commits

...

4 Commits

Author SHA1 Message Date
junleea 35823bee30 Merge branch 'refs/heads/feat-tool'
# Conflicts:
#	proto/conf.go
2024-11-24 15:10:47 +08:00
junleea f15a1d605b 服务默认端口 2024-11-07 15:55:06 +08:00
junleea b83195da91 监控接口不进行登录检测,修改接口错误返回 2024-11-07 15:52:55 +08:00
junleea 42942e8876 添加服务器、设备监控功能,邮件通知;添加服务端口及监控配置文件更改功能 2024-11-07 15:46:12 +08:00
5 changed files with 136 additions and 1 deletions

View File

@ -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.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": "设备为空"})
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)
}
}

View File

@ -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()
}
}

View File

@ -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

View File

@ -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"
}

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
}