From 48802cc4329a0700ce96ab7f079ec692d6bfa0c2 Mon Sep 17 00:00:00 2001 From: Akkia Date: Sat, 15 Apr 2023 19:04:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=94=B9=E5=8F=98=E6=97=B6=20=E6=B8=85?= =?UTF-8?q?=E9=99=A4=E5=8F=A6=E4=B8=80=E4=B8=AA=E7=8A=B6=E6=80=81=E7=9A=84?= =?UTF-8?q?muteLabel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/singleton/alertsentinel.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/singleton/alertsentinel.go b/service/singleton/alertsentinel.go index 8b30e2e..4df2687 100644 --- a/service/singleton/alertsentinel.go +++ b/service/singleton/alertsentinel.go @@ -164,6 +164,9 @@ func checkStatus() { }), server.Name, IPDesensitize(server.Host.IP), alert.Name) go SendTriggerTasks(alert.FailTriggerTasks, curServer.ID) go SendNotification(alert.NotificationTag, message, NotificationMuteLabel.ServerIncident(server.ID, alert.ID), &curServer) + // 清除恢复通知的静音缓存 + resolvedMuteLabel := fmt.Sprintf("%s:%s", *NotificationMuteLabel.ServerIncidentResolved(server.ID, alert.ID), alert.NotificationTag) + Cache.Delete(resolvedMuteLabel) } } else { // 本次通过检查但上一次的状态为失败,则发送恢复通知 @@ -173,6 +176,9 @@ func checkStatus() { }), server.Name, IPDesensitize(server.Host.IP), alert.Name) go SendTriggerTasks(alert.RecoverTriggerTasks, curServer.ID) go SendNotification(alert.NotificationTag, message, NotificationMuteLabel.ServerIncidentResolved(server.ID, alert.ID), &curServer) + // 清除失败通知的静音缓存 + incidentMuteLabel := fmt.Sprintf("%s:%s", *NotificationMuteLabel.ServerIncident(server.ID, alert.ID), alert.NotificationTag) + Cache.Delete(incidentMuteLabel) } alertsPrevState[alert.ID][server.ID] = _RuleCheckPass } From e5704157e396bf0972f7355a05bde71a9ffe6bb5 Mon Sep 17 00:00:00 2001 From: Akkia Date: Sat, 15 Apr 2023 19:04:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=A7=A6=E5=8F=91=E4=BB=BB=E5=8A=A1=E6=89=A7?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/member_api.go | 36 ++++++++----- model/monitor.go | 30 +++++++++++ resource/l10n/zh-CN.toml | 3 ++ resource/static/main.js | 53 ++++++++++++++++--- resource/template/component/monitor.html | 29 +++++++++- .../template/dashboard-default/monitor.html | 6 +++ service/singleton/servicesentinel.go | 48 ++++++++++++++--- 7 files changed, 178 insertions(+), 27 deletions(-) diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index fb4d5ba..23b8f0b 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -396,18 +396,21 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) { } type monitorForm struct { - ID uint64 - Name string - Target string - Type uint8 - Cover uint8 - Notify string - NotificationTag string - SkipServersRaw string - Duration uint64 - MinLatency float32 - MaxLatency float32 - LatencyNotify string + ID uint64 + Name string + Target string + Type uint8 + Cover uint8 + Notify string + NotificationTag string + SkipServersRaw string + Duration uint64 + MinLatency float32 + MaxLatency float32 + LatencyNotify string + EnableTriggerTask string + FailTriggerTasksRaw string + RecoverTriggerTasksRaw string } func (ma *memberAPI) addOrEditMonitor(c *gin.Context) { @@ -427,6 +430,9 @@ func (ma *memberAPI) addOrEditMonitor(c *gin.Context) { m.LatencyNotify = mf.LatencyNotify == "on" m.MinLatency = mf.MinLatency m.MaxLatency = mf.MaxLatency + m.EnableTriggerTask = mf.EnableTriggerTask == "on" + m.RecoverTriggerTasksRaw = mf.RecoverTriggerTasksRaw + m.FailTriggerTasksRaw = mf.FailTriggerTasksRaw err = m.InitSkipServers() } if err == nil { @@ -434,6 +440,12 @@ func (ma *memberAPI) addOrEditMonitor(c *gin.Context) { if m.NotificationTag == "" { m.NotificationTag = "default" } + if err == nil { + err = utils.Json.Unmarshal([]byte(mf.FailTriggerTasksRaw), &m.FailTriggerTasks) + } + if err == nil { + err = utils.Json.Unmarshal([]byte(mf.RecoverTriggerTasksRaw), &m.RecoverTriggerTasks) + } if m.ID == 0 { err = singleton.DB.Create(&m).Error } else { diff --git a/model/monitor.go b/model/monitor.go index f019c92..7f24267 100644 --- a/model/monitor.go +++ b/model/monitor.go @@ -48,6 +48,12 @@ type Monitor struct { NotificationTag string // 当前服务监控所属的通知组 Cover uint8 + EnableTriggerTask bool `gorm:"default: false"` + FailTriggerTasksRaw string `gorm:"default:'[]'"` + RecoverTriggerTasksRaw string `gorm:"default:'[]'"` + FailTriggerTasks []uint64 `gorm:"-" json:"-"` // 失败时执行的触发任务id + RecoverTriggerTasks []uint64 `gorm:"-" json:"-"` // 恢复时执行的触发任务id + MinLatency float32 MaxLatency float32 LatencyNotify bool @@ -73,6 +79,21 @@ func (m *Monitor) CronSpec() string { return fmt.Sprintf("@every %ds", m.Duration) } +func (m *Monitor) BeforeSave(tx *gorm.DB) error { + + if data, err := utils.Json.Marshal(m.FailTriggerTasks); err != nil { + return err + } else { + m.FailTriggerTasksRaw = string(data) + } + if data, err := utils.Json.Marshal(m.RecoverTriggerTasks); err != nil { + return err + } else { + m.RecoverTriggerTasksRaw = string(data) + } + return nil +} + func (m *Monitor) AfterFind(tx *gorm.DB) error { m.SkipServers = make(map[uint64]bool) var skipServers []uint64 @@ -83,6 +104,15 @@ func (m *Monitor) AfterFind(tx *gorm.DB) error { for i := 0; i < len(skipServers); i++ { m.SkipServers[skipServers[i]] = true } + + // 加载触发任务列表 + if err := utils.Json.Unmarshal([]byte(m.FailTriggerTasksRaw), &m.FailTriggerTasks); err != nil { + return err + } + if err := utils.Json.Unmarshal([]byte(m.RecoverTriggerTasksRaw), &m.RecoverTriggerTasks); err != nil { + return err + } + return nil } diff --git a/resource/l10n/zh-CN.toml b/resource/l10n/zh-CN.toml index f096e09..125245d 100644 --- a/resource/l10n/zh-CN.toml +++ b/resource/l10n/zh-CN.toml @@ -184,6 +184,9 @@ other = "始终触发" [ModeOnetimeTrigger] other = "单次触发" +[EnableTriggerTask] +other = "启用触发任务" + [FailTriggerTasks] other = "故障时触发任务" diff --git a/resource/static/main.js b/resource/static/main.js index 42b1b68..edc5bf5 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -330,22 +330,61 @@ function addOrEditMonitor(monitor) { modal.find("a.ui.label.visible").each((i, el) => { el.remove(); }); + if (monitor && monitor.EnableTriggerTask) { + modal.find(".ui.nb-EnableTriggerTask.checkbox").checkbox("set checked"); + } else { + modal.find(".ui.nb-EnableTriggerTask.checkbox").checkbox("set unchecked"); + } var servers; + var failTriggerTasks; + var recoverTriggerTasks; if (monitor) { servers = monitor.SkipServersRaw; const serverList = JSON.parse(servers || "[]"); - const node = modal.find("i.dropdown.icon"); + const node = modal.find("i.dropdown.icon.specificServer"); for (let i = 0; i < serverList.length; i++) { node.after( - 'ID:' + - serverList[i] + - '' + 'ID:' + + serverList[i] + + '' + ); + } + + failTriggerTasks = monitor.FailTriggerTasksRaw; + recoverTriggerTasks = monitor.RecoverTriggerTasksRaw; + const failTriggerTasksList = JSON.parse(failTriggerTasks || "[]"); + const recoverTriggerTasksList = JSON.parse(recoverTriggerTasks || "[]"); + const node1 = modal.find("i.dropdown.icon.failTask"); + const node2 = modal.find("i.dropdown.icon.recoverTask"); + for (let i = 0; i < failTriggerTasksList.length; i++) { + node1.after( + 'ID:' + + failTriggerTasksList[i] + + '' + ); + } + for (let i = 0; i < recoverTriggerTasksList.length; i++) { + node2.after( + 'ID:' + + recoverTriggerTasksList[i] + + '' ); } } - modal +modal + .find("input[name=FailTriggerTasksRaw]") + .val(monitor ? "[]," + failTriggerTasks.substr(1, failTriggerTasks.length - 2) : "[]"); +modal + .find("input[name=RecoverTriggerTasksRaw]") + .val(monitor ? "[]," + recoverTriggerTasks.substr(1, recoverTriggerTasks.length - 2) : "[]"); + +modal .find("input[name=SkipServersRaw]") .val(monitor ? "[]," + servers.substr(1, servers.length - 2) : "[]"); showFormModal(".monitor.modal", "#monitorForm", "/api/monitor"); diff --git a/resource/template/component/monitor.html b/resource/template/component/monitor.html index dd08eab..dff1efb 100644 --- a/resource/template/component/monitor.html +++ b/resource/template/component/monitor.html @@ -39,7 +39,7 @@ @@ -68,6 +68,33 @@ + +
+
+ + +
+
+ +
+ + +
+
+ + +
+

diff --git a/resource/template/dashboard-default/monitor.html b/resource/template/dashboard-default/monitor.html index 2d97c75..14589bc 100644 --- a/resource/template/dashboard-default/monitor.html +++ b/resource/template/dashboard-default/monitor.html @@ -22,6 +22,9 @@ {{tr "NotificationMethodGroup"}} {{tr "FailureNotification"}} {{tr "LatencyNotification"}} + {{tr "EnableTriggerTask"}} + {{tr "FailTriggerTasks"}} + {{tr "RecoverTriggerTasks"}} {{tr "Administration"}} @@ -41,6 +44,9 @@ {{$monitor.NotificationTag}} {{$monitor.Notify}} {{$monitor.LatencyNotify}} + {{$monitor.EnableTriggerTask}} + {{$monitor.FailTriggerTasksRaw}} + {{$monitor.RecoverTriggerTasksRaw}}