diff --git a/elk/elasticClient/client.go b/elk/elasticClient/client.go index d90c6c774e5234912e9259c1a88bc76ca8e3bc55..a8e9f4c9c7e95154f816d1dbd55a12f3714ef57f 100644 --- a/elk/elasticClient/client.go +++ b/elk/elasticClient/client.go @@ -2,12 +2,13 @@ package elasticClient import ( "context" - "errors" "fmt" "net" "net/http" "time" + "github.com/pkg/errors" + "gitee.com/openeuler/PilotGo-plugin-elk/conf" "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" @@ -37,7 +38,7 @@ func InitElasticClient() { es_client, err := elastic.NewClient(cfg) if err != nil { - err = errors.New("failed to init kibana client **errstackfatal**0") // err top + err = errors.Errorf("failed to init elastic client: %+v **errstackfatal**0", err.Error()) // err top errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) return } diff --git a/elk/handler/router.go b/elk/handler/router.go index 9fcc79dc91f3e9372b835a3332df96552025f13a..d0c9de16bd16f1e19478d1e9caac6bf717611719 100644 --- a/elk/handler/router.go +++ b/elk/handler/router.go @@ -48,6 +48,7 @@ func InitRouter(router *gin.Engine) { api := router.Group("/plugin/elk/api") { api.POST("/create_policy", CreatePolicyHandle) + api.POST("/search", SearchHandle) } timeoutapi := router.Group("/plugin/elk/api") diff --git a/elk/handler/search.go b/elk/handler/search.go new file mode 100644 index 0000000000000000000000000000000000000000..8c80d80fefece79149efcea763018bec9057dd95 --- /dev/null +++ b/elk/handler/search.go @@ -0,0 +1,56 @@ +package handler + +import ( + "context" + "encoding/json" + "io" + + "github.com/pkg/errors" + + "gitee.com/openeuler/PilotGo-plugin-elk/elasticClient" + "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" +) + +func SearchHandle(ctx *gin.Context) { + defer ctx.Request.Body.Close() + if elasticClient.Global_elastic.Client == nil { + err := errors.Errorf("%+v **warn**0", "global_elastic is null") + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, err.Error()) + return + } + resp, err := elasticClient.Global_elastic.Client.Search( + elasticClient.Global_elastic.Client.Search.WithContext(context.Background()), + elasticClient.Global_elastic.Client.Search.WithIndex(".ds-logs-system.syslog-default-2024.06.04-000001"), + elasticClient.Global_elastic.Client.Search.WithBody(ctx.Request.Body), + elasticClient.Global_elastic.Client.Search.WithTrackTotalHits(true), + elasticClient.Global_elastic.Client.Search.WithPretty(), + ) + defer resp.Body.Close() + if err != nil { + err = errors.Errorf("%+v **warn**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, errors.Cause(err).Error()) + return + } + if resp.IsError() { + err = errors.Errorf("%+v **warn**0", resp.String()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, resp.String()) + return + } else { + resp_body_data := map[string]interface{}{} + resp_body_bytes, err := io.ReadAll(resp.Body) + if err != nil { + err = errors.Errorf("%+v **warn**0", err.Error()) + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, resp.String()) + return + } + json.Unmarshal(resp_body_bytes, &resp_body_data) + response.Success(ctx, resp_body_data, "") + } +} diff --git a/elk/handler/static.go b/elk/handler/static.go index 315516922233dc4e136696ba2ceeefdbbba60fb2..e7fbb968c1d33d94c5da607876cb0339ceb4268c 100644 --- a/elk/handler/static.go +++ b/elk/handler/static.go @@ -13,7 +13,7 @@ import ( func StaticRouter(router *gin.Engine) { static := router.Group("/plugin/elk") { - static.Static("/assets", "web/dist/assets") + static.Static("/assets", "web/dist/static") static.StaticFile("/", "web/dist/index.html") // 解决页面刷新404的问题 diff --git a/elk/kibanaClient/7_17_16/client.go b/elk/kibanaClient/7_17_16/client.go index 2b4b1715c2c1d36b38d42c5e85d5dc1c9bfeae25..19ed9efb2a3f4b783cfd5335bd4c8b54c704c597 100644 --- a/elk/kibanaClient/7_17_16/client.go +++ b/elk/kibanaClient/7_17_16/client.go @@ -2,9 +2,10 @@ package kibanaClient import ( "context" - "errors" "fmt" + "github.com/pkg/errors" + "gitee.com/openeuler/PilotGo-plugin-elk/conf" "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" "gitee.com/openeuler/PilotGo-plugin-elk/global" @@ -30,7 +31,7 @@ func InitKibanaClient() { ki_client, err := kibana.NewClientWithConfig(cfg, "", "", "", "") if err != nil { - err = errors.New("failed to init kibana client **errstackfatal**0") // err top + err = errors.Errorf("fail to init kibana client: %+v **errstackfatal**0", err.Error()) // err top errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) return } diff --git a/elk/kibanaClient/8_13_0/client.go b/elk/kibanaClient/8_13_0/client.go index 4b3c2ca38313933dafdfd7135cbc93725fa0aa93..dfaed389f8cd4bdd9bba9f5efe1406b9f1104b4e 100644 --- a/elk/kibanaClient/8_13_0/client.go +++ b/elk/kibanaClient/8_13_0/client.go @@ -2,9 +2,10 @@ package kibanaClient import ( "context" - "errors" "fmt" + "github.com/pkg/errors" + "gitee.com/openeuler/PilotGo-plugin-elk/conf" "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" "gitee.com/openeuler/PilotGo-plugin-elk/global" @@ -30,7 +31,7 @@ func InitKibanaClient() { ki_client, err := kibana.NewClientWithConfig(cfg, "", "", "", "") if err != nil { - err = errors.New("failed to init kibana client **errstackfatal**0") // err top + err = errors.Errorf("failed to init kibana client: %+v **errstackfatal**0", err.Error()) // err top errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) return }