1.需求:用shell写一个脚本来监控redis-cluster测试环境的基本指标。
2.现实:用python已实现,但是python代码过多不够简洁,所有改用shell来做监控脚本的模版。
3.展示:garafana/zabbix
4.脚本如下
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
HOST=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
PORT=7000
PASS="redis-cluster-gmjk"
if [[ $# == 1 ]];then
case $1 in
cluster_state)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_state" | awk -F':' '{print $2}'| grep -c ok`
echo $result
;;
#检测集群的节点是否失去连接,有可能关闭也有可能因为其他网络问题无法连接。
cluster_node_isclose)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | awk -F' ' '{print $8}'|grep "disconnected"|wc -l`
echo $result
;;
cluster_slots_assigned)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_slots_assigned" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_ok)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_slots_ok" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_pfail)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_slots_pfail" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_fail)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_slots_fail" | awk -F':' '{print $2}'`
echo $result
;;
cluster_known_nodes)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_known_nodes" | awk -F':' '{print $2}'`
echo $result
;;
cluster_size)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_size" | awk -F':' '{print $2}'`
echo $result
;;
cluster_current_epoch)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_current_epoch" | awk -F':' '{print $2}'`
echo $result
;;
cluster_my_epoch)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_my_epoch" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_ping_sent)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_ping_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_pong_sent)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_pong_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_sent)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_ping_received)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_ping_received" | awk -F':'` '{print $2}'
echo $result
;;
cluster_stats_messages_pong_received)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_pong_received" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_received)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT -c cluster info | grep -w "cluster_stats_messages_received" | awk -F':' '{print $2}'`
echo $result
;;
uptime)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info server | grep -w "uptime_in_seconds" | awk -F':' '{print $2}'`
echo $result
;;
connected_clients)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "connected_clients" | awk -F':' '{print $2}'`
echo $result
;;
blocked_clients)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info clients | grep -w "blocked_clients" | awk -F':' '{print $2}'`
echo $result
;;
used_memory)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info memory | grep -w "used_memory" | awk -F':' '{print $2}'`
echo $result
;;
used_cpu_user)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info cpu | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
echo $result
;;
rdb_last_bgsave_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_bgrewrite_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
aof_last_write_status)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info Persistence | grep -w "aof_last_write_status" | awk -F':' '{print $2}' | grep -c ok`
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received|connected_clients|blocked_clients|used_memory|used_memory_rss|used_memory_peak|used_memory_lua|used_cpu_sys|used_cpu_user|used_cpu_sys_children|used_cpu_user_children|rdb_last_bgsave_status|aof_last_bgrewrite_status|aof_last_write_status}\033[0m"
;;
esac
elif [[ $# == 2 ]];then
case $2 in
keys)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
echo $result
;;
expires)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
echo $result
;;
avg_ttl)
result=`$REDISCLI -h $HOST -a $PASS -p $PORT info | grep -w "$1" | grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {db0 keys|db0 expires|db0 avg_ttl}\033[0m"
;;
esac
fi