需要:实现类似redis-trib.rb call 命令的功能,输出redis cluster集群所有节点指定的参数的配置
redis-trib.rb的输出
[redis@lxd-vm3 ~]$ redis-trib.rb call 5.5.5.101:29001 config get *timeout* /usr/local/ruby2.5.1/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated >>> Calling CONFIG get *timeout* 5.5.5.101:29001: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.101:29004: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.102:29001: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.101:29003: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.102:29004: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.103:29003: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.102:29002: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"] 5.5.5.103:29002: ["timeout", "0", "repl-timeout", "60", "cluster-node-timeout", "15000"]
编写脚本
ip_port=`redis-cli -h $1 -p $2 -a abc123 -c cluster nodes | awk '{print $2}' | awk -F'@' '{print $1}'` for i in $ip_port do redis_ip=`echo $i | awk -F':' '{print $1}'|sed 's/ //g'` redis_port=`echo $i | awk -F':' '{print $2}'|sed 's/ //g'` redis_cmd="redis-cli -h $redis_ip -p $redis_port -a abc123 -c" echo -n "$redis_ip:$redis_port " $redis_cmd config get $3 > config cat config | awk 'NR % 2 == 0' > even cat config | awk 'NR % 2 == 1' > odd paste -d ':' odd even > tmp.txt tail_line=$(cat tmp.txt|sed -n '$p') printf '【' for x in `cat tmp.txt` do if [[ "$x" == "$tail_line" ]];then printf "%s" $x else printf '%s ' $x fi done printf '】' echo done
在后续学习awk命令中,遇到了getline命令,发现可以用一行命令替代上面红色字体的3条语句。
cat config | awk '{printf $0":";getline;print $0}' > tmp.txt
可以作为奇数行和偶数行两两配对一种方法。
说明:getline是获取当前行的下一行,即第一个$0输出的是奇数行,第二个$0输出的是偶数行,依次循环输出所有两两配对的键值对。
测试结果
[redis@lxd-vm1 ~]$ sh get_redis_para.sh 5.5.5.101 29001 *timeout* 5.5.5.101:29001 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.101:29004 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.102:29001 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.101:29003 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.102:29004 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.103:29003 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.102:29002 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】 5.5.5.103:29002 【timeout:0 repl-timeout:60 cluster-node-timeout:15000】