1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import os
import optparse
import sys
import re
import socket
import qpid
from threading import Condition
from qpid.management import managementClient
from qpid.peer import Closed
from qpid.connection import Connection
from qpid.util import connect
from time import sleep
class Broker:
def __init__ (self, text):
colon = text.find (":")
if colon == -1:
host = text
self.port = 5672
else:
host = text[:colon]
self.port = int (text[colon+1:])
self.host = socket.gethostbyname (host)
def name (self):
return self.host + ":" + str (self.port)
class mgmtObject (object):
""" Generic object that holds the contents of a management object with its
attributes set as object attributes. """
def __init__ (self, classKey, timestamps, row):
self.classKey = classKey
self.timestamps = timestamps
for cell in row:
setattr (self, cell[0], cell[1])
class BrokerManager:
def __init__ (self):
self.dest = None
self.src = None
self.broker = None
self.objects = {}
self.filter = None
self.specpath="/usr/share/amqp/amqp.0-10.xml"
def SetBroker (self, broker):
self.broker = broker
def ConnectToBroker (self):
try:
self.spec = qpid.spec.load (self.specpath)
self.sessionId = "%s.%d" % (os.uname()[1], os.getpid())
self.conn = Connection (connect (self.broker.host, self.broker.port), self.spec)
self.conn.start ()
self.mclient = managementClient (self.spec, None, self.configCb, self.instCb)
self.mchannel = self.mclient.addChannel (self.conn.session(self.sessionId))
except socket.error, e:
print "Connect Error:", e
exit (1)
def setFilter(self,filter):
self.filter = filter
def setSpecpath(self,spec):
self.specpath = spec
def Disconnect (self):
self.mclient.removeChannel (self.mchannel)
def configCb (self, context, classKey, row, timestamps):
className = classKey[1]
if className != "queue":
return
obj = mgmtObject (classKey, timestamps, row)
if obj.id not in self.objects:
self.objects[obj.id] = (obj.name, None, None)
def instCb (self, context, classKey, row, timestamps):
className = classKey[1]
if className != "queue":
return
obj = mgmtObject (classKey, timestamps, row)
if obj.id not in self.objects:
return
(name, first, last) = self.objects[obj.id]
if first == None:
self.objects[obj.id] = (name, obj, None)
return
if len(self.filter) > 0 :
match = False
for x in self.filter:
if x.match(name):
match = True
break
if match == False:
return
if last == None:
lastSample = first
else:
lastSample = last
self.objects[obj.id] = (name, first, obj)
deltaTime = float (obj.timestamps[0] - lastSample.timestamps[0])
enqueueRate = float (obj.msgTotalEnqueues - lastSample.msgTotalEnqueues) / (deltaTime / 1000000000.0)
dequeueRate = float (obj.msgTotalDequeues - lastSample.msgTotalDequeues) / (deltaTime / 1000000000.0)
print "%-41s%10.2f%10d..%-10d%13.2f%13.2f" % \
(name, deltaTime / 1000000000, obj.msgDepthLow, obj.msgDepthHigh, enqueueRate, dequeueRate)
def Display (self):
self.ConnectToBroker ()
print "Queue Name Sec Depth Range Enq Rate Deq Rate"
print "==================================================================================================="
try:
while True:
sleep (1)
except KeyboardInterrupt:
pass
self.Disconnect ()
##
## Main Program
##
def main():
p = optparse.OptionParser()
p.add_option('--broker-address','-a', default='localhost' , help='broker-addr is in the form: hostname | ip-address [:<port>] \n ex: localhost, 10.1.1.7:10000, broker-host:10000')
p.add_option('--amqp-spec-file','-s', default='"/usr/share/amqp/amqp.0-10.xml', help='the path to the amqp spec file')
p.add_option('--filter','-f' ,default=None ,help='a list of comma separated queue names (regex are accepted) to show')
options, arguments = p.parse_args()
host = options.broker_address
specpath = options.amqp_spec_file
filter = []
if options.filter != None:
for s in options.filter.split(","):
filter.append(re.compile(s))
bm = BrokerManager ()
bm.SetBroker (Broker (host))
bm.setSpecpath(specpath)
bm.setFilter(filter)
bm.Display()
if __name__ == '__main__':
main()
|