zabbix监控redis性能

创建采集脚本

mkdir -p /etc/zabbix/scripts

chown -R zabbix.root /etc/zabbix/scripts

vim redis_status.sh 

#!/bin/bash
REDISPATH="/usr/local/redis/bin/redis-cli"
HOST="127.0.0.1"
PORT="6379"
REDIS_INFO="$REDISPATH -h $HOST -p $PORT -a xxx info"   ##-a 后面跟的是redis密码
if [[ $# == 1 ]];then
case $1 in
cluster)
result=`$REDIS_INFO|/bin/grep cluster|awk -F":" '{print $NF}'`
echo $result 
;; 
uptime_in_seconds)
result=`$REDIS_INFO|/bin/grep uptime_in_seconds|awk -F":" '{print $NF}'`
echo $result 
;; 
connected_clients)
result=`$REDIS_INFO|/bin/grep connected_clients|awk -F":" '{print $NF}'`
echo $result 
;; 
client_longest_output_list)
result=`$REDIS_INFO|/bin/grep client_longest_output_list|awk -F":" '{print $NF}'`
echo $result 
;; 
client_biggest_input_buf)
result=`$REDIS_INFO|/bin/grep client_biggest_input_buf|awk -F":" '{print $NF}'`
echo $result 
;; 
blocked_clients)
result=`$REDIS_INFO|/bin/grep blocked_clients|awk -F":" '{print $NF}'`
echo $result 
;; 
#内存
used_memory)
result=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
echo $result 
;; 
used_memory_human)
result=`$REDIS_INFO|/bin/grep used_memory_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'` 
echo $result 
;; 
used_memory_rss)
result=`$REDIS_INFO|/bin/grep used_memory_rss|awk -F":" '{print $NF}'`
echo $result 
;; 
used_memory_peak)
result=`$REDIS_INFO|/bin/grep used_memory_peak|awk -F":" '{print $NF}'|awk 'NR==1'`
echo $result 
;; 
used_memory_peak_human)
result=`$REDIS_INFO|/bin/grep used_memory_peak_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'`
echo $result 
;; 
used_memory_lua)
result=`$REDIS_INFO|/bin/grep used_memory_lua|awk -F":" '{print $NF}'`
echo $result 
;; 
mem_fragmentation_ratio)
result=`$REDIS_INFO|/bin/grep mem_fragmentation_ratio|awk -F":" '{print $NF}'`
echo $result 
;; 
#rdb
rdb_changes_since_last_save)
result=`$REDIS_INFO|/bin/grep rdb_changes_since_last_save|awk -F":" '{print $NF}'`
echo $result 
;; 
rdb_bgsave_in_progress)
result=`$REDIS_INFO|/bin/grep rdb_bgsave_in_progress|awk -F":" '{print $NF}'`
echo $result 
;; 
rdb_last_save_time)
result=`$REDIS_INFO|/bin/grep rdb_last_save_time|awk -F":" '{print $NF}'`
echo $result 
;; 
rdb_last_bgsave_status)
result=`$REDIS_INFO|/bin/grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
echo $result 
;; 
rdb_current_bgsave_time_sec)
result=`$REDIS_INFO|/bin/grep -w "rdb_current_bgsave_time_sec" | awk -F':' '{print $2}'`
echo $result 
;; 
#rdbinfo
aof_enabled)
result=`$REDIS_INFO|/bin/grep -w "aof_enabled" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_rewrite_scheduled)
result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_scheduled" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_last_rewrite_time_sec)
result=`$REDIS_INFO|/bin/grep -w "aof_last_rewrite_time_sec" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_current_rewrite_time_sec)
result=`$REDIS_INFO|/bin/grep -w "aof_current_rewrite_time_sec" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_last_bgrewrite_status)
result=`$REDIS_INFO|/bin/grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
echo $result 
;; 
#aofinfo
aof_current_size)
result=`$REDIS_INFO|/bin/grep -w "aof_current_size" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_base_size)
result=`$REDIS_INFO|/bin/grep -w "aof_base_size" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_pending_rewrite)
result=`$REDIS_INFO|/bin/grep -w "aof_pending_rewrite" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_buffer_length)
result=`$REDIS_INFO|/bin/grep -w "aof_buffer_length" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_rewrite_buffer_length)
result=`$REDIS_INFO|/bin/grep -w "aof_rewrite_buffer_length" | awk -F':' '{print $2}'`
echo $result 
;; 
aof_pending_bio_fsync)
result=`$REDIS_INFO|/bin/grep -w "aof_pending_bio_fsync" | awk -F':' '{print $2}'`
echo $result 
;;
aof_delayed_fsync)
result=`$REDIS_INFO|/bin/grep -w "aof_delayed_fsync" | awk -F':' '{print $2}'`
echo $result 
;; 
#stats
total_connections_received)
result=`$REDIS_INFO|/bin/grep -w "total_connections_received" | awk -F':' '{print $2}'`
echo $result 
;; 
total_commands_processed)
result=`$REDIS_INFO|/bin/grep -w "total_commands_processed" | awk -F':' '{print $2}'`
echo $result 
;; 
instantaneous_ops_per_sec)
result=`$REDIS_INFO|/bin/grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
echo $result 
;; 
rejected_connections)
result=`$REDIS_INFO|/bin/grep -w "rejected_connections" | awk -F':' '{print $2}'` 
echo $result 
;; 
expired_keys)
result=`$REDIS_INFO|/bin/grep -w "expired_keys" | awk -F':' '{print $2}'`
echo $result 
;; 
evicted_keys)
result=`$REDIS_INFO|/bin/grep -w "evicted_keys" | awk -F':' '{print $2}'` 
echo $result 
;; 
keyspace_hits)
result=`$REDIS_INFO|/bin/grep -w "keyspace_hits" | awk -F':' '{print $2}'` 
echo $result 
;; 
keyspace_misses)
result=`$REDIS_INFO|/bin/grep -w "keyspace_misses" | awk -F':' '{print $2}'`
echo $result 
;;
pubsub_channels)
result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
echo $result 
;;
pubsub_channels)
result=`$REDIS_INFO|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
echo $result 
;;
pubsub_patterns)
result=`$REDIS_INFO|/bin/grep -w "pubsub_patterns" | awk -F':' '{print $2}'`
echo $result 
;;
latest_fork_usec)
result=`$REDIS_INFO|/bin/grep -w "latest_fork_usec" | awk -F':' '{print $2}'`
echo $result 
;; 
connected_slaves)
result=`$REDIS_INFO|/bin/grep -w "connected_slaves" | awk -F':' '{print $2}'`
echo $result 
;;
master_link_status)
result=`$REDIS_INFO|/bin/grep -w "master_link_status"|awk -F':' '{print $2}'|/bin/grep -c up`
echo $result 
;;
master_last_io_seconds_ago)
result=`$REDIS_INFO|/bin/grep -w "master_last_io_seconds_ago"|awk -F':' '{print $2}'`
echo $result 
;;
master_sync_in_progress)
result=`$REDIS_INFO|/bin/grep -w "master_sync_in_progress"|awk -F':' '{print $2}'`
echo $result 
;;
slave_priority)
result=`$REDIS_INFO|/bin/grep -w "slave_priority"|awk -F':' '{print $2}'`
echo $result 
;;
#cpu
used_cpu_sys)
result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys"|awk -F':' '{print $2}'`
echo $result 
;;
used_cpu_user)
result=`$REDIS_INFO|/bin/grep -w "used_cpu_user"|awk -F':' '{print $2}'`
echo $result 
;;
used_cpu_sys_children)
result=`$REDIS_INFO|/bin/grep -w "used_cpu_sys_children"|awk -F':' '{print $2}'`
echo $result 
;;
used_cpu_user_children)
result=`$REDIS_INFO|/bin/grep -w "used_cpu_user_children"|awk -F':' '{print $2}'`
echo $result 
;;
*)
echo "argu error"
;;
esac
#db0:key
elif [[ $# == 2 ]];then
case $2 in
keys)
result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "keys" | awk -F'=|,' '{print $2}'`
echo $result 
;;
expires)
result=`$REDIS_INFO| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "expires" | awk -F'=|,' '{print $4}'`
echo $result 
;;
avg_ttl)
result=`$REDIS_INFO|/bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
echo $result 
;;
*)
echo "argu error" ;;
esac
fi

通过redis命令info查看出来的信息:

  1. server : Redis 服务器信息,包含以下域:
  2. redis_version : Redis 服务器版本
  3. redis_git_sha1 : Git SHA1
  4. redis_git_dirty : Git dirty flag
  5. os : Redis 服务器的宿主操作系统
  6. arch_bits : 架构(32 64 位)
  7. multiplexing_api : Redis 所使用的事件处理机制
  8. gcc_version : 编译 Redis 时所使用的 GCC 版本
  9. process_id : 服务器进程的 PID
  10. run_id : Redis 服务器的随机标识符(用于 Sentinel 和集群)
  11. tcp_port : TCP/IP 监听端口
  12. uptime_in_seconds : Redis 服务器启动以来,经过的秒数
  13. uptime_in_days : Redis 服务器启动以来,经过的天数
  14. lru_clock : 以分钟为单位进行自增的时钟,用于 LRU 管理
  15. clients : 已连接客户端信息,包含以下域:
  16. connected_clients : 已连接客户端的数量(不包括通过从属服务器连接的客户端)
  17. client_longest_output_list : 当前连接的客户端当中,最长的输出列表
  18. client_longest_input_buf : 当前连接的客户端当中,最大输入缓存
  19. blocked_clients : 正在等待阻塞命令(BLPOPBRPOPBRPOPLPUSH)的客户端的数量
  20. memory : 内存信息,包含以下域:
  21. used_memory : Redis 分配器分配的内存总量,以字节(byte)为单位
  22. used_memory_human : 以人类可读的格式返回 Redis 分配的内存总量
  23. used_memory_rss : 从操作系统的角度,返回 Redis 已分配的内存总量(俗称常驻集大小)。这个值和 top ps 等命令的输出一致。
  24. used_memory_peak : Redis 的内存消耗峰值(以字节为单位)
  25. used_memory_peak_human : 以人类可读的格式返回 Redis 的内存消耗峰值
  26. used_memory_lua : Lua 引擎所使用的内存大小(以字节为单位)
  27. mem_fragmentation_ratio : used_memory_rss used_memory 之间的比率
  28. persistence : RDB AOF 的相关信息
  29. stats : 一般统计信息
  30. replication : 主/从复制信息
  31. cpu : CPU 计算量统计信息、
  32. commandstats : Redis 命令统计信息
  33. cluster : Redis 集群信息
  34. keyspace : 数据库相关的统计信息
  35. 参数还可以是下面这两个:
  36. all : 返回所有信息
  37. default : 返回默认选择的信息

当不带参数直接调用 INFO 命令时,使用 default 作为默认参数。

 赋予权限:chmod +x redis_status.sh

修改zabbix_agentd配置文件重启生效。

 UserParameter=redis_status[*],/etc/zabbix/scripts/redis_status.sh $1

redis监控的参数比较多,具体根据自己的实际情况启用redis里边的应用集或者监控项,也可以自己再添加,。
默认aof未开启 以及slave这里都有监控,用不到的就停掉,这个模板。当前模板四个触发器,其中关于aof和主从的关了,只保留了redis存活状态和redis磁盘回写失败与否两个触发器。

 然后zabbixweb界面创建模板,添加项目

监控项都是自定义的key值,监控项的键值需要写:redis_status[你脚本里面的一些参数,实际上也就是redis info出来的参数]

 

posted @ 2018-03-14 14:31  GeminiMp  阅读(4223)  评论(0编辑  收藏  举报