Company RABBITMQ Consumer Reconnect after the old connection will not be disconnected, will always exist in the service side, as for what is not yet found. This causes the number of connections each time after a lot of increase, so write a Python script delete invalid connection, according to each connection inside the data transfer to define whether it is valid, if 1 hours are not data transmission then is invalid, the implementation code is as follows:
#!/usr/bin/python#coding:utf8 "" "Delete rabbitmq all connections with no data within 1 hours" "from optparse import optionparserimport sysimport jsonimport urllib2import requests# use option Help information can be used in Chinese reload (SYS) Sys.setdefaultencoding ("Utf-8") usage = sys.argv[0] + " <Options>" parser = optionparser (usage) parser.add_option ("-V", dest= "Vhost", action= "Store", default=False, help= "Select Vhost, defaulte all ") Options, args = parser.parse_args () class RabbitMQ: def __init__ (Self, user= ' guest ', passwd= ' guest ', server_ip= ' 192.168.1.31 '): self.user = user self.password = passwd self.server_ip = server_ip def getallconnections (self): "" " get all connections back json "" " if not options.vhost: connections = requests.get ("Http://{0}:15672/api/connections"). Format (SELF.SERVER_IP), auth= (Self.user, self.password)) else: connections = Requests.get ("Http://{0}:15672/apI/vhosts/{1}/connections ". Format (self.server_ip, options.vhost), auth= (Self.user, self.password)) connections = connections.json ( ) return connections def Connectionsnumber (self): "" " process The JSON returned above, get the key data, return to the list "" list1 = [] data = self.getallconnections () for i in Data: &nBsp; list1.append (i[' name ') return list1 def url (self): "" Generate final Access API, return list "" " url_list = [] data = self.connectionsnumber () for i in data: send_ip = i.split (",") [0].split (":") [0] send_port = i.split (",") [0].split (":") [1].strip () receive_ip = i.split (",") [1].split (":") [0] . Strip () &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; url = ' http://{0}:15672/api/connections/{1}%3a{2}%20-% 3e%20{3}%3a5672?data_rates_age=3600&data_rates_incr=60 '. Format (self.server_ip, send_ip, send_ PORT,&NBSP;RECEIVE_IP) url_list.append ( URL) return url_list def Getalldata (self): "" " Get all the information for the API, return to the list "" data = self.url () if self.server_ip == "10.8.5.3": authorization = "basic z3vlc3q6umrun3lsv2fmzws2sjk4aa==" else : &NBSP;&NBSP;&NBSP;&Nbsp; authorization = "Basic Z3Vlc3Q6Z3Vlc3Q=" user_agent = "mozilla/5.0 (windows nt 6.1; win64; x64) AppleWebKit/537.36 (Khtml, like gecko) chrome/ 59.0.3071.115 safari/537.36 " list1 = [] for i in data: url = i request = urllib2. Request (i, headers={"user-agent":user_agent, "Authorization": Authorization}) req = urllib2.urlopen (Request) list1.append (Json.loads (Req.read ())) return list1 def getavgrate (self): "" get recv_oct_ for each connection Details and Send_oct_details, based on a 1-hour average, if no data transfer is an invalid connection, generate the Delete API and delete the "" " data = self.getalldata () for i in data: n = 2 recv_ Avg_rate = i.get ("Recv_oct_details"). Get ("Avg_rate") n += recv_avg_rate send_avg_rate = i.get ("Send_oct_details"). Get ("Avg_rate") n += send_avg_rate if n == 2: name = i.get ("name") send_ip = name.split (",") [0].split (":") [0] send_port = name.split (",") [0].split (":") [1].strip () receive_ip = name.split (",") [1].split (":") [0].strip () api = ' http://{0}:15672/api/connections/{1}:{2}%20-%3e%20{3}:5672 '. Format (self.server_ip, send_ip, send_ PORT,&NBSP;RECEIVE_IP) code = Requests.delete (I, auth= (self.user, self.passwd)) if code.status_code == 204: print ("{ 0} Delete succeeded ". Format (send_port)) else: print ("{0} Delete failed". Format (send_port)) sys.exit (1) print ("Done ...") if __name__ == ' __main__ ': &NBSP;MQ&NBSP;=&NBSP;RABBITMQ () &NBSp; mq.getavgrate ()
This article from the "Blue _ Storm" blog, reproduced please contact the author!
Use Python to clean up the 1-hour connection with no data in RABBITMQ