From 979da5107bbe8da29c2277cc361d9638e8945b19 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Fri, 12 Nov 2021 16:21:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(monitor):=20=E5=91=8A=E8=AD=A6=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.https://bug.yunion.io/zentao/bug-view-7933.html --- pkg/monitor/models/commonalert.go | 5 +++++ pkg/monitor/models/monitor_resource.go | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/monitor/models/commonalert.go b/pkg/monitor/models/commonalert.go index 2966c39e1c..dfcecb8d29 100644 --- a/pkg/monitor/models/commonalert.go +++ b/pkg/monitor/models/commonalert.go @@ -1346,6 +1346,11 @@ jointLoop: continue jointLoop } } + // 排除近期有报警状态的情况:system.uptime + if joint.AlertState == monitor.MONITOR_RESOURCE_ALERT_STATUS_ALERTING && time.Now().Sub(joint.TriggerTime). + Minutes() < 30 { + continue + } deleteJointIds = append(deleteJointIds, joint.RowId) } if len(resourceIds) == 0 { diff --git a/pkg/monitor/models/monitor_resource.go b/pkg/monitor/models/monitor_resource.go index fa5bc59a88..a15ff39e6e 100644 --- a/pkg/monitor/models/monitor_resource.go +++ b/pkg/monitor/models/monitor_resource.go @@ -299,7 +299,7 @@ func (self *SMonitorResource) RealDelete(ctx context.Context, userCred mcclient. if err != nil { return err } - return self.SVirtualResourceBase.Delete(ctx, userCred) + return self.SVirtualResourceBase.Delete(ctx, nil) } func (self *SMonitorResource) DetachJoint(ctx context.Context, userCred mcclient.TokenCredential) error { @@ -433,6 +433,13 @@ func (self *SMonitorResource) UpdateAttachJoint(alertRecord *SAlertRecord, match return errors.Wrapf(err, "SMonitorResource:%s UpdateAttachJoint err", self.Name) } errs := make([]error, 0) + // 报警时发现没有进行关联,增加attach + if len(joints) == 0 { + self.AttachAlert(context.Background(), nil, alertRecord.AlertId) + joints, _ = MonitorResourceAlertManager.GetJoinsByListInput(monitor. + MonitorResourceJointListInput{MonitorResourceId: self. + ResId, AlertId: alertRecord.AlertId}) + } for _, joint := range joints { err := joint.UpdateAlertRecordData(alertRecord, &match) if err != nil { From 018a653deb8c58ecd969a7bf1cfa67590f0d73b8 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Fri, 12 Nov 2021 16:31:18 +0800 Subject: [PATCH 2/2] fix(monitor): remove websocket import in monitor service --- pkg/monitor/models/alertrecord.go | 2 +- pkg/monitor/models/alertresource.go | 61 ++++++++----------- .../tasks/detach_alertresource_task.go | 9 +-- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/pkg/monitor/models/alertrecord.go b/pkg/monitor/models/alertrecord.go index e9bc8c19eb..0b1aa171b1 100644 --- a/pkg/monitor/models/alertrecord.go +++ b/pkg/monitor/models/alertrecord.go @@ -340,7 +340,7 @@ func (record *SAlertRecord) PostCreate(ctx context.Context, userCred mcclient.To 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 diff --git a/pkg/monitor/models/alertresource.go b/pkg/monitor/models/alertresource.go index 5ba5859e42..b9e9a291cb 100644 --- a/pkg/monitor/models/alertresource.go +++ b/pkg/monitor/models/alertresource.go @@ -30,8 +30,6 @@ import ( "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modules/identity" - "yunion.io/x/onecloud/pkg/mcclient/modules/notify" - "yunion.io/x/onecloud/pkg/mcclient/modules/websocket" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -432,37 +430,30 @@ func (res *SAlertResource) CustomizeDelete( return nil } -func (manager *SAlertResourceManager) NotifyAlertResourceCount(ctx context.Context) error { - log.Errorln("exec NotifyAlertResourceCount func") - cn, err := manager.getResourceCount() - if err != nil { - return err - } - alertResourceCount := resourceCount{ - AlertResourceCount: cn, - } - if adminUsers == nil { - manager.GetAdminRoleUsers(ctx, nil, true) - } - adminUsersTmp := *adminUsers - ids := make([]string, 0) - adminUsersTmp.Range(func(key, value interface{}) bool { - ids = append(ids, key.(string)) - return true - }) - if len(ids) == 0 { - return fmt.Errorf("no find users in receivers has admin role") - } - //if len(ids) != 0 { - // notifyclient.RawNotifyWithCtx(ctx, ids, false, npk.NotifyByWebConsole, npk.NotifyPriorityCritical, - // "alertResourceCount", jsonutils.Marshal(&alertResourceCount)) - // return nil - //} else { - // return fmt.Errorf("no find users in receivers has admin role") - //} - manager.sendWebsocketInfo(ids, alertResourceCount) - return nil -} +//func (manager *SAlertResourceManager) NotifyAlertResourceCount(ctx context.Context) error { +// log.Errorln("exec NotifyAlertResourceCount func") +// cn, err := manager.getResourceCount() +// if err != nil { +// return err +// } +// alertResourceCount := resourceCount{ +// AlertResourceCount: cn, +// } +// if adminUsers == nil { +// manager.GetAdminRoleUsers(ctx, nil, true) +// } +// adminUsersTmp := *adminUsers +// ids := make([]string, 0) +// adminUsersTmp.Range(func(key, value interface{}) bool { +// ids = append(ids, key.(string)) +// return true +// }) +// if len(ids) == 0 { +// return fmt.Errorf("no find users in receivers has admin role") +// } +// //manager.sendWebsocketInfo(ids, alertResourceCount) +// return nil +//} type resourceCount struct { AlertResourceCount int `json:"alert_resource_count"` @@ -519,7 +510,7 @@ func (manager *SAlertResourceManager) GetAdminRoleUsers(ctx context.Context, use } } -func (manager *SAlertResourceManager) sendWebsocketInfo(uids []string, alertResourceCount resourceCount) { +/*func (manager *SAlertResourceManager) sendWebsocketInfo(uids []string, alertResourceCount resourceCount) { session := auth.GetAdminSession(context.Background(), "", "") params := jsonutils.NewDict() params.Set("obj_type", jsonutils.NewString("monitor")) @@ -538,4 +529,4 @@ func (manager *SAlertResourceManager) sendWebsocketInfo(uids []string, alertReso log.Errorf("websocket send info err:%v", err) } } -} +}*/ diff --git a/pkg/monitor/tasks/detach_alertresource_task.go b/pkg/monitor/tasks/detach_alertresource_task.go index 154448e3c6..37d42e25ba 100644 --- a/pkg/monitor/tasks/detach_alertresource_task.go +++ b/pkg/monitor/tasks/detach_alertresource_task.go @@ -45,10 +45,11 @@ func (self *DetachAlertResourceTask) OnInit(ctx context.Context, obj db.IStandal self.taskFail(ctx, alert, msg) return } - err := models.GetAlertResourceManager().NotifyAlertResourceCount(ctx) - if err != nil { - log.Errorf("DetachAlertResourceTask NotifyAlertResourceCount error:%v", err) - } + var err error + //err := models.GetAlertResourceManager().NotifyAlertResourceCount(ctx) + //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()})