From 9d7dfb681088ea61f5b3fe4478b25eb7000bdfc3 Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Thu, 15 Jun 2023 16:24:48 +0800 Subject: [PATCH] add (o *opsclient)sendjsonmode to galaops plugin --- .../{ => prometheus}/cpu_usage_rate.json | 0 gala-ops/server/httphandler/component.go | 44 +++++++++++++++++++ gala-ops/server/main.go | 10 ++++- 3 files changed, 52 insertions(+), 2 deletions(-) rename gala-ops/server/gui-json-mode/{ => prometheus}/cpu_usage_rate.json (100%) diff --git a/gala-ops/server/gui-json-mode/cpu_usage_rate.json b/gala-ops/server/gui-json-mode/prometheus/cpu_usage_rate.json similarity index 100% rename from gala-ops/server/gui-json-mode/cpu_usage_rate.json rename to gala-ops/server/gui-json-mode/prometheus/cpu_usage_rate.json diff --git a/gala-ops/server/httphandler/component.go b/gala-ops/server/httphandler/component.go index f48f3122..183ef4e5 100644 --- a/gala-ops/server/httphandler/component.go +++ b/gala-ops/server/httphandler/component.go @@ -7,10 +7,16 @@ import ( "io" "net/http" "net/url" + "os" + "path" + "path/filepath" + "runtime" + "strings" "time" "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" + "gitee.com/openeuler/PilotGo-plugins/sdk/utils/httputils" ) type Opsclient struct { @@ -92,3 +98,41 @@ func (o *Opsclient) Getplugininfo(pilotgoserver string, pluginname string) (map[ } return PromePlugin, nil } + +func (o *Opsclient) SendJsonMode(jsonmodeURL string) (string, error) { + url := Galaops.PromePlugin["url"].(string) + jsonmodeURL + + _, thisfile, _, _ := runtime.Caller(0) + dirpath := filepath.Dir(thisfile) + files := make(map[string]string) + err := filepath.Walk(path.Join(dirpath, "..", "gui-json-mode"), func(jsonfilepath string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.Mode().IsRegular() { + return nil + } + + data, err := os.ReadFile(jsonfilepath) + if err != nil { + return err + } + + _, jsonfilename := filepath.Split(jsonfilepath) + files[strings.Split(jsonfilename, ".")[0]] = string(data) + + return nil + }) + if err != nil { + return "", err + } + + bs, err := httputils.Post(url, &httputils.Params{ + Body: files, + }) + if err != nil { + return "", err + } + return string(bs), nil +} diff --git a/gala-ops/server/main.go b/gala-ops/server/main.go index 96badbfc..824b3241 100644 --- a/gala-ops/server/main.go +++ b/gala-ops/server/main.go @@ -40,7 +40,6 @@ func main() { PluginClient := client.DefaultClient(PluginInfo) // 临时给server赋值 PluginClient.Server = "http://192.168.75.100:8888" - httphandler.Galaops = &httphandler.Opsclient{ Sdkmethod: PluginClient, PromePlugin: nil, @@ -53,7 +52,14 @@ func main() { os.Exit(1) } httphandler.Galaops.PromePlugin = promeplugin - + + // 向prometheus插件发送可视化插件json模板 + _, err = httphandler.Galaops.SendJsonMode("") + if err != nil { + logger.Error("failed to sendjsonmode to prometheus plugin: ", err) + } + + // 设置router httphandler.Galaops.Sdkmethod.RegisterHandlers(engine) router.InitRouter(engine) if err := engine.Run(config.Config().Http.Addr); err != nil { -- Gitee