diff --git a/gala-ops/server/httphandler/metricdata.go b/gala-ops/server/httphandler/metricdata.go index a3b87ab331154c5bdcca776e4b1fa6363b473201..1def2d68bbe69efd7fa6ce8706ab071e3e1a5c99 100644 --- a/gala-ops/server/httphandler/metricdata.go +++ b/gala-ops/server/httphandler/metricdata.go @@ -2,14 +2,12 @@ package httphandler import ( "fmt" - "strings" "time" "gitee.com/openeuler/PilotGo-plugins/sdk/logger" - "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/api" - v1 "github.com/prometheus/client_golang/api/prometheus/v1" + prome "github.com/prometheus/client_golang/api/prometheus/v1" ) type Plugin struct { @@ -24,7 +22,7 @@ type Plugin struct { Status string `json:"status"` } -func PrometheusAPI(URL string) (v1.API, error) { +func PrometheusAPI(URL string) (prome.API, error) { // 创建一个HTTP客户端 client, err := api.NewClient(api.Config{ Address: "http://" + URL, @@ -35,11 +33,11 @@ func PrometheusAPI(URL string) (v1.API, error) { } // 创建一个API客户端 - v1api := v1.NewAPI(client) - return v1api, nil + promeapi := prome.NewAPI(client) + return promeapi, nil } -func PrometheusMetrics(ctx *gin.Context, client *client.Client) { +func PrometheusMetrics(ctx *gin.Context, promeclient prome.API) { // bs, err := utils.Request("GET", client.Server+"plugins") // if err != nil { // logger.Error("faild to get plugin list: ", err) @@ -55,19 +53,9 @@ func PrometheusMetrics(ctx *gin.Context, client *client.Client) { // Prometheus_addr = p.Url // } // } - plugin, err := client.GetPluginInfo("prometheus") - if err != nil { - logger.Error("failed to get plugin info from pilotgoserver: ", err) - return - } - - promAPI, err := PrometheusAPI(strings.Split(plugin.Url, "/")[2]) - if err != nil { - logger.Error("failed to create prometheus api: ", err) - } // 查询所有metrics列表 - result, warnings, err := promAPI.Query(ctx, "up", time.Now()) + result, warnings, err := promeclient.Query(ctx, "up", time.Now()) if err != nil { logger.Error("failed to query prometheus: ", err) diff --git a/gala-ops/server/main.go b/gala-ops/server/main.go index bafdc727cfa6cd4df04084b5a26f92568f6cb654..7b5096d29b29761ff3b581b37c8148542bd6b12e 100644 --- a/gala-ops/server/main.go +++ b/gala-ops/server/main.go @@ -1,12 +1,17 @@ package main import ( + "encoding/json" "fmt" + "io/ioutil" + "net/http" "os" + "strings" "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" + prome "github.com/prometheus/client_golang/api/prometheus/v1" "openeuler.org/PilotGo/gala-ops-plugin/config" "openeuler.org/PilotGo/gala-ops-plugin/database" "openeuler.org/PilotGo/gala-ops-plugin/httphandler" @@ -14,6 +19,8 @@ import ( const Version = "0.0.1" +var PromeClient prome.API + var PluginInfo = &client.PluginInfo{ Name: "gala-ops", Version: Version, @@ -29,20 +36,40 @@ func main() { if err := database.MysqlInit(config.Config().Mysql); err != nil { fmt.Println("failed to initialize database") - os.Exit(-1) + os.Exit(1) } InitLogger() - server := gin.Default() + router := gin.Default() GlobalClient := client.DefaultClient(PluginInfo) // 临时给server赋值 GlobalClient.Server = "http://192.168.75.100:8888" - GlobalClient.RegisterHandlers(server) - InitRouter(server) + GlobalClient.RegisterHandlers(router) + InitRouter(router) + + // 临时自定义获取prometheus地址方式 + PromePlugin, err := getpromeplugininfo(GlobalClient.Server) + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + + // PromePlugin, err := client.GetClient().GetPluginInfo("prometheus") + // if err != nil { + // logger.Error("failed to get plugin info from pilotgoserver: ", err) + // os.Exit(1) + // } - if err := server.Run(config.Config().Http.Addr); err != nil { + promeclient, err := httphandler.PrometheusAPI(strings.Split(PromePlugin["url"].(string), "/")[2]) + if err != nil { + logger.Error("failed to create prometheus api: ", err) + os.Exit(1) + } + PromeClient = promeclient + + if err := router.Run(config.Config().Http.Addr); err != nil { logger.Fatal("failed to run server") } } @@ -50,7 +77,7 @@ func main() { func InitLogger() { if err := logger.Init(config.Config().Logopts); err != nil { fmt.Printf("logger init failed, please check the config file: %s", err) - os.Exit(-1) + os.Exit(1) } } @@ -64,5 +91,38 @@ func InitRouter(router *gin.Engine) { api.PUT("/install_gopher", httphandler.InstallGopher) api.PUT("/upgrade_gopher", httphandler.UpgradeGopher) api.DELETE("/uninstall_gopher", httphandler.UninstallGopher) + + // 从prometheus中获取监控数据 + api.GET("/metrics", func(ctx *gin.Context) { + httphandler.PrometheusMetrics(ctx, PromeClient) + }) + } +} + +func getpromeplugininfo(pilotgoserver string) (map[string]interface{}, error) { + resp, err := http.Get(pilotgoserver + "/api/v1/plugins") + if err != nil { + logger.Error("faild to get plugin list: ", err) + } + bs, _ := ioutil.ReadAll(resp.Body) + resp.Body.Close() + data := map[string]interface{}{ + "code": nil, + "data": nil, + "msg": nil, + } + err = json.Unmarshal(bs, &data) + if err != nil { + logger.Error("unmarshal request plugin info error:%s", err.Error()) + } + var PromePlugin map[string]interface{} + for _, p := range data["data"].([]interface{}) { + if p.(map[string]interface{})["name"] == "prometheus" { + PromePlugin = p.(map[string]interface{}) + } + } + if len(PromePlugin) == 0 { + return nil, fmt.Errorf("pilotgo server not add prometheus plugin") } + return PromePlugin, nil }