mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-31 01:35:56 +08:00
Merge pull request #12307 from zhaoxiangchun/fix/zxc-monitor-notify-3.8
fix(monitor): monitor alert support multi robot
This commit is contained in:
@@ -56,6 +56,8 @@ type CommonAlertCreateInput struct {
|
||||
Channel []string `json:"channel"`
|
||||
// 通知接受者
|
||||
Recipients []string `json:"recipients"`
|
||||
|
||||
RobotIds []string `json:"robot_ids"`
|
||||
// 静默期
|
||||
SilentPeriod string `json:"silent_period"`
|
||||
// 报警类型
|
||||
@@ -132,6 +134,7 @@ type CommonAlertDetails struct {
|
||||
NotifierId string `json:"notifier_id"`
|
||||
Channel []string `json:"channel"`
|
||||
Recipients []string `json:"recipients"`
|
||||
RobotIds []string `json:"robot_ids"`
|
||||
// 静默期
|
||||
SilentPeriod string `json:"silent_period"`
|
||||
Status string `json:"status"`
|
||||
|
||||
@@ -78,8 +78,9 @@ type NotificationListInput struct {
|
||||
}
|
||||
|
||||
type NotificationSettingOneCloud struct {
|
||||
Channel string `json:"channel"`
|
||||
UserIds []string `json:"user_ids"`
|
||||
Channel string `json:"channel"`
|
||||
UserIds []string `json:"user_ids"`
|
||||
RobotIds []string `json:"robot_ids"`
|
||||
}
|
||||
|
||||
type SendWebhookSync struct {
|
||||
|
||||
@@ -68,7 +68,7 @@ func init() {
|
||||
if err := input.Settings.Unmarshal(settings); err != nil {
|
||||
return input, errors.Wrap(err, "unmarshal setting")
|
||||
}
|
||||
if settings.Channel == "" {
|
||||
if settings.Channel == "" && len(settings.RobotIds) == 0 {
|
||||
return input, httperrors.NewInputParameterError("channel is empty")
|
||||
}
|
||||
ids := make([]string, 0)
|
||||
@@ -187,6 +187,12 @@ func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONOb
|
||||
return oc.notifyByContextLang(langContext, ctx, ids)
|
||||
})
|
||||
}
|
||||
if len(oc.Setting.RobotIds) != 0 {
|
||||
withLangTag := i18n.WithLangTag(context.Background(), language.English)
|
||||
langNotifyGroup.Go(func() error {
|
||||
return oc.notifyByContextLang(withLangTag, ctx, []string{})
|
||||
})
|
||||
}
|
||||
return langNotifyGroup.Wait()
|
||||
}
|
||||
|
||||
@@ -228,6 +234,7 @@ func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *al
|
||||
|
||||
msg := notify.SNotifyMessage{
|
||||
Uid: uids,
|
||||
Robots: oc.Setting.RobotIds,
|
||||
ContactType: notify.TNotifyChannel(oc.Setting.Channel),
|
||||
Topic: config.Title,
|
||||
Priority: notify.TNotifyPriority(config.Priority),
|
||||
@@ -291,6 +298,9 @@ func (oc *OneCloudNotifier) newMeterRemoteMobileContent(config *monitor.Notifica
|
||||
}
|
||||
|
||||
func GetUserLangIdsMap(ids []string) (map[string][]string, error) {
|
||||
if len(ids) == 0 {
|
||||
return map[string][]string{}, nil
|
||||
}
|
||||
session := auth.GetAdminSession(context.Background(), "", "")
|
||||
langIdsMap := make(map[string][]string)
|
||||
params := jsonutils.NewDict()
|
||||
@@ -362,6 +372,10 @@ func (f *sendBodyFactory) newSendnotify(evalCtx *alerting.EvalContext, notifier
|
||||
mobile := new(sendMobileImpl)
|
||||
mobile.sendnotifyBase = def
|
||||
return mobile
|
||||
case string(notify.NotifyByRobot):
|
||||
robot := new(sendRobotImpl)
|
||||
robot.sendnotifyBase = def
|
||||
return robot
|
||||
default:
|
||||
return def
|
||||
}
|
||||
@@ -435,6 +449,19 @@ func (s *sendMobileImpl) send() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type sendRobotImpl struct {
|
||||
*sendnotifyBase
|
||||
}
|
||||
|
||||
func (s *sendRobotImpl) send() error {
|
||||
return SendNotifyInfo(s.sendnotifyBase, s)
|
||||
}
|
||||
|
||||
func (s *sendRobotImpl) execNotifyFunc() error {
|
||||
return notifyclient.NotifyRobotWithCtx(s.Ctx, s.msg.Robots, notify.TNotifyPriority(s.msg.Priority),
|
||||
"DEFAULT", jsonutils.Marshal(&s.config))
|
||||
}
|
||||
|
||||
func SendNotifyInfo(base *sendnotifyBase, imp Isendnotify) error {
|
||||
tmpMatches := base.config.Matches
|
||||
batch := 10
|
||||
|
||||
@@ -41,6 +41,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/notify"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
|
||||
merrors "yunion.io/x/onecloud/pkg/monitor/errors"
|
||||
"yunion.io/x/onecloud/pkg/monitor/validators"
|
||||
@@ -313,20 +314,28 @@ func (alert *SCommonAlert) customizeCreateNotis(ctx context.Context, userCred mc
|
||||
}
|
||||
//user_by 弃用
|
||||
if input.AlertType == monitor.CommonAlertSystemAlertType {
|
||||
return alert.createAlertNoti(ctx, userCred, input.Name, "webconsole", []string{}, input.SilentPeriod, true)
|
||||
return alert.createAlertNoti(ctx, userCred, input.Name, "webconsole", []string{}, nil, input.SilentPeriod, true)
|
||||
}
|
||||
for _, channel := range input.Channel {
|
||||
err := alert.createAlertNoti(ctx, userCred, input.Name, channel, input.Recipients, input.SilentPeriod, false)
|
||||
err := alert.createAlertNoti(ctx, userCred, input.Name, channel, input.Recipients, nil, input.SilentPeriod, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("create notify[channel is %s]error", channel))
|
||||
}
|
||||
}
|
||||
if len(input.RobotIds) != 0 {
|
||||
err := alert.createAlertNoti(ctx, userCred, input.Name, string(notify.NotifyByRobot), []string{},
|
||||
input.RobotIds,
|
||||
input.SilentPeriod, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create notify channel is robot error")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (alert *SCommonAlert) createAlertNoti(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
notiName, channel string, userIds []string, silentPeriod string, isSysNoti bool) error {
|
||||
noti, err := NotificationManager.CreateOneCloudNotification(ctx, userCred, notiName, channel, userIds, silentPeriod)
|
||||
func (alert *SCommonAlert) createAlertNoti(ctx context.Context, userCred mcclient.TokenCredential, notiName, channel string, userIds []string, robotIds []string, silentPeriod string, isSysNoti bool) error {
|
||||
noti, err := NotificationManager.CreateOneCloudNotification(ctx, userCred, notiName, channel, userIds, robotIds,
|
||||
silentPeriod)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create notification")
|
||||
}
|
||||
@@ -620,12 +629,16 @@ func (alert *SCommonAlert) GetMoreDetails(ctx context.Context, out monitor.Commo
|
||||
if i == 0 {
|
||||
out.Recipients = settings.UserIds
|
||||
}
|
||||
if settings.Channel != monitor.DEFAULT_SEND_NOTIFY_CHANNEL {
|
||||
if !utils.IsInStringArray(settings.Channel,
|
||||
[]string{monitor.DEFAULT_SEND_NOTIFY_CHANNEL, string(notify.NotifyByRobot)}) {
|
||||
channel.Insert(settings.Channel)
|
||||
}
|
||||
if noti.Frequency != 0 {
|
||||
out.SilentPeriod = fmt.Sprintf("%dm", noti.Frequency/60)
|
||||
}
|
||||
if len(settings.RobotIds) != 0 {
|
||||
out.RobotIds = settings.RobotIds
|
||||
}
|
||||
}
|
||||
out.Channel = channel.List()
|
||||
out.Status = alert.GetStatus()
|
||||
|
||||
@@ -84,7 +84,7 @@ func (v1man *SV1AlertManager) CreateNotification(
|
||||
channel string,
|
||||
recipients string) (*SNotification, error) {
|
||||
userIds := strings.Split(recipients, ",")
|
||||
return NotificationManager.CreateOneCloudNotification(ctx, userCred, alertName, channel, userIds, "")
|
||||
return NotificationManager.CreateOneCloudNotification(ctx, userCred, alertName, channel, userIds, nil, "")
|
||||
}
|
||||
|
||||
func (man *SNodeAlertManager) ValidateCreateData(
|
||||
|
||||
@@ -160,15 +160,11 @@ func (man *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchemy
|
||||
return q, err
|
||||
}
|
||||
|
||||
func (man *SNotificationManager) CreateOneCloudNotification(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
alertName string,
|
||||
channel string,
|
||||
userIds []string, silentPeriod string) (*SNotification, error) {
|
||||
func (man *SNotificationManager) CreateOneCloudNotification(ctx context.Context, userCred mcclient.TokenCredential, alertName string, channel string, userIds []string, robotIds []string, silentPeriod string) (*SNotification, error) {
|
||||
settings := &monitor.NotificationSettingOneCloud{
|
||||
Channel: channel,
|
||||
UserIds: userIds,
|
||||
Channel: channel,
|
||||
UserIds: userIds,
|
||||
RobotIds: robotIds,
|
||||
}
|
||||
|
||||
newName, err := db.GenerateName(ctx, man, userCred, alertName)
|
||||
|
||||
Reference in New Issue
Block a user