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 +}