zabbix 监控redis

1.安装python支持

yum -y install python-simplejson

2创建redis pytho 脚本(3.6版本)

  #!/usr/bin/python
  # -*- coding: utf-8 -*-
  import os
  #import json
  import simplejson as json  #特别要注意的地方
  t=os.popen("""sudo netstat -tlpn |grep redis-server|grep 0.0.0.0|awk '{print $4}'|awk -F: '{print $2}' """)
  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=(',',':')))


注意2.6版本最后一句改成

print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))

3测试pytho脚本

python redis_port.py

root@lemon zabbix]# python redis_port.py 
{
    "data":[
        {
            "{#REDISPORT}":"6499"
        }
    ]
}

返回数据为成功


4编辑zabbix_agentd.conf

 UserParameter=redis.discovery,/etc/zabbix/redis_port.py
  UserParameter=redis_stats[*],/usr/redis/redis-cli -h 127.0.0.1 -a 123456 -p $1 info|grep $2|cut -d : -f2

赋予脚本权限
chmod a+x/etc/zabbix/redis_port.py

sudo权限

[root@lemon zabbix]# tail -n 1 /etc/sudoers
zabbix ALL=(ALL)   NOPASSWD:ALL
[root@lemon zabbix]# sed -i 's/^Defaults.*.requiretty/#Defaults    requiretty/' /etc/sudoers
[root@lemon zabbix]# grep requiretty /etc/sudoers
#Defaults    requiretty
# changed in order to be able to use sudo without a tty. See requiretty above.
[root@lemon zabbix]# 


6 zabbix服务器验证

[root@qihoo zabbix]# /usr/local/zabbix/bin/zabbix_get -s 10.96.143.138 -k redis.discovery
ZBX_NOTSUPPORTED: Timeout while executing a shell script.
[root@qihoo zabbix]# /usr/local/zabbix/bin/zabbix_get -s 10.96.143.138 -k redis.discovery
{
    "data":[
        {
            "{#REDISPORT}":"6499"
        }
    ]
}
[root@qihoo zabbix]# /usr/local/zabbix/bin/zabbix_get -s 10.96.143.138 -k redis_stats[6499,used_memory:]
21287624

 
  

7 zabbix web界面导入下载的模板,添加监控项后检查监控图像

redis 模板下载地址 http://download.csdn.net/detail/a445588996/9845663


错误解决sudo: no tty present and no askpass program specified

参考http://www.th7.cn/system/lin/201508/123714.shtml

mark.



你可能感兴趣的:(zabbix 监控redis)