Python script to query RocketMQ's JMX interface to get the number of connections for producers and consumers

The following is a simple Python script to query RocketMQ's JMX interface for producer and consumer connection counts. This script relies on requestslibraries to send HTTP requests.

First, you need to enable JMX in the Broker configuration file:

com.alibaba.rocketmq.common.MQVersion.enable_jmx = true

Then, you can use the following script to get the number of connections:

import requests
import json

# 定义JMX接口地址
broker_address = "localhost"
broker_port = "10911"
jmx_url = f"http://{
      
      broker_address}:{
      
      broker_port}/jmx/query"

# 定义要查询的MBeans
mbeans = [
    "com.alibaba.rocketmq.common:type=BrokerController,name=BrokerControllerState,service=brokerAddr={broker_address}:{broker_port}",
]

# 发送请求并获取响应
for mbean in mbeans:
    response = requests.get(f"{
      
      jmx_url}?objectName={
      
      mbean}")

    # 解析并打印结果
    data = json.loads(response.text)
    for datum in data:
        if datum["name"] == "producerConnectionCount":
            print(f"Producer connection count: {
      
      datum['value']}")
        elif datum["name"] == "consumerConnectionCount":
            print(f"Consumer connection count: {
      
      datum['value']}")

Then, you can create a new item in Zabbix that executes this script and collects the results. For example, you can use system.runkeys to execute scripts:

system.run[/path/to/your/script.py]

Then, you can create a new trigger that detects if the number of connections exceeds your preset threshold. For example, if you want to trigger an alert when the number of connections exceeds 100, you can use the following trigger expression:

{your_host:system.run[/path/to/your/script.py].last()} > 100

Note that this is just a basic example, you may need to adjust it based on your actual RocketMQ and Zabbix configuration. If you encounter any problems, I suggest you refer to the official documentation of RocketMQ and Zabbix or seek help from the community.

The JMX switch of RocketMQ is enabled by default, that is, com.alibaba.rocketmq.common.MQVersion.enable_jmx = truethe default configuration, and there is no need to explicitly specify it under normal circumstances.

But if you want to check whether JMX is enabled in the current configuration, you can check it in the RocketMQ Broker configuration file. This file is generally located in the directory under the RocketMQ installation directory conf, and the file name may be broker.propertiesor other names you customize.

You can open this file and look for com.alibaba.rocketmq.common.MQVersion.enable_jmxthis item. If there is no such item in the file, then JMX is enabled by default. If there is this item, and its value is set to false, then JMX is turned off.

For example, you can use grepthe command to find out:

grep "com.alibaba.rocketmq.common.MQVersion.enable_jmx" /path/to/your/broker.properties

If there is no output from the command, then JMX is enabled by default. If the command outputs com.alibaba.rocketmq.common.MQVersion.enable_jmx = false, then JMX is turned off.

If you want to enable JMX, you only need to add or modify this item in the configuration file true:

com.alibaba.rocketmq.common.MQVersion.enable_jmx = true

Then restart Broker for the configuration to take effect.

Guess you like

Origin blog.csdn.net/u011197085/article/details/131546641