From d98f41a5844690aba37e82a862c740944de9b628 Mon Sep 17 00:00:00 2001 From: wubijie Date: Sat, 17 Jun 2023 16:12:17 +0800 Subject: [PATCH] Split the file --- redis/server/config.yml.templete | 11 +++++++ redis/server/config/config.go | 1 - redis/server/httphandler/agent.go | 49 +++++------------------------ redis/server/service/agent.go | 50 ++++++++++++++++++++++++++++++ redis/server/service/agent_test.go | 23 ++++++++++++++ 5 files changed, 92 insertions(+), 42 deletions(-) create mode 100644 redis/server/config.yml.templete create mode 100644 redis/server/service/agent.go create mode 100644 redis/server/service/agent_test.go diff --git a/redis/server/config.yml.templete b/redis/server/config.yml.templete new file mode 100644 index 00000000..42a65bdf --- /dev/null +++ b/redis/server/config.yml.templete @@ -0,0 +1,11 @@ +redis: + url: "http://localhost:8090/plugin/Redis" + reverseDest: "http://localhost:9121" +http: + addr: "localhost:9120" +log: + level: debug + driver: stdout #可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 + path: ./log/plugin_redis.log + max_file: 1 + max_size: 10485760 diff --git a/redis/server/config/config.go b/redis/server/config/config.go index fe0076c3..3892a76c 100644 --- a/redis/server/config/config.go +++ b/redis/server/config/config.go @@ -18,7 +18,6 @@ type HttpConf struct { Addr string `yaml:"addr"` } -// redis的密码怎么添加至配置文件? type ServerConfig struct { Redis *Redis `yaml:"redis"` Http *HttpConf `yaml:"http"` diff --git a/redis/server/httphandler/agent.go b/redis/server/httphandler/agent.go index 36ba6a45..1a0435e0 100644 --- a/redis/server/httphandler/agent.go +++ b/redis/server/httphandler/agent.go @@ -1,65 +1,32 @@ package httphandler import ( - "fmt" "net/http" + "gitee.com/openeuler/PilotGo-plugins/sdk/common" "github.com/gin-gonic/gin" - "openeuler.org/PilotGo/redis-plugin/global" - "openeuler.org/PilotGo/redis-plugin/plugin" + "openeuler.org/PilotGo/redis-plugin/service" ) // 安装运行 func InstallRedisExporter(ctx *gin.Context) { - // TODO - param := &struct { - Batch []string - }{} + // TODOs + var param *common.Batch + if err := ctx.BindJSON(param); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": -1, - "status": "redis", + "status": err, }) } - cmd := "yum install -y redis_exporter && systemctl start redis_exporter" - cmdResults, err := global.GlobalClient.RunScript(param.Batch, cmd) + ret, err := service.Install(param) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": -1, - "status": fmt.Sprintf("run redis exporter error:%s", err), + "status": err, }) } - ret := []interface{}{} - monitorTargets := []string{} - for _, result := range cmdResults { - d := struct { - MachineUUID string - MachineIP string - InstallStatus string - Error string - }{ - MachineUUID: result.MachineUUID, - InstallStatus: "ok", - Error: "", - } - - if result.Code != 0 { - d.InstallStatus = "error" - d.Error = result.Stderr - } else { - // TODO: add redis exporter to prometheus monitor target here - // default exporter port :9121 - monitorTargets = append(monitorTargets, result.MachineIP+":9121") - } - - ret = append(ret, d) - } - err = plugin.MonitorTargets(monitorTargets) - if err != nil { - fmt.Println("error: failed to add redis exporter to prometheus monitor targets") - } - ctx.JSON(http.StatusOK, gin.H{ "code": 0, "status": "ok", diff --git a/redis/server/service/agent.go b/redis/server/service/agent.go new file mode 100644 index 00000000..509865ab --- /dev/null +++ b/redis/server/service/agent.go @@ -0,0 +1,50 @@ +package service + +import ( + "gitee.com/openeuler/PilotGo-plugins/sdk/common" + + "openeuler.org/PilotGo/redis-plugin/global" + "openeuler.org/PilotGo/redis-plugin/plugin" +) + +func Install(param *common.Batch) ([]interface{}, error) { + cmd := "yum install -y redis_exporter && systemctl start redis_exporter" + + cmdResults, err := global.GlobalClient.RunScript(param, cmd) + if err != nil { + return nil, err + } + ret := []interface{}{} + monitorTargets := []string{} + for _, result := range cmdResults { + d := struct { + MachineUUID string + MachineIP string + InstallStatus string + Error string + }{ + MachineUUID: result.MachineUUID, + MachineIP: result.MachineIP, + InstallStatus: "ok", + Error: "", + } + + if result.RetCode != 0 { + d.InstallStatus = "error" + d.Error = result.Stderr + } else { + // TODO: add redis exporter to prometheus monitor target here + // default exporter port :9121 + monitorTargets = append(monitorTargets, result.MachineIP+":9121") + } + + ret = append(ret, d) + } + + err = plugin.MonitorTargets(monitorTargets) + if err != nil { + return nil, err + } + + return ret, nil +} diff --git a/redis/server/service/agent_test.go b/redis/server/service/agent_test.go new file mode 100644 index 00000000..32ec8513 --- /dev/null +++ b/redis/server/service/agent_test.go @@ -0,0 +1,23 @@ +package service + +import ( + "fmt" + "testing" + + "gitee.com/openeuler/PilotGo-plugins/sdk/common" +) + +func TestInstall(t *testing.T) { + + //var param *common.Batch + param := &common.Batch{ + BatchUUID: "001", + DepartmentIDs: []string{"2", "15"}, + MachineUUIDs: []string{"b6acfeec-375c-4856-5610-0cdgef8cdgf4 ", "336d63a9-ca45-4b44-8577-c3fa0c0a46e9"}, + } + _, err := Install(param) + if err != nil { + fmt.Println(err) + } + fmt.Println("ok") +} -- Gitee