change request es using elastic client
This commit is contained in:
parent
24e1c81d51
commit
8f44364441
2
main.go
2
main.go
|
@ -14,7 +14,6 @@ import (
|
||||||
"infini.sh/framework/core/env"
|
"infini.sh/framework/core/env"
|
||||||
"infini.sh/framework/core/module"
|
"infini.sh/framework/core/module"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
_ "infini.sh/framework/plugins"
|
|
||||||
queue2 "infini.sh/framework/modules/disk_queue"
|
queue2 "infini.sh/framework/modules/disk_queue"
|
||||||
elastic2 "infini.sh/framework/modules/elastic"
|
elastic2 "infini.sh/framework/modules/elastic"
|
||||||
"infini.sh/framework/modules/filter"
|
"infini.sh/framework/modules/filter"
|
||||||
|
@ -24,6 +23,7 @@ import (
|
||||||
"infini.sh/framework/modules/stats"
|
"infini.sh/framework/modules/stats"
|
||||||
"infini.sh/framework/modules/task"
|
"infini.sh/framework/modules/task"
|
||||||
"infini.sh/framework/modules/ui"
|
"infini.sh/framework/modules/ui"
|
||||||
|
_ "infini.sh/framework/plugins"
|
||||||
api2 "infini.sh/gateway/api"
|
api2 "infini.sh/gateway/api"
|
||||||
_ "infini.sh/gateway/proxy"
|
_ "infini.sh/gateway/proxy"
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,9 +6,11 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
httprouter "infini.sh/framework/core/api/router"
|
|
||||||
"infini.sh/framework/core/orm"
|
|
||||||
"infini.sh/console/model/alerting"
|
"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"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -130,39 +132,22 @@ func GetAlerts (w http.ResponseWriter, req *http.Request, ps httprouter.Params){
|
||||||
indexName := getAlertIndexName(alertType)
|
indexName := getAlertIndexName(alertType)
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, indexName )
|
esClient := elastic.GetClient(config.ID)
|
||||||
|
res, err := esClient.SearchWithRawQueryDSL(indexName, util.MustToJSONBytes(reqBody))
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var alertRes = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &alertRes)
|
|
||||||
defer res.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
alerts := []interface{}{}
|
alerts := []interface{}{}
|
||||||
rawAlerts := queryValue(alertRes, "hits.hits", nil)
|
rawAlerts := res.Hits.Hits
|
||||||
if ds, ok := rawAlerts.([]interface{}); ok {
|
for _, alert := range rawAlerts {
|
||||||
for _, alert := range ds {
|
alert.Source["id"] = alert.ID
|
||||||
if alertItem, ok := alert.(map[string]interface{}); ok {
|
alerts = append(alerts, alert.Source)
|
||||||
//alertItem["version"] = queryValue(alertItem, "alert_version", "")
|
|
||||||
if alertID, ok := queryValue(alertItem, "_source.id", "").(string); ok && alertID == "" {
|
|
||||||
if source, ok := alertItem["_source"].(map[string]interface{}); ok {
|
|
||||||
source["id"] = alertItem["_id"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
alerts = append(alerts, alertItem["_source"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"alerts": alerts,
|
"alerts": alerts,
|
||||||
"totalAlerts": queryValue(alertRes, "hits.total.value", 0),
|
"totalAlerts": res.GetTotal(),
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -235,7 +220,7 @@ func doRequest(requestUrl string, method string, params map[string]string, body
|
||||||
}
|
}
|
||||||
req, _ = http.NewRequest(method, requestUrl, reader)
|
req, _ = http.NewRequest(method, requestUrl, reader)
|
||||||
req.Header.Set("content-type", "application/json")
|
req.Header.Set("content-type", "application/json")
|
||||||
req.Header.Set("User-Agent", "Kibana")
|
req.Header.Set("User-Agent", "infini-client")
|
||||||
return alertClient.Do(req)
|
return alertClient.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -99,38 +99,26 @@ func GetDestinations(w http.ResponseWriter, req *http.Request, ps httprouter.Par
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
config := getDefaultConfig()
|
esConfig := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
esClient := elastic.GetClient(esConfig.ID)
|
||||||
|
res, err := esClient.SearchWithRawQueryDSL( orm.GetIndexName(alerting.Config{}), util.MustToJSONBytes(reqBody))
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
totalDestinations := queryValue(resBody, "hits.total.value", 0)
|
totalDestinations := res.GetTotal()
|
||||||
rawHits := queryValue(resBody, "hits.hits", []interface{}{})
|
rawHits := res.Hits.Hits
|
||||||
dests := []IfaceMap{}
|
dests := []IfaceMap{}
|
||||||
|
|
||||||
if rh, ok := rawHits.([]interface{}); ok {
|
for _, hit := range rawHits {
|
||||||
for _, hit := range rh {
|
newItem := IfaceMap{}
|
||||||
if destination, ok := hit.(map[string]interface{}); ok {
|
newItem["id"] = hit.ID
|
||||||
newItem := IfaceMap{}
|
source := queryValue(hit.Source,DESTINATION_FIELD, nil)
|
||||||
newItem["id"] = queryValue(destination, "_id", "")
|
if ms, ok := source.(map[string]interface{}); ok {
|
||||||
source := queryValue(destination, "_source."+DESTINATION_FIELD, nil)
|
assignTo(newItem, ms)
|
||||||
if ms, ok := source.(map[string]interface{}); ok {
|
|
||||||
assignTo(newItem, ms)
|
|
||||||
}
|
|
||||||
dests = append(dests, newItem)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
dests = append(dests, newItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
|
@ -141,9 +129,8 @@ func GetDestinations(w http.ResponseWriter, req *http.Request, ps httprouter.Par
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func CreateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
config := getDefaultConfig()
|
esConfig := getDefaultConfig()
|
||||||
destId := util.GetUUID()
|
destId := util.GetUUID()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), destId)
|
|
||||||
var destination = &alerting.Destination{}
|
var destination = &alerting.Destination{}
|
||||||
err := decodeJSON(req.Body, &destination)
|
err := decodeJSON(req.Body, &destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -167,9 +154,8 @@ func CreateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
writeError(w, errors.New("type unsupported"))
|
writeError(w, errors.New("type unsupported"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
esClient := elastic.GetClient(esConfig.ID)
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index(orm.GetIndexName(alerting.Config{}), "", destId, IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
DESTINATION_FIELD: toSaveDest,
|
DESTINATION_FIELD: toSaveDest,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -177,20 +163,13 @@ func CreateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": IfaceMap{
|
"resp": IfaceMap{
|
||||||
DESTINATION_FIELD: toSaveDest,
|
DESTINATION_FIELD: toSaveDest,
|
||||||
"_id": queryValue(resBody, "_id", ""),
|
"_id": indexRes.ID,
|
||||||
"_version": queryValue(resBody, "_version", 0),
|
"_version": indexRes.Version,
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
|
@ -200,7 +179,6 @@ func UpdateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
destinationId := ps.ByName("destinationId")
|
destinationId := ps.ByName("destinationId")
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), destinationId)
|
|
||||||
var destination = &alerting.Destination{}
|
var destination = &alerting.Destination{}
|
||||||
err := decodeJSON(req.Body, &destination)
|
err := decodeJSON(req.Body, &destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -224,9 +202,8 @@ func UpdateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
writeError(w, errors.New("type unsupported"))
|
writeError(w, errors.New("type unsupported"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodPut, map[string]string{
|
esClient := elastic.GetClient(config.ID)
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index(orm.GetIndexName(alerting.Config{}), "", destinationId, IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
DESTINATION_FIELD: toSaveDest,
|
DESTINATION_FIELD: toSaveDest,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -234,17 +211,10 @@ func UpdateDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"version": queryValue(resBody, "_version", ""),
|
"version": indexRes.Version,
|
||||||
"id": queryValue(resBody, "_id", ""),
|
"id": indexRes.ID,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -253,24 +223,15 @@ func DeleteDestination(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
destinationId := ps.ByName("destinationId")
|
destinationId := ps.ByName("destinationId")
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s?refresh=wait_for", config.Endpoint, orm.GetIndexName(alerting.Config{}), destinationId)
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodDelete, nil, nil)
|
deleteRes, err := esClient.Delete(orm.GetIndexName(alerting.Config{}), "", destinationId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resultIfce := queryValue(resBody, "result", "")
|
|
||||||
var isOk = false
|
var isOk = false
|
||||||
if result, ok := resultIfce.(string); ok && result == "deleted" {
|
if deleteRes.Result == "deleted" {
|
||||||
isOk = true
|
isOk = true
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
|
@ -312,7 +273,6 @@ func InitAppConfig(config *config.AppConfig){
|
||||||
func CreateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func CreateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
|
||||||
var emailAccount = &alerting.EmailAccount{}
|
var emailAccount = &alerting.EmailAccount{}
|
||||||
err := decodeJSON(req.Body, &emailAccount)
|
err := decodeJSON(req.Body, &emailAccount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -321,9 +281,8 @@ func CreateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
}
|
}
|
||||||
//var password = emailAccount.Password
|
//var password = emailAccount.Password
|
||||||
//emailAccount.Password = ""
|
//emailAccount.Password = ""
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
esClient := elastic.GetClient(config.ID)
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index(orm.GetIndexName(alerting.Config{}), "", util.GetUUID(), IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
EMAIL_ACCOUNT_FIELD: emailAccount,
|
EMAIL_ACCOUNT_FIELD: emailAccount,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -331,14 +290,6 @@ func CreateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//kk := fmt.Sprintf("search-center_alerting.destination.email.%s.password", emailAccount.Name)
|
//kk := fmt.Sprintf("search-center_alerting.destination.email.%s.password", emailAccount.Name)
|
||||||
//secStr, _ := getKeystore().Retrieve(kk)
|
//secStr, _ := getKeystore().Retrieve(kk)
|
||||||
//kst := getKeystore()
|
//kst := getKeystore()
|
||||||
|
@ -357,8 +308,8 @@ func CreateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": IfaceMap{
|
"resp": IfaceMap{
|
||||||
EMAIL_ACCOUNT_FIELD: emailAccount,
|
EMAIL_ACCOUNT_FIELD: emailAccount,
|
||||||
"_id": queryValue(resBody, "_id", ""),
|
"_id": indexRes.ID,
|
||||||
"_version": queryValue(resBody, "_version", 0),
|
"_version": indexRes.Version,
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
@ -367,17 +318,14 @@ func UpdateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
emailAccountId := ps.ByName("emailAccountId")
|
emailAccountId := ps.ByName("emailAccountId")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailAccountId)
|
|
||||||
var emailAccount = &alerting.EmailAccount{}
|
var emailAccount = &alerting.EmailAccount{}
|
||||||
err := decodeJSON(req.Body, &emailAccount)
|
err := decodeJSON(req.Body, &emailAccount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodPut, map[string]string{
|
indexRes, err := esClient.Index(orm.GetIndexName(alerting.Config{}),"", emailAccountId, IfaceMap{
|
||||||
"refresh": "wait_for",
|
|
||||||
}, IfaceMap{
|
|
||||||
EMAIL_ACCOUNT_FIELD: emailAccount,
|
EMAIL_ACCOUNT_FIELD: emailAccount,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -385,17 +333,9 @@ func UpdateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"id": queryValue(resBody, "_id", ""),
|
"id": indexRes.ID,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -403,28 +343,14 @@ func UpdateEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.
|
||||||
func DeleteEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func DeleteEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
emailAccountId := ps.ByName("emailAccountId")
|
emailAccountId := ps.ByName("emailAccountId")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailAccountId)
|
deleteRes, err := esClient.Delete(orm.GetIndexName(alerting.Config{}), "", emailAccountId, "wait_for")
|
||||||
|
|
||||||
res, err := doRequest(reqUrl, http.MethodDelete, map[string]string{
|
|
||||||
"refresh": "wait_for",
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resultIfce := queryValue(resBody, "result", "")
|
|
||||||
var isOk = false
|
var isOk = false
|
||||||
if result, ok := resultIfce.(string); ok && result == "deleted" {
|
if deleteRes.Result == "deleted" {
|
||||||
isOk = true
|
isOk = true
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
|
@ -477,37 +403,25 @@ func GetEmailAccounts(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
}
|
}
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
searchRes, err := esClient.SearchWithRawQueryDSL( orm.GetIndexName(alerting.Config{}), util.MustToJSONBytes(reqBody))
|
||||||
|
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
totalEmailAccounts := searchRes.GetTotal()
|
||||||
err = decodeJSON(res.Body, &resBody)
|
rawHits := searchRes.Hits.Hits
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
totalEmailAccounts := queryValue(resBody, "hits.total.value", 0)
|
|
||||||
rawHits := queryValue(resBody, "hits.hits", []interface{}{})
|
|
||||||
emailAccounts := []IfaceMap{}
|
emailAccounts := []IfaceMap{}
|
||||||
|
|
||||||
if rh, ok := rawHits.([]interface{}); ok {
|
for _, hit := range rawHits {
|
||||||
for _, hit := range rh {
|
newItem := IfaceMap{}
|
||||||
if emailAccount, ok := hit.(map[string]interface{}); ok {
|
newItem["id"] = hit.ID
|
||||||
newItem := IfaceMap{}
|
source := queryValue(hit.Source, EMAIL_ACCOUNT_FIELD, nil)
|
||||||
newItem["id"] = queryValue(emailAccount, "_id", "")
|
if ms, ok := source.(map[string]interface{}); ok {
|
||||||
source := queryValue(emailAccount, "_source."+EMAIL_ACCOUNT_FIELD, nil)
|
assignTo(newItem, ms)
|
||||||
if ms, ok := source.(map[string]interface{}); ok {
|
|
||||||
assignTo(newItem, ms)
|
|
||||||
}
|
|
||||||
emailAccounts = append(emailAccounts, newItem)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
emailAccounts = append(emailAccounts, newItem)
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
|
@ -520,22 +434,13 @@ func GetEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Par
|
||||||
emailAccountId := ps.ByName("emailAccountId")
|
emailAccountId := ps.ByName("emailAccountId")
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailAccountId)
|
getRes, err := esClient.Get(orm.GetIndexName(alerting.Config{}), "", emailAccountId)
|
||||||
|
|
||||||
res, err := doRequest(reqUrl, http.MethodGet,nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//name := queryValue(resBody,"_source.email_account.name", "")
|
//name := queryValue(resBody,"_source.email_account.name", "")
|
||||||
//kk := fmt.Sprintf("search-center_alerting.destination.email.%s.password", name)
|
//kk := fmt.Sprintf("search-center_alerting.destination.email.%s.password", name)
|
||||||
//secStr, _ := getKeystore().Retrieve(kk)
|
//secStr, _ := getKeystore().Retrieve(kk)
|
||||||
|
@ -546,7 +451,7 @@ func GetEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Par
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": queryValue(resBody, "_source.email_account", nil),
|
"resp": queryValue(getRes.Source, "email_account", nil),
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -555,16 +460,14 @@ func GetEmailAccount(w http.ResponseWriter, req *http.Request, ps httprouter.Par
|
||||||
|
|
||||||
func CreateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func CreateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
|
||||||
var emailGroup = &alerting.EmailGroup{}
|
var emailGroup = &alerting.EmailGroup{}
|
||||||
err := decodeJSON(req.Body, &emailGroup)
|
err := decodeJSON(req.Body, &emailGroup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
esClient := elastic.GetClient(config.ID)
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index(orm.GetIndexName(alerting.Config{}), "", util.GetUUID(), IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
EMAIL_GROUP_FIELD: emailGroup,
|
EMAIL_GROUP_FIELD: emailGroup,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -572,14 +475,6 @@ func CreateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": IfaceMap{
|
"resp": IfaceMap{
|
||||||
|
@ -588,8 +483,8 @@ func CreateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
"name": emailGroup.Name,
|
"name": emailGroup.Name,
|
||||||
"schema_version": emailGroup.SchemaVersion,
|
"schema_version": emailGroup.SchemaVersion,
|
||||||
},
|
},
|
||||||
"_id": queryValue(resBody, "_id", ""),
|
"_id": indexRes.ID,
|
||||||
"_version": queryValue(resBody, "_version", 0),
|
"_version": indexRes.Version,
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
@ -597,16 +492,14 @@ func CreateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
func UpdateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func UpdateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
emailGroupId := ps.ByName("emailGroupId")
|
emailGroupId := ps.ByName("emailGroupId")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailGroupId)
|
|
||||||
var emailGroup = &alerting.EmailGroup{}
|
var emailGroup = &alerting.EmailGroup{}
|
||||||
err := decodeJSON(req.Body, &emailGroup)
|
err := decodeJSON(req.Body, &emailGroup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodPut, map[string]string{
|
esClient := elastic.GetClient(config.ID)
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index( orm.GetIndexName(alerting.Config{}), "", emailGroupId, IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
EMAIL_GROUP_FIELD: emailGroup,
|
EMAIL_GROUP_FIELD: emailGroup,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -614,16 +507,9 @@ func UpdateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"id": queryValue(resBody, "_id", ""),
|
"id": indexRes.ID,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -631,25 +517,15 @@ func UpdateEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Pa
|
||||||
func DeleteEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func DeleteEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
emailGroupId := ps.ByName("emailGroupId")
|
emailGroupId := ps.ByName("emailGroupId")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailGroupId)
|
esClient := elastic.GetClient(config.ID)
|
||||||
|
res, err := esClient.Delete(orm.GetIndexName(alerting.Config{}), "", emailGroupId)
|
||||||
res, err := doRequest(reqUrl, http.MethodDelete, nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resultIfce := queryValue(resBody, "result", "")
|
|
||||||
var isOk = false
|
var isOk = false
|
||||||
if result, ok := resultIfce.(string); ok && result == "deleted" {
|
if res.Result == "deleted" {
|
||||||
isOk = true
|
isOk = true
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
|
@ -703,36 +579,25 @@ func GetEmailGroups(w http.ResponseWriter, req *http.Request, ps httprouter.Para
|
||||||
}
|
}
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
esClient := elastic.GetClient(config.ID)
|
||||||
|
res, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(alerting.Config{}), util.MustToJSONBytes(reqBody))
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
totalEmailGroups := res.GetTotal()
|
||||||
err = decodeJSON(res.Body, &resBody)
|
rawHits := res.Hits.Hits
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
totalEmailGroups := queryValue(resBody, "hits.total.value", 0)
|
|
||||||
rawHits := queryValue(resBody, "hits.hits", []interface{}{})
|
|
||||||
emailGroups := []IfaceMap{}
|
emailGroups := []IfaceMap{}
|
||||||
|
|
||||||
if rh, ok := rawHits.([]interface{}); ok {
|
for _, hit := range rawHits {
|
||||||
for _, hit := range rh {
|
newItem := IfaceMap{}
|
||||||
if emailGroup, ok := hit.(map[string]interface{}); ok {
|
newItem["id"] = hit.ID
|
||||||
newItem := IfaceMap{}
|
source := queryValue(hit.Source, EMAIL_GROUP_FIELD, nil)
|
||||||
newItem["id"] = queryValue(emailGroup, "_id", "")
|
if ms, ok := source.(map[string]interface{}); ok {
|
||||||
source := queryValue(emailGroup, "_source."+EMAIL_GROUP_FIELD, nil)
|
assignTo(newItem, ms)
|
||||||
if ms, ok := source.(map[string]interface{}); ok {
|
|
||||||
assignTo(newItem, ms)
|
|
||||||
}
|
|
||||||
emailGroups = append(emailGroups, newItem)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
emailGroups = append(emailGroups, newItem)
|
||||||
}
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
|
@ -745,21 +610,14 @@ func GetEmailGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
emailGroupId := ps.ByName("emailGroupId")
|
emailGroupId := ps.ByName("emailGroupId")
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), emailGroupId)
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet,nil, req.Body)
|
getRes, err := esClient.Get(orm.GetIndexName(alerting.Config{}), "", emailGroupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
emailGroup := queryValue(getRes.Source, EMAIL_GROUP_FIELD, nil)
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
emailGroup := queryValue(resBody, "_source."+EMAIL_GROUP_FIELD, nil)
|
|
||||||
if emailGroup == nil {
|
if emailGroup == nil {
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": false,
|
"ok": false,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
httprouter "infini.sh/framework/core/api/router"
|
httprouter "infini.sh/framework/core/api/router"
|
||||||
"infini.sh/framework/core/elastic"
|
"infini.sh/framework/core/elastic"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -24,17 +25,9 @@ func Search(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, body.Index)
|
esClient := elastic.GetClient(config.ID)
|
||||||
|
|
||||||
body.Query["size"] = body.Size
|
body.Query["size"] = body.Size
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, nil, body.Query)
|
searchRes, err := esClient.SearchWithRawQueryDSL(body.Index, util.MustToJSONBytes(body.Query))
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -42,15 +35,15 @@ func Search(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": resBody,
|
"resp": searchRes,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIndices(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func GetIndices(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
id := ps.ByName("id")
|
id := ps.ByName("id")
|
||||||
meta := elastic.GetMetadata(id)
|
esClient := elastic.GetClient(id)
|
||||||
if meta == nil {
|
if esClient == nil {
|
||||||
writeError(w, errors.New("cluster not found"))
|
writeError(w, errors.New("cluster not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -63,27 +56,19 @@ func GetIndices(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqUrl := fmt.Sprintf("%s/_cat/indices/%s", meta.GetActiveEndpoint(), body.Index)
|
|
||||||
params := map[string]string{
|
|
||||||
"format": "json",
|
|
||||||
"h": "health,index,status",
|
|
||||||
}
|
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, params, nil)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
var resBody = []IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
indexInfos, err := esClient.GetIndices( body.Index)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
indices := make([]elastic.IndexInfo, 0, len(*indexInfos))
|
||||||
|
for _, info := range *indexInfos {
|
||||||
|
indices = append(indices, info)
|
||||||
|
}
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": resBody,
|
"resp": indices,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,8 +79,8 @@ func GetAliases(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
id := ps.ByName("id")
|
id := ps.ByName("id")
|
||||||
meta := elastic.GetMetadata(id)
|
esClient := elastic.GetClient(id)
|
||||||
if meta == nil {
|
if esClient == nil {
|
||||||
writeError(w, errors.New("cluster not found"))
|
writeError(w, errors.New("cluster not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -108,34 +93,30 @@ func GetAliases(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqUrl := fmt.Sprintf("%s/_cat/aliases/%s", meta.GetActiveEndpoint(), body.Alias)
|
//reqUrl := fmt.Sprintf("%s/_cat/aliases/%s", meta.GetActiveEndpoint(), body.Alias)
|
||||||
params := map[string]string{
|
//params := map[string]string{
|
||||||
"format": "json",
|
// "format": "json",
|
||||||
"h": "alias,index",
|
// "h": "alias,index",
|
||||||
}
|
//}
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, params, nil)
|
res, err := esClient.GetAliases()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
aliases := make([]elastic.AliasInfo, 0, len(*res))
|
||||||
var resBody = []IfaceMap{}
|
for _, alias := range *res {
|
||||||
err = decodeJSON(res.Body, &resBody)
|
aliases =append(aliases, alias)
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": resBody,
|
"resp": aliases,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMappings(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func GetMappings(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
id := ps.ByName("id")
|
id := ps.ByName("id")
|
||||||
meta := elastic.GetMetadata(id)
|
esClient := elastic.GetClient(id)
|
||||||
if meta == nil {
|
if esClient == nil {
|
||||||
writeError(w, errors.New("cluster not found"))
|
writeError(w, errors.New("cluster not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -148,15 +129,7 @@ func GetMappings(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_mapping", meta.GetActiveEndpoint(), strings.Join(body.Index, ","))
|
_, _, mappings, err := esClient.GetMapping(false, strings.Join(body.Index, ","))
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -164,7 +137,7 @@ func GetMappings(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
|
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": resBody,
|
"resp": mappings,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package alerting
|
package alerting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"infini.sh/console/model/alerting"
|
"infini.sh/console/model/alerting"
|
||||||
|
@ -9,7 +10,10 @@ import (
|
||||||
"infini.sh/framework/core/elastic"
|
"infini.sh/framework/core/elastic"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
"infini.sh/framework/core/util"
|
"infini.sh/framework/core/util"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"src/github.com/buger/jsonparser"
|
||||||
|
"src/github.com/valyala/fasttemplate"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -25,21 +29,14 @@ func GetMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
|
|
||||||
mid := ps.ByName("monitorID")
|
mid := ps.ByName("monitorID")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
|
res, err := esClient.Get(orm.GetIndexName(alerting.Config{}), "", mid)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), mid)
|
if !res.Found {
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
if found, ok := resBody["found"].(bool); ok && !found {
|
|
||||||
writeError(w, errors.New("monitor not found"))
|
writeError(w, errors.New("monitor not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -70,47 +67,40 @@ func GetMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
queryDSL = fmt.Sprintf(queryDSL, mid)
|
queryDSL = fmt.Sprintf(queryDSL, mid)
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_search", config.Endpoint, getAlertIndexName(INDEX_ALL_ALERTS))
|
searchRes, err := esClient.SearchWithRawQueryDSL(getAlertIndexName(INDEX_ALL_ALERTS), []byte(queryDSL))
|
||||||
res, err = doRequest(reqUrl, http.MethodPost,nil, queryDSL)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var dayCountBuckets interface{}
|
||||||
var searchResBody = IfaceMap{}
|
if agg, ok := searchRes.Aggregations["24_hour_count"]; ok {
|
||||||
err = decodeJSON(res.Body, &searchResBody)
|
dayCountBuckets = agg.Buckets
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
dayCountBuckets := queryValue(searchResBody, "aggregations.24_hour_count.buckets", 0)
|
|
||||||
dayCount := 0
|
dayCount := 0
|
||||||
if dcb, ok := dayCountBuckets.([]interface{}); ok {
|
if dcb, ok := dayCountBuckets.([]elastic.BucketBase); ok {
|
||||||
if dayAgg, ok := dcb[0].(map[string]interface{}); ok {
|
dayCount = int(dcb[0]["doc_count"].(float64))
|
||||||
dayCount = int(dayAgg["doc_count"].(float64))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
activeBuckets := queryValue(searchResBody, "aggregations.active_count.buckets",[]interface{}{})
|
var activeBuckets interface{}
|
||||||
|
if agg, ok := searchRes.Aggregations["active_count"]; ok {
|
||||||
|
activeBuckets = agg.Buckets
|
||||||
|
}
|
||||||
activeCount := 0
|
activeCount := 0
|
||||||
if ab, ok := activeBuckets.([]interface{}); ok {
|
if ab, ok := activeBuckets.([]elastic.BucketBase); ok {
|
||||||
for _, bk := range ab {
|
for _, bk := range ab {
|
||||||
if curr, ok := bk.(map[string]interface{}); ok {
|
if bk["key"].(string) == "ACTIVE" {
|
||||||
if curr["key"].(string) == "ACTIVE" {
|
activeCount = int(bk["doc_count"].(float64))
|
||||||
activeCount = int(curr["doc_count"].(float64))
|
break
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
monitor := queryValue(resBody, "_source.monitor", nil)
|
monitor := queryValue(res.Source, "monitor", nil)
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"resp": monitor,
|
"resp": monitor,
|
||||||
"activeCount": activeCount,
|
"activeCount": activeCount,
|
||||||
"dayCount": dayCount,
|
"dayCount": dayCount,
|
||||||
"version": queryValue(resBody, "_version", nil),
|
"version": res.Version,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,44 +189,33 @@ func GetMonitors(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
}
|
}
|
||||||
assignTo(params, sortPageData)
|
assignTo(params, sortPageData)
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(alerting.Config{}) )
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, params)
|
resBody, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(alerting.Config{}), util.MustToJSONBytes(params))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
totalMonitors := resBody.GetTotal()
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
totalMonitors := queryValue(resBody, "hits.total.value", 0)
|
|
||||||
var monitors []IfaceMap
|
var monitors []IfaceMap
|
||||||
var hits = queryValue(resBody, "hits.hits", []IfaceMap{})
|
var hits = resBody.Hits.Hits
|
||||||
monitorIDs := []interface{}{}
|
monitorIDs := []interface{}{}
|
||||||
monitorMap := map[string]int{}
|
monitorMap := map[string]int{}
|
||||||
if hitsArr, ok := hits.([]interface{}); ok {
|
for i, hit := range hits {
|
||||||
for i, hitIface := range hitsArr {
|
monitorIDs = append(monitorIDs, hit.ID)
|
||||||
if hit, ok := hitIface.(map[string]interface{}); ok {
|
monitor := hit.Source["monitor"].(map[string]interface{})
|
||||||
id := queryValue(hit, "_id", "")
|
monitorMap[hit.ID] = i
|
||||||
monitorIDs = append(monitorIDs, id)
|
monitors = append(monitors, IfaceMap{
|
||||||
monitor := queryValue(hit, "_source.monitor", IfaceMap{}).(map[string]interface{})
|
"id": hit.ID,
|
||||||
monitorMap[id.(string)] = i
|
//"version": hit.,
|
||||||
monitors = append(monitors, IfaceMap{
|
"name": monitor["name"],
|
||||||
"id": id,
|
"enabled": monitor["enabled"],
|
||||||
"version": queryValue(hit, "_version", ""),
|
"monitor": monitor,
|
||||||
"name": queryValue(monitor, "name", ""),
|
})
|
||||||
"enabled": queryValue(monitor, "enabled", false),
|
|
||||||
"monitor": monitor,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
aggsOrderData := IfaceMap{}
|
aggsOrderData := IfaceMap{}
|
||||||
aggsSorts := IfaceMap{
|
aggsSorts := IfaceMap{
|
||||||
"active": "active",
|
"active": "active",
|
||||||
|
@ -288,41 +267,35 @@ func GetMonitors(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_search", config.Endpoint, getAlertIndexName(INDEX_ALL_ALERTS))
|
searchRes, err := esClient.SearchWithRawQueryDSL(getAlertIndexName(INDEX_ALL_ALERTS), util.MustToJSONBytes(aggsParams))
|
||||||
searchRes, err := doRequest(reqUrl, http.MethodPost, nil, aggsParams)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchResBody = IfaceMap{}
|
var buckets interface{}
|
||||||
err = decodeJSON(searchRes.Body, &searchResBody)
|
if agg, ok := searchRes.Aggregations["uniq_monitor_ids"]; ok {
|
||||||
if err != nil {
|
buckets = agg.Buckets
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
buckets := queryValue(searchResBody, "aggregations.uniq_monitor_ids.buckets",[]IfaceMap{})
|
if bks, ok := buckets.([]elastic.BucketBase); ok {
|
||||||
if bks, ok := buckets.([]interface{}); ok {
|
|
||||||
for _, bk := range bks {
|
for _, bk := range bks {
|
||||||
if bk, ok := bk.(map[string]interface{}); ok {
|
id := queryValue(bk, "key", "")
|
||||||
id := queryValue(bk, "key", "")
|
monitor := monitors[monitorMap[id.(string)]]
|
||||||
monitor := monitors[monitorMap[id.(string)]]
|
monitor["lastNotificationTime"] = queryValue(bk, "last_notification_time.value", "")
|
||||||
monitor["lastNotificationTime"] = queryValue(bk, "last_notification_time.value", "")
|
monitor["ignored"] = queryValue(bk, "ignored.doc_count", 0)
|
||||||
monitor["ignored"] = queryValue(bk, "ignored.doc_count", 0)
|
alertHits := queryValue(bk, "latest_alert.hits.hits", nil)
|
||||||
alertHits := queryValue(bk, "latest_alert.hits.hits", nil)
|
var latestAlert interface{}
|
||||||
var latestAlert interface{}
|
if hits, ok := alertHits.([]interface{}); ok && len(hits) > 0 {
|
||||||
if hits, ok := alertHits.([]interface{}); ok && len(hits) > 0 {
|
if hitMap, ok := hits[0].(map[string]interface{}); ok {
|
||||||
if hitMap, ok := hits[0].(map[string]interface{}); ok {
|
latestAlert = queryValue(hitMap, "_source.trigger_name", nil)
|
||||||
latestAlert = queryValue(hitMap, "_source.trigger_name", nil)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
monitor["latestAlert"] = latestAlert
|
|
||||||
monitor["active"] = queryValue(bk, "active.doc_count", 0)
|
|
||||||
monitor["errors"] = queryValue(bk, "errors.doc_count", 0)
|
|
||||||
monitor["acknowledged"] = queryValue(bk, "acknowledged.doc_count", 0)
|
|
||||||
monitor["currentTime"] = time.Now().UnixNano() / 1e6
|
|
||||||
delete(monitorMap, id.(string))
|
|
||||||
}
|
}
|
||||||
|
monitor["latestAlert"] = latestAlert
|
||||||
|
monitor["active"] = queryValue(bk, "active.doc_count", 0)
|
||||||
|
monitor["errors"] = queryValue(bk, "errors.doc_count", 0)
|
||||||
|
monitor["acknowledged"] = queryValue(bk, "acknowledged.doc_count", 0)
|
||||||
|
monitor["currentTime"] = time.Now().UnixNano() / 1e6
|
||||||
|
delete(monitorMap, id.(string))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,10 +341,9 @@ func CreateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
|
|
||||||
monitor.LastUpdateTime = time.Now().UnixNano()/1e6
|
monitor.LastUpdateTime = time.Now().UnixNano()/1e6
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
indexName := orm.GetIndexName(alerting.Config{})
|
||||||
"refresh": "wait_for",
|
indexRes, err := esClient.Index(indexName,"",util.GetUUID(),IfaceMap{
|
||||||
}, IfaceMap{
|
|
||||||
"cluster_id": id,
|
"cluster_id": id,
|
||||||
MONITOR_FIELD: monitor,
|
MONITOR_FIELD: monitor,
|
||||||
})
|
})
|
||||||
|
@ -380,14 +352,8 @@ func CreateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
monitorId := indexRes.ID
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
monitorId := queryValue(resBody, "_id", "").(string)
|
|
||||||
GetScheduler().AddMonitor(monitorId, &ScheduleMonitor{
|
GetScheduler().AddMonitor(monitorId, &ScheduleMonitor{
|
||||||
Monitor: monitor,
|
Monitor: monitor,
|
||||||
ClusterID: id,
|
ClusterID: id,
|
||||||
|
@ -398,7 +364,7 @@ func CreateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
"resp": IfaceMap{
|
"resp": IfaceMap{
|
||||||
MONITOR_FIELD: monitor,
|
MONITOR_FIELD: monitor,
|
||||||
"_id": monitorId,
|
"_id": monitorId,
|
||||||
"_version": queryValue(resBody, "_version", 0),
|
"_version": indexRes.Version,
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
@ -413,9 +379,8 @@ func DeleteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
|
|
||||||
monitorId := ps.ByName("monitorID")
|
monitorId := ps.ByName("monitorID")
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(config.ID)
|
||||||
//change alert state to deleted and move alert to history
|
//change alert state to deleted and move alert to history
|
||||||
reqUrl := fmt.Sprintf("%s/_reindex", config.Endpoint)
|
|
||||||
query := IfaceMap{
|
query := IfaceMap{
|
||||||
"bool": IfaceMap{
|
"bool": IfaceMap{
|
||||||
"must": []IfaceMap{
|
"must": []IfaceMap{
|
||||||
|
@ -437,47 +402,38 @@ func DeleteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
"source": fmt.Sprintf("ctx._source['state'] = '%s';", ALERT_DELETED),
|
"source": fmt.Sprintf("ctx._source['state'] = '%s';", ALERT_DELETED),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err := doRequest(reqUrl, http.MethodPost,nil, reqBody)
|
_, err := esClient.Reindex(util.MustToJSONBytes(reqBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//delete alert
|
//delete alert
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_delete_by_query", config.Endpoint, getAlertIndexName(INDEX_ALERT))
|
_, err = esClient.DeleteByQuery(getAlertIndexName(INDEX_ALERT), util.MustToJSONBytes(IfaceMap{
|
||||||
_, err = doRequest(reqUrl, http.MethodPost, nil, IfaceMap{
|
|
||||||
"query" : query,
|
"query" : query,
|
||||||
})
|
}) )
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//logic delete monitor
|
//logic delete monitor
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_update/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), monitorId)
|
var indexName = orm.GetIndexName(alerting.Config{})
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
getRes, err := esClient.Get(indexName, "", monitorId)
|
||||||
"refresh": "wait_for",
|
if err != nil {
|
||||||
}, IfaceMap{
|
writeError(w, err)
|
||||||
"script" : IfaceMap{
|
return
|
||||||
"source": "ctx._source.monitor.status = 'DELETED';",
|
}
|
||||||
"lang": "painless",
|
source := util.MapStr(getRes.Source)
|
||||||
},
|
source.Put("monitor.status", "DELETED")
|
||||||
})
|
indexRes, err := esClient.Index(indexName, "", monitorId, source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resultIfce := queryValue(resBody, "result", "")
|
|
||||||
var isOk = false
|
var isOk = false
|
||||||
if result, ok := resultIfce.(string); ok && result == "updated" {
|
if indexRes.Result == "updated" {
|
||||||
isOk = true
|
isOk = true
|
||||||
GetScheduler().RemoveMonitor(monitorId)
|
GetScheduler().RemoveMonitor(monitorId)
|
||||||
}
|
}
|
||||||
|
@ -496,8 +452,6 @@ func UpdateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
}
|
}
|
||||||
|
|
||||||
monitorId := ps.ByName("monitorID")
|
monitorId := ps.ByName("monitorID")
|
||||||
config := getDefaultConfig()
|
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", config.Endpoint, orm.GetIndexName(alerting.Config{}), monitorId)
|
|
||||||
|
|
||||||
var monitor = &alerting.Monitor{}
|
var monitor = &alerting.Monitor{}
|
||||||
err := decodeJSON(req.Body, &monitor)
|
err := decodeJSON(req.Body, &monitor)
|
||||||
|
@ -520,20 +474,23 @@ func UpdateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
monitor.LastUpdateTime = time.Now().UnixNano()/1e6
|
monitor.LastUpdateTime = time.Now().UnixNano()/1e6
|
||||||
res, err := doRequest(reqUrl, http.MethodPut, map[string]string{
|
config := getDefaultConfig()
|
||||||
"refresh": "wait_for",
|
esClient := elastic.GetClient(config.ID)
|
||||||
}, IfaceMap{
|
indexName := orm.GetIndexName(alerting.Config{})
|
||||||
"cluster_id": id,
|
getRes, err := esClient.Get(indexName, "", monitorId)
|
||||||
MONITOR_FIELD: monitor,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !getRes.Found {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
indexRes, err := esClient.Index(indexName, "", monitorId, IfaceMap{
|
||||||
err = decodeJSON(res.Body, &resBody)
|
"cluster_id": getRes.Source["cluster_id"],
|
||||||
res.Body.Close()
|
MONITOR_FIELD: monitor,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
|
@ -545,8 +502,8 @@ func UpdateMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Param
|
||||||
})
|
})
|
||||||
writeJSON(w, IfaceMap{
|
writeJSON(w, IfaceMap{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"version": queryValue(resBody, "_version", ""),
|
"version": indexRes.Version,
|
||||||
"id": queryValue(resBody, "_id", ""),
|
"id": indexRes.ID,
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -570,7 +527,6 @@ func AcknowledgeAlerts(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
}
|
}
|
||||||
|
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_update_by_query", config.Endpoint, getAlertIndexName(INDEX_ALERT))
|
|
||||||
reqBody := IfaceMap{
|
reqBody := IfaceMap{
|
||||||
"query": IfaceMap{
|
"query": IfaceMap{
|
||||||
"bool": IfaceMap{
|
"bool": IfaceMap{
|
||||||
|
@ -591,24 +547,15 @@ func AcknowledgeAlerts(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
"source": fmt.Sprintf("ctx._source['state'] = '%s';ctx._source['acknowledged_time'] = %dL;", ALERT_ACKNOWLEDGED, time.Now().UnixNano()/1e6),
|
"source": fmt.Sprintf("ctx._source['state'] = '%s';ctx._source['acknowledged_time'] = %dL;", ALERT_ACKNOWLEDGED, time.Now().UnixNano()/1e6),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodPost, map[string]string{
|
esClient := elastic.GetClient(config.ID)
|
||||||
"refresh":"",
|
res, err := esClient.UpdateByQuery( getAlertIndexName(INDEX_ALERT), util.MustToJSONBytes(reqBody))
|
||||||
}, reqBody)
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var isOk = false
|
var isOk = false
|
||||||
if failed, ok := resBody["failures"].([]interface{}); ok && len(failed) == 0 {
|
if len(res.Failures) == 0 {
|
||||||
isOk = true
|
isOk = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -616,7 +563,7 @@ func AcknowledgeAlerts(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
"ok": isOk,
|
"ok": isOk,
|
||||||
"resp": IfaceMap{
|
"resp": IfaceMap{
|
||||||
"success": ackAlertsReq.AlertIDs,
|
"success": ackAlertsReq.AlertIDs,
|
||||||
"failed": []string{},
|
"failed": res.Failures,
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
|
|
||||||
|
@ -624,8 +571,8 @@ func AcknowledgeAlerts(w http.ResponseWriter, req *http.Request, ps httprouter.P
|
||||||
|
|
||||||
func ExecuteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func ExecuteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
id := ps.ByName("id")
|
id := ps.ByName("id")
|
||||||
meta := elastic.GetMetadata(id)
|
esClient := elastic.GetClient(id)
|
||||||
if meta == nil {
|
if esClient == nil {
|
||||||
writeError(w, errors.New("cluster not found"))
|
writeError(w, errors.New("cluster not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -648,26 +595,29 @@ func ExecuteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Para
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
periodStart := time.Now()
|
periodStart := time.Now()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", meta.GetActiveEndpoint(), strings.Join(monitor.Inputs[0].Search.Indices, ","))
|
period := alertUtil.GetMonitorPeriod(periodStart, &monitor.Schedule)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, monitor.Inputs[0].Search.Query)
|
//strQuery := string(util.MustToJSONBytes(monitor.Inputs[0].Search.Query))
|
||||||
|
//resolveQuery(strQuery, IfaceMap{
|
||||||
|
//
|
||||||
|
// "periodStart": period.Start,
|
||||||
|
// "periodEnd": period.End,
|
||||||
|
//})
|
||||||
|
|
||||||
|
queryDsl := util.MustToJSONBytes(monitor.Inputs[0].Search.Query)
|
||||||
|
searchRes, err := esClient.SearchWithRawQueryDSL(strings.Join(monitor.Inputs[0].Search.Indices, ","), queryDsl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
writeError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var resBody = IfaceMap{}
|
var resBody = IfaceMap{}
|
||||||
err = decodeJSON(res.Body, &resBody)
|
util.MustFromJSONBytes(searchRes.RawResult.Body, &resBody)
|
||||||
res.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var triggerResults = IfaceMap{}
|
var triggerResults = IfaceMap{}
|
||||||
sm := ScheduleMonitor{
|
sm := ScheduleMonitor{
|
||||||
Monitor: monitor,
|
Monitor: monitor,
|
||||||
}
|
}
|
||||||
period := alertUtil.GetMonitorPeriod(periodStart, &monitor.Schedule)
|
|
||||||
var monitorCtx []byte
|
var monitorCtx []byte
|
||||||
if dryrun == "true" {
|
if dryrun == "true" {
|
||||||
for _, trigger := range monitor.Triggers {
|
for _, trigger := range monitor.Triggers {
|
||||||
|
@ -676,7 +626,10 @@ func ExecuteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Para
|
||||||
"action_results": IfaceMap{},
|
"action_results": IfaceMap{},
|
||||||
"name": trigger.Name,
|
"name": trigger.Name,
|
||||||
}
|
}
|
||||||
monitorCtx, err = createMonitorContext(&trigger, resBody, &sm, IfaceMap{})
|
monitorCtx, err = createMonitorContext(&trigger, resBody, &sm, IfaceMap{
|
||||||
|
"periodStart": period.Start,
|
||||||
|
"periodEnd": period.End,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
triggerResult["error"] = err.Error()
|
triggerResult["error"] = err.Error()
|
||||||
triggerResults[trigger.ID] = triggerResult
|
triggerResults[trigger.ID] = triggerResult
|
||||||
|
@ -735,3 +688,20 @@ func ExecuteMonitor(w http.ResponseWriter, req *http.Request, ps httprouter.Para
|
||||||
},
|
},
|
||||||
}, http.StatusOK)
|
}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveQuery(query string, ctx IfaceMap ) ([]byte, error){
|
||||||
|
ctxBytes := util.MustToJSONBytes(ctx)
|
||||||
|
msg := query
|
||||||
|
tpl := fasttemplate.New(msg, "{{", "}}")
|
||||||
|
msgBuffer := bytes.NewBuffer(nil)
|
||||||
|
_, err := tpl.ExecuteFunc(msgBuffer, func(writer io.Writer, tag string)(int, error){
|
||||||
|
keyParts := strings.Split(tag,".")
|
||||||
|
value, _, _, err := jsonparser.Get(ctxBytes, keyParts...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return writer.Write(value)
|
||||||
|
})
|
||||||
|
return msgBuffer.Bytes(), err
|
||||||
|
//return json.Marshal(msg)
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package alerting
|
package alerting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
httprouter "infini.sh/framework/core/api/router"
|
httprouter "infini.sh/framework/core/api/router"
|
||||||
"infini.sh/framework/core/elastic"
|
"infini.sh/framework/core/elastic"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -56,11 +56,9 @@ func getAlertByState() (IfaceMap, error){
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var metricData = IfaceMap{}
|
var metricData = IfaceMap{}
|
||||||
if bks, ok := buckets.([]interface{}); ok {
|
if bks, ok := buckets.([]elastic.BucketBase); ok {
|
||||||
for _, bk := range bks {
|
for _, bk := range bks {
|
||||||
if bkm, ok := bk.(map[string]interface{}); ok {
|
metricData[queryValue(bk, "key", "").(string)]= queryValue(bk, "doc_count", 0)
|
||||||
metricData[queryValue(bkm, "key", "").(string)]= queryValue(bkm, "doc_count", 0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return metricData, nil
|
return metricData, nil
|
||||||
|
@ -68,18 +66,15 @@ func getAlertByState() (IfaceMap, error){
|
||||||
|
|
||||||
func queryMetricBuckets(reqBody IfaceMap, metricKey, indexName string)(interface{}, error){
|
func queryMetricBuckets(reqBody IfaceMap, metricKey, indexName string)(interface{}, error){
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", conf.Endpoint, getAlertIndexName(indexName))
|
esClient := elastic.GetClient(conf.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
res, err := esClient.SearchWithRawQueryDSL(getAlertIndexName(indexName), util.MustToJSONBytes(reqBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result := IfaceMap{}
|
var buckets interface{}
|
||||||
defer res.Body.Close()
|
if agg, ok := res.Aggregations[metricKey]; ok {
|
||||||
err = decodeJSON(res.Body, &result)
|
buckets = agg.Buckets
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
buckets := queryValue(result, fmt.Sprintf("aggregations.%s.buckets", metricKey), []interface{}{})
|
|
||||||
return buckets, nil
|
return buckets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,11 +104,10 @@ func getTopTenAlertCluster()(interface{}, error){
|
||||||
}
|
}
|
||||||
var metricData []IfaceMap
|
var metricData []IfaceMap
|
||||||
var clusterIDs []interface{}
|
var clusterIDs []interface{}
|
||||||
if bks, ok := buckets.([]interface{}); ok {
|
if bks, ok := buckets.([]elastic.BucketBase); ok {
|
||||||
for _, bk := range bks {
|
for _, bk := range bks {
|
||||||
if bkm, ok := bk.(map[string]interface{}); ok {
|
stateBuckets := queryValue(bk, "group_by_state.buckets", nil )
|
||||||
stateBuckets := queryValue(bkm, "group_by_state.buckets", nil )
|
key := queryValue(bk, "key", "" )
|
||||||
key := queryValue(bkm, "key", "" )
|
|
||||||
clusterIDs = append(clusterIDs, key)
|
clusterIDs = append(clusterIDs, key)
|
||||||
if stateBKS, ok := stateBuckets.([]interface{}); ok{
|
if stateBKS, ok := stateBuckets.([]interface{}); ok{
|
||||||
for _, stateBK := range stateBKS {
|
for _, stateBK := range stateBKS {
|
||||||
|
@ -127,7 +121,6 @@ func getTopTenAlertCluster()(interface{}, error){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
reqBody = IfaceMap{
|
reqBody = IfaceMap{
|
||||||
"_source": "name",
|
"_source": "name",
|
||||||
|
@ -138,17 +131,11 @@ func getTopTenAlertCluster()(interface{}, error){
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(elastic.ElasticsearchConfig{}))
|
esClient := elastic.GetClient(config.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
resBody, err := esClient.SearchWithRawQueryDSL( orm.GetIndexName(elastic.ElasticsearchConfig{}), util.MustToJSONBytes(reqBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var resBody = &elastic.SearchResponse{}
|
|
||||||
err = decodeJSON(res.Body, resBody)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
clusterMap := IfaceMap{}
|
clusterMap := IfaceMap{}
|
||||||
for _, hit := range resBody.Hits.Hits {
|
for _, hit := range resBody.Hits.Hits {
|
||||||
clusterMap[hit.ID] = hit.Source["name"]
|
clusterMap[hit.ID] = hit.Source["name"]
|
||||||
|
@ -189,14 +176,12 @@ func getLastAlertDayCount() (interface{}, error){
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var metricData []interface{}
|
var metricData []interface{}
|
||||||
if bks, ok := buckets.([]interface{}); ok {
|
if bks, ok := buckets.([]elastic.BucketBase); ok {
|
||||||
for _, bk := range bks {
|
for _, bk := range bks {
|
||||||
if bkm, ok := bk.(map[string]interface{}); ok {
|
metricData = append(metricData, []interface{}{
|
||||||
metricData = append(metricData, []interface{}{
|
queryValue(bk, "key", ""),
|
||||||
queryValue(bkm, "key", ""),
|
queryValue(bk, "doc_count", 0),
|
||||||
queryValue(bkm, "doc_count", 0),
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return metricData, nil
|
return metricData, nil
|
||||||
|
|
|
@ -12,12 +12,12 @@ import (
|
||||||
"github.com/valyala/fasttemplate"
|
"github.com/valyala/fasttemplate"
|
||||||
"infini.sh/console/model/alerting"
|
"infini.sh/console/model/alerting"
|
||||||
"infini.sh/console/service/alerting/action"
|
"infini.sh/console/service/alerting/action"
|
||||||
"infini.sh/console/service/alerting/util"
|
util1 "infini.sh/console/service/alerting/util"
|
||||||
"infini.sh/framework/core/conditions"
|
"infini.sh/framework/core/conditions"
|
||||||
"infini.sh/framework/core/elastic"
|
"infini.sh/framework/core/elastic"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -154,7 +154,7 @@ func generateMonitorJob(smt *ScheduleMonitor) MonitorJob{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
periods := util.GetMonitorPeriod(startTime, &sm.Monitor.Schedule)
|
periods := util1.GetMonitorPeriod(startTime, &sm.Monitor.Schedule)
|
||||||
for _, trigger := range sm.Monitor.Triggers {
|
for _, trigger := range sm.Monitor.Triggers {
|
||||||
monitorCtx, err := createMonitorContext(&trigger, queryResult, &sm, IfaceMap{
|
monitorCtx, err := createMonitorContext(&trigger, queryResult, &sm, IfaceMap{
|
||||||
"periodStart": periods.Start,
|
"periodStart": periods.Start,
|
||||||
|
@ -274,7 +274,7 @@ func doAction(act alerting.Action, monitorCtx []byte) ([]byte, error) {
|
||||||
|
|
||||||
func getLastAlert(monitorID, triggerID, clusterID string) (map[string]interface{}, error) {
|
func getLastAlert(monitorID, triggerID, clusterID string) (map[string]interface{}, error) {
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", conf.Endpoint, getAlertIndexName(INDEX_ALERT))
|
esClient := elastic.GetClient(conf.ID)
|
||||||
reqBody := IfaceMap{
|
reqBody := IfaceMap{
|
||||||
"size": 1,
|
"size": 1,
|
||||||
"query": IfaceMap{
|
"query": IfaceMap{
|
||||||
|
@ -303,12 +303,8 @@ func getLastAlert(monitorID, triggerID, clusterID string) (map[string]interface{
|
||||||
{"start_time": IfaceMap{"order":"desc"}},
|
{"start_time": IfaceMap{"order":"desc"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
|
||||||
if err != nil {
|
resBody, err := esClient.SearchWithRawQueryDSL(getAlertIndexName(INDEX_ALERT), util.MustToJSONBytes(reqBody))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var resBody = &elastic.SearchResponse{}
|
|
||||||
err = decodeJSON(res.Body, resBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -320,8 +316,8 @@ func getLastAlert(monitorID, triggerID, clusterID string) (map[string]interface{
|
||||||
|
|
||||||
func saveAlertInfo(alertItem *alerting.Alert) error {
|
func saveAlertInfo(alertItem *alerting.Alert) error {
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
|
esClient := elastic.GetClient(conf.ID)
|
||||||
indexName := getAlertIndexName(INDEX_ALERT)
|
indexName := getAlertIndexName(INDEX_ALERT)
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", conf.Endpoint, indexName)
|
|
||||||
reqBody := IfaceMap{
|
reqBody := IfaceMap{
|
||||||
"size": 1,
|
"size": 1,
|
||||||
"query": IfaceMap{
|
"query": IfaceMap{
|
||||||
|
@ -352,28 +348,20 @@ func saveAlertInfo(alertItem *alerting.Alert) error {
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
resBody, err := esClient.SearchWithRawQueryDSL(indexName, util.MustToJSONBytes(reqBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var resBody = elastic.SearchResponse{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
if len(resBody.Hits.Hits) == 0 {
|
if len(resBody.Hits.Hits) == 0 {
|
||||||
if alertItem.State == ALERT_COMPLETED {
|
if alertItem.State == ALERT_COMPLETED {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_doc", conf.Endpoint, indexName)
|
_, err = esClient.Index(indexName,"", util.GetUUID(), alertItem)
|
||||||
_,err = doRequest(reqUrl, http.MethodPost, nil, alertItem)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
currentState := queryValue(resBody.Hits.Hits[0].Source, "state", "").(string)
|
currentState := queryValue(resBody.Hits.Hits[0].Source, "state", "").(string)
|
||||||
alertItem.Id = resBody.Hits.Hits[0].ID
|
alertItem.Id = resBody.Hits.Hits[0].ID
|
||||||
if currentState != alertItem.State {
|
if currentState != alertItem.State {
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, getAlertIndexName(INDEX_ALERT_HISTORY), alertItem.Id)
|
|
||||||
source := resBody.Hits.Hits[0].Source
|
source := resBody.Hits.Hits[0].Source
|
||||||
source["end_time"] = time.Now().UnixNano()/1e6
|
source["end_time"] = time.Now().UnixNano()/1e6
|
||||||
if alertItem.State == ALERT_COMPLETED {
|
if alertItem.State == ALERT_COMPLETED {
|
||||||
|
@ -381,15 +369,13 @@ func saveAlertInfo(alertItem *alerting.Alert) error {
|
||||||
source["state"] = ALERT_COMPLETED
|
source["state"] = ALERT_COMPLETED
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_,err = doRequest(reqUrl, http.MethodPut, nil, source)
|
esClient.Index( getAlertIndexName(INDEX_ALERT_HISTORY), "", alertItem.Id, source)
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, indexName, resBody.Hits.Hits[0].ID)
|
_,err = esClient.Delete(indexName, "", resBody.Hits.Hits[0].ID)
|
||||||
_,err = doRequest(reqUrl, http.MethodDelete, nil, alertItem)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
alertItem.StartTime = int64(queryValue(resBody.Hits.Hits[0].Source, "start_time", 0).(float64))
|
alertItem.StartTime = int64(queryValue(resBody.Hits.Hits[0].Source, "start_time", 0).(float64))
|
||||||
|
|
||||||
reqUrl = fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, indexName, alertItem.Id)
|
_, err = esClient.Index(indexName, "", alertItem.Id, alertItem )
|
||||||
_,err = doRequest(reqUrl, http.MethodPut, nil, alertItem)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,74 +402,55 @@ func getEmailRecipient(recipients []alerting.Recipient) ([]string, error){
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveDestination(ID string)(*alerting.Destination, error){
|
func resolveDestination(ID string)(*alerting.Destination, error){
|
||||||
//todo may be cache destination ?
|
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, orm.GetIndexName(alerting.Config{}), ID)
|
esClient := elastic.GetClient(conf.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, nil)
|
res, err := esClient.Get( orm.GetIndexName(alerting.Config{}), "", ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
destination := &alerting.Destination{}
|
destination := &alerting.Destination{}
|
||||||
buf, _ := json.Marshal(queryValue(resBody, "_source."+DESTINATION_FIELD, IfaceMap{}))
|
buf, _ := json.Marshal(queryValue(res.Source, DESTINATION_FIELD, IfaceMap{}))
|
||||||
_ = json.Unmarshal(buf, destination)
|
_ = json.Unmarshal(buf, destination)
|
||||||
return destination, nil
|
return destination, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveEmailAccount(ID string)(*alerting.EmailAccount, error){
|
func resolveEmailAccount(ID string)(*alerting.EmailAccount, error){
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, orm.GetIndexName(alerting.Config{}), ID)
|
esClient := elastic.GetClient(conf.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, nil)
|
res, err := esClient.Get( orm.GetIndexName(alerting.Config{}), "", ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
email := &alerting.EmailAccount{}
|
email := &alerting.EmailAccount{}
|
||||||
buf, _ := json.Marshal(queryValue(resBody, "_source."+EMAIL_ACCOUNT_FIELD, IfaceMap{}))
|
buf, _ := json.Marshal(queryValue(res.Source, EMAIL_ACCOUNT_FIELD, IfaceMap{}))
|
||||||
_ = json.Unmarshal(buf, email)
|
_ = json.Unmarshal(buf, email)
|
||||||
return email, nil
|
return email, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveEmailGroup(ID string)(*alerting.EmailGroup, error){
|
func resolveEmailGroup(ID string)(*alerting.EmailGroup, error){
|
||||||
conf := getDefaultConfig()
|
conf := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_doc/%s", conf.Endpoint, orm.GetIndexName(alerting.Config{}), ID)
|
esClient := elastic.GetClient(conf.ID)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, nil)
|
res, err := esClient.Get( orm.GetIndexName(alerting.Config{}), "", ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
var resBody = IfaceMap{}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
emailGroup := &alerting.EmailGroup{}
|
emailGroup := &alerting.EmailGroup{}
|
||||||
buf, _ := json.Marshal(queryValue(resBody, "_source."+EMAIL_GROUP_FIELD, IfaceMap{}))
|
buf, _ := json.Marshal(queryValue(res.Source, EMAIL_GROUP_FIELD, IfaceMap{}))
|
||||||
_ = json.Unmarshal(buf, emailGroup)
|
err = json.Unmarshal(buf, emailGroup)
|
||||||
return emailGroup, nil
|
return emailGroup, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getQueryResult(clusterID string, input *alerting.MonitorInput) (IfaceMap, error) {
|
func getQueryResult(clusterID string, input *alerting.MonitorInput) (IfaceMap, error) {
|
||||||
meta := elastic.GetMetadata(clusterID)
|
esClient := elastic.GetClient(clusterID)
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", meta.GetActiveEndpoint(), strings.Join(input.Search.Indices, ","))
|
queryDsl := util.MustToJSONBytes(input.Search.Query)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, input.Search.Query)
|
searchRes, err := esClient.SearchWithRawQueryDSL(strings.Join(input.Search.Indices, ","), queryDsl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
var resBody = IfaceMap{}
|
||||||
resBody := IfaceMap{}
|
util.MustFromJSONBytes(searchRes.RawResult.Body, &resBody)
|
||||||
err = decodeJSON(res.Body, &resBody)
|
return resBody, nil
|
||||||
return resBody, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveMessage(messageTemplate IfaceMap, monitorCtx []byte ) ([]byte, error){
|
func resolveMessage(messageTemplate IfaceMap, monitorCtx []byte ) ([]byte, error){
|
||||||
|
@ -544,7 +511,7 @@ func resolveTriggerResult(trigger *alerting.Trigger, monitorCtx []byte ) (bool,
|
||||||
|
|
||||||
func getEnabledMonitors() (map[string]ScheduleMonitor, error){
|
func getEnabledMonitors() (map[string]ScheduleMonitor, error){
|
||||||
config := getDefaultConfig()
|
config := getDefaultConfig()
|
||||||
reqUrl := fmt.Sprintf("%s/%s/_search", config.Endpoint, orm.GetIndexName(alerting.Config{}))
|
esClient := elastic.GetClient(config.ID)
|
||||||
must := []IfaceMap{
|
must := []IfaceMap{
|
||||||
{
|
{
|
||||||
"exists": IfaceMap{
|
"exists": IfaceMap{
|
||||||
|
@ -562,15 +529,18 @@ func getEnabledMonitors() (map[string]ScheduleMonitor, error){
|
||||||
"query": IfaceMap{
|
"query": IfaceMap{
|
||||||
"bool": IfaceMap{
|
"bool": IfaceMap{
|
||||||
"must": must,
|
"must": must,
|
||||||
|
"must_not": []IfaceMap{
|
||||||
|
{
|
||||||
|
"match": IfaceMap{
|
||||||
|
MONITOR_FIELD+".status": "DELETED",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resBody := elastic.SearchResponse{}
|
queryDsl := util.MustToJSONBytes(reqBody)
|
||||||
res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody)
|
resBody, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(alerting.Config{}),queryDsl)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = decodeJSON(res.Body, &resBody)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue