Merge pull request #9817 from zhaoxiangchun/feature/zxc-alerthistory-overview-master

fix(monitor): fix monitoroverview and meter send alerting info to  notify
This commit is contained in:
Zexi Li
2021-01-09 19:55:58 +08:00
committed by GitHub
5 changed files with 71 additions and 10 deletions
@@ -111,11 +111,18 @@ func (c *NoDataQueryCondition) getOnecloudResources(evalContext *alerting.EvalCo
query := jsonutils.NewDict()
query.Add(jsonutils.NewStringArray([]string{"running", "ready"}), "status")
query.Add(jsonutils.NewString("true"), "admin")
if len(c.Query.Model.Tags) != 0 {
query, err = c.convertTagsQuery(evalContext, query)
if err != nil {
return nil, errors.Wrap(err, "NoDataQueryCondition convertTagsQuery error")
}
}
switch evalContext.Rule.RuleDescription[0].ResType {
case monitor.METRIC_RES_TYPE_HOST:
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)
case monitor.METRIC_RES_TYPE_RDS:
allResources, err = ListAllResources(&mc_mds.DBInstance, query)
case monitor.METRIC_RES_TYPE_REDIS:
@@ -135,6 +142,29 @@ func (c *NoDataQueryCondition) getOnecloudResources(evalContext *alerting.EvalCo
return allResources, nil
}
func (c *NoDataQueryCondition) convertTagsQuery(evalContext *alerting.EvalContext,
query *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
alertDetails, err := c.GetCommonAlertDetails(evalContext)
if err != nil {
return nil, err
}
for i, _ := range c.Query.Model.Tags {
filterCount := 0
if c.Query.Model.Tags[i].Operator == "=" {
if tag, ok := monitor.MEASUREMENT_TAG_KEYWORD[alertDetails.ResType]; ok {
if c.Query.Model.Tags[i].Key == tag {
query.Set("name", jsonutils.NewString(c.Query.Model.Tags[i].Value))
continue
}
}
}
query.Set(fmt.Sprintf("filter.%d", filterCount),
jsonutils.NewString(fmt.Sprintf("%s.notin(%s)", c.Query.Model.Tags[i].Key, c.Query.Model.Tags[i].Value)))
filterCount++
}
return query, nil
}
func ListAllResources(manager modulebase.Manager, params *jsonutils.JSONDict) ([]jsonutils.JSONObject, error) {
if params == nil {
params = jsonutils.NewDict()
+11 -3
View File
@@ -194,12 +194,10 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.Conditio
func (c *QueryCondition) NewEvalMatch(context *alerting.EvalContext, series tsdb.TimeSeries,
meta *tsdb.QueryResultMeta, value *float64, valStrArr []string) (*monitor.EvalMatch, error) {
evalMatch := new(monitor.EvalMatch)
alert, err := models.CommonAlertManager.GetAlert(context.Rule.Id)
alertDetails, err := c.GetCommonAlertDetails(context)
if err != nil {
return nil, errors.Wrap(err, "GetAlert to NewEvalMatch error")
}
settings, _ := alert.GetSettings()
alertDetails := alert.GetCommonAlertMetricDetailsFromAlertCondition(c.Index, &settings.Conditions[c.Index])
evalMatch.Metric = fmt.Sprintf("%s.%s", alertDetails.Measurement, alertDetails.Field)
queryKeyInfo := ""
if len(alertDetails.MeasurementDisplayName) > 0 && len(alertDetails.FieldDescription.DisplayName) > 0 {
@@ -226,6 +224,16 @@ func (c *QueryCondition) NewEvalMatch(context *alerting.EvalContext, series tsdb
return evalMatch, nil
}
func (c *QueryCondition) GetCommonAlertDetails(context *alerting.EvalContext) (*monitor.CommonAlertMetricDetails, error) {
alert, err := models.CommonAlertManager.GetAlert(context.Rule.Id)
if err != nil {
return nil, errors.Wrap(err, "GetAlert to NewEvalMatch error")
}
settings, _ := alert.GetSettings()
alertDetails := alert.GetCommonAlertMetricDetailsFromAlertCondition(c.Index, &settings.Conditions[c.Index])
return alertDetails, nil
}
func (c *QueryCondition) jointPointStr(series tsdb.TimeSeries, value string, valStrArr []string) string {
str := ""
for i := 0; i < len(valStrArr); i++ {
+11 -1
View File
@@ -128,7 +128,17 @@ func (c *EvalContext) GetCallbackURLPrefix() string {
return ""
}
url, _ := config.GetString("config", "default", "api_server")
return url + "/alertrecord"
defaultWebUri := "alertrecord"
matchTag := map[string]string{}
if c.Firing {
matchTag = c.EvalMatches[0].Tags
} else {
matchTag = c.AlertOkEvalMatches[0].Tags
}
if uri, ok := matchTag["web_url"]; ok {
defaultWebUri = uri
}
return fmt.Sprintf("%s/%s", url, defaultWebUri)
}
// GetNewState returns the new state from the alert rule evaluation.
+18 -3
View File
@@ -336,12 +336,27 @@ func (manager *SAlertRecordManager) getNowAlertingRecord(ctx context.Context, us
query = manager.FilterByOwner(query, userCred, rbacutils.String2Scope(scope))
query = query.GE("created_at", startTime.UTC().Format(timeutils.MysqlTimeFormat))
query = query.Equals("state", monitor.AlertStateAlerting)
sQuery := CommonAlertManager.Query("id").Equals("state", monitor.AlertStateAlerting).IsNull("used_by").SubQuery()
query = query.In("alert_id", sQuery).IsNotNull("res_type").IsNotEmpty("res_type").GroupBy("alert_id")
query = query.IsNotNull("res_type").IsNotEmpty("res_type").Desc("created_at")
alertsQuery := CommonAlertManager.Query("id").Equals("state", monitor.AlertStateAlerting).IsNull("used_by")
alertsQuery = CommonAlertManager.FilterByOwner(alertsQuery, userCred, rbacutils.String2Scope(scope))
alerts := make([]SCommonAlert, 0)
records := make([]SAlertRecord, 0)
err := db.FetchModelObjects(manager, query, &records)
err := db.FetchModelObjects(CommonAlertManager, alertsQuery, &alerts)
if err != nil {
return nil, err
}
for _, alert := range alerts {
tmp := *query
recordModel, err := db.NewModelObject(manager)
if err != nil {
return nil, err
}
if err := (&tmp).Equals("alert_id", alert.GetId()).First(recordModel); err == nil {
records = append(records, *(recordModel.(*SAlertRecord)))
} else {
return nil, errors.Wrapf(err, "getRecordByAlertId:%s error", alert.GetId())
}
}
return records, nil
}
+1 -3
View File
@@ -226,9 +226,7 @@ func (alert *SCommonAlert) CustomizeCreate(
query jsonutils.JSONObject,
data jsonutils.JSONObject,
) error {
if err := alert.SAlert.CustomizeCreate(ctx, userCred, ownerId, query, data); err != nil {
return err
}
alert.State = string(monitor.AlertStateUnknown)
input := new(monitor.CommonAlertCreateInput)
if err := data.Unmarshal(input); err != nil {
return err