rabbitmq死信队列消息监听
#邮件通知并发送队列消息
#!/bin/bash maillog="/var/log/mq.maillog" message_file="/tmp/mq_message" echo "$(date +%Y-%m-%d-%H:%M:%S)" >> $maillog receiver="xxx01@xxx.com xxx02@xxx.com"
user="xxx"
password="xxxx"
message_count=`./rabbitmqadmin.py -H mq服务器地址 -u $user -p $password list queues|column -t|grep -vE 'message|\+'|grep "deadLetterQueue"|awk '$4!=0 {print $4}'` for i in seq `1 $message_count` do #requeue=false会将消息取出,测试可以配成requeue=true message_body=`./rabbitmqadmin.py -H mq服务器地址 -u $user -p $password get queue=deadLetterQueue requeue=false` echo $message_body >> $message_file done message=`cat $message_file` echo $message >> $maillog cat << EOF |mail -s "Rabbitmq Dead Letter Message" $receiver $message EOF rm -f "$message_file"
#rabbitmqadmin.py可以通过官方的接口去下载
#短信通知 #!/bin/bash #echo "$(date +%Y-%m-%d-%H:%M:%S)" >> $maillog cd /root/monitor/service/rabbitmq/ message_count=`./rabbitmqadmin.py -H mq服务器地址 -u xxx -p xxxx list queues|column -t|grep -vE 'message|\+'|grep "deadLetterQueue"|awk '{print $4}'` #message_count=1 if [ $message_count -eq 0 ];then echo "0" > ./messagefile fi #配置告警收敛 if [ `cat messagefile` -eq 0 ];then if [ $message_count -gt 0 ];then /usr/bin/python ./message.py echo "1" > ./messagefile fi fi
echo "0" > messagefile
message.py #使用云片网发送短信
#!/usr/bin/python
#-*- coding:utf-8 -*-
# apikey:成功注册后登录云片官网,进入后台可查看
# text:需要使用已审核通过的模板或者默认模板
# mobile:接收的手机号,仅支持单号码发送
import os
import urllib,urllib2,httplib,time
from subprocess import Popen,PIPE
def send_sms(apikey, text, mobile):
#服务地址
sms_host = "sms.yunpian.com"
#端口号
port = 443
#版本号
version = "v2"
#智能匹配模板短信接口的URI
sms_send_uri = "/" + version + "/sms/batch_send.json"
params = urllib.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
conn.request("POST", sms_send_uri, params, headers)
response = conn.getresponse()
response_str = response.read()
conn.close()
return response_str
print "#"*50
if __name__=='__main__':
apikey='xxxxx'
mobile='xxxxx,xxxxxx'
message="【xx告警】Rabbitmq死信队列存在堆积消息,请注意查看!"
print message
send_sms(apikey,message,mobile)
赠人玫瑰,手有余香,如果我的文章有幸能够帮到你,麻烦帮忙点下右下角的推荐,谢谢!
作者: imcati
出处: https://www.cnblogs.com/imcati/>
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接