zabbix☞redis

阅读量: zyh 2020-05-14 09:34:44
Categories: > Tags:

zabbix 模板

  1. 自动发现规则

image-20200520152740815

  1. 过滤器

    image-20200520152922570

  2. 监控项原型

    名称 键值 间隔 历史记录 趋势 类型
    {#IP},{#TCP_PORT} connected_clients[客户端连接数] redis_info[{#IP},{#TCP_PORT},connected_clients] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} maxmemory[redis配置的内存上限] redis_info[{#IP},{#TCP_PORT},maxmemory] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} mem_fragmentation_ratio[内存碎片率] redis_info[{#IP},{#TCP_PORT},mem_fragmentation_ratio] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} redis_instantaneous_ops_per_sec[每秒执行的命令个数] redis_info[{#IP},{#TCP_PORT},instantaneous_ops_per_sec] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} redis 存活状态 redis_info[{#IP},{#TCP_PORT},exist] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} rejected_connections[被拒绝的客户端连接数] redis_info[{#IP},{#TCP_PORT},rejected_connections] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_cpu_sys[redis-master sys-cpu占用] redis_info[{#IP},{#TCP_PORT},used_cpu_sys] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_cpu_sys_children[redis-children sys-cpu占用] redis_info[{#IP},{#TCP_PORT},used_cpu_sys_children] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_cpu_user[redis-master user-cpu占用] redis_info[{#IP},{#TCP_PORT},used_cpu_user] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_cpu_user_children[redis-children user-cpu占用] redis_info[{#IP},{#TCP_PORT},used_cpu_user_children] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_memory[redis层面已使用内存-不含碎片] redis_info[{#IP},{#TCP_PORT},used_memory] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_memory_pct[操作系统层面已使用内存百分比] redis_info[{#IP},{#TCP_PORT},used_memory_pct] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_memory_peak[操作系统层面已使用内存历史峰值-含内存碎片] redis_info[{#IP},{#TCP_PORT},used_memory_peak] 30s 30d 90d Zabbix客户端(主动式)
    {#IP},{#TCP_PORT} used_memory_rss[操作系统层面已使用内存-含内存碎片] redis_info[{#IP},{#TCP_PORT},used_memory_rss] 30s 30d 90d Zabbix客户端(主动式)
  3. 触发器原型

    严重性 名称 表达式
    一般严重 {#IP}:{#TCP_PORT} Redis 内存碎片化超过50%, 剩余可用内存低于30% {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},mem_fragmentation_ratio].last()}>1.5 and {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},used_memory_pct].last()}>0.7
    严重 {#IP}:{#TCP_PORT} Redis 可用内存低, 存在使用交换分区 {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},mem_fragmentation_ratio].last()}<1 and {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},used_memory_pct].last()}>0.7
    一般严重 {#IP}:{#TCP_PORT} Redis 操作系统层面内存占用百分比过高 {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},used_memory_pct].last()}>0.7
    灾难 {#IP}:{#TCP_PORT} Redis 端口无法访问 {Template Redis Auto Discovert Active mode:redis_info[{#IP},{#TCP_PORT},exist].last()}<>1
  4. 图形原型,就不详细写了,这里只列出我自己的分类

    名称 图形类别
    {#IP},{#TCP_PORT} redis 连接数监控 900 200 正常
    {#IP},{#TCP_PORT} redis 其他监控 900 200 正常
    {#IP},{#TCP_PORT} redis qps 监控 900 200 正常
    {#IP},{#TCP_PORT} redis mem 监控 900 200 正常
    {#IP},{#TCP_PORT} redis cpu 监控 900 200 正常

脚本

脚本会生成自动发现进程脚本和redis检测脚本

zabbix的redis配置路径:ZabbixEtc

zabbix的脚本路径:ZabbixShell

redis的cli命令路径:RedisCli

自动发现脚本:ip_port_discovery.sh

redis检测脚本:redis_info.sh

ZabbixEtc=/etc/zabbix/zabbix_agentd.d
ZabbixShell=/etc/zabbix/shell
RedisCli='docker exec -t redis redis-cli'

[[ -d ${ZabbixShell} ]] || mkdir -p ${ZabbixShell}
[[ -z ${ZabbixEtc} ]] && [[ -z ${ZabbixShell} ]] || [[ -z ${RedisCli} ]] && exit

cat>${ZabbixEtc}/redis.conf<<EOF
##redis monitor
UserParameter=redis_info[*],${ZabbixShell}/redis_info.sh \$1 \$2 \$3 \$4
UserParameter=ip_port_discovery[*],${ZabbixShell}/ip_port_discovery.sh \$1
EOF
cat>${ZabbixShell}/redis_info.sh<<"EOF"
#!/bin/bash
RedisCli=
HOST=$1
PORT=$2
REDIS_INFO="$RedisCli -h $HOST -p $PORT info"
[[ $# -ne 3 ]] && [[ $# -ne 4 ]] && {
 exit
}
if [[ $# -eq 3 ]];then
case $3 in
exist)
 result=`$RedisCli -h $HOST -p $PORT ping 2>/dev/null |grep -c PONG`
 echo $result
;;
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
;;
#内存
maxmemory)
        result=`$REDIS_INFO|/bin/grep maxmemory|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result
;;
used_memory)
        result=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
        echo $result
;;
used_memory_rss)
        result=`$REDIS_INFO|/bin/grep -w used_memory_rss|awk -F":" '{print $NF}'|awk -F'k' '{print $1}'`
        echo $result
;;
used_memory_pct)
 A=`$REDIS_INFO|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
 B=`$REDIS_INFO|/bin/grep maxmemory|awk -F":" '{print $NF}'|awk 'NR==1'`
 result=`echo $A $B | awk '{printf"%0.2f",$1/$2}'`
        echo $result
;;
used_memory_peak)
        result=`$REDIS_INFO|/bin/grep used_memory_peak|awk -F":" '{print $NF}'|awk 'NR==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 [[ $# -eq 4 ]];then
case $4 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
EOF
cat>${ZabbixShell}/ip_port_discovery.sh<<"EOF"
#!/bin/bash
#$1是要发现的进程部分词汇
portarray=(`sudo ss -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
iparray=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
  do
     printf '\n\t\t{'
     printf "\"{#IP}\":\"${iparray}\",\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
     if [ $i -lt $[$length-1] ];then
                printf ','
     fi
  done
printf "\n\t]\n"
printf "}\n"
EOF
chmod a+x ${ZabbixShell}/redis_info.sh
chmod a+x ${ZabbixShell}/ip_port_discovery.sh
sed -i 's#RedisCli=#RedisCli=\"'"${RedisCli}"'\"#' ${ZabbixShell}/redis_info.sh


echo '------------------------------------我是 zabbix 监控信息----------------------------------'
echo '编辑 visudo,添加如下信息'
echo "
#zabbix用户可以以sudo执行ss
zabbix ALL = NOPASSWD: /usr/sbin/ss
#zabbix用户使用sudo无需tty
Defaults:zabbix    !requiretty
"
echo '若redis运行在docker中,执行如下命令'
echo 'usermod -a -G docker zabbix'