From 0919c2ee3ee443a67fa49804b88efeffb5b0b083 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Wed, 7 Jul 2021 17:49:42 +0800 Subject: [PATCH] feat(monitor): update monitor resource overview MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.调整监控总览-报警资源逻辑 启用禁用策略时对报警资源进行对应的挂载和解绑 --- pkg/apis/monitor/monitor_resource_alert.go | 5 +++-- pkg/monitor/alerting/conditions/query.go | 1 + pkg/monitor/models/commonalert.go | 22 ++++++++++++++++++- pkg/monitor/models/monitor_resource.go | 19 ++++++++++++++++ pkg/monitor/models/monitor_resource_alert.go | 7 ++++-- pkg/monitor/models/monitor_resource_sync.go | 1 + .../tasks/detach_alertresource_task.go | 7 ++++++ 7 files changed, 57 insertions(+), 5 deletions(-) diff --git a/pkg/apis/monitor/monitor_resource_alert.go b/pkg/apis/monitor/monitor_resource_alert.go index a7b488be44..3ae8c51ecb 100644 --- a/pkg/apis/monitor/monitor_resource_alert.go +++ b/pkg/apis/monitor/monitor_resource_alert.go @@ -7,8 +7,9 @@ import ( ) type MonitorResourceJointListInput struct { - MonitorResourceId string `json:"monitor_resource_id"` - AlertId string `json:"alert_id"` + MonitorResourceId string `json:"monitor_resource_id"` + AlertId string `json:"alert_id"` + JointId []int64 `json:"joint_id"` } type MonitorResourceJointCreateInput struct { diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index 0c63883ca0..286f44954c 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -516,6 +516,7 @@ func (c *QueryCondition) setResType() { metricMeasurement, _ := models.MetricMeasurementManager.GetCache().Get(c.Query.Model.Measurement) if metricMeasurement != nil { resType = metricMeasurement.ResType + c.ResType = resType } if len(resType) != 0 && c.Query.Model.GroupBy[0].Params[0] != monitor. MEASUREMENT_TAG_ID[resType] { diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 1bb4adbb1c..f9fb496c3d 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -1193,6 +1193,19 @@ func PerformConfigLog(model db.IModel, userCred mcclient.TokenCredential) { logclient.AddSimpleActionLog(model, logclient.ACT_UPDATE_RULE, nil, userCred, true) } +func (alert *SCommonAlert) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) bool { + return db.IsProjectAllowPerform(userCred, alert, "enable") +} + +func (alert *SCommonAlert) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) (jsonutils.JSONObject, error) { + err := db.EnabledPerformEnable(alert, ctx, userCred, true) + if err != nil { + return nil, errors.Wrap(err, "EnabledPerformEnable") + } + alert.StartUpdateMonitorAlertJointTask(ctx, userCred) + return nil, nil +} + func (alert *SCommonAlert) AllowPerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) bool { return db.IsProjectAllowPerform(userCred, alert, "disable") } @@ -1282,6 +1295,7 @@ func (alert *SCommonAlert) UpdateMonitorResourceJoint(ctx context.Context, userC } resourceIds = append(resourceIds, resourceId) } + deleteJointIds := make([]int64, 0) joints, _ := MonitorResourceAlertManager.GetJoinsByListInput(monitor.MonitorResourceJointListInput{AlertId: alert.GetId()}) jointLoop: for _, joint := range joints { @@ -1291,11 +1305,17 @@ jointLoop: continue jointLoop } } - joint.Detach(ctx, userCred) + deleteJointIds = append(deleteJointIds, joint.RowId) } if len(resourceIds) == 0 { return nil } + if len(deleteJointIds) != 0 { + err := MonitorResourceAlertManager.DetachJoint(ctx, userCred, monitor.MonitorResourceJointListInput{JointId: deleteJointIds}) + if err != nil { + return errors.Wrapf(err, "DetachJoint By alertName:%s err", alert.GetName()) + } + } monitorResources, _ := MonitorResourceManager.GetMonitorResources(monitor.MonitorResourceListInput{ResId: resourceIds}) errs := make([]error, 0) for _, monRes := range monitorResources { diff --git a/pkg/monitor/models/monitor_resource.go b/pkg/monitor/models/monitor_resource.go index be8865e8ea..43a3676e63 100644 --- a/pkg/monitor/models/monitor_resource.go +++ b/pkg/monitor/models/monitor_resource.go @@ -304,11 +304,13 @@ func (manager *SMonitorResourceManager) UpdateMonitorResourceAttachJoint(ctx con } matches, _ := alertRecord.GetEvalData() errs := make([]error, 0) + matchResourceIds := make([]string, 0) for _, matche := range matches { resId := matche.Tags[monitor.MEASUREMENT_TAG_ID[alertRecord.ResType]] if len(resId) == 0 { continue } + matchResourceIds = append(matchResourceIds, resId) monitorResources, err := manager.GetMonitorResources(monitor.MonitorResourceListInput{ResType: resType, ResId: []string{resId}}) if err != nil { errs = append(errs, errors.Wrapf(err, "SMonitorResourceManager GetMonitorResources by resId:%s err", resId)) @@ -321,6 +323,23 @@ func (manager *SMonitorResourceManager) UpdateMonitorResourceAttachJoint(ctx con } } } + resourceAlerts, err := MonitorResourceAlertManager.GetJoinsByListInput(monitor.MonitorResourceJointListInput{AlertId: alertRecord.AlertId}) + if err != nil { + return errors.Wrapf(err, "get monitor_resource_joint by alertId:%s err", alertRecord.AlertId) + } + deleteJointIds := make([]int64, 0) + for _, joint := range resourceAlerts { + if utils.IsInStringArray(joint.MonitorResourceId, matchResourceIds) { + continue + } + deleteJointIds = append(deleteJointIds, joint.RowId) + } + if len(deleteJointIds) != 0 { + err = MonitorResourceAlertManager.DetachJoint(ctx, userCred, monitor.MonitorResourceJointListInput{JointId: deleteJointIds}) + if err != nil { + return errors.Wrapf(err, "DetachJoint by alertId:%s err", alertRecord.AlertId) + } + } return errors.NewAggregate(errs) } diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index dc7f9ebb33..396bee7f03 100644 --- a/pkg/monitor/models/monitor_resource_alert.go +++ b/pkg/monitor/models/monitor_resource_alert.go @@ -59,7 +59,7 @@ func (manager *SMonitorResourceAlertManager) DetachJoint(ctx context.Context, us } errs := make([]error, 0) for _, joint := range joints { - err := joint.Delete(ctx, userCred) + err := joint.Delete(ctx, nil) if err != nil { errs = append(errs, errors.Wrapf(err, "joint %s:%s ,%s:%s", manager.GetMasterFieldName(), joint.MonitorResourceId, manager.GetSlaveFieldName(), joint.AlertId)) @@ -102,9 +102,12 @@ func (manager *SMonitorResourceAlertManager) GetJoinsByListInput(input monitor.M if len(input.AlertId) != 0 { query.Equals(manager.GetSlaveFieldName(), input.AlertId) } + if len(input.JointId) != 0 { + query.In("row_id", input.JointId) + } err := db.FetchModelObjects(manager, query, &joints) if err != nil { - return nil, errors.Wrapf(err, "FetchModelObjects by GetJoinsByMasterId:%s err", input) + return nil, errors.Wrapf(err, "FetchModelObjects by GetJoinsByMasterId:%s err", input.MonitorResourceId) } return joints, nil } diff --git a/pkg/monitor/models/monitor_resource_sync.go b/pkg/monitor/models/monitor_resource_sync.go index c76bceb2fc..f0488065f7 100644 --- a/pkg/monitor/models/monitor_resource_sync.go +++ b/pkg/monitor/models/monitor_resource_sync.go @@ -132,6 +132,7 @@ monLoop: errs = append(errs, errors.Wrapf(err, "monitorResource:%s Update err", resource.Name)) continue monLoop } + resource.UpdateAlertState() if index == len(resources)-1 { resources = resources[0:index] } else { diff --git a/pkg/monitor/tasks/detach_alertresource_task.go b/pkg/monitor/tasks/detach_alertresource_task.go index 5c6cbf8295..c3df316597 100644 --- a/pkg/monitor/tasks/detach_alertresource_task.go +++ b/pkg/monitor/tasks/detach_alertresource_task.go @@ -8,6 +8,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/monitor/models" @@ -34,6 +35,12 @@ func (self *DetachAlertResourceTask) OnInit(ctx context.Context, obj db.IStandal if err != nil { log.Errorf("DetachAlertResourceTask NotifyAlertResourceCount error:%v", err) } + // detach MonitorResourceJoint when alert disabel + err = models.MonitorResourceAlertManager.DetachJoint(ctx, self.GetUserCred(), + monitor.MonitorResourceJointListInput{AlertId: alert.GetId()}) + if err != nil { + log.Errorf("DetachJoint when alert:%s disable err:%v", alert.GetName(), err) + } logclient.AddActionLogWithStartable(self, alert, logclient.ACT_DETACH_ALERTRESOURCE, nil, self.UserCred, true) self.SetStageComplete(ctx, nil) }