需求:原生的redis-trib.rb功能是强大,但输出的内容过于繁杂,比如我需要关注哪些master对应哪些slots,不是很直观,如果集群的规模更大的话,那么输出的结果获取信息更加困难。
说明:这里给出一种方法,是在4.0.10版上测试的 ,3.X版本分隔符不一样,这里不进一步说明。
原生的输出
[redis@lxd-vm3 ~]$ redis-trib.rb check 5.5.5.101:29001 >>> Performing Cluster Check (using node 5.5.5.101:29001) M: 1adfa7f3e80c47d5bd3072e9d07c1b2bcc8ef4a1 5.5.5.101:29001 slots:2500-3000,3002-3299,3301-5461,6829-7646,12291-13108 (4596 slots) master 1 additional replica(s) S: 6178b88137d2dd621f5445f13faa9936b300333a 5.5.5.102:29004 slots: (0 slots) slave replicates 32fc101a2920c98f279a31292ace9ff3aeaa35a3 M: 32fc101a2920c98f279a31292ace9ff3aeaa35a3 5.5.5.101:29004 slots:824-1382,2200-2499,3001,5462-6828,10923-12290 (3595 slots) master 1 additional replica(s) S: c27b170791473946085c85c6fa7c163b24efe6e0 5.5.5.102:29001 slots: (0 slots) slave replicates 1adfa7f3e80c47d5bd3072e9d07c1b2bcc8ef4a1 S: cb88f415e0dc61616e1ed861a00aa8416dd6dd3f 5.5.5.101:29003 slots: (0 slots) slave replicates 31ba5414196da8b6ed8e0640472d3870d5f6f591 M: 76a8592df53de4d45fb66fa8e48ef9fb0991ae14 5.5.5.102:29002 slots:821-823,1383-2199,7647-10922 (4096 slots) master 1 additional replica(s) S: 723bfff7241ab8abfc46eeea280d673f84311056 5.5.5.103:29002 slots: (0 slots) slave replicates 76a8592df53de4d45fb66fa8e48ef9fb0991ae14 M: 31ba5414196da8b6ed8e0640472d3870d5f6f591 5.5.5.103:29003 slots:0-820,3300,13109-16383 (4097 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
编写脚本获取需要的
[redis@lxd-vm3 redis]$ cat get_slot_range2.sh redis-cli -h $1 -p $2 -c cluster nodes |grep master > ./all cat all | awk '{print $2}' | awk '$1 ~/@.*/{gsub("@.*","",$1);print $0}' > 222 cat all | awk -F " " '{for (i=9;i<=NF;i++)printf("%s ", "["$i"]");print " "}' > 999 paste -d "#" 222 999 > 2_9.txt cat 2_9.txt
执行后输出的结果:
[redis@lxd-vm3 redis]$ sh get_slot_range2.sh 5.5.5.101 29001
5.5.5.101:29004#[824-1382] [2200-2499] [3001] [5462-6828] [10923-12290]
5.5.5.102:29002#[821-823] [1383-2199] [7647-10922]
5.5.5.101:29001#[2500-3000] [3002-3299] [3301-5461] [6829-7646] [12291-13108]
5.5.5.103:29003#[0-820] [3300] [13109-16383]