prometheus监控RabbitMQ策略

一般用官方的rabbitmq_exporter采取数据即可,然后在普米配置。但如果rabbitmq节点的队列数超过了5000,往往rabbitmq_exporter就会瘫痪,因为rabbitmq_exporter采集的信息太多,尤其是那些队列的细节,所以队列多了,rabbitmq_exporter就没法用了。所以我们不得不自己写脚本探测MQ,脚本分享如下:

首先 pip3 install prometheus-client

bash 复制代码
import prometheus_client as prom
import pandas as pd
from sqlalchemy import create_engine
import requests,time
#自定义普米MQ监控指标
port = '15672'
username = 'username'
password = 'password'
g0 = prom.Gauge("rabbitmq_up", 'life of the node',labelnames=['node','region'])
g1 = prom.Gauge("rabbitmq_queues", 'total queue num of the node',labelnames=['node','region'])
g2 = prom.Gauge("rabbitmq_channels", 'total queue num of the node',labelnames=['node','region'])
g3 = prom.Gauge("rabbitmq_connections", 'total queue num of the node',labelnames=['node','region'])
g4 = prom.Gauge("rabbitmq_consumers", 'total queue num of the node',labelnames=['node','region'])
g5 = prom.Gauge("rabbitmq_exchanges", 'total queue num of the node',labelnames=['node','region'])
g6 = prom.Gauge("rabbitmq_messages", 'total messages of the node',labelnames=['node','region'])
g7 = prom.Gauge("rabbitmq_vhosts", 'total vhost num of the node',labelnames=['node','region'])
g8 = prom.Gauge("rabbitmq_node_mem_used", 'mem used of the node',labelnames=['node','region'])
g9 = prom.Gauge("rabbitmq_node_mem_limit", 'mem limit of the node',labelnames=['node','region'])
g10 = prom.Gauge("rabbitmq_node_mem_alarm", 'mem alarm of the node',labelnames=['node','region'])
g11 = prom.Gauge("rabbitmq_node_disk_free_alarm", 'free disk alarm of the node',labelnames=['node','region'])

prom.start_http_server(8086)
#要监控的MQ节点
nodelist=['1.1.1.1','1.1.1.2','1.1.1.3'] 
while True:
  for node in nodelist:                      #遍历各个node
    status=1
    try:                                                                              #测试连通性
      requests.get(url=f"http://{node}:{port}/api/overview", auth=(username, password),timeout=5)
    except:
      status=0
      continue
    finally:
      g0.labels(node=node,region=region).set(status)
    info1=requests.get(url=f"http://{node}:{port}/api/overview", auth=(username, password),timeout=5)
    info2=requests.get(url=f"http://{node}:{port}/api/nodes", auth=(username, password),timeout=5)[0]
    info3=requests.get(url=f"http://{node}:{port}/api/vhosts", auth=(username, password),timeout=5)
    g1.labels(node=node,region=region).set(info1.get('object_totals').get('queues'))  
    g2.labels(node=node,region=region).set(info1.get('object_totals').get('channels')) 
    g3.labels(node=node,region=region).set(info1.get('object_totals').get('connections')) 
    g4.labels(node=node,region=region).set(info1.get('object_totals').get('consumers')) 
    g5.labels(node=node,region=region).set(info1.get('object_totals').get('exchanges')) 
    g6.labels(node=node,region=region).set(info1.get('queue_totals').get('messages')) 
    g7.labels(node=node,region=region).set(len(info3)) 
    g8.labels(node=node,region=region).set(info2.get('mem_used'))
    g9.labels(node=node,region=region).set(info2.get('mem_limit'))
    g10.labels(node=node,region=region).set(info2.get('mem_alarm'))
    g11.labels(node=node,region=region).set(info2.get('disk_free_alarm'))
  time.sleep(30)

python3 执行这个脚本,就会运行一个页面如下

于是就可以用普米采集了

注意honor_labels 指标,就是让job,instance指标不被普米自身的覆盖

或者写脚本推送到Pushgateway也可以,如下

bash 复制代码
import prometheus_client as prom
import pandas as pd
from sqlalchemy import create_engine
import requests,time
#自定义普米MQ监控指标
port = '15672'
username = 'username'
password = 'password'
url='http://pushgateway地址:9091'
registry = prom.CollectorRegistry()
url='http://172.18.2.143:8181'
job='rabbitmq'
g0 = prom.Gauge("rabbitmq_up", 'life of the node',labelnames=['node','region'],registry=registry)
g1 = prom.Gauge("rabbitmq_queues", 'total queue num of the node',labelnames=['node','region'],registry=registry)
g2 = prom.Gauge("rabbitmq_channels", 'total queue num of the node',labelnames=['node','region'],registry=registry)
g3 = prom.Gauge("rabbitmq_connections", 'total queue num of the node',labelnames=['node','region'],registry=registry)
g4 = prom.Gauge("rabbitmq_consumers", 'total queue num of the node',labelnames=['node','region'],registry=registry)
g5 = prom.Gauge("rabbitmq_exchanges", 'total queue num of the node',labelnames=['node','region'],registry=registry)
g6 = prom.Gauge("rabbitmq_messages", 'total messages of the node',labelnames=['node','region'],registry=registry)
g7 = prom.Gauge("rabbitmq_vhosts", 'total vhost num of the node',labelnames=['node','region'],registry=registry)
g8 = prom.Gauge("rabbitmq_node_mem_used", 'mem used of the node',labelnames=['node','region'],registry=registry)
g9 = prom.Gauge("rabbitmq_node_mem_limit", 'mem limit of the node',labelnames=['node','region'],registry=registry)
g10 = prom.Gauge("rabbitmq_node_mem_alarm", 'mem alarm of the node',labelnames=['node','region'],registry=registry)
g11 = prom.Gauge("rabbitmq_node_disk_free_alarm", 'free disk alarm of the node',labelnames=['node','region'],registry=registry)

#要监控的MQ节点
nodelist=['1.1.1.1','1.1.1.2','1.1.1.3'] 
while True:
  for node in nodelist:                      #遍历各个node
    status=1
    try:                                                                              #测试连通性
      requests.get(url=f"http://{node}:{port}/api/overview", auth=(username, password),timeout=5)
    except:
      status=0
      continue
    finally:
      g0.labels(node=node,region=region).set(status)
    info1=requests.get(url=f"http://{node}:{port}/api/overview", auth=(username, password),timeout=5)
    info2=requests.get(url=f"http://{node}:{port}/api/nodes", auth=(username, password),timeout=5)[0]
    info3=requests.get(url=f"http://{node}:{port}/api/vhosts", auth=(username, password),timeout=5)
    g1.labels(node=node,region=region).set(info1.get('object_totals').get('queues'))  
    g2.labels(node=node,region=region).set(info1.get('object_totals').get('channels')) 
    g3.labels(node=node,region=region).set(info1.get('object_totals').get('connections')) 
    g4.labels(node=node,region=region).set(info1.get('object_totals').get('consumers')) 
    g5.labels(node=node,region=region).set(info1.get('object_totals').get('exchanges')) 
    g6.labels(node=node,region=region).set(info1.get('queue_totals').get('messages')) 
    g7.labels(node=node,region=region).set(len(info3)) 
    g8.labels(node=node,region=region).set(info2.get('mem_used'))
    g9.labels(node=node,region=region).set(info2.get('mem_limit'))
    g10.labels(node=node,region=region).set(info2.get('mem_alarm'))
    g11.labels(node=node,region=region).set(info2.get('disk_free_alarm'))
    prom.push_to_gateway(url,job=job,registry=registry, timeout=20)
  time.sleep(30)
相关推荐
bug_null15 小时前
RabbitMQ消息可靠性保证机制4--消费端限流
rabbitmq
青石路15 小时前
不依赖 Spring,你会如何自实现 RabbitMQ 消息的消费(一)
rabbitmq
呼啦啦啦啦啦啦啦啦19 小时前
【Rabbitmq篇】RabbitMQ⾼级特性----持久性,发送⽅确认,重试机制
分布式·rabbitmq
菜鸟挣扎史1 天前
grafana+prometheus+windows_exporter实现windows进程资源占用的监控
windows·grafana·prometheus·进程·process
斯普信专业组1 天前
RabbitMQ实战启程:从配置到故障排查的实战处理(下)
网络·分布式·rabbitmq
呼啦啦啦啦啦啦啦啦1 天前
【Rabbitmq篇】RabbitMQ⾼级特性----消息确认
分布式·rabbitmq
泰山小张只吃荷园1 天前
快速入门消息队列MQ、RabbitMQ
java·spring boot·分布式·spring·spring cloud·rabbitmq·java-rabbitmq
呼啦啦啦啦啦啦啦啦2 天前
基于SpringBoot+RabbitMQ完成应⽤通信
spring boot·rabbitmq·java-rabbitmq
小马爱打代码2 天前
SpringBoot 整合 RabbitMQ
spring boot·rabbitmq·java-rabbitmq
牙牙7053 天前
Prometheus结合K8s(二)使用
容器·kubernetes·prometheus