mirror of
https://gitee.com/mirrors/AllinSSL.git
synced 2026-03-08 15:51:11 +08:00
修改监控为证书监控支持文件导入和smtp监控
监控支持多渠道通知 将静态文件打包到二进制文件
This commit is contained in:
232
backend/app/api/monitor/monitor.go
Normal file
232
backend/app/api/monitor/monitor.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"ALLinSSL/backend/internal/monitor"
|
||||
"ALLinSSL/backend/public"
|
||||
"ALLinSSL/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetMonitorList(c *gin.Context) {
|
||||
var form struct {
|
||||
Search string `form:"search"`
|
||||
Page int64 `form:"p"`
|
||||
Limit int64 `form:"limit"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
data, count, err := monitor.GetList(form.Search, form.Page, form.Limit)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessData(c, data, count)
|
||||
return
|
||||
}
|
||||
|
||||
func AddMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
Name string `form:"name"`
|
||||
Target string `form:"target"`
|
||||
MonitorType string `form:"monitor_type"`
|
||||
ReportTypes string `form:"report_types"`
|
||||
Cycle string `form:"cycle"`
|
||||
RepeatSendGap string `form:"repeat_send_gap"`
|
||||
Active string `form:"active"`
|
||||
AdvanceDay string `form:"advance_day"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
form.Name = strings.TrimSpace(form.Name)
|
||||
form.Target = strings.TrimSpace(form.Target)
|
||||
|
||||
err = monitor.AddMonitor(form.Name, form.Target, form.MonitorType, form.ReportTypes, form.Cycle, form.RepeatSendGap, form.Active, form.AdvanceDay)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessMsg(c, "添加成功")
|
||||
return
|
||||
}
|
||||
|
||||
func UpdMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
Target string `form:"target"`
|
||||
Name string `form:"name"`
|
||||
Cycle string `form:"cycle"`
|
||||
ReportTypes string `form:"report_types"`
|
||||
RepeatSendGap string `form:"repeat_send_gap"`
|
||||
Active string `form:"active"`
|
||||
AdvanceDay string `form:"advance_day"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
form.ID = strings.TrimSpace(form.ID)
|
||||
form.Target = strings.TrimSpace(form.Target)
|
||||
form.Name = strings.TrimSpace(form.Name)
|
||||
form.ReportTypes = strings.TrimSpace(form.ReportTypes)
|
||||
|
||||
err = monitor.UpdMonitor(form.ID, form.Name, form.Target, form.ReportTypes, form.Cycle, form.RepeatSendGap, form.Active, form.AdvanceDay)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessMsg(c, "修改成功")
|
||||
return
|
||||
}
|
||||
|
||||
func DelMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
err = monitor.DelMonitor(form.ID)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessMsg(c, "删除成功")
|
||||
return
|
||||
}
|
||||
|
||||
func SetMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
Active int `form:"active"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
err = monitor.SetMonitor(form.ID, form.Active)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessMsg(c, "操作成功")
|
||||
return
|
||||
}
|
||||
|
||||
func GetMonitorInfo(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
form.ID = strings.TrimSpace(form.ID)
|
||||
data, err := monitor.GetInfo(form.ID)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessData(c, data, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func GetErrRecord(c *gin.Context) {
|
||||
var form struct {
|
||||
ID int64 `form:"id"`
|
||||
Page int64 `form:"p"`
|
||||
Limit int64 `form:"limit"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
data, count, err := monitor.GetErrRecord(form.ID, form.Page, form.Limit)
|
||||
if err != nil {
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessData(c, data, count)
|
||||
return
|
||||
}
|
||||
|
||||
func FileAddMonitor(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
public.FailMsg(c, "上传文件失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if file.Size > 10*1024*1024 { // 限制文件大小为10MB
|
||||
public.FailMsg(c, "上传文件过大,最大限制10MB")
|
||||
return
|
||||
}
|
||||
data, err := monitor.ParseMonitorFile(file)
|
||||
if err != nil {
|
||||
public.FailMsg(c, "文件解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(data) == 0 {
|
||||
public.FailMsg(c, "文件中没有有效的监控数据")
|
||||
return
|
||||
}
|
||||
|
||||
err = monitor.MultiAddMonitor(data)
|
||||
if err != nil {
|
||||
public.FailMsg(c, "文件导入失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
public.SuccessMsg(c, "文件导入成功")
|
||||
return
|
||||
}
|
||||
|
||||
func GetTemplate(c *gin.Context) {
|
||||
t := c.Query("type")
|
||||
if t == "" {
|
||||
c.String(http.StatusBadRequest, "参数 type 不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
fileMap := map[string]string{
|
||||
"txt": "monitor_templates/template.txt",
|
||||
"csv": "monitor_templates/template.csv",
|
||||
"json": "monitor_templates/template.json",
|
||||
"xlsx": "monitor_templates/template.xlsx",
|
||||
}
|
||||
|
||||
filePath, ok := fileMap[strings.ToLower(t)]
|
||||
if !ok {
|
||||
c.String(http.StatusBadRequest, "不支持的类型")
|
||||
return
|
||||
}
|
||||
|
||||
data, err := static.MonitorTemplatesFS.ReadFile(filePath)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "模板文件读取失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置 Content-Type
|
||||
contentTypes := map[string]string{
|
||||
"txt": "text/plain",
|
||||
"csv": "text/csv",
|
||||
"json": "application/json",
|
||||
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"xls": "application/vnd.ms-excel",
|
||||
}
|
||||
c.Header("Content-Type", contentTypes[t])
|
||||
c.Header("Content-Disposition", "attachment; filename=template."+t)
|
||||
c.Data(http.StatusOK, contentTypes[t], data)
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"ALLinSSL/backend/internal/siteMonitor"
|
||||
"ALLinSSL/backend/public"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetMonitorList(c *gin.Context) {
|
||||
var form struct {
|
||||
Search string `form:"search"`
|
||||
Page int64 `form:"p"`
|
||||
Limit int64 `form:"limit"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
data, count, err := siteMonitor.GetList(form.Search, form.Page, form.Limit)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
// c.JSON(http.StatusOK, public.ResOK(len(data), data, ""))
|
||||
public.SuccessData(c, data, count)
|
||||
return
|
||||
}
|
||||
|
||||
func AddMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
Name string `form:"name"`
|
||||
Domain string `form:"domain"`
|
||||
Cycle int `form:"cycle"`
|
||||
ReportType string `form:"report_type"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
form.Name = strings.TrimSpace(form.Name)
|
||||
form.Domain = strings.TrimSpace(form.Domain)
|
||||
|
||||
err = siteMonitor.AddMonitor(form.Name, form.Domain, form.ReportType, form.Cycle)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
// c.JSON(http.StatusOK, public.ResOK(0, nil, "添加成功"))
|
||||
public.SuccessMsg(c, "添加成功")
|
||||
return
|
||||
}
|
||||
|
||||
func UpdMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
Name string `form:"name"`
|
||||
Domain string `form:"domain"`
|
||||
Cycle int `form:"cycle"`
|
||||
ReportType string `form:"report_type"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
form.ID = strings.TrimSpace(form.ID)
|
||||
form.Name = strings.TrimSpace(form.Name)
|
||||
form.Domain = strings.TrimSpace(form.Domain)
|
||||
form.ReportType = strings.TrimSpace(form.ReportType)
|
||||
|
||||
err = siteMonitor.UpdMonitor(form.ID, form.Name, form.Domain, form.ReportType, form.Cycle)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
// c.JSON(http.StatusOK, public.ResOK(0, nil, "修改成功"))
|
||||
public.SuccessMsg(c, "修改成功")
|
||||
return
|
||||
}
|
||||
|
||||
func DelMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
err = siteMonitor.DelMonitor(form.ID)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
// c.JSON(http.StatusOK, public.ResOK(0, nil, "删除成功"))
|
||||
public.SuccessMsg(c, "删除成功")
|
||||
return
|
||||
}
|
||||
|
||||
func SetMonitor(c *gin.Context) {
|
||||
var form struct {
|
||||
ID string `form:"id"`
|
||||
Active int `form:"active"`
|
||||
}
|
||||
err := c.Bind(&form)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
err = siteMonitor.SetMonitor(form.ID, form.Active)
|
||||
if err != nil {
|
||||
// c.JSON(http.StatusBadRequest, public.ResERR(err.Error()))
|
||||
public.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
// c.JSON(http.StatusOK, public.ResOK(0, nil, "操作成功"))
|
||||
public.SuccessMsg(c, "操作成功")
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user