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/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/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 diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index dcc8cf18f4..0809681add 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 } @@ -238,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 }