update alerting api
This commit is contained in:
parent
78ad60478c
commit
01de0fc42c
|
@ -17,6 +17,7 @@ type ConditionItem struct {
|
||||||
Operator string `json:"operator"`
|
Operator string `json:"operator"`
|
||||||
Values []string `json:"values"`
|
Values []string `json:"values"`
|
||||||
Severity string `json:"severity"`
|
Severity string `json:"severity"`
|
||||||
|
Expression string `json:"expression,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cond *ConditionItem) GenerateConditionExpression()(conditionExpression string, err error){
|
func (cond *ConditionItem) GenerateConditionExpression()(conditionExpression string, err error){
|
||||||
|
|
|
@ -218,11 +218,10 @@ func (h *AlertAPI) getAlertMessage(w http.ResponseWriter, req *http.Request, ps
|
||||||
h.WriteError(w, fmt.Sprintf("rule[%s] not found", rule.ID), http.StatusInternalServerError)
|
h.WriteError(w, fmt.Sprintf("rule[%s] not found", rule.ID), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conditionExpressions := make([]string, 0, len(rule.Conditions.Items))
|
|
||||||
metricExpression, _ := rule.Metrics.GenerateExpression()
|
metricExpression, _ := rule.Metrics.GenerateExpression()
|
||||||
for _, cond := range rule.Conditions.Items {
|
for i, cond := range rule.Conditions.Items {
|
||||||
expression, _ := cond.GenerateConditionExpression()
|
expression, _ := cond.GenerateConditionExpression()
|
||||||
conditionExpressions = append(conditionExpressions, strings.ReplaceAll(expression, "result", metricExpression))
|
rule.Conditions.Items[i].Expression = strings.ReplaceAll(expression, "result", metricExpression)
|
||||||
}
|
}
|
||||||
var duration time.Duration
|
var duration time.Duration
|
||||||
if message.Status == alerting.MessageStateRecovered {
|
if message.Status == alerting.MessageStateRecovered {
|
||||||
|
@ -238,8 +237,9 @@ func (h *AlertAPI) getAlertMessage(w http.ResponseWriter, req *http.Request, ps
|
||||||
"updated": message.Updated,
|
"updated": message.Updated,
|
||||||
"resource_name": rule.Resource.Name,
|
"resource_name": rule.Resource.Name,
|
||||||
"resource_object": rule.Resource.Objects,
|
"resource_object": rule.Resource.Objects,
|
||||||
"condition_expressions": conditionExpressions,
|
"conditions": rule.Conditions,
|
||||||
"duration": duration.Milliseconds(),
|
"duration": duration.Milliseconds(),
|
||||||
|
"ignored_time": message.IgnoredTime,
|
||||||
"status": message.Status,
|
"status": message.Status,
|
||||||
}
|
}
|
||||||
h.WriteJSON(w, detailObj, http.StatusOK)
|
h.WriteJSON(w, detailObj, http.StatusOK)
|
||||||
|
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"infini.sh/framework/modules/elastic/api"
|
"infini.sh/framework/modules/elastic/api"
|
||||||
"infini.sh/framework/modules/elastic/common"
|
"infini.sh/framework/modules/elastic/common"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -125,11 +124,10 @@ func (alertAPI *AlertAPI) getRuleDetail(w http.ResponseWriter, req *http.Request
|
||||||
}, http.StatusNotFound)
|
}, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conditionExpressions := make([]string, 0, len(obj.Conditions.Items))
|
|
||||||
metricExpression, _ := obj.Metrics.GenerateExpression()
|
metricExpression, _ := obj.Metrics.GenerateExpression()
|
||||||
for _, cond := range obj.Conditions.Items {
|
for i, cond := range obj.Conditions.Items {
|
||||||
expression, _ := cond.GenerateConditionExpression()
|
expression, _ := cond.GenerateConditionExpression()
|
||||||
conditionExpressions = append(conditionExpressions, strings.ReplaceAll(expression, "result", metricExpression))
|
obj.Conditions.Items[i].Expression = strings.ReplaceAll(expression, "result", metricExpression)
|
||||||
}
|
}
|
||||||
alertNumbers, err := alertAPI.getRuleAlertMessageNumbers([]string{obj.ID})
|
alertNumbers, err := alertAPI.getRuleAlertMessageNumbers([]string{obj.ID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -179,11 +177,12 @@ func (alertAPI *AlertAPI) getRuleDetail(w http.ResponseWriter, req *http.Request
|
||||||
detailObj := util.MapStr{
|
detailObj := util.MapStr{
|
||||||
"resource_name": obj.Resource.Name,
|
"resource_name": obj.Resource.Name,
|
||||||
"resource_objects": obj.Resource.Objects,
|
"resource_objects": obj.Resource.Objects,
|
||||||
"period_interval": obj.Metrics.PeriodInterval,
|
"period_interval": obj.Metrics.PeriodInterval, //统计周期
|
||||||
"updated": obj.Updated,
|
"updated": obj.Updated,
|
||||||
"condition_expressions": conditionExpressions,
|
"conditions": obj.Conditions,
|
||||||
"message_count": alertNumbers[obj.ID],
|
"message_count": alertNumbers[obj.ID], //所有关联告警消息数(包括已恢复的)
|
||||||
"state": state,
|
"state": state,
|
||||||
|
"enabled": obj.Enabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
alertAPI.WriteJSON(w, detailObj, 200)
|
alertAPI.WriteJSON(w, detailObj, 200)
|
||||||
|
@ -372,11 +371,11 @@ func (alertAPI *AlertAPI) getRuleAlertMessageNumbers(ruleIDs []string) ( map[str
|
||||||
"rule_id": ruleIDs,
|
"rule_id": ruleIDs,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
//{
|
||||||
"terms": util.MapStr{
|
// "terms": util.MapStr{
|
||||||
"status": []string{alerting.MessageStateAlerting, alerting.MessageStateIgnored},
|
// "status": []string{alerting.MessageStateAlerting, alerting.MessageStateIgnored},
|
||||||
},
|
// },
|
||||||
},
|
//},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -444,21 +443,12 @@ func (alertAPI *AlertAPI) fetchAlertInfos(w http.ResponseWriter, req *http.Reque
|
||||||
alertAPI.WriteJSON(w, util.MapStr{}, http.StatusOK)
|
alertAPI.WriteJSON(w, util.MapStr{}, http.StatusOK)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
alertNumbers, err := alertAPI.getRuleAlertMessageNumbers(ruleIDs)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
alertAPI.WriteJSON(w, util.MapStr{
|
|
||||||
"error": err.Error(),
|
|
||||||
}, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
latestAlertInfos := map[string]util.MapStr{}
|
latestAlertInfos := map[string]util.MapStr{}
|
||||||
for _, hit := range searchRes.Hits.Hits {
|
for _, hit := range searchRes.Hits.Hits {
|
||||||
if ruleID, ok := hit.Source["rule_id"].(string); ok {
|
if ruleID, ok := hit.Source["rule_id"].(string); ok {
|
||||||
latestAlertInfos[ruleID] = util.MapStr{
|
latestAlertInfos[ruleID] = util.MapStr{
|
||||||
"status": hit.Source["state"],
|
"status": hit.Source["state"],
|
||||||
"alert_count": alertNumbers[ruleID],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -676,36 +666,36 @@ func getRuleMetricData( rule *alerting.Rule, filterParam *alerting.FilterParam)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
//add guidelines
|
//add guidelines
|
||||||
for _, cond := range rule.Conditions.Items {
|
//for _, cond := range rule.Conditions.Items {
|
||||||
if len(cond.Values) > 0 {
|
// if len(cond.Values) > 0 {
|
||||||
val, err := strconv.ParseFloat(cond.Values[0], 64)
|
// val, err := strconv.ParseFloat(cond.Values[0], 64)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Errorf("parse condition value error: %v", err)
|
// log.Errorf("parse condition value error: %v", err)
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
if sampleData != nil {
|
// if sampleData != nil {
|
||||||
newData := make([]alerting.TimeMetricData, 0, len(sampleData))
|
// newData := make([]alerting.TimeMetricData, 0, len(sampleData))
|
||||||
for _, td := range sampleData {
|
// for _, td := range sampleData {
|
||||||
if len(td) < 2 {
|
// if len(td) < 2 {
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
newData = append(newData, alerting.TimeMetricData{
|
// newData = append(newData, alerting.TimeMetricData{
|
||||||
td[0], val,
|
// td[0], val,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
metricItem.Lines = append(metricItem.Lines, &common.MetricLine{
|
// metricItem.Lines = append(metricItem.Lines, &common.MetricLine{
|
||||||
Data: newData,
|
// Data: newData,
|
||||||
BucketSize: filterParam.BucketSize,
|
// BucketSize: filterParam.BucketSize,
|
||||||
Metric: common.MetricSummary{
|
// Metric: common.MetricSummary{
|
||||||
Label: "",
|
// Label: "",
|
||||||
Group: rule.ID,
|
// Group: rule.ID,
|
||||||
TickFormat: "0,0.[00]",
|
// TickFormat: "0,0.[00]",
|
||||||
FormatType: "num",
|
// FormatType: "num",
|
||||||
},
|
// },
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
return &metricItem, nil
|
return &metricItem, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue