From 70efff1dabfeac81f589e7b311fd8d7f55a11c20 Mon Sep 17 00:00:00 2001 From: OMG-By <504094596@qq.com> Date: Fri, 15 Nov 2024 17:30:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(redis):=20redis=E4=B8=BB=E4=BB=8E=E4=B8=8B?= =?UTF-8?q?=E6=9E=B6=E8=8E=B7=E5=8F=96=E5=AF=86=E7=A0=81=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=20#7988?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dbactuator/models/myredis/myredis.go | 60 ++++++++++++++++--- .../pkg/atomjobs/atomsys/redis_capturer.go | 2 +- 2 files changed, 53 insertions(+), 9 deletions(-) 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 14e2eaeb5c..a6a003b3f9 100644 --- a/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go +++ b/dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go @@ -30,14 +30,7 @@ func GetRedisLoccalConfFile(port int) (confFile string, err error) { return } -// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码 -func GetRedisPasswdFromConfFile(port int) (password string, err error) { - confFile, err := GetRedisLoccalConfFile(port) - if err != nil { - err = fmt.Errorf("get redis local config file failed,err:%v,port:%d", err, port) - mylog.Logger.Error(err.Error()) - return - } +func GetRedisPasswd(confFile string) (password string, err error) { cmd01 := fmt.Sprintf(`grep -E '^requirepass' %s|awk '{print $2}'|head -1`, confFile) password, err = util.RunBashCmd(cmd01, "", nil, 10*time.Second) if err != nil { @@ -48,6 +41,57 @@ func GetRedisPasswdFromConfFile(port int) (password string, err error) { return } +// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码 +func GetRedisPasswdFromConfFile(port int) (password string, err error) { + // config rewrite的时候,重写的东西是写进redis.conf, 原来的实现方式如果存在instance.conf,就会获取到老的配置。 + // 默认先从redis.conf中获取密码 + dataDir := consts.GetRedisDataDir() + redisConf := filepath.Join(dataDir, "redis", strconv.Itoa(port), "redis.conf") + instConf := filepath.Join(dataDir, "redis", strconv.Itoa(port), "instance.conf") + if util.FileExists(redisConf) { + password, err = GetRedisPasswd(redisConf) + if err == nil && password != "" { + mylog.Logger.Info(fmt.Sprintf("get pwd success from redis.conf")) + return + } + } + // 尝试从instance.conf中获取密码 + if util.FileExists(instConf) { + password, err = GetRedisPasswd(instConf) + if err == nil && password != "" { + mylog.Logger.Info(fmt.Sprintf("get pwd success from instance.conf")) + return + } + } + // 文件都不存在 + if !util.FileExists(instConf) && !util.FileExists(redisConf) { + err = fmt.Errorf("[%s,%s] not exists", instConf, redisConf) + mylog.Logger.Error(err.Error()) + return + } + // 有可能存在redis实例密码确实为空的情况。。 + mylog.Logger.Info(fmt.Sprintf("get pwd success. but pwd is empty")) + return +} + +// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码 +//func GetRedisPasswdFromConfFile(port int) (password string, err error) { +// confFile, err := GetRedisLoccalConfFile(port) +// if err != nil { +// err = fmt.Errorf("get redis local config file failed,err:%v,port:%d", err, port) +// mylog.Logger.Error(err.Error()) +// return +// } +// cmd01 := fmt.Sprintf(`grep -E '^requirepass' %s|awk '{print $2}'|head -1`, confFile) +// password, err = util.RunBashCmd(cmd01, "", nil, 10*time.Second) +// if err != nil { +// return +// } +// password = strings.TrimPrefix(password, "\"") +// password = strings.TrimSuffix(password, "\"") +// return +//} + // GetTwemproxyLocalConfFile 本地获取twemproxy实例配置文件 func GetTwemproxyLocalConfFile(port int) (confFile string, err error) { psCmd := fmt.Sprintf(` diff --git a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomsys/redis_capturer.go b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomsys/redis_capturer.go index 372a279eef..b619b6177f 100644 --- a/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomsys/redis_capturer.go +++ b/dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomsys/redis_capturer.go @@ -131,7 +131,7 @@ func (job *RedisCapturer) Monitor(port int) { var err error running, err := job.IsRedisRunning(port) if err != nil || !running { - job.errChan <- fmt.Errorf("port[%d] is not running", port) + // 如果端口运行异常,那没必要去做抓包,直接返回 return }