需要:昨晚学习了cluster slots命令,之前写的脚本,有可以换种方法获取到redis cluster slots 分布情况。
cluster slots的结果如下:
4 5460 5.5.5.102 29001 eb7dfedad330c2421ed04321ca24c33ebcadb1a9 5.5.5.101 29001 6ecf0faaedafcf2e4b14c221721d3b279f2c5748 1 3 5.5.5.101 29003 7c79008aac1a2b1aa3f64e9d3747146f766386bd 5.5.5.103 29003 a3f384efcd3e38642b5fb68507fde01a5117704f 10923 16383 5.5.5.101 29003 7c79008aac1a2b1aa3f64e9d3747146f766386bd 5.5.5.103 29003 a3f384efcd3e38642b5fb68507fde01a5117704f 0 0 5.5.5.103 29002 a76debe07f1b481a4ac634d3bfaac782c0cb2024 5.5.5.102 29002 2da5169ef3cf069a5088eb4c84363e2bea34fbba 5461 10922 5.5.5.103 29002 a76debe07f1b481a4ac634d3bfaac782c0cb2024 5.5.5.102 29002 2da5169ef3cf069a5088eb4c84363e2bea34fbba
获取redis cluster slots分布的小脚本
redis-cli -h $1 -p $2 -c cluster slots | xargs -n8 | awk '{print $3":"$4" "$1"-"$2}' > tmp_slot_range awk '{a[$1]++}END{for(i in a)print i}' tmp_slot_range | sort -nk2 -t ":" > ip_port for i in `cat ip_port` do slots=($(awk '/'$i'/{print $2}' tmp_slot_range)) len=${#slots[@]} printf "$i-> " for(( j=0;j<=len-1;j++)) do printf "${slots[j]} " done echo done
脚本说明:
第1行获取到结果如下:
5.5.5.102:29001 4-5460
5.5.5.101:29003 1-3
5.5.5.101:29003 10923-16383
5.5.5.103:29002 0-0
5.5.5.103:29002 5461-10922
第2行获取的结果如下:
5.5.5.102:29001
5.5.5.103:29002
5.5.5.101:29003
第4-14行获取第2行的结果的每一行对应的slot范围
测试结果
[redis@lxd-vm1 ~]$ sh a8.sh 5.5.5.101 29001 5.5.5.102:29001-> 4-5460 5.5.5.103:29002-> 0-0 5461-10922 5.5.5.101:29003-> 1-3 10923-16383 [redis@lxd-vm1 ~]$
之前迁移了一个slot到其他节点,这里再优化一下输出的结果,将单个slot的情况,如0-0 输出为0
redis-cli -h $1 -p $2 -c cluster slots | xargs -n8 | awk '{print $3":"$4" "$1"-"$2}' > tmp_slot_range awk '{a[$1]++}END{for(i in a)print i}' tmp_slot_range | sort -nk2 -t ":" > ip_port for i in `cat ip_port` do slots=($(awk '/'$i'/{print $2}' tmp_slot_range)) len=${#slots[@]} printf "$i-> " for(( j=0;j<=len-1;j++)) do s1=$(echo ${slots[j]} | awk -F[-] '{print $1}') s2=$(echo ${slots[j]} | awk -F[-] '{print $2}') if [[ $s1 -eq $s2 ]];then printf "$s1 " else printf "${slots[j]} " fi done echo done
测试结果:
[redis@lxd-vm1 ~]$ sh a8.sh 5.5.5.101 29001 5.5.5.102:29001-> 4-5460 5.5.5.103:29002-> 0 5461-10922 5.5.5.101:29003-> 1-3 10923-16383 [redis@lxd-vm1 ~]$