需求:获取redis cluster集群中所有节点的内存使用情况。

ip_port=`redis-cli -h $ -p $ -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/\r//g'`
  redis_port=`echo $i | awk -F':' '{print $2}'|sed 's/\r//g'`
  redis_cmd="redis-cli -h $redis_ip -p $redis_port -a abc123 -c"
  $redis_cmd info all > info.txt
  used=$(cat info.txt | grep -w used_memory_rss_human | awk -F":" '{print $2}'| sed 's/\r//g')
  max=$(cat info.txt | grep -w maxmemory_human | awk -F":" '{print $2}'|sed 's/\r//g')
  system=$(cat info.txt | grep -w total_system_memory_human | awk -F":" '{print $2}'|sed 's/\r//g')
  role=$(cat info.txt | grep -w role | awk -F":" '{print $2}'|sed 's/\r//g')
  printf "%s\t%s\t%s\t%s\t%s\t%s\n" "redis_ip="$redis_ip "redis_port="$redis_port "redis_role="$role "used="$used "max="$max "system="$system
  rm -rf info.txt
done

测试:

[redis@lxd-vm1 ~]$ sh get_redis_memory.sh 5.5.5.101
redis_ip=5.5.5.102   redis_port=   redis_role=slave    used=2.95M   max=.00G   system=.82G
redis_ip=5.5.5.101   redis_port=   redis_role=master   used=2.98M   max=.00G   system=.82G
redis_ip=5.5.5.103   redis_port=   redis_role=slave    used=2.80M   max=.00G   system=.83G
redis_ip=5.5.5.102   redis_port=   redis_role=master   used=2.85M   max=.00G   system=.82G
redis_ip=5.5.5.101   redis_port=   redis_role=slave    used=2.99M   max=.00G   system=.82G
redis_ip=5.5.5.103   redis_port=   redis_role=master   used=3.02M   max=.00G   system=.83G
[redis@lxd-vm1 ~]$
05-13 18:36