方法一,zabbix监控redis
1、导入模板
同样,模板我已上传到 github项目 上 。直接下载,通过下面的步骤导入模板:点击configuration ----> templates ------> import -------> 导入下载的xml文件 。
2、客户端key配置
在被监控的主机上,打开/etc/zabbix/zabbix_agentd.conf 配置文件,在最后一行加入:
UserParameter=redis_stats[*],redis-cli -h 127.0.0.1 -p $1 info|grep $2|cut -d : -f2
配置完成后通过/etc/init.d/zabbix_agentd restart 重启服务并应用最新的配置文件。
注:在该文件中还要确认UnsafeUserParameters=1 。
3、服务端验证
验证方式还是通过zabbix_get 程序去获取数据,能正常得到数据,证明通信正常。
代码如下 | 复制代码 |
# zabbix_get -s 172.20.0.20 -k redis_stats[6379,total_connections_received] |
上面正常从被监控主机上取得了数据,c/s通信正常 。
4、添加主机关联模板
通过configuration ------> hosts ------> create host 创建新的主机,并在templates项中,查找Templates Redis_6379 模板,并link 该模板 。就可以正常获取数据了。同样,验证数据使用 monitoring ----- lastest data ---- 打到刚刚添加的主机,查看是否有数据 。
5、查看监控图
通过monitoring ----- graphs 可以查看刚刚添加的监控项的监控图,这里只截下内存使用情况图,
方法二,利用zabbix low level discovery 自动发现监控redis
一、服务器配置
1)配置正则表达式,在后面添加discovery rules时会用到
#这里是个坑,我这里正则匹配的是端口号,刚开始写的是”d+”测试也通过了,全部都配置好了,在服务端通过zabbix_get也能正常获取到client的值,无奈就是不自动上报,检查了一个中午,后来问了几个群友,终于找到原因了,正则要用^(8899|10000)$ 类似这种格式,说多了都是泪呀
最后再查阅官方文档,原来对zabbix的正则没有理解清楚,它的正则是对上报结果的二次处理,其实如果上报的就是准确的,就不用进行二次过滤,最终采用正则一处留空,不进行二次匹配处理
2)创建模板
点击Discovery rules 创建自定义规则
PTSMlLtNoJwYk+vA/Px+/kK/nz0tifj/yi9l/5TXpWn/5PcZimMv+05GqEZdZ+43SePGt5SPUwR7+oyg6ePCgtlG0zTSybiZuGCMh/Gv7vPiROFdU297WGP7/8hcXL1++sra2/snNm3fu3n3w8OHDzc2HDzfv33/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r6srjuuqH/6m57v7Zzv65+EV3/2zXu/D/xE0a/uPV3D/b3T/X3T/XnZqNa6CbjY8/mu3sn+uPn5rrTM1p6kpczv7Z7tRst/9RutjZTvw6m2WuO5V+3Vxahs5U8qXpEtTyTCXfkn5dMkFHW5645FkBpGX2p+xMxbP3V7aTFF6zjtQVdUVdUVfUFXVFXVFX1BV1RV1RV9SVX3WVhf+puaWpue6Bue7U3NKB+YLw32q1hg/t0kJarVY6Rl2+5RsHC/8H5pcOzHUPzPfXd2puaWp+6UA8zHUPzC8dmFuamu9OzS9NzXcPzHcPzHUPzC3FtSTV1YG57lQ8VzzM9z9N32bfNd+VlnZgPi5Dd2q+OzWXfDSffMv80oH5pfijqbnk9Xx/rv5k893+8uPi9b99KStbsoIH+kM3maubFuDA3FK2kPl0xniuLnVFXVFX1BV1RV1RV9QVdUVdUVfUFXXldV31w/9z80vPLSw9t7DUfzG/VHjmX5vGW2bqNNq36vLjF4Xh//+5ScP/c/NL0/NL0wtL0wv9dZ+eX3pufimtinR8XCHZp0pdpZPlZhEmmF7ITTk9b54sXeZ8bsnyxOJiF8SJl6fVr5hP3s7L008vKKsjTDAtvqauqCvqirqirqgr6oq6oq6oK+qKuqKuvK2rfvifPr588PjSwePL6aAN/4XZXgr5ha/tqV77pabw/9hNGv7j1ZxeWDp4fClZ96WDx5cOLixNH1+eXlg+uLB08PjydDo+qRm1rpJ544+WDx5fmpYnyL5leqH/dro/SNMsT/dLtTydlWp5eiFbeFqM6ePLyRLiBS5Py+Vcns4vJ1/+penjywcX4pFLydKE5Szkv5G6oq6oK+qKuqKuqCvqirqirqgr6oq68rOu/h45qX8cokxdqQAAAABJRU5ErkJggg==" alt="zabbix监控redis数据库状态两种方法-数据库字段状态监控">
点击 Item prototypes添加监控项
我这里需要监控4个指标,建立4个item
Key值在客户端定义
二、客户端配置:
先看zabbix_agentd.conf配置文件
代码如下 | 复制代码 |
UnsafeUserParameters=1 #允许zabbix用户运行root命令 UserParameter=redis.discovery,/usr/local/zabbix/bin/redis_port.py #获取json格式数据,上报端口 UserParameter=redis[*],/usr/local/zabbix/bin/redis_stats.sh $1 $2 #自定义Key值 |
需要两个脚本:
代码如下 | 复制代码 |
ll /usr/local/zabbix/bin/ total 516 -rwxr-xr-x 1 root root 342 Oct 25 16:56 redis_port.py -rwxr-xr-x 1 root root 773 Oct 25 14:59 redis_stats.sh -rwxr-xr-x 1 root root 236737 Jul 23 2012 zabbix_get -rwxr-xr-x 1 root root 278582 Jul 23 2012 zabbix_sender JSON格式脚本 [root@ ~]# cat /usr/local/zabbix/bin/redis_port.py ##json串 #!/usr/bin/env python import os import json t=os.popen("""netstat -natp|awk -F: '/redis-server/&&/LISTEN/{print $2}'|awk '{print $1}' """) ports = [] for port in t.readlines(): r = os.path.basename(port.strip()) ports += [{'{#REDISPORT}':r}] print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':')) |
获取端口数据状态脚本:
代码如下 | 复制代码 |
[root@ ~]# cat /usr/local/zabbix/bin/redis_stats.sh ##获取状态 #!/bin/bash (echo -en "INFOrn"; sleep 1;) | nc $HOSTNAME $PORT > $CACHE_FILE 2>/dev/null || exit 1 grep "^$METRIC:" $CACHE_FILE |awk -F':|,' '{print $2}'|sed "s/[^0-9]//g" |
以上是全部配置完成,下来进行测试
在zabbix_server上进行测试,执行以下命令:
代码如下 | 复制代码 |
zabbix_get -s 192.168.250.206 -k redis.discovery
得到如下结果:
zabbix_get -s 192.168.250.206 -k redis[used_memory,8899]
|
4834072
OK,得到所需要的值!
然后将模板添加到所需要监控的机器上,这个就不上图了
添加完模版以后,查看数据:
在做的过程中遇到以下问题:
1.关于正则的问题
正则匹配不同于普通的正则,多个之间的匹配正则要用^(8899|10000)$ 类似这种格式
或者正则留空
2.权限问题,zabbix_agentd是zabbix用户启动的,默认不能执行netstat -p等命令,导致从服务器取到的自动发现脚本为空
代码如下 | 复制代码 |
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) 解决方法 : chmod +s /bin/netstat |