#!/bin/bash
#Date：2017-5-20 13:14:00
#Author blog:
#       https://www.yangxingzhen.com
#Author site:
#       https://www.yangxingzhen.com/sitemap.html
#Author mirrors site:
#       https://mirrors.yangxingzhen.com
#About the Author
#       BY：、、、小柒
#       QQ：675583110
#	Mail：675583110@qq.com
#Zabbix监控Redis状态和性能

HOST='127.0.0.1'
PORT='8877'

if [[ $# == 1 ]];then
case $1 in
	version)
	redis-cli -h $HOST -p $PORT info | grep redis_version | awk -F: '{print $2}'
	;;
	uptime)
	redis-cli -h $HOST -p $PORT info | grep uptime_in_seconds | awk -F: '{print $2}'
	;;
	connected_clients)
	redis-cli -h $HOST -p $PORT info | grep connected_clients | awk -F: '{print $2}'
	;;
	blocked_clients)
	redis-cli -h $HOST -p $PORT info | grep blocked_clients | awk -F: '{print $2}'
	;;
	used_memory)
	redis-cli -h $HOST -p $PORT info | grep used_memory_human | awk NR==1 | awk -F: '{print $2}'
	;;
	used_memory_rss)
	redis-cli -h $HOST -p $PORT info | grep used_memory_rss_human | awk NR==1 | awk -F: '{print $2}'
	;;
	used_memory_peak)
	redis-cli -h $HOST -p $PORT info | grep used_memory_peak_human | awk NR==1 | awk -F: '{print $2}'
	;;
	used_memory_lua)
	redis-cli -h $HOST -p $PORT info | grep used_memory_lua_human | awk NR==1 |awk -F: '{print $2}'
	;;
	used_cpu_sys)
	redis-cli -h $HOST -p $PORT info | grep used_cpu_sys | awk NR==1 | awk -F: '{print $2}'
	;;
	used_cpu_user)
	redis-cli -h $HOST -p $PORT info | grep used_cpu_user | awk NR==1 | awk -F: '{print $2}'
	;;
	used_cpu_sys_children)
	redis-cli -h $HOST -p $PORT info | grep used_cpu_sys_children | awk -F: '{print $2}'
	;;
	used_cpu_user_children)
	redis-cli -h $HOST -p $PORT info | grep used_cpu_user_children | awk -F: '{print $2}'
	;;
	rdb_last_bgsave_status)
	redis-cli -h $HOST -p $PORT info | grep rdb_last_bgsave_status | awk -F: '{print $2}' | wc -l
	;;
	aof_last_bgrewrite_status)
	redis-cli -h $HOST -p $PORT info | grep aof_last_bgrewrite_status | awk -F: '{print $2}' | wc -l
	;;
	aof_last_write_status)
	redis-cli -h $HOST -p $PORT info | grep aof_last_write_status | awk -F: '{print $2}' | wc -l
	;;
	db0_keys)
	key=`redis-cli -h $HOST -p $PORT info | grep -w keys | awk -F '=|,' '{print $2}'` && echo ${key}|awk '{print $1}'
	;;
	db0_expires)
	expires=`redis-cli -h $HOST -p $PORT info | grep -w expires | awk -F '=|,' '{print $4}'` && echo ${expires}|awk '{print $1}'
	;;
	db0_avg_ttl)
	avg_ttl=`redis-cli -h $HOST -p $PORT info | grep avg_ttl | awk -F '=' '{print $4}'` && echo ${avg_ttl}|awk '{print $1}'
	;;
	*)
	echo -e "\033[32mUsage: $0 { uptime | 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 | db0_keys | db0_expires | db0_avg_ttl}\033[0m"
	;;
esac
else
	echo -e "\033[32mUsage: $0 { uptime | 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 | db0_keys | db0_expires | db0_avg_ttl}\033[0m"
fi
