diff --git a/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go b/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go index cc222347aa..d184af4231 100644 --- a/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go +++ b/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go @@ -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 diff --git a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/bkdbmon_install.go b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/bkdbmon_install.go index 2f3455712e..6d99b442b9 100644 --- a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/bkdbmon_install.go +++ b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/bkdbmon_install.go @@ -158,6 +158,10 @@ func (job *BkDbmonInstall) Run() (err error) { if err != nil { return } + err = job.newExporterConfig() + if err != nil { + return + } return } @@ -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 diff --git a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/redis_install.go b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/redis_install.go index 794252c454..15168c70ec 100644 --- a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/redis_install.go +++ b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomredis/redis_install.go @@ -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 } } diff --git a/dbm-services/redis/db-tools/dbactuator/pkg/common/exporter_conf.go b/dbm-services/redis/db-tools/dbactuator/pkg/common/exporter_conf.go index ed3a8ad077..55f196e012 100644 --- a/dbm-services/redis/db-tools/dbactuator/pkg/common/exporter_conf.go +++ b/dbm-services/redis/db-tools/dbactuator/pkg/common/exporter_conf.go @@ -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" ) @@ -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 +} diff --git a/dbm-services/redis/db-tools/dbactuator/pkg/consts/consts.go b/dbm-services/redis/db-tools/dbactuator/pkg/consts/consts.go index 5582cdbd69..33b9941d79 100644 --- a/dbm-services/redis/db-tools/dbactuator/pkg/consts/consts.go +++ b/dbm-services/redis/db-tools/dbactuator/pkg/consts/consts.go @@ -192,6 +192,8 @@ const ( const ( MetaRoleRedisMaster = "redis_master" MetaRoleRedisSlave = "redis_slave" + MetaRolePredixy = "predixy" + MetaRoleTwemproxy = "twemproxy" ) // proxy operations diff --git a/dbm-services/redis/db-tools/dbmon/models/myredis/myredis.go b/dbm-services/redis/db-tools/dbmon/models/myredis/myredis.go index 78f3507ca8..c400bedc6c 100644 --- a/dbm-services/redis/db-tools/dbmon/models/myredis/myredis.go +++ b/dbm-services/redis/db-tools/dbmon/models/myredis/myredis.go @@ -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 {