需求:模拟redis-trib.rb info ip:port输出的结果
如:
[redis@lxd-vm3 ~]$ redis-trib.rb info 5.5.5.101:29001 /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 5.5.5.103:29002 (a76debe0...) -> 1514 keys | 5463 slots | 1 slaves. 5.5.5.102:29001 (eb7dfeda...) -> 8196781 keys | 5457 slots | 1 slaves. 5.5.5.101:29003 (7c79008a...) -> 4497 keys | 5464 slots | 1 slaves. [OK] 8202792 keys in 3 masters. 500.66 keys per slot on average.
通过shell脚本实现类似的:
redis-cli -h $1 -p $2 -c cluster slots | xargs -n8 | awk '{print $3":"$4" "$2-$1+1}' > tmp_slot_num awk '{a[$1]++}END{for(i in a)print i}' tmp_slot_num | sort -nk2 -t ":" > ip_port total_keys=0 master_num=0 for i in `cat ip_port` do let master_num=master_num+1 instance_slots=0 instance_keys=0 redis_ip=$(echo $i | awk -F[:] '{print $1}') redis_port=$(echo $i | awk -F[:] '{print $2}') instance_keys=$(redis-cli -c -h $redis_ip -p $redis_port dbsize) let total_keys=total_keys+instance_keys slots=($(awk '/'$i'/{print $2}' tmp_slot_num)) len=${#slots[@]} for(( j=0;j<=len-1;j++)) do let instance_slots=instance_slots+${slots[j]} done echo "$redis_ip:$redis_port->$instance_keys keys->$instance_slots slots" done echo "[ok] $total_keys keys in $master_num masters." avg=$(awk 'BEGIN{printf "%.2f ",'$total_keys'/16384}') echo "$avg keys per slot on average."
测试结果
[redis@lxd-vm3 ~]$ sh a10.sh 5.5.5.101 29001 5.5.5.102:29001->8196781 keys->5457 slots 5.5.5.103:29002->1514 keys->5463 slots 5.5.5.101:29003->4497 keys->5464 slots [ok] 8202792 keys in 3 masters. 500.66 keys per slot on average. [redis@lxd-vm3 ~]$