diff --git a/redis/server/config.yml.templete b/redis/server/config.yml.templete new file mode 100644 index 0000000000000000000000000000000000000000..42a65bdf96863166a46b666d8ab4578f06a266b1 --- /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 fe0076c38a1a010d8e75d7307da2d1954027dfb5..3892a76cc5b6e55a799fa76d2b695492abcc3ec8 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 36ba6a451b51b58e55ea33d5b8347802f34493d7..1a0435e099c6874164eba48f1e8ead43e01ccdec 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 0000000000000000000000000000000000000000..509865abab7cd87b1d864a13db80634a3b7e50c1 --- /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 0000000000000000000000000000000000000000..32ec85131f378d37817ca7f6dafc1217c1eb90a0 --- /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") +}