需求:获取redis cluster集群中所有节点的内存使用情况。
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" $redis_cmd info all > info.txt used=$(cat info.txt | grep -w used_memory_rss_human | awk -F":" '{print $2}'| sed 's/ //g') max=$(cat info.txt | grep -w maxmemory_human | awk -F":" '{print $2}'|sed 's/ //g') system=$(cat info.txt | grep -w total_system_memory_human | awk -F":" '{print $2}'|sed 's/ //g') role=$(cat info.txt | grep -w role | awk -F":" '{print $2}'|sed 's/ //g') printf "%s %s %s %s %s %s " "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 29001 redis_ip=5.5.5.102 redis_port=29001 redis_role=slave used=2.95M max=1.00G system=2.82G redis_ip=5.5.5.101 redis_port=29001 redis_role=master used=2.98M max=1.00G system=2.82G redis_ip=5.5.5.103 redis_port=29002 redis_role=slave used=2.80M max=1.00G system=1.83G redis_ip=5.5.5.102 redis_port=29002 redis_role=master used=2.85M max=1.00G system=2.82G redis_ip=5.5.5.101 redis_port=29003 redis_role=slave used=2.99M max=1.00G system=2.82G redis_ip=5.5.5.103 redis_port=29003 redis_role=master used=3.02M max=1.00G system=1.83G [redis@lxd-vm1 ~]$