From 8c51294fe7b14efd20bf14147579b63c362a1b02 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Tue, 22 Jun 2021 17:04:09 +0800 Subject: [PATCH] fix(monitor): fix monitor overview MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.监控总览中资源报警统计支持项目钻取 2. 资源报警状态修复 --- pkg/monitor/alerting/conditions/query.go | 6 +++--- pkg/monitor/alerting/notifier.go | 1 - pkg/monitor/models/alertrecord.go | 23 ++++++++++++++++----- pkg/monitor/models/monitor_resource.go | 10 ++++++--- pkg/monitor/models/monitor_resource_sync.go | 2 +- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index a3f8c6b1da..b61a9e531c 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -109,9 +109,9 @@ func (c FormatCond) String() string { func (c *QueryCondition) filterTags(tags map[string]string, details monitor.CommonAlertMetricDetails) map[string]string { ret := make(map[string]string) for key, val := range tags { - if strings.HasSuffix(key, "_id") { - continue - } + //if strings.HasSuffix(key, "_id") { + // continue + //} if len(val) == 0 { continue } diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go index 55f0294944..2ba99b5d08 100644 --- a/pkg/monitor/alerting/notifier.go +++ b/pkg/monitor/alerting/notifier.go @@ -182,7 +182,6 @@ func (n *notificationService) createAlertRecordWhenNotify(evalCtx *EvalContext, recordCreateInput.ResType = recordCreateInput.AlertRule.ResType createData := recordCreateInput.JSON(recordCreateInput) alert, _ := models.CommonAlertManager.GetAlert(evalCtx.Rule.Id) - log.Errorf("alert:%v", jsonutils.Marshal(alert)) record, err := db.DoCreate(models.AlertRecordManager, evalCtx.Ctx, evalCtx.UserCred, jsonutils.NewDict(), createData, evalCtx.UserCred) if err != nil { diff --git a/pkg/monitor/models/alertrecord.go b/pkg/monitor/models/alertrecord.go index 5de39c3a26..c26414ed7d 100644 --- a/pkg/monitor/models/alertrecord.go +++ b/pkg/monitor/models/alertrecord.go @@ -3,6 +3,7 @@ package models import ( "context" "database/sql" + "strings" "time" "yunion.io/x/jsonutils" @@ -213,6 +214,9 @@ func (record *SAlertRecord) GetMoreDetails(out monitor.AlertRecordDetails) (moni if err != nil { return out, errors.Wrap(err, "record Unmarshal evalMatchs error") } + for i, _ := range evalMatchs { + evalMatchs[i] = record.filterTags(evalMatchs[i]) + } out.ResNum = int64(len(evalMatchs)) } commonAlert, _ := CommonAlertManager.GetAlert(record.AlertId) @@ -220,6 +224,15 @@ func (record *SAlertRecord) GetMoreDetails(out monitor.AlertRecordDetails) (moni return out, nil } +func (record *SAlertRecord) filterTags(match monitor.EvalMatch) monitor.EvalMatch { + for key, _ := range match.Tags { + if strings.HasSuffix(key, "_id") { + delete(match.Tags, key) + } + } + return match +} + func (man *SAlertRecordManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, _ jsonutils.JSONObject, data monitor.AlertRecordCreateInput) (monitor.AlertRecordCreateInput, error) { return data, nil } @@ -292,19 +305,19 @@ getNewMatchTag: 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) + err := MonitorResourceManager.UpdateMonitorResourceAttachJoint(ctx, userCred, record) + if err != nil { + log.Errorf("UpdateMonitorResourceAttachJoint error: %v", err) + } if err := GetAlertResourceManager().ReconcileFromRecord(ctx, userCred, ownerId, record); err != nil { log.Errorf("Reconcile from alert record error: %v", err) return } - err := GetAlertResourceManager().NotifyAlertResourceCount(ctx) + err = GetAlertResourceManager().NotifyAlertResourceCount(ctx) if err != nil { log.Errorf("NotifyAlertResourceCount error: %v", err) return } - err = MonitorResourceManager.UpdateMonitorResourceAttachJoint(ctx, userCred, record) - if err != nil { - log.Errorf("UpdateMonitorResourceAttachJoint error: %v", err) - } } func (record *SAlertRecord) GetState() monitor.AlertStateType { diff --git a/pkg/monitor/models/monitor_resource.go b/pkg/monitor/models/monitor_resource.go index fb4b86e935..1c4ce83c66 100644 --- a/pkg/monitor/models/monitor_resource.go +++ b/pkg/monitor/models/monitor_resource.go @@ -253,15 +253,19 @@ type AlertStatusCount struct { } func (manager *SMonitorResourceManager) GetPropertyAlert(ctx context.Context, userCred mcclient.TokenCredential, - query jsonutils.JSONObject) (jsonutils.JSONObject, error) { - scope, _ := query.GetString("scope") + data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + scope, _ := data.GetString("scope") if len(scope) == 0 { scope = "system" } result := jsonutils.NewDict() for resType, _ := range GetResourceSyncMap() { query := manager.Query("alert_state") - manager.FilterByOwner(query, userCred, rbacutils.TRbacScope(scope)) + owner, _ := manager.FetchOwnerId(ctx, data) + if owner == nil { + owner = userCred + } + manager.FilterByOwner(query, owner, rbacutils.TRbacScope(scope)) query.AppendField(sqlchemy.COUNT("count_id", query.Field("id"))) input := monitor.MonitorResourceListInput{ResType: resType} query = manager.FieldListFilter(query, input) diff --git a/pkg/monitor/models/monitor_resource_sync.go b/pkg/monitor/models/monitor_resource_sync.go index 3b2b2f5a12..b367403174 100644 --- a/pkg/monitor/models/monitor_resource_sync.go +++ b/pkg/monitor/models/monitor_resource_sync.go @@ -179,7 +179,7 @@ func GetOnecloudResources(resTyep string) ([]jsonutils.JSONObject, error) { query.Add(jsonutils.NewString("true"), "admin") switch resTyep { case monitor.METRIC_RES_TYPE_HOST: - query.Set("host-type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) + //query.Set("host-type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) allResources, err = ListAllResources(&mc_mds.Hosts, query) case monitor.METRIC_RES_TYPE_GUEST: allResources, err = ListAllResources(&mc_mds.Servers, query)