summaryrefslogtreecommitdiff
path: root/qpid/tools/src/py/qpid-cluster
blob: 312d59f6704207c5c6e26262abd4728e234c720e (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/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
from optparse import OptionParser, OptionGroup
import sys
import locale
import socket
import re
from qmf.console import Session

class Config:
    def __init__(self):
        self._host = "localhost"
        self._connTimeout = 10
        self._stopId = None
        self._stopAll = False
        self._force = False
        self._numeric = False
        self._showConn = False
        self._delConn = None

class IpAddr:
    def __init__(self, text):
        if text.find("@") != -1:
            tokens = text.split("@")
            text = tokens[1]
        if text.find(":") != -1:
            tokens = text.split(":")
            text = tokens[0]
            self.port = int(tokens[1])
        else:
            self.port = 5672
        self.dottedQuad = socket.gethostbyname(text)
        nums = self.dottedQuad.split(".")
        self.addr = (int(nums[0]) << 24) + (int(nums[1]) << 16) + (int(nums[2]) << 8) + int(nums[3])

    def bestAddr(self, addrPortList):
        bestDiff = 0xFFFFFFFFL
        bestAddr = None
        for addrPort in addrPortList:
            diff = IpAddr(addrPort[0]).addr ^ self.addr
            if diff < bestDiff:
                bestDiff = diff
                bestAddr = addrPort
        return bestAddr

class BrokerManager:
    def __init__(self, config):
        self.config = config
        self.brokerName = None
        self.qmf        = None
        self.broker     = None
        self.brokers    = []

    def SetBroker(self, brokerUrl):
        self.url = brokerUrl
        self.qmf = Session()
        self.broker = self.qmf.addBroker(brokerUrl, self.config._connTimeout)
        agents = self.qmf.getAgents()
        for a in agents:
            if a.getAgentBank() == '0':
                self.brokerAgent = a

    def Disconnect(self):
        """ Release any allocated brokers.  Ignore any failures as the tool is
        shutting down.
        """
        try:
            if self.broker:
                self.qmf.delBroker(self.broker)
                self.broker = None
            while len(self.brokers):
                b = self.brokers.pop()
                self.qmf.delBroker(b)
        except:
            pass

    def _getClusters(self):
        packages = self.qmf.getPackages()
        if "org.apache.qpid.cluster" not in packages:
            raise Exception("Clustering is not installed on the broker.")

        clusters = self.qmf.getObjects(_class="cluster", _agent=self.brokerAgent)
        if len(clusters) == 0:
            raise Exception("Clustering is installed but not enabled on the broker.")

        return clusters

    def _getHostList(self, urlList):
        hosts = []
        hostAddr = IpAddr(self.config._host)
        for url in urlList:
            if url.find("amqp:") != 0:
                raise Exception("Invalid URL 1")
            url = url[5:]
            addrs = str(url).split(",")
            addrList = []
            for addr in addrs:
                tokens = addr.split(":")
                if len(tokens) != 3:
                    raise Exception("Invalid URL 2")
                addrList.append((tokens[1], tokens[2]))

            # Find the address in the list that is most likely to be in the same subnet as the address
            # with which we made the original QMF connection.  This increases the probability that we will
            # be able to reach the cluster member.

            best = hostAddr.bestAddr(addrList)
            bestUrl = best[0] + ":" + best[1]
            hosts.append(bestUrl)
        return hosts

    def overview(self):
        clusters = self._getClusters()
        cluster = clusters[0]
        memberList = cluster.members.split(";")
        idList = cluster.memberIDs.split(";")

        print "  Cluster Name: %s" % cluster.clusterName
        print "Cluster Status: %s" % cluster.status
        print "  Cluster Size: %d" % cluster.clusterSize
        print "       Members: ID=%s URL=%s" % (idList[0], memberList[0])
        for idx in range(1,len(idList)):
            print "              : ID=%s URL=%s" % (idList[idx], memberList[idx])

    def stopMember(self, id):
        clusters = self._getClusters()
        cluster = clusters[0]
        idList = cluster.memberIDs.split(";")
        if id not in idList:
            raise Exception("No member with matching ID found")

        if not self.config._force:
            prompt = "Warning: "
            if len(idList) == 1:
                prompt += "This command will shut down the last running cluster member."
            else:
                prompt += "This command will shut down a cluster member."
            prompt += " Are you sure? [N]: "

            confirm = raw_input(prompt)
            if len(confirm) == 0 or confirm[0].upper() != 'Y':
                raise Exception("Operation canceled")

        cluster.stopClusterNode(id)

    def stopAll(self):
        clusters = self._getClusters()
        if not self.config._force:
            prompt = "Warning: This command will shut down the entire cluster."
            prompt += " Are you sure? [N]: "

            confirm = raw_input(prompt)
            if len(confirm) == 0 or confirm[0].upper() != 'Y':
                raise Exception("Operation canceled")

        cluster = clusters[0]
        cluster.stopFullCluster()

    def showConnections(self):
        clusters = self._getClusters()
        cluster = clusters[0]
        memberList = cluster.members.split(";")
        idList = cluster.memberIDs.split(";")
        displayList = []
        hostList = self._getHostList(memberList)
        self.qmf.delBroker(self.broker)
        self.broker = None

        idx = 0
        for host in hostList:
            if self.config._showConn == "all" or self.config._showConn == idList[idx] or self.config._delConn:
                self.brokers.append(self.qmf.addBroker(host, self.config._connTimeout))
                displayList.append(idList[idx])
            idx += 1

        idx = 0
        found = False
        for broker in self.brokers:
            if not self.config._delConn:
                print "Clients on Member: ID=%s:" % displayList[idx]
            connList = self.qmf.getObjects(_class="connection", _package="org.apache.qpid.broker", _broker=broker)
            for conn in connList:
                if not conn.shadow:
                    if self.config._numeric or self.config._delConn:
                        a = conn.address
                    else:
                        tokens = conn.address.split(":")
                        try:
                            hostList = socket.gethostbyaddr(tokens[0])
                            host = hostList[0]
                        except:
                            host = tokens[0]
                        a = host + ":" + tokens[1]
                    if self.config._delConn:
                        tokens = self.config._delConn.split(":")
                        ip = socket.gethostbyname(tokens[0])
                        toDelete = ip + ":" + tokens[1]
                        if a == toDelete:
                            print "Closing connection from client: %s" % a
                            conn.close()
                            found = True
                    else:
                        print "    %s" % a
            idx += 1
            if not self.config._delConn:
                print
        if self.config._delConn and not found:
            print "Client connection '%s' not found" % self.config._delConn

        while len(self.brokers):
            broker = self.brokers.pop()
            self.qmf.delBroker(broker)

def main(argv=None):

    try:
        config = Config()

        parser = OptionParser(usage="usage: %prog [options] BROKER",
                      description="Example: $ qpid-cluster -C  broker-host:10000")

        parser.add_option("-t", "--timeout", action="store", type="int", default=10, metavar="SECS", help="Maximum time to wait for broker connection (in seconds)")
        parser.add_option("-C", "--all-connections", action="store_true", default=False, help="View client connections to all cluster members")
        parser.add_option("-c", "--connections",  metavar="ID", help="View client connections to specified member")
        parser.add_option("-d", "--del-connection",  metavar="HOST:PORT", help="Disconnect a client connection")
        parser.add_option("-s", "--stop",  metavar="ID", help="Stop one member of the cluster by its ID")
        parser.add_option("-k", "--all-stop", action="store_true", default=False, help="Shut down the whole cluster")
        parser.add_option("-f", "--force", action="store_true", default=False, help="Suppress the 'are you sure' prompt")
        parser.add_option("-n", "--numeric", action="store_true", default=False, help="Don't resolve names")
	
        opts, args = parser.parse_args(args=argv)

        if args:
            config._host = args[0]

        if opts.timeout != 0:
            config._connTimeout = opts.timeout
        else:
            config._connTimeout = None

        if opts.all_connections:
            config._showConn = "all"

        if opts.connections:
            config._connections = opts.connections
            if len(config._connections.split(":")) != 2:
                parser.error("Member ID must be of form: <host or ip>:<number>")

        if opts.del_connection:
            config._delConn = opts.del_connection
            if len(config._delConn.split(":")) != 2: 
                parser.error("Member ID must be of form: <host or ip>:<number>")

        if opts.stop:
            config._stopID = opts.stop
            if len(config._stopId.split(":")) != 2: 
                parser.error("Member ID must be of form: <host or ip>:<number>")

        config._stopAll = opts.all_stop
        config._force = opts.force
        config._numeric = opts.numeric

        bm    = BrokerManager(config)

        try:
            bm.SetBroker(config._host)
            if config._stopId:
                bm.stopMember(config._stopId)
            elif config._stopAll:
                bm.stopAll()
            elif config._showConn or config._delConn:
                bm.showConnections()
            else:
                bm.overview()
        except KeyboardInterrupt:
            print
        except Exception,e:
            bm.Disconnect()   # try to deallocate brokers - ignores errors
            if str(e).find("connection aborted") > 0:
                # we expect this when asking the connected broker to shut down
                return 0
            raise Exception("Failed: %s - %s" % (e.__class__.__name__, e))

        bm.Disconnect()
    except Exception, e:
        print str(e)
        return 1

if __name__ == "__main__":
        sys.exit(main())