From b25c52352823c4cc4ffc85cad4d311dba94cb0ae Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Thu, 14 Apr 2022 18:56:14 +0800 Subject: [PATCH] code refactor, use NotifyBuiltinChannels to control --- etc/server.conf | 4 ++-- src/server/config/config.go | 14 +++++++------- src/server/engine/notify.go | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/etc/server.conf b/etc/server.conf index bd425485..6c9a3cb7 100644 --- a/etc/server.conf +++ b/etc/server.conf @@ -70,8 +70,8 @@ Batch = 5 [Alerting] TemplatesDir = "./etc/template" NotifyConcurrency = 10 -# use builtin go code notify by default -NotifyBuiltinEnable = true +# use builtin go code notify +NotifyBuiltinChannels = ["email", "dingtalk", "wecom", "feishu"] [Alerting.CallScript] # built in sending capability in go code diff --git a/src/server/config/config.go b/src/server/config/config.go index b9c81bc3..10f5df7d 100644 --- a/src/server/config/config.go +++ b/src/server/config/config.go @@ -153,13 +153,13 @@ type SMTPConfig struct { } type Alerting struct { - TemplatesDir string - NotifyConcurrency int - NotifyBuiltinEnable bool - CallScript CallScript - CallPlugin CallPlugin - RedisPub RedisPub - Webhook Webhook + TemplatesDir string + NotifyConcurrency int + NotifyBuiltinChannels []string + CallScript CallScript + CallPlugin CallPlugin + RedisPub RedisPub + Webhook Webhook } type CallScript struct { diff --git a/src/server/engine/notify.go b/src/server/engine/notify.go index 530c7b94..52933a45 100644 --- a/src/server/engine/notify.go +++ b/src/server/engine/notify.go @@ -19,6 +19,7 @@ import ( "github.com/toolkits/pkg/file" "github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/runner" + "github.com/toolkits/pkg/slice" "github.com/didi/nightingale/v5/src/models" "github.com/didi/nightingale/v5/src/pkg/sys" @@ -123,7 +124,7 @@ func handleNotice(notice Notice, bs []byte) { alertingCallPlugin(bs) - if !config.C.Alerting.NotifyBuiltinEnable { + if len(config.C.Alerting.NotifyBuiltinChannels) == 0 { return } @@ -173,6 +174,10 @@ func handleNotice(notice Notice, bs []byte) { continue } + if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "email") { + continue + } + subject, has := notice.Tpls["subject.tpl"] if !has { subject = "subject.tpl not found" @@ -189,6 +194,10 @@ func handleNotice(notice Notice, bs []byte) { continue } + if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "dingtalk") { + continue + } + content, has := notice.Tpls["dingtalk.tpl"] if !has { content = "dingtalk.tpl not found" @@ -205,6 +214,10 @@ func handleNotice(notice Notice, bs []byte) { continue } + if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "wecom") { + continue + } + content, has := notice.Tpls["wecom.tpl"] if !has { content = "wecom.tpl not found" @@ -218,6 +231,10 @@ func handleNotice(notice Notice, bs []byte) { continue } + if !slice.ContainsString(config.C.Alerting.NotifyBuiltinChannels, "feishu") { + continue + } + content, has := notice.Tpls["feishu.tpl"] if !has { content = "feishu.tpl not found"