add alert stats api
This commit is contained in:
parent
2ecf947d68
commit
5a3fcb2033
|
@ -39,7 +39,7 @@ type ActionExecutionResult struct {
|
|||
|
||||
const (
|
||||
AlertStateActive string = "active"
|
||||
AlertStateAcknowledge = "acknowledge"
|
||||
AlertStateAcknowledge = "acknowledged"
|
||||
AlertStateNormal = "normal"
|
||||
AlertStateError = "error"
|
||||
)
|
||||
|
|
|
@ -58,9 +58,7 @@ type ConditionResultItem struct {
|
|||
type Severity string
|
||||
|
||||
var SeverityWeights = map[string]int{
|
||||
"verbose": 1,
|
||||
"info": 2,
|
||||
"warning": 3,
|
||||
"error": 4,
|
||||
"critical": 5,
|
||||
"warning": 1,
|
||||
"error": 2,
|
||||
"critical": 3,
|
||||
}
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"infini.sh/console/model/alerting"
|
||||
httprouter "infini.sh/framework/core/api/router"
|
||||
"infini.sh/framework/core/elastic"
|
||||
"infini.sh/framework/core/orm"
|
||||
"infini.sh/framework/core/util"
|
||||
"net/http"
|
||||
|
@ -88,10 +89,28 @@ func (h *AlertAPI) searchAlert(w http.ResponseWriter, req *http.Request, ps http
|
|||
queryDSL = `{"sort":[{"created":{ "order": "desc"}}],"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}`
|
||||
strSize = h.GetParameterOrDefault(req, "size", "20")
|
||||
strFrom = h.GetParameterOrDefault(req, "from", "0")
|
||||
state = h.GetParameterOrDefault(req, "state", "")
|
||||
severity = h.GetParameterOrDefault(req, "severity", "")
|
||||
mustBuilder = &strings.Builder{}
|
||||
)
|
||||
hasFilter := false
|
||||
if keyword != "" {
|
||||
mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword))
|
||||
hasFilter = true
|
||||
}
|
||||
if state != "" {
|
||||
if hasFilter {
|
||||
mustBuilder.WriteString(",")
|
||||
}
|
||||
mustBuilder.WriteString(fmt.Sprintf(`{"term":{"state":{"value":"%s"}}}`, state))
|
||||
hasFilter = true
|
||||
}
|
||||
if severity != "" {
|
||||
if hasFilter {
|
||||
mustBuilder.WriteString(",")
|
||||
}
|
||||
mustBuilder.WriteString(fmt.Sprintf(`{"term":{"severity":{"value":"%s"}}}`, severity))
|
||||
hasFilter = true
|
||||
}
|
||||
size, _ := strconv.Atoi(strSize)
|
||||
if size <= 0 {
|
||||
|
@ -114,3 +133,59 @@ func (h *AlertAPI) searchAlert(w http.ResponseWriter, req *http.Request, ps http
|
|||
}
|
||||
h.Write(w, res.Raw)
|
||||
}
|
||||
|
||||
func (h *AlertAPI) getAlertStats(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||
esClient := elastic.GetClient(h.Config.Elasticsearch)
|
||||
queryDsl := util.MapStr{
|
||||
"size": 0,
|
||||
"query": util.MapStr{
|
||||
"bool": util.MapStr{
|
||||
"must_not": []util.MapStr{
|
||||
{
|
||||
"terms": util.MapStr{
|
||||
"state": []string{
|
||||
"acknowledged",
|
||||
"normal",
|
||||
"",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"aggs": util.MapStr{
|
||||
"terms_by_state": util.MapStr{
|
||||
"terms": util.MapStr{
|
||||
"field": "severity",
|
||||
"size": 5,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
searchRes, err := esClient.SearchWithRawQueryDSL(orm.GetWildcardIndexName(alerting.Alert{}), util.MustToJSONBytes(queryDsl) )
|
||||
if err != nil {
|
||||
h.WriteJSON(w, util.MapStr{
|
||||
"error": err.Error(),
|
||||
}, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
severityAlerts := map[string]interface{}{}
|
||||
if termsAgg, ok := searchRes.Aggregations["terms_by_state"]; ok {
|
||||
for _, bk := range termsAgg.Buckets {
|
||||
if severity, ok := bk["key"].(string); ok {
|
||||
severityAlerts[severity] = bk["doc_count"]
|
||||
}
|
||||
}
|
||||
}
|
||||
for severity, _ := range alerting.SeverityWeights {
|
||||
if _, ok := severityAlerts[severity]; !ok {
|
||||
severityAlerts[severity] = 0
|
||||
}
|
||||
}
|
||||
h.WriteJSON(w, util.MapStr{
|
||||
"alert": util.MapStr{
|
||||
"current": severityAlerts,
|
||||
},
|
||||
}, http.StatusOK)
|
||||
}
|
|
@ -21,6 +21,7 @@ func (alert *AlertAPI) Init() {
|
|||
api.HandleAPIMethod(api.DELETE, "/alerting/rule/:rule_id", alert.deleteRule)
|
||||
api.HandleAPIMethod(api.PUT, "/alerting/rule/:rule_id", alert.updateRule)
|
||||
api.HandleAPIMethod(api.GET, "/alerting/rule/_search", alert.searchRule)
|
||||
api.HandleAPIMethod(api.GET, "/alerting/stats", alert.getAlertStats)
|
||||
api.HandleAPIMethod(api.POST, "/alerting/rule/info", alert.fetchAlertInfos)
|
||||
api.HandleAPIMethod(api.POST, "/alerting/rule/:rule_id/_enable", alert.enableRule)
|
||||
|
||||
|
|
Loading…
Reference in New Issue