diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go old mode 100644 new mode 100755 index 2ec838bbaec92cac39f4a7ab2a33a8943bc1c9f6..295002678c3b7ca6c4fa7669829cdd99dc949ace --- a/server/elasticClient/client.go +++ b/server/elasticClient/client.go @@ -9,14 +9,12 @@ import ( "io" "net" "net/http" - "strings" "time" "github.com/pkg/errors" "gitee.com/openeuler/PilotGo-plugin-elk/server/conf" "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" - "gitee.com/openeuler/PilotGo-plugin-elk/server/global/template" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" elastic "github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v7/esapi" @@ -25,8 +23,8 @@ import ( var Global_elastic *ElasticClient_v7 type ElasticClient_v7 struct { - Client *elastic.Client - Ctx context.Context + Client *elastic.Client + Ctx context.Context } func InitElasticClient() { @@ -59,25 +57,6 @@ func InitElasticClient() { Client: es_client, Ctx: pluginclient.Global_Context, } - - Global_elastic.initSearchTemplate() -} - -// 在elasticsearch中添加查询模板 -func (client *ElasticClient_v7) initSearchTemplate() { - for key, value := range template.DSL_template_map { - reqbody := strings.NewReader(value[0]) - _, err := client.Client.PutScript( - key, - reqbody, - client.Client.PutScript.WithContext(client.Ctx), - client.Client.PutScript.WithPretty(), - ) - if err != nil { - err = errors.Errorf("fail to put script: %s, %s **warn**0", key, err.Error()) // err top - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - } - } } // 通过dsl查询 diff --git a/server/handler/router.go b/server/handler/router.go old mode 100644 new mode 100755 index 0a1e8af9fcf34dae72724ef9f8135e6e30f14f4a..c17377f9cecac8b3dfd0159854dfa0d0e23177ea --- a/server/handler/router.go +++ b/server/handler/router.go @@ -48,7 +48,7 @@ func InitRouter(router *gin.Engine) { api := router.Group("/plugin/elk/api") { api.POST("/create_policy", CreatePolicyHandle) - + api.POST("/log_clusterhost_timeaxis_data", SearchByTemplateHandle) api.POST("/log_hostprocess_timeaxis_data", SearchByTemplateHandle) api.POST("/log_stream_data", SearchByTemplateHandle) diff --git a/server/handler/searchHandle.go b/server/handler/searchHandle.go old mode 100644 new mode 100755 index 53ee5ba4df0e07b70e4667931198ad84137cb68a..1257ec19b3bf75212d1377088fda373c2bcc79f9 --- a/server/handler/searchHandle.go +++ b/server/handler/searchHandle.go @@ -11,7 +11,7 @@ import ( "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" "gitee.com/openeuler/PilotGo-plugin-elk/server/global" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" - "gitee.com/openeuler/PilotGo-plugin-elk/server/service" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/template" "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" ) @@ -56,7 +56,7 @@ func SearchByTemplateHandle(ctx *gin.Context) { "id": req_body.Id, "params": params, } - data, err := service.TemplateHandleFuncMap[query_body["id"].(string)](index, query_body) + data, err := template.QueryTemplateMap[query_body["id"].(string)].Func(index, query_body) if err != nil { wrapError(ctx, err) return diff --git a/server/main.go b/server/main.go old mode 100644 new mode 100755 index 59976c34b4874f052ba0bd80dc654fab085e11ee..875f40f4ae647f48d6ec470e8acaa5b32a5571a0 --- a/server/main.go +++ b/server/main.go @@ -9,6 +9,7 @@ import ( kibanaclient "gitee.com/openeuler/PilotGo-plugin-elk/server/kibanaClient/7_17_16" "gitee.com/openeuler/PilotGo-plugin-elk/server/logger" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/template" "gitee.com/openeuler/PilotGo-plugin-elk/server/signal" ) @@ -45,8 +46,10 @@ func main() { /* init elasticsearch client + init search template */ elasticClient.InitElasticClient() + template.InitSearchTemplate() /* init kibana client diff --git a/server/service/cluster/LogTimeAixs.go b/server/service/cluster/LogTimeAixs.go old mode 100644 new mode 100755 index 169b4c870211d2d880e560ace2bf8170ea108ad3..69b5cce864bd4ab42537839e7dfe6bd5c5482202 --- a/server/service/cluster/LogTimeAixs.go +++ b/server/service/cluster/LogTimeAixs.go @@ -8,7 +8,7 @@ import ( "github.com/tidwall/gjson" ) -func ProcessLogTimeAixsData(index string, querybody map[string]interface{}) (interface{}, error) { +func ProcessLogTimeAxisData(index string, querybody map[string]interface{}) (interface{}, error) { search_result_body_bytes, err := elasticClient.Global_elastic.SearchByTemplate(index, querybody) if err != nil { err = errors.Wrap(err, "fail to process log timeaxis data") diff --git a/server/service/meta.go b/server/service/meta.go deleted file mode 100644 index 10fffed9f2075f85c7fff3505c3b5a803bc3aa64..0000000000000000000000000000000000000000 --- a/server/service/meta.go +++ /dev/null @@ -1,22 +0,0 @@ -package service - -import ( - "gitee.com/openeuler/PilotGo-plugin-elk/server/global/template" - "gitee.com/openeuler/PilotGo-plugin-elk/server/service/cluster" -) - -type SearchTemplateFunc func(string, map[string]interface{}) (interface{}, error) - -var TemplateHandleFuncMap map[string]SearchTemplateFunc - -func init() { - TemplateHandleFuncMap = make(map[string]SearchTemplateFunc) - for template_id, content := range template.DSL_template_map { - switch content[1] { - case "ProcessLogTimeAixsData": - TemplateHandleFuncMap[template_id] = cluster.ProcessLogTimeAixsData - case "ProcessLogStreamData": - TemplateHandleFuncMap[template_id] = cluster.ProcessLogStreamData - } - } -} diff --git a/server/global/template/cluster.go b/server/service/template/meta.go old mode 100644 new mode 100755 similarity index 92% rename from server/global/template/cluster.go rename to server/service/template/meta.go index 994a7316ac2ed08b01ed5164e6cc7a9feb2c6e3e..323e0b82f056a028e7a430128bfcf0d4b63a5867 --- a/server/global/template/cluster.go +++ b/server/service/template/meta.go @@ -1,8 +1,18 @@ package template -var ( - DSL_template_map map[string][]string -) +/* +key: 查询模板id + +value: 查询模板文本及处理函数 +*/ +var QueryTemplateMap map[string]QueryTemplateMeta + +type SearchTemplateFunc func(string, map[string]interface{}) (interface{}, error) + +type QueryTemplateMeta struct { + Text string `json:"text"` // 查询模板文本 + Func SearchTemplateFunc `json:"func"` // 查询模板请求处理函数 +} const ( DSL_log_clusterhost_timeaxis_template = `{ @@ -251,20 +261,3 @@ const ( } }` ) - -func init() { - DSL_template_map = map[string][]string{ - "log_clusterhost_timeaxis": { - DSL_log_clusterhost_timeaxis_template, - "ProcessLogTimeAixsData", - }, - "log_hostprocess_timeaxis": { - DSL_log_hostprocess_timeaxis_template, - "ProcessLogTimeAixsData", - }, - "log_stream": { - DSL_log_stream_template, - "ProcessLogStreamData", - }, - } -} diff --git a/server/service/template/template.go b/server/service/template/template.go new file mode 100755 index 0000000000000000000000000000000000000000..8d546977447f968e46fa811ee3b76a216dc11df9 --- /dev/null +++ b/server/service/template/template.go @@ -0,0 +1,51 @@ +package template + +import ( + "strings" + + "gitee.com/openeuler/PilotGo-plugin-elk/server/elasticClient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/cluster" + "github.com/pkg/errors" +) + +func init() { + QueryTemplateMap = map[string]QueryTemplateMeta{ + "log_clusterhost_timeaxis": { + Text: DSL_log_clusterhost_timeaxis_template, + Func: cluster.ProcessLogTimeAxisData, + }, + "log_hostprocess_timeaxis": { + Text: DSL_log_hostprocess_timeaxis_template, + Func: cluster.ProcessLogTimeAxisData, + }, + "log_stream": { + Text: DSL_log_stream_template, + Func: cluster.ProcessLogStreamData, + }, + } +} + +// 在elasticsearch中添加查询模板 +func InitSearchTemplate() { + if elasticClient.Global_elastic == nil { + err := errors.Errorf("elasticClient is nil **errstackfatal**0") // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) + return + } + + for template_id, template_meta := range QueryTemplateMap { + reqbody := strings.NewReader(template_meta.Text) + _, err := elasticClient.Global_elastic.Client.PutScript( + template_id, + reqbody, + elasticClient.Global_elastic.Client.PutScript.WithContext(elasticClient.Global_elastic.Ctx), + elasticClient.Global_elastic.Client.PutScript.WithPretty(), + ) + if err != nil { + err = errors.Errorf("fail to put script: %s, %s **errstackfatal**0", template_id, err.Error()) // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) + } + } +}