告警中心 - 新增统一告警(alert_unified)、监控告警规则(monitor_alert_rule)、指标目录(alert_metric_catalog) - 通知策略与消息分发(alert_notify_policy/dispatch/message)、静默规则 - 告警引擎按阈值/SNMP 拆分(alert_engine_monitor/snmp) 监控采集 - SNMP 常量/模板与历史指标(snmp templates/constants、snmp_metrics_history、snmp_collect) - 中间件监控 Redis 采集与种子(middleware_monitor_redis/seed) - 站点探测 pkg(siteprobe) + 站点视图(site_view) 自动化任务 - cron 调度器(cronscheduler)、脚本运行器(scriptrunner)、系统任务(systask)及运行记录(cron_job_run、system_task) 审计与凭证 - 审计设置(audit_setting) + 历史清理(auditcleaner) 前端 - 监控页面(host/middleware/site/snmp)拆分为子模块并大屏化 - 告警中心多 Tab(规则/指标目录/通知策略/静默)、任务管理组件重构、Agent CLI Markdown 渲染 - 移除旧脚本卡片/详情/日志组件,统一为 ScriptTable/ScriptCodeEditor/TerminalOutput Co-Authored-By: Claude <noreply@anthropic.com>
312 lines
7.5 KiB
Go
312 lines
7.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ListCronJobs 获取定时任务列表
|
|
// @Summary 获取定时任务列表
|
|
// @Description 获取所有定时任务列表
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param page query int false "页码"
|
|
// @Param page_size query int false "每页数量"
|
|
// @Success 200 {object} model.Response{data=model.ListResponse}
|
|
// @Router /cron-jobs [get]
|
|
func (h *AutomationHandler) ListCronJobs(c *gin.Context) {
|
|
var query model.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&model.CronJob{}).Count(&total)
|
|
|
|
var jobs []model.CronJob
|
|
h.db.Preload("Script").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&jobs)
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: model.ListResponse{
|
|
List: jobs,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetCronJob 获取定时任务详情
|
|
// @Summary 获取定时任务详情
|
|
// @Description 获取指定定时任务的详细信息
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id} [get]
|
|
func (h *AutomationHandler) GetCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job model.CronJob
|
|
if err := h.db.Preload("Script").First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// CreateCronJob 创建定时任务
|
|
// @Summary 创建定时任务
|
|
// @Description 创建新定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body model.CreateCronJobRequest true "创建请求"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs [post]
|
|
func (h *AutomationHandler) CreateCronJob(c *gin.Context) {
|
|
var req model.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
job := model.CronJob{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
CronExpr: req.CronExpr,
|
|
Type: req.Type,
|
|
Content: req.Content,
|
|
ScriptID: req.ScriptID,
|
|
TargetType: req.TargetType,
|
|
Timeout: req.Timeout,
|
|
NotifyOn: req.NotifyOn,
|
|
Enabled: true,
|
|
}
|
|
|
|
if job.Timeout == 0 {
|
|
job.Timeout = 300
|
|
}
|
|
if job.NotifyOn == "" {
|
|
job.NotifyOn = "failure"
|
|
}
|
|
|
|
if err := h.db.Create(&job).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// UpdateCronJob 更新定时任务
|
|
// @Summary 更新定时任务
|
|
// @Description 更新指定定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Param request body model.CreateCronJobRequest true "更新请求"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id} [put]
|
|
func (h *AutomationHandler) UpdateCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req model.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var job model.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
updates := map[string]interface{}{
|
|
"name": req.Name,
|
|
"description": req.Description,
|
|
"cron_expr": req.CronExpr,
|
|
"type": req.Type,
|
|
"content": req.Content,
|
|
"script_id": req.ScriptID,
|
|
"target_type": req.TargetType,
|
|
"timeout": req.Timeout,
|
|
"notify_on": req.NotifyOn,
|
|
}
|
|
|
|
if err := h.db.Model(&job).Updates(updates).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.Preload("Script").First(&job, id)
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// DeleteCronJob 删除定时任务
|
|
// @Summary 删除定时任务
|
|
// @Description 删除指定定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response
|
|
// @Router /cron-jobs/{id} [delete]
|
|
func (h *AutomationHandler) DeleteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&model.CronJob{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// BatchDeleteCronJobs 批量删除定时任务
|
|
// @Summary 批量删除定时任务
|
|
// @Description 批量删除定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body model.IDsRequest true "删除请求"
|
|
// @Success 200 {object} model.Response
|
|
// @Router /cron-jobs/batch-delete [delete]
|
|
func (h *AutomationHandler) BatchDeleteCronJobs(c *gin.Context) {
|
|
var req model.IDsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
if err := h.db.Delete(&model.CronJob{}, req.IDs).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// ToggleCronJob 切换定时任务状态
|
|
// @Summary 切换定时任务状态
|
|
// @Description 启用或禁用定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id}/toggle [post]
|
|
func (h *AutomationHandler) ToggleCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job model.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
job.Enabled = !job.Enabled
|
|
if err := h.db.Save(&job).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "操作失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "操作成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// ExecuteCronJob 执行定时任务
|
|
// @Summary 执行定时任务
|
|
// @Description 手动执行定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.TaskExecution}
|
|
// @Router /cron-jobs/{id}/execute [post]
|
|
func (h *AutomationHandler) ExecuteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
execution, err := RunCronJobByID(h.db, uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
msg := "执行成功"
|
|
if execution.Status == "failed" {
|
|
msg = "执行失败"
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: msg,
|
|
Data: execution,
|
|
})
|
|
}
|