# with two parameters: # 1) single redis node # 2) redis password if test $# -eq 0 -o $# -gt 2; then usage exit 1 fi
# 如果有两个参数,则第2个参数为密码 redis_password= if test $# -eq 2; then redis_password="$2" fi
eval $(echo "$1" | awk -F[\:] '{ printf("REDIS_IP=%s\nREDIS_PORT=%s\n",$1,$2) }') if test -z "$REDIS_IP" -o -z "$REDIS_PORT"; then echo "Parameter error" usage exit 1 fi
# 确保redis-cli可用 which "$REDIS_CLI" > /dev/null 2>&1 if test $? -ne 0; then echo "\`redis-cli\` not exists or not executable" exit 1 fi
if test -z "$redis_password"; then redis_nodes=`redis-cli -h $REDIS_IP -p $REDIS_PORT cluster nodes \ | awk -F[\ \:\@] '!/ERR/{ printf("%s:%s\n",$2,$3); }'` else redis_nodes=`redis-cli --no-auth-warning -a "$redis_password" -h $REDIS_IP -p $REDIS_PORT cluster nodes \ | awk -F[\ \:\@] '!/ERR/{ printf("%s:%s\n",$2,$3); }'` fi if test -z "$redis_nodes"; then # standlone #$REDIS_CLI -h $REDIS_IP -p $REDIS_PORT FLUSHALL $REDIS_CLI -h $REDIS_IP -p $REDIS_PORT INFO else # cluster for redis_node in $redis_nodes; do if test ! -z "$redis_node"; then eval $(echo "$redis_node" | awk -F[\:] '{ printf("redis_node_ip=%s\nredis_node_port=%s\n",$1,$2) }')
if test ! -z "$redis_node_ip" -a ! -z "$redis_node_port"; then if test -z "$redis_password"; then items=(`$REDIS_CLI -h $redis_node_ip -p $redis_node_port INFO MEMORY 2>&1 | tr '\r' ' '`) else items=(`$REDIS_CLI --no-auth-warning -a "$redis_password" -h $redis_node_ip -p $redis_node_port INFO MEMORY 2>&1 | tr '\r' ' '`) fi
used_memory_human=0 used_memory_rss_human=0 used_memory_peak_human=0 maxmemory_human=0 total_system_memory_human=0 for item in "${items[@]}" do eval $(echo "$item" | awk -F[\:] '{ printf("name=%s\nvalue=%s\n",$1,$2) }')
if test "$name" = "used_memory_human"; then used_memory_human=$value elif test "$name" = "used_memory_rss_human"; then used_memory_rss_human=$value elif test "$name" = "used_memory_peak_human"; then used_memory_peak_human=$value elif test "$name" = "maxmemory_human"; then maxmemory_human=$value elif test "$name" = "total_system_memory_human"; then total_system_memory_human=$value fi done
echo -e "[\033[1;33m${redis_node_ip}:${redis_node_port}\033[m]\tVIRT: \033[0;32;32m$used_memory_human\033[m\tRSS: \033[0;32;32m$used_memory_rss_human\033[m\tPeak: \033[0;32;32m$used_memory_peak_human\033[m\tMax: \033[0;32;32m$maxmemory_human\033[m\tSystem: \033[0;32;32m$total_system_memory_human\033[m" fi fi done fi