该篇参考大佬文章:https://blog.51cto.com/kangfs/3671614
在crontab -e 添加任务,
特别注意:rabbitmq不是绝对路径,所以crontab环境变量一般为/bin,特别注意rabbitmqctl相关命令做个软连接至/bin目录下:
建立软链
ln -s /sbin/rabbitmqctl /bin/rabbitmqctl
ln -s /usr/lib/rabbitmq/bin/rabbitmq-env /bin/rabbitmq-env
ln -s /usr/lib/erlang/bin/erl /bin/erl
cat zabbix_agent2.d/Items.conf
UserParameter=rabbitmq.queues,/bin/bash /etc/zabbix/script/rabbitmq/rabbitmq-queues.sh
UserParameter=queues.check[*],/bin/bash /etc/zabbix/script/rabbitmq/queues_check.sh $1
mkdir -p /etc/zabbix/script/rabbitmq
上传如下脚本 到这个目录 /etc/zabbix/script/rabbitmq:
chown -R zabbix.zabbix /etc/zabbix/script/rabbitmq
chmod 755 /etc/zabbix/script/rabbitmq/*
crontab -e设置定时任务
# RMQ消息堆积告警监控
* */1 * * * /bin/bash /etc/zabbix/script/rabbitmq/queues_list.sh
cat messages.sh
#!/bin/bash
curl -s -u username:passwd http://127.0.0.1:15672/api/queues/%2f/$1 |awk -F '"messages":' '{print $2}'|awk -F , '{print $1}'
cat queues_check.sh
#!/bin/bash
###############################
#zabbix获取key
###############################
queues=$1
function rmq_queues {
queues_key=`cat /tmp/.queues.txt | grep "\b${queues}\b" | awk '{print $2}'`
echo "$queues_key"
}
rmq_queues
cat queues_list.sh
#!/bin/bash
rabbitmqctl list_queues |grep -v 'Listing queues ...' | grep -wv messages | grep -v Timeout: > /tmp/.queues.txt
cat rabbitmq-queues.sh
#!/bin/bash
####################################
#实现zabbix自动发现功能
####################################
proc_array=(`cat /tmp/.queues.txt | awk '{print $1}'`)
length=${#proc_array[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf "\n\t\t{"
printf "\"{#QUEUES_NAME}\":\"${proc_array[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ","
fi
done
printf "\n\t]\n"
printf "}\n"
image.png
进入zabbix UI 配置模板
image.png
image.png
image.png
image.png
image.png
image.png
image.png
网友评论