zabbix自動發現監控redis數據庫

zabbix客戶端:python

添加redis自動發現腳本以下:git

mkdir -p /etc/zabbix/monitor_scripts/ && cd /etc/zabbix/monitor_scripts/
vim discover_redis.py
#coding:utf-8
import json
import commands

(status, output) = commands.getstatusoutput('''sudo netstat -tlnp|grep 'redis-server'|awk '{print $4}'|awk -F':' '{print $(NF)}'|sort -u''')
outputs = output.split('\n')
ports = []
for port in  outputs:
    ports += [{'{#REDISPORT}': port}]

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

添加獲取redis數據的腳本以下:其中-k 表示獲取redis.info的哪一個值,-p表示端口,-P表示密碼,若是有須要的話能夠加入。redis

vim check_redis.sh
#!/bin/sh
while getopts "p:k:P:" opt
do
        case $opt in
                p ) redis_port=$OPTARG;;
                k ) info_key=$OPTARG;;
                P ) redis_passwd=$OPTARG;;
                ? )
                echo 'parameter is wrong!'
                exit 1;;
        esac
done
if [ ! "${redis_port}" ] || [ ! "${info_key}" ];then
        echo "parameter is null"        
        exit 1
fi

if [ "${redis_passwd}" ];then
        result=`/usr/bin/redis-cli -a ${redis_passwd} -p ${redis_port} info|grep "${info_key}:"|cut -d: -f2`
else
        result=`/usr/bin/redis-cli -p ${redis_port} info|grep "${info_key}:"|cut -d: -f2`
fi
echo $result

chmod +x /etc/zabbix/monitor_scripts/*json

而後須要在zabbix_agentd客戶端配置redis的鍵值,配置以下:vim

vim /etc/zabbix/zabbix_agentd.d/redis.conf 
UserParameter=redis.discover,python /etc/zabbix/monitor_scripts/discover_redis.py 
UserParameter=redis.status[*],sh /etc/zabbix/monitor_scripts/check_redis.sh -k $1 -p $2 -P mimamimamima

還要容許zabbix用戶無密碼運行netstat,ui

echo "zabbix ALL=(root) NOPASSWD:/bin/netstat">>/etc/sudoers
sed -i 's/^Defaults.*.requiretty/#Defaults    requiretty/' /etc/sudoers

最後重啓一下客戶端/etc/init.d/zabbix_agent restart.net

導入模板。。。。rest

模板見:https://git.oschina.net/zhuangweihong/opscode

相關文章
相關標籤/搜索