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

commonalert bugfix:
This commit is contained in:
Zexi Li
2020-11-15 07:54:59 +08:00
committed by GitHub
9 changed files with 100 additions and 55 deletions
@@ -1,18 +1,9 @@
## {{.title}}
- 时间: {{.start_time}}
- 级别: {{.level}}
- 详情地址: {{.web_url}}/commonalerts
{{.title}}
策略名称: {{.name}}
触发时间: {{.start_time}}
报警级别: {{.level}}
触发条件: {{.description}}
资源数量:{{len .matches}}
资源名称:{{.resource_name}}
{{ range .matches}}
- 指标: {{.metric}}
- 触发值: {{html .value_str}}
### 触发条件:
- {{html $.description}}
### 标签
> 名称: {{ index .tags "name" }}
> ip: {{ index .tags "ip" }}
> 平台: {{ index .tags "brand" }}
------
{{- end}}
详情地址: {{.web_url}}/commonalerts
@@ -1,18 +1,9 @@
## {{.title}}
- Time: {{.start_time}}
- Level: {{.level}}
- WebUrl: {{.web_url}}/commonalerts
{{.title}}
AlertName: {{.name}}
Time: {{.start_time}}
Level: {{.level}}
TriggerCondition: {{html .description}}
ResourceCount: {{len .matches}}
ResourceName: {{.resource_name}}
{{ range .matches}}
- Metric: {{.metric}}
- Trigger value: {{html .value_str}}
### Trigger condition:
- {{html $.description }}
### Label
> Name: {{ index .tags "name" }}
> IP: {{ index .tags "ip" }}
> Brand: {{ index .tags "brand" }}
------
{{- end}}
WebUrl: {{.web_url}}/commonalerts
+4 -3
View File
@@ -19,9 +19,10 @@ type NotificationTemplateCreateInput struct {
}
type NotificationTemplateConfig struct {
Title string `json:"title"`
Name string `json:"name"`
Matches []EvalMatch `json:"matches"`
Title string `json:"title"`
Name string `json:"name"`
ResourceName string `json:"resource_name"`
Matches []EvalMatch `json:"matches"`
// PrevAlertState AlertStateType `json:"prev_alert_state"`
// State AlertStateType `json:"state"`
NoDataFound bool `json:"no_data"`
+1
View File
@@ -51,4 +51,5 @@ type MetricInputQuery struct {
ProjectId string `json:"project_id"`
MetricQuery []*AlertQuery `json:"metric_query"`
Signature string `json:"signature"`
ShowMeta bool `json:"show_meta"`
}
+25 -9
View File
@@ -17,6 +17,7 @@ package alerting
import (
"context"
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
@@ -186,15 +187,16 @@ func (c *EvalContext) GetNotificationTemplateConfig() monitor.NotificationTempla
desc += "Error: " + c.Error.Error()
}
return monitor.NotificationTemplateConfig{
Title: c.GetNotificationTitle(),
Name: c.Rule.Name,
Matches: c.GetEvalMatches(),
StartTime: c.StartTime.Format("2006-01-02 15:04:05"),
EndTime: c.EndTime.Format("2006-01-02 15:04:05"),
Description: desc,
Level: c.Rule.Level,
NoDataFound: c.NoDataFound,
WebUrl: c.GetCallbackURLPrefix(),
Title: c.GetNotificationTitle(),
Name: c.Rule.Name,
ResourceName: c.GetResourceNameOfMathes(),
Matches: c.GetEvalMatches(),
StartTime: c.StartTime.Format("2006-01-02 15:04:05"),
EndTime: c.EndTime.Format("2006-01-02 15:04:05"),
Description: desc,
Level: c.Rule.Level,
NoDataFound: c.NoDataFound,
WebUrl: c.GetCallbackURLPrefix(),
}
}
@@ -211,3 +213,17 @@ func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch {
}
return ret
}
func (c *EvalContext) GetResourceNameOfMathes() string {
names := strings.Builder{}
matches := c.GetEvalMatches()
for i, match := range matches {
if name, ok := match.Tags["name"]; ok {
names.WriteString(name)
if i < len(matches)-1 {
names.WriteString("、")
}
}
}
return names.String()
}
+5 -7
View File
@@ -153,15 +153,15 @@ func GetNotifyTemplateConfigOfEN(ctx *alerting.EvalContext) monitor.Notification
// Notify sends the alert notification.
func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONObject) error {
log.Infof("Sending alert notification %s to onecloud", ctx.GetRuleTitle())
oc.Ctx = i18n.WithLangTag(oc.Ctx, language.Chinese)
var config monitor.NotificationTemplateConfig
lang := i18n.Lang(ctx.Ctx)
lang := i18n.Lang(oc.Ctx)
switch lang {
case language.English:
config = GetNotifyTemplateConfigOfEN(ctx)
default:
config = GetNotifyTemplateConfig(ctx)
}
oc.filterMatchTagsForConfig(&config)
contentConfig := oc.buildContent(config)
@@ -197,9 +197,7 @@ var (
)
func (oc *OneCloudNotifier) filterMatchTagsForConfig(config *monitor.NotificationTemplateConfig) {
ctx := context.Background()
ctx = i18n.WithLangTag(ctx, language.Chinese)
sCompanyInfo, err := models.GetCompanyInfo(ctx)
sCompanyInfo, err := models.GetCompanyInfo(oc.Ctx)
if err != nil {
log.Errorf("GetCompanyInfo error:%#v", err)
return
@@ -265,7 +263,7 @@ type sendUserImpl struct {
}
func (s *sendUserImpl) send() error {
return notifyclient.NotifyAllWithoutRobot(s.Setting.UserIds, false, notify.TNotifyPriority(s.msg.Priority),
return notifyclient.NotifyAllWithoutRobotWithCtx(s.Ctx, s.Setting.UserIds, false, notify.TNotifyPriority(s.msg.Priority),
"DEFAULT", jsonutils.Marshal(&s.config))
}
@@ -274,7 +272,7 @@ type sendSysImpl struct {
}
func (s *sendSysImpl) send() error {
notifyclient.SystemNotify(notify.TNotifyPriority(s.msg.Priority), "DEFAULT",
notifyclient.SystemNotifyWithCtx(s.Ctx, notify.TNotifyPriority(s.msg.Priority), "DEFAULT",
jsonutils.Marshal(&s.config))
return nil
}
+1 -1
View File
@@ -47,7 +47,7 @@ func (drvF *nodeDriverF) IsEvalMatched(input monitor.EvalMatch) bool {
if !hasHostType {
return false
}
if hostType != hostconsts.TELEGRAF_TAG_ONECLOUD_HOST_TYPE_HOST ||
if hostType != hostconsts.TELEGRAF_TAG_ONECLOUD_HOST_TYPE_HOST &&
hostType != hostconsts.TELEGRAF_TAG_ONECLOUD_HOST_TYPE_CONTROLLER {
return false
}
+45
View File
@@ -109,15 +109,49 @@ func (m *SAlertResourceManager) ReconcileFromRecord(ctx context.Context, userCre
if err != nil {
return errors.Wrapf(err, "Get record %s eval data", record.GetId())
}
oldResources, err := m.getResourceFromAlertId(record.AlertId)
if err != nil {
return errors.Wrap(err, "ReconcileFromRecord getResourceFromAlertId error")
}
errs := make([]error, 0)
for _, match := range matches {
if err := m.reconcileFromRecordMatch(ctx, userCred, ownerId, record, match); err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
delErrs := m.deleteOldResource(ctx, userCred, record, oldResources)
if len(delErrs) != 0 {
errs = append(errs, delErrs...)
}
}
return errors.NewAggregate(errs)
}
func (m *SAlertResourceManager) deleteOldResource(ctx context.Context, userCred mcclient.TokenCredential,
record *SAlertRecord, oldResources []SAlertResource) (errs []error) {
matches, _ := record.GetEvalData()
needDelResources := make([]SAlertResource, 0)
LoopRes:
for _, oldResource := range oldResources {
for _, match := range matches {
resourceD, _ := GetAlertResourceDriver(match)
if oldResource.Name == resourceD.GetUniqCond().Name {
continue LoopRes
}
}
needDelResources = append(needDelResources, oldResource)
}
for i, _ := range needDelResources {
if err := needDelResources[i].DetachAlert(ctx, userCred, record.AlertId); err != nil {
errs = append(errs, errors.Wrapf(err, "deleteOldResource remove resource %s alert %s",
needDelResources[i].GetName(),
record.AlertId))
}
}
return
}
type AlertResourceUniqCond struct {
Type monitor.AlertResourceType
Name string
@@ -142,6 +176,17 @@ func (m *SAlertResourceManager) getResourceFromMatch(ctx context.Context, userCr
return &objs[0], nil
}
func (m *SAlertResourceManager) getResourceFromAlertId(alertId string) ([]SAlertResource, error) {
searchResourceIdQuery := GetAlertResourceAlertManager().Query(GetAlertResourceAlertManager().GetMasterFieldName())
searchResourceIdQuery = searchResourceIdQuery.Equals(GetAlertResourceAlertManager().GetSlaveFieldName(), alertId)
query := m.Query().In("id", searchResourceIdQuery.SubQuery())
objs := make([]SAlertResource, 0)
if err := db.FetchModelObjects(m, query, &objs); err != nil {
return nil, errors.Wrapf(err, "getResourceFromAlertId:%s error", alertId)
}
return objs, nil
}
func (m *SAlertResourceManager) reconcileFromRecordMatch(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, record *SAlertRecord, match monitor.EvalMatch) error {
drv, err := GetAlertResourceDriver(match)
if err != nil {
+3 -1
View File
@@ -252,7 +252,9 @@ func doQuery(query monitor.MetricInputQuery) (*mq.Metrics, error) {
return nil, err
}
// drop metas contains raw_query
metrics.Metas = nil
if !query.ShowMeta {
metrics.Metas = nil
}
return metrics, nil
}