Skip to content

Commit

Permalink
fix(redis): actuator安装bk-dbmon时更新exporter配置文件 #1648
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemakeit committed Nov 6, 2023
1 parent 0d495c1 commit 99f4695
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 15 deletions.
19 changes: 19 additions & 0 deletions dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ func GetPasswordFromLocalConfFile(port int) (password string, err error) {
return
}

// GetProxyPasswdFromConfFlie (从配置文件中)获取本地proxy实例密码
func GetProxyPasswdFromConfFlie(port int, role string) (password string, err error) {
var grepCmd string
if role == consts.MetaRoleTwemproxy {
grepCmd = fmt.Sprintf(`grep -w "password" %s/twemproxy*/%d/nutcracker.%d.yml|grep -vE "#"|awk '{print $2}'`,
consts.DataPath, port, port)
} else if role == consts.MetaRolePredixy {
grepCmd = fmt.Sprintf(`grep -iw "auth" %s/predixy/%d/predixy.conf|awk '{print $2}'`,
consts.Data1Path, port)
}
password, err = util.RunBashCmd(grepCmd, "", nil, 10*time.Second)
if err != nil {
return
}
password = strings.TrimPrefix(password, "\"")
password = strings.TrimSuffix(password, "\"")
return
}

type connTestItem struct {
IP string
Port int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (job *BkDbmonInstall) Run() (err error) {
if err != nil {
return
}
err = job.newExporterConfig()
if err != nil {
return
}

return
}
Expand Down Expand Up @@ -398,6 +402,25 @@ func (job *BkDbmonInstall) GenerateConfigFile() (err error) {
return
}

func (job *BkDbmonInstall) newExporterConfig() (err error) {
job.runtime.Logger.Info("begin to new exporter config file")
err = util.MkDirsIfNotExists([]string{consts.ExporterConfDir})
if err != nil {
job.runtime.Logger.Error("newExporterConfig mkdirIfNotExists %s failed,err:%v", consts.ExporterConfDir, err)
return err
}
for _, server := range job.params.Servers {
for _, port := range server.ServerPorts {
err = common.CreateLocalExporterConfigFile(server.ServerIP, port, server.MetaRole, "")
if err != nil {
return
}
}
}
util.LocalDirChownMysql(consts.ExporterConfDir)
return nil
}

// Retry times
func (job *BkDbmonInstall) Retry() uint {
return 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,25 +434,14 @@ func (job *RedisInstall) GenerateConfigFile(port int) error {
}
func (job *RedisInstall) newExporterConfig() (err error) {
job.runtime.Logger.Info("begin to new exporter config file")
var addr map[string]string
var key, val string
var fileData []byte
var confFile string
err = util.MkDirsIfNotExists([]string{consts.ExporterConfDir})
if err != nil {
job.runtime.Logger.Error("newExporterConfig mkdirIfNotExists %s failed,err:%v", consts.ExporterConfDir, err)
return err
}
for _, port := range job.params.Ports {
addr = map[string]string{}
confFile = filepath.Join(consts.ExporterConfDir, fmt.Sprintf("%d.conf", port))
key = fmt.Sprintf("redis://%s:%d", job.params.IP, port)
val = job.params.Password
addr[key] = val
fileData, _ = json.Marshal(addr)
err = ioutil.WriteFile(confFile, fileData, 0755)
err = common.CreateLocalExporterConfigFile(job.params.IP, port, consts.MetaRoleRedisMaster, job.params.Password)
if err != nil {
job.runtime.Logger.Error("newExporterConfig writeFile %s fail,err:%v", confFile, err)
return err
}
}
Expand Down
45 changes: 45 additions & 0 deletions dbm-services/redis/db-tools/dbactuator/pkg/common/exporter_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"

"dbm-services/redis/db-tools/dbactuator/models/myredis"
"dbm-services/redis/db-tools/dbactuator/mylog"
"dbm-services/redis/db-tools/dbactuator/pkg/consts"
"dbm-services/redis/db-tools/dbactuator/pkg/util"
)
Expand Down Expand Up @@ -45,3 +47,46 @@ func DeleteExporterConfigFile(port int) (err error) {
confFile = getConfFileName(port)
return os.Remove(confFile)
}

// CreateLocalExporterConfigFile 创建本地Exporter配置文件.
func CreateLocalExporterConfigFile(ip string, port int, metaRole, password string) (err error) {
addr := map[string]string{}
var key, val string
if password == "" {
// 从本地配置文件获取密码
if metaRole == consts.MetaRoleRedisMaster ||
metaRole == consts.MetaRoleRedisSlave {
password, err = myredis.GetPasswordFromLocalConfFile(port)
} else if metaRole == consts.MetaRolePredixy ||
metaRole == consts.MetaRoleTwemproxy {
password, err = myredis.GetProxyPasswdFromConfFlie(port, metaRole)
}
if err != nil {
mylog.Logger.Error("get password from local conf file failed,err:%v,ip:%s,port:%d,metaRole:%s",
err, ip, port, metaRole)
return err
}
}
if metaRole == consts.MetaRoleRedisMaster ||
metaRole == consts.MetaRoleRedisSlave {
key = fmt.Sprintf("redis://%s:%d", ip, port)
val = password
addr[key] = val
} else if metaRole == consts.MetaRolePredixy {
key = fmt.Sprintf("%s:%d", ip, port)
val = password
addr[key] = val
} else if metaRole == consts.MetaRoleTwemproxy {
key = fmt.Sprintf("%s:%d", ip, port)
val = password
addr[key] = val
key = fmt.Sprintf("%s:%d:stat", ip, port)
val = fmt.Sprintf("%s:%d", ip, port+1000)
addr[key] = val
}
err = WriteExporterConfigFile(port, addr)
if err != nil {
mylog.Logger.Error("WriteExporterConfigFile %d failed,err:%v", port, err)
}
return nil
}
2 changes: 2 additions & 0 deletions dbm-services/redis/db-tools/dbactuator/pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ const (
const (
MetaRoleRedisMaster = "redis_master"
MetaRoleRedisSlave = "redis_slave"
MetaRolePredixy = "predixy"
MetaRoleTwemproxy = "twemproxy"
)

// proxy operations
Expand Down
6 changes: 3 additions & 3 deletions dbm-services/redis/db-tools/dbmon/models/myredis/myredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func GetRedisPasswdFromConfFile(port int) (password string, err error) {

// GetProxyPasswdFromConfFlie (从配置文件中)获取本地proxy实例密码
func GetProxyPasswdFromConfFlie(port int, role string) (password string, err error) {
dataDir := consts.GetRedisDataDir()
var grepCmd string
if role == consts.MetaRoleTwemproxy {
grepCmd = fmt.Sprintf(`grep -w "password" %s/twemproxy*/%d/nutcracker.%d.yml|grep -vE "#"|awk '{print $2}'`,
dataDir, port, port)
consts.DataPath, port, port)
} else if role == consts.MetaRolePredixy {
grepCmd = fmt.Sprintf(`grep -iw "auth" %s/predixy/%d/predixy.conf|awk '{print $2}'`, dataDir, port)
grepCmd = fmt.Sprintf(`grep -iw "auth" %s/predixy/%d/predixy.conf|awk '{print $2}'`,
consts.Data1Path, port)
}
password, err = util.RunBashCmd(grepCmd, "", nil, 10*time.Second)
if err != nil {
Expand Down

0 comments on commit 99f4695

Please sign in to comment.