From f3e16e85b1b3a90e9e546542633fcebce68c4f5a Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Wed, 3 Nov 2021 17:04:05 +0800 Subject: [PATCH 1/3] fix(monitor): monitor alert support:{level} filter --- pkg/apis/monitor/monitor_resource_alert.go | 1 + pkg/monitor/models/monitor_resource_alert.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/apis/monitor/monitor_resource_alert.go b/pkg/apis/monitor/monitor_resource_alert.go index 155e984aba..12201f1e14 100644 --- a/pkg/apis/monitor/monitor_resource_alert.go +++ b/pkg/apis/monitor/monitor_resource_alert.go @@ -32,6 +32,7 @@ type MonitorResourceJointListInput struct { ResType string `json:"res_type"` ResName string `json:"res_name"` AlertName string `json:"alert_name"` + Level string `json:"level"` } type MonitorResourceJointCreateInput struct { diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index dcc8cf18f4..307fc8994a 100644 --- a/pkg/monitor/models/monitor_resource_alert.go +++ b/pkg/monitor/models/monitor_resource_alert.go @@ -180,11 +180,15 @@ func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sq } q.Filter(sqlchemy.In(q.Field("monitor_resource_id"), resQ.SubQuery())) } + alertQuery := CommonAlertManager.Query("id") if len(input.AlertName) != 0 { - alertQuery := CommonAlertManager.Query("id") CommonAlertManager.FieldListFilter(alertQuery, monitor.CommonAlertListInput{Name: input.AlertName}) q.Filter(sqlchemy.In(q.Field(m.GetSlaveFieldName()), alertQuery.SubQuery())) } + if len(input.Level) != 0 { + CommonAlertManager.FieldListFilter(alertQuery, monitor.CommonAlertListInput{Level: input.Level}) + q.Filter(sqlchemy.In(q.Field(m.GetSlaveFieldName()), alertQuery.SubQuery())) + } return q, nil } From 20e16dc86bfb50199aabad1f438f3961fcd18d60 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Thu, 4 Nov 2021 15:42:08 +0800 Subject: [PATCH 2/3] fix(monitor): onecloud notifier send sys when user_ids and robot_ids length == 0 --- pkg/monitor/alerting/notifiers/onecloud.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go index a849582891..0d9440fe60 100644 --- a/pkg/monitor/alerting/notifiers/onecloud.go +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -358,7 +358,8 @@ func (f *sendBodyFactory) newSendnotify(evalCtx *alerting.EvalContext, notifier def.evalCtx = *evalCtx def.msg = message def.config = config - if len(notifier.Setting.UserIds) == 0 { + // 系统内置报警处理 + if len(notifier.Setting.UserIds) == 0 && len(notifier.Setting.RobotIds) == 0 { sys := new(sendSysImpl) sys.sendnotifyBase = def return sys From 23ebe099ccb8c39f0479acc3f39e869b50ece856 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Thu, 4 Nov 2021 16:19:09 +0800 Subject: [PATCH 3/3] fix(monitor): add monitor sheild {expired} state --- pkg/apis/monitor/alert_record_shield.go | 15 ++++++++++----- pkg/monitor/models/monitor_resource_alert.go | 3 ++- pkg/monitor/models/recordshield.go | 9 +++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/apis/monitor/alert_record_shield.go b/pkg/apis/monitor/alert_record_shield.go index 8cb88f1d51..89b299793b 100644 --- a/pkg/apis/monitor/alert_record_shield.go +++ b/pkg/apis/monitor/alert_record_shield.go @@ -15,6 +15,8 @@ package monitor import ( + "time" + "yunion.io/x/onecloud/pkg/apis" ) @@ -37,6 +39,7 @@ type AlertRecordShieldDetails struct { CommonAlertDetails AlertName string `json:"alert_name"` ResName string `json:"res_name"` + Expired bool `json:"expired"` } type AlertRecordShieldListInput struct { @@ -46,9 +49,11 @@ type AlertRecordShieldListInput struct { apis.EnabledResourceBaseListInput apis.StatusStandaloneResourceListInput - AlertName string `json:"alert_name"` - ResType string `json:"res_type"` - ResName string `json:"res_name"` - ResId string `json:"res_id"` - AlertId string `json:"alert_id"` + AlertName string `json:"alert_name"` + ResType string `json:"res_type"` + ResName string `json:"res_name"` + ResId string `json:"res_id"` + AlertId string `json:"alert_id"` + StartTime *time.Time `json:"start_time"` + EndTime *time.Time `json:"end_time"` } diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index 307fc8994a..0809681add 100644 --- a/pkg/monitor/models/monitor_resource_alert.go +++ b/pkg/monitor/models/monitor_resource_alert.go @@ -242,8 +242,9 @@ func (obj *SMonitorResourceAlert) getMoreDetails(detail monitor.MonitorResourceJ } detail.AlertName = alert.Name + now := time.Now() shields, err := AlertRecordShieldManager.GetRecordShields(monitor.AlertRecordShieldListInput{ResId: obj.MonitorResourceId, - AlertId: obj.AlertId}) + AlertId: obj.AlertId, EndTime: &now}) if err != nil { log.Errorf("SMonitorResourceAlert get GetRecordShields by resId: %s,alertId: %s, err: %v", obj.MonitorResourceId, obj.AlertId, err) diff --git a/pkg/monitor/models/recordshield.go b/pkg/monitor/models/recordshield.go index cb01913478..4459354744 100644 --- a/pkg/monitor/models/recordshield.go +++ b/pkg/monitor/models/recordshield.go @@ -133,6 +133,12 @@ func (manager *SAlertRecordShieldManager) shieldListByDetailsFeild(query *sqlche query.Join(resQuery, sqlchemy.Equals(query.Field("res_id"), resQuery.Field("res_id"))).Filter(sqlchemy.Equals( resQuery.Field("name"), input.ResName)) } + if input.StartTime != nil { + query.Filter(sqlchemy.LE(query.Field("start_time"), input.StartTime)) + } + if input.EndTime != nil { + query.Filter(sqlchemy.GE(query.Field("end_time"), input.EndTime)) + } return query } @@ -195,6 +201,9 @@ func (shield *SAlertRecordShield) GetMoreDetails(ctx context.Context, out monito if len(resources) == 0 { return out, nil } + if shield.EndTime.Before(time.Now()) { + out.Expired = true + } out.ResName = resources[0].Name return out, nil }