Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8139361
  • 博文数量: 594
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10324
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(594)

分类: 系统运维

2018-09-02 21:54:31

工具的作用:

1)比“cluster nodes”更为直观的显示结果

2)指出落在同一个IP上的master

3)指出落在同一个IP上的master和slave对


运行效果图:



源代码(可从下载):

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # 源码位置:
  3. #
  4. # Redis集群部署注意事项:
  5. # 在一个物理IP上部署多个redis实例时,要避免:
  6. # 1) 一对master和slave出现在同一物理IP上(影响:物理机挂掉,部分keys彻底不可用)
  7. # 2) 同一物理IP上出现多个master(影响:物理机挂掉,将导致两对切换)
  8. #
  9. # 使用示例(带一个参数):
  10. # show_redis_map.sh 192.168.0.21:6380
  11. #
  12. # 检查Redis集群master和slave映射关系的命令行工具:
  13. # 1) 查看是否多master出现在同一IP;
  14. # 2) 查看一对master和slave出现在同一IP。
  15. #
  16. # 当同一IP出现二个或多个master,则相应的行标星显示,
  17. # 如果一对master和slave出现在同一IP上,则相应的行标星显示。
  18. #
  19. # 输出效果:
  20. # [01][MASTER] 192.168.0.21:6379 00cc3f37d938ee8ba672bc77b71d8e0a3881a98b
  21. # [02][MASTER] 192.168.0.22:6379 1115713e3c311166207f3a9f1445b4e32a9202d7
  22. # [03][MASTER] 192.168.0.23:6379 5cb6946f46ccdf543e5a1efada6806f3df72b727
  23. # [04][MASTER] 192.168.0.24:6379 b91b1309b05f0dcc1e3a2a9521b8c00702999744
  24. # [05][MASTER] 192.168.0.25:6379 00a1ba8e5cb940ba4171e0f4415b91cea96977bc
  25. # [06][MASTER] 192.168.0.26:6379 64facb201cc5c7d8cdccb5fa211af5e1a04a9786
  26. # [07][MASTER] 192.168.0.27:6379 f119780359c0e43d19592db01675df2f776181b1
  27. # [08][MASTER] 192.168.0.28:6379 d374e28578967f96dcb75041e30a5a1e23693e56
  28. # [09][MASTER] 192.168.0.29:6380 a153d2071251657004dbe77abd10e2de7f0a209a
  29. #
  30. # [01][SLAVE=>MASTER] 192.168.0.21:6380 => 192.168.0.28:6379
  31. # [02][SLAVE=>MASTER] 192.168.0.22:6380 => 192.168.0.25:6379
  32. # [03][SLAVE=>MASTER] 192.168.0.23:6380 => 192.168.0.24:6379
  33. # [04][SLAVE=>MASTER] 192.168.0.24:6380 => 192.168.0.23:6379
  34. # [05][SLAVE=>MASTER] 192.168.0.25:6380 => 192.168.0.22:6379
  35. # [06][SLAVE=>MASTER] 192.168.0.26:6380 => 192.168.0.27:6379
  36. # [07][SLAVE=>MASTER] 192.168.0.27:6380 => 192.168.0.29:6380
  37. # [08][SLAVE=>MASTER] 192.168.0.28:6380 => 192.168.0.21:6379
  38. # [09][SLAVE=>MASTER] 192.168.0.29:6379 => 192.168.0.26:6379
  39. REDIS_CLI=${REDIS_CLI:-redis-cli}
  40. REDIS_IP=${REDIS_IP:-127.0.0.1}
  41. REDIS_PORT=${REDIS_PORT:-6379}
  42. function usage()
  43. {
  44. echo "usage: `basename $0` redis_node"
  45. echo "example: `basename $0` 127.0.0.1:6379"
  46. }
  47. # with a parameter: single redis node
  48. if test $# -ne 1; then
  49. usage
  50. exit 1
  51. fi
  52. # 检查参数
  53. eval $(echo "$1" | awk -F[\:] '{ printf("REDIS_IP=%s\nREDIS_PORT=%s\n",$1,$2) }')
  54. if test -z "$REDIS_IP" -o -z "$REDIS_PORT"; then
  55. echo "parameter error"
  56. usage
  57. exit 1
  58. fi
  59. # 确保redis-cli可用
  60. which "$REDIS_CLI" > /dev/null 2>&1
  61. if test $? -ne 0; then
  62. echo -e "\`redis-cli\` not exists or not executable"
  63. exit 1
  64. fi
  65. # master映射表,key为master的id,value为master的“ip:port”
  66. declare -A master_map=()
  67. # slave映表,key为master的id,value为slave的“ip:port”
  68. declare -A slave_map=()
  69. master_nodes_str=
  70. master_slave_maps_str=
  71. # 找出所有master
  72. masters=`$REDIS_CLI -h $REDIS_IP -p $REDIS_PORT CLUSTER NODES | awk -F[\ \@] '/master/{ printf("%s,%s\n",$1,$2); }' | sort`
  73. for master in $masters;
  74. do
  75. eval $(echo $master | awk -F[,] '{ printf("master_id=%s\nmaster_node=%s\n",$1,$2); }')
  76. master_map[$master_id]=$master_node
  77. if test -z "$master_nodes_str"; then
  78. master_nodes_str="$master_node|$master_id"
  79. else
  80. master_nodes_str="$master_node|$master_id,$master_nodes_str"
  81. fi
  82. done
  83. # 找出所有slave
  84. # “CLUSTER NODES”命令的输出格式当前有两个版本,需要awk需要根据NF的值做区分
  85. slaves=`$REDIS_CLI -h $REDIS_IP -p $REDIS_PORT CLUSTER NODES | awk -F[\ \@] '/slave/{ if (NF==9) printf("%s,%s\n",$5,$2); else printf("%s,%s\n",$4,$2); }' | sort`
  86. for slave in $slaves;
  87. do
  88. eval $(echo $slave | awk -F[,] '{ printf("master_id=%s\nslave_node=%s\n",$1,$2); }')
  89. slave_map[$master_id]=$slave_node
  90. done
  91. for key in ${!master_map[@]}
  92. do
  93. master_node=${master_map[$key]}
  94. slave_node=${slave_map[$key]}
  95. if test -z "$master_slave_maps_str"; then
  96. master_slave_maps_str="$slave_node|$master_node"
  97. else
  98. master_slave_maps_str="$slave_node|$master_node,$master_slave_maps_str"
  99. fi
  100. done
  101. # 显示所有master
  102. index=1
  103. old_master_node_ip=
  104. master_nodes_str=`echo "$master_nodes_str" | tr ',' '\n' | sort`
  105. for master_node_str in $master_nodes_str;
  106. do
  107. eval $(echo "$master_node_str" | awk -F[\|] '{ printf("master_node=%s\nmaster_id=%s\n", $1, $2); }')
  108. eval $(echo "$master_node" | awk -F[\:] '{ printf("master_node_ip=%s\nmaster_node_port=%s\n", $1, $2); }')
  109. tag=
  110. # 同一IP上出现多个master,标星
  111. if test "$master_node_ip" = "$old_master_node_ip"; then
  112. tag=" (*)"
  113. fi
  114. printf "[%02d][MASTER] %-20s \033[0;32;31m%s\033[m%s\n" $index "$master_node" "$master_id" "$tag"
  115. old_master_node_ip=$master_node_ip
  116. index=$((++index))
  117. done
  118. # 显示所有slave到master的映射
  119. index=1
  120. echo ""
  121. master_slave_maps_str=`echo "$master_slave_maps_str" | tr ',' '\n' | sort`
  122. for master_slave_map_str in $master_slave_maps_str;
  123. do
  124. eval $(echo "$master_slave_map_str" | awk -F[\|] '{ printf("slave_node=%s\nmaster_node=%s\n", $1, $2); }')
  125. eval $(echo "$slave_node" | awk -F[\:] '{ printf("slave_node_ip=%s\nslave_node_port=%s\n", $1, $2); }')
  126. eval $(echo "$master_node" | awk -F[\:] '{ printf("master_node_ip=%s\nmaster_node_port=%s\n", $1, $2); }')
  127. tag=
  128. # 一对master和slave出现在同一IP,标星
  129. if test ! -z "$slave_node_ip" -a "$slave_node_ip" = "$master_node_ip"; then
  130. tag=" (*)"
  131. fi
  132. n=$(($index % 2))
  133. if test $n -eq 0; then
  134. printf "[%02d][SLAVE=>MASTER] \033[1;33m%21s\033[m => \033[1;33m%s\033[m%s\n" $index $slave_node $master_node "$tag"
  135. else
  136. printf "[%02d][SLAVE=>MASTER] %21s => %s%s\n" $index $slave_node $master_node "$tag"
  137. fi
  138. index=$((++index))
  139. done
  140. echo ""


阅读(2688) | 评论(0) | 转发(0) |
0

上一篇:LVS初步

下一篇:Redis集群命令行部署工具

给主人留下些什么吧!~~