Merge pull request #6556 from zhaoxiangchun/automated-cherry-pick-of-#6544-upstream-release-3.2

Automated cherry pick of #6544: 类型是onecloud的notifier增加webconsole的alert
This commit is contained in:
yunion-ci-robot
2020-05-28 18:59:00 +08:00
committed by GitHub
@@ -114,6 +114,11 @@ func GetNotifyTemplateConfig(ctx *alerting.EvalContext) monitor.NotificationTemp
// Notify sends the alert notification.
func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONObject) error {
//onecloud 默认向webconsole发送消息
if err := WebConsoleNotify(ctx, oc.Setting.UserIds); err != nil {
log.Errorf("failed to send webconsole %s: %v", oc.GetNotifierId(), err)
}
log.Infof("Sending alert notification %s to onecloud", ctx.GetRuleTitle())
config := GetNotifyTemplateConfig(ctx)
contentConfig := oc.buildContent(config)
@@ -144,3 +149,24 @@ func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONOb
func (oc *OneCloudNotifier) buildContent(config monitor.NotificationTemplateConfig) *templates.TemplateConfig {
return templates.NewTemplateConfig(config)
}
func WebConsoleNotify(ctx *alerting.EvalContext, ids []string) error {
log.Infof("Sending alert notification %s to webconsole", ctx.GetRuleTitle())
config := GetNotifyTemplateConfig(ctx)
contentConfig := templates.NewTemplateConfig(config)
content, err := contentConfig.GenerateMarkdown()
if err != nil {
return errors.Wrap(err, "build content")
}
msg := notify.SNotifyMessage{
Uid: ids,
ContactType: notify.NotifyByWebConsole,
Topic: config.Title,
Priority: notify.TNotifyPriority(config.Priority),
Msg: content,
Broadcast: true,
}
session := auth.GetAdminSession(ctx.Ctx, "", "")
return notify.Notifications.Send(session, msg)
}