How to batch delete connections on a RabbitMQ cluster
search cancel

How to batch delete connections on a RabbitMQ cluster

book

Article ID: 293244

calendar_today

Updated On:

Products

VMware RabbitMQ

Issue/Introduction

If you have a lot of external systems that connect to this RabbiMQ cluster which are producing and consuming a lot of messages, sometimes some external systems create several connections that can cause problems with the cluster and to the other applications that use that cluster. In this KB article, we'll provide a fast way to kill all of those connections without using the UI (if there are 2000 connections, it starts to be a little bit difficult).


Environment

Product Version: 1.18

Resolution

Take a look at "rabbitmqctl list_connections" in https://www.rabbitmq.com/rabbitmqctl.8.html#list_connections to see if there would be a way to identify those connections based on the info that commands can provide.

For example, they may share the same user, or peer_host, or there is some common info in client_properties that no other connections have. Then, using grep and awk or cut, you can get the connection pid and pass it to a loop containing the "rabbimqctl close_connection" in https://www.rabbitmq.com/rabbitmqctl.8.html#close_connection

For example:
while IFS= read -r line1
do   
  rabbitmqctl close_connection "$line1" "connection closed" &
done < <(rabbitmqctl list_connections user peer_host state pid | grep -F 127.0.0.1 | awk ' {print $4}')


The script above gets the connections with the columns user, peer_host, state, and pid. Then filters by peer_host "127.0.0.1" with grep and gets the 4th column with awk, which is the connection pid. Then it uses "rabbitmqctl close_connection" with the pid and adds the "explanation" argument with value "connection closed".


Note: Use the command at your own risk and test the "rabbitmqctl list_connections ..." part thoroughly to be sure you are listing the connections you intend to close.