Merge pull request #8703 from zhaoxiangchun/bugfix/zxc-commonalert

commonalert bugfix:
This commit is contained in:
Zexi Li
2020-11-09 10:44:20 +08:00
committed by GitHub
3 changed files with 112 additions and 0 deletions
@@ -27,6 +27,7 @@ import (
"yunion.io/x/onecloud/pkg/apis/monitor"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/i18n"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -34,6 +35,7 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/modules/notify"
"yunion.io/x/onecloud/pkg/monitor/alerting"
"yunion.io/x/onecloud/pkg/monitor/alerting/notifiers/templates"
"yunion.io/x/onecloud/pkg/monitor/models"
"yunion.io/x/onecloud/pkg/monitor/options"
)
@@ -159,6 +161,9 @@ func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONOb
default:
config = GetNotifyTemplateConfig(ctx)
}
oc.filterMatchTagsForConfig(&config)
contentConfig := oc.buildContent(config)
var content string
@@ -187,6 +192,27 @@ func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONOb
return sendImp.send()
}
var (
companyInfo models.SCompanyInfo
)
func (oc *OneCloudNotifier) filterMatchTagsForConfig(config *monitor.NotificationTemplateConfig) {
ctx := context.Background()
ctx = i18n.WithLangTag(ctx, language.Chinese)
sCompanyInfo, err := models.GetCompanyInfo(ctx)
if err != nil {
log.Errorf("GetCompanyInfo error:%#v", err)
return
}
for i, _ := range config.Matches {
if val, ok := config.Matches[i].Tags[hostconsts.TELEGRAF_TAG_KEY_BRAND]; ok {
if val == hostconsts.TELEGRAF_TAG_ONECLOUD_BRAND {
config.Matches[i].Tags[hostconsts.TELEGRAF_TAG_KEY_BRAND] = sCompanyInfo.Name
}
}
}
}
func (oc *OneCloudNotifier) buildContent(config monitor.NotificationTemplateConfig) *templates.TemplateConfig {
return templates.NewTemplateConfig(config)
}
+52
View File
@@ -2,6 +2,7 @@ package models
import (
"context"
"database/sql"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -165,6 +166,57 @@ func (record *SAlertRecord) GetEvalData() ([]monitor.EvalMatch, error) {
return ret, nil
}
func (record *SAlertRecord) CustomizeCreate(
ctx context.Context, userCred mcclient.TokenCredential,
ownerId mcclient.IIdentityProvider,
query jsonutils.JSONObject,
data jsonutils.JSONObject,
) error {
obj, err := db.NewModelObject(AlertRecordManager)
if err != nil {
return errors.Wrapf(err, "NewModelObject %s", AlertRecordManager.Keyword())
}
q := AlertRecordManager.Query().Equals("alert_id", record.AlertId).Desc("created_at")
if err := q.First(obj); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil
} else {
return errors.Wrapf(err, "Get latest alertrecord error by alertId:%s", record.AlertId)
}
}
latestRecord := obj.(*SAlertRecord)
if latestRecord.GetState() == monitor.AlertStateAlerting && record.GetState() == monitor.AlertStateOK {
err := record.unionEvalMatch(latestRecord)
if err != nil {
return errors.Wrap(err, "unionEvalMatch error")
}
}
return nil
}
func (record *SAlertRecord) unionEvalMatch(alertingRecord *SAlertRecord) error {
matches, err := record.GetEvalData()
if err != nil {
return err
}
alertingMatches, err := alertingRecord.GetEvalData()
if err != nil {
return err
}
newEvalMatchs := make([]monitor.EvalMatch, 0)
getNewMatchTag:
for i, _ := range matches {
for j, _ := range alertingMatches {
if matches[i].Tags["name"] == alertingMatches[j].Tags["name"] {
newEvalMatchs = append(newEvalMatchs, matches[i])
continue getNewMatchTag
}
}
}
record.EvalData = jsonutils.Marshal(&newEvalMatchs)
return nil
}
func (record *SAlertRecord) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
record.SStatusStandaloneResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
if err := GetAlertResourceManager().ReconcileFromRecord(ctx, userCred, ownerId, record); err != nil {
+34
View File
@@ -20,6 +20,8 @@ import (
"yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
merrors "yunion.io/x/onecloud/pkg/monitor/errors"
"yunion.io/x/onecloud/pkg/monitor/validators"
"yunion.io/x/onecloud/pkg/util/rbacutils"
@@ -30,6 +32,10 @@ const (
CommonAlertMetadataAlertType = "alert_type"
CommonAlertMetadataFieldOpt = "field_opt"
CommonAlertMetadataPointStr = "point_str"
COMPANY_COPYRIGHT_ONECLOUD = "北京云联万维技术有限公司"
BRAND_ONECLOUD_NAME_CN = "云联壹云"
BRAND_ONECLOUD_NAME_EN = "YunionCloud"
)
var (
@@ -957,3 +963,31 @@ func (alert *SCommonAlert) PerformConfig(ctx context.Context, userCred mcclient.
})
return jsonutils.Marshal(alert), err
}
type SCompanyInfo struct {
Copyright string `json:"copyright"`
Name string `json:"name"`
}
func GetCompanyInfo(ctx context.Context) (SCompanyInfo, error) {
session := auth.GetAdminSession(context.Background(), "", "")
obj, err := modules.Info.Get(session, "info", jsonutils.NewDict())
if err != nil {
return SCompanyInfo{}, err
}
var info SCompanyInfo
err = obj.Unmarshal(&info)
if err != nil {
return SCompanyInfo{}, err
}
if strings.Contains(info.Copyright, COMPANY_COPYRIGHT_ONECLOUD) {
//lang := i18n.Lang(ctx)
//switch lang {
//case language.English:
// info.Name = BRAND_ONECLOUD_NAME_EN
//default:
//}
info.Name = BRAND_ONECLOUD_NAME_CN
}
return info, nil
}