From 5c2d88c6d9323db87e3599150022a8d0ebfdbe8e Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Wed, 27 Jan 2021 15:47:05 +0800 Subject: [PATCH] feat(monitor): monitor alert policy add mobile notify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.报警策略增加短信通知 --- pkg/monitor/alerting/notifiers/onecloud.go | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go index bda0765e1a..02683a28a3 100644 --- a/pkg/monitor/alerting/notifiers/onecloud.go +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -44,6 +44,9 @@ import ( const ( SUFFIX = "onecloudNotifier" + + MOBILE_TOPIC_CN = "monitor-cn" + MOBILE_TOPIC_EN = "monitor-en" ) var ( @@ -211,6 +214,8 @@ func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *al switch oc.Setting.Channel { case string(notify.NotifyByEmail): content, err = contentConfig.GenerateEmailMarkdown() + case string(notify.NotifyByMobile): + content = oc.newRemoteMobileContent(&config, evalCtx, lang) default: content, err = contentConfig.GenerateMarkdown() } @@ -232,6 +237,19 @@ func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *al return sendImp.send() } +func (oc *OneCloudNotifier) newRemoteMobileContent(config *monitor.NotificationTemplateConfig, + evalCtx *alerting.EvalContext, lang language.Tag) string { + switch lang { + case language.English: + config.Title = MOBILE_TOPIC_EN + default: + config.Title = MOBILE_TOPIC_CN + } + content := jsonutils.NewDict() + content.Set("alert_name", jsonutils.NewString(evalCtx.Rule.Name)) + return content.String() +} + func GetUserLangIdsMap(ids []string) (map[string][]string, error) { session := auth.GetAdminSession(context.Background(), "", "") langIdsMap := make(map[string][]string) @@ -298,6 +316,10 @@ func (f *sendBodyFactory) newSendnotify(notifier *OneCloudNotifier, message noti user := new(sendUserImpl) user.sendnotifyBase = def return user + case string(notify.NotifyByMobile): + mobile := new(sendMobileImpl) + mobile.sendnotifyBase = def + return mobile default: return def } @@ -340,3 +362,19 @@ func (s *sendSysImpl) send() error { jsonutils.Marshal(&s.config)) return nil } + +type sendMobileImpl struct { + *sendnotifyBase +} + +func (s *sendMobileImpl) send() error { + msgObj, err := jsonutils.ParseString(s.msg.Msg) + if err != nil { + return err + } + notifyclient.RawNotifyWithCtx(s.Ctx, s.msg.Uid, false, notify.TNotifyChannel(s.Setting.Channel), + notify.TNotifyPriority(s.msg.Priority), + s.msg.Topic, + msgObj) + return nil +}