From e5df95069aa02cd841aab885ae1352c5fd1fa246 Mon Sep 17 00:00:00 2001 From: Jonathan Robie Date: Wed, 22 Dec 2010 22:22:13 +0000 Subject: Added logging to QMF console connections. Warning if a broker can not be found, error if SASL authentication fails or other connection errors when connecting to an existing broker. Default log level is ERROR. qpid-printevents allows the log level to be set. It also allows the user to specify that a connection is required, in which case it terminates if a connection can not be established. Examples: $ ./qpid-printevents --sasl-mechanism PLAIN nonexistent-broker => Not an error. Waits for the broker to be started. $ ./qpid-printevents --sasl-mechanism PLAIN localhost 2010-12-22 17:07:18,365 ERROR Could not connect to broker localhost:5672 (None, 'No acceptable SASL authentication mechanism available') => Connection error condition in output - SASL authentication failed because user name and password are not supplied. But qpid-printevents keeps running, waiting for you to start the broker. $ ./qpid-printevents --sasl-mechanism PLAIN --log-level critical => Connection error condition in output - SASL authentication failed because user name and password are not supplied. No output in this case, because the log level has been set to critical. $ ./qpid-printevents --sasl-mechanism PLAIN --require-connection localhost 2010-12-22 17:11:03,791 ERROR Could not connect to broker localhost:5672 (None, 'No acceptable SASL authentication mechanism available') Failed: ConnectionFailed - (None, 'No acceptable SASL authentication mechanism available') => Connection error condition. qpid-printevents terminates because --require-connection was specified. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1052086 13f79535-47bb-0310-9956-ffa450edef68 --- extras/qmf/src/py/qmf/console.py | 12 ++++++++++-- tools/src/py/qpid-config | 8 ++++++-- tools/src/py/qpid-printevents | 36 +++++++++++++++++++++++++++++++----- tools/src/py/qpid-stat | 2 +- tools/src/py/qpid-tool | 4 ++++ 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/extras/qmf/src/py/qmf/console.py b/extras/qmf/src/py/qmf/console.py index 15e12fbb6b..319cc97d89 100644 --- a/extras/qmf/src/py/qmf/console.py +++ b/extras/qmf/src/py/qmf/console.py @@ -39,8 +39,8 @@ from Queue import Queue, Empty from time import time, strftime, gmtime, sleep from cStringIO import StringIO -#import qpid.log -#qpid.log.enable(name="qpid.io.cmd", level=qpid.log.DEBUG) +import qpid.log +qpid.log.enable(name="qpid.qmf") #=================================================================================================== # CONSOLE @@ -2509,6 +2509,14 @@ class Broker(Thread): except Exception, e: self.error = "Exception during connection setup: %s - %s" % (e.__class__.__name__, e) + + log = qpid.log.getLogger("qpid.qmf") + + if e[0] == -2: + log.warning("Could not connect to broker " + self.host + ":" + str(self.port) + " " + str(e)) + else: + log.error("Could not connect to broker " + self.host + ":" + str(self.port) + " " + str(e)) + self.conn_exc = e return False # connection failed diff --git a/tools/src/py/qpid-config b/tools/src/py/qpid-config index 6e3bafd49d..c8641615f8 100755 --- a/tools/src/py/qpid-config +++ b/tools/src/py/qpid-config @@ -24,6 +24,7 @@ from optparse import OptionParser, OptionGroup, IndentedHelpFormatter import sys import locale from qmf.console import Session +import qpid.log class Config: def __init__(self): @@ -546,6 +547,9 @@ def YN(bool): def main(argv=None): + + qpid.log.getLogger("qpid.qmf").setLevel(qpid.log.ERROR) + args = OptionsAndArguments(argv) bm = BrokerManager() @@ -591,13 +595,13 @@ def main(argv=None): except KeyboardInterrupt: print except IOError, e: - print e + print "Failed: %s: %s" % (e.__class__.__name__, e) bm.Disconnect() return 1 except SystemExit, e: bm.Disconnect() return 1 - except Exception,e: + except Exception, e: if e.__class__.__name__ != "Timeout": # ignore Timeout exception, handle in the loop below print "Failed: %s: %s" % (e.__class__.__name__, e) diff --git a/tools/src/py/qpid-printevents b/tools/src/py/qpid-printevents index 8c9649c4aa..4a225c69d8 100755 --- a/tools/src/py/qpid-printevents +++ b/tools/src/py/qpid-printevents @@ -26,7 +26,7 @@ import sys import socket from time import time, strftime, gmtime, sleep from qmf.console import Console, Session - +import qpid.log class EventConsole(Console): def event(self, broker, event): @@ -75,26 +75,52 @@ $ %prog guest/guest@broker-host:10000 """ def main(argv=None): + p = optparse.OptionParser(usage=_usage, description=_description, formatter=JHelpFormatter()) p.add_option("--heartbeats", action="store_true", default=False, help="Use heartbeats.") p.add_option("--sasl-mechanism", action="store", choices=["EXTERNAL","ANONYMOUS","PLAIN","CRAM-MD5","DIGEST-MD5","GSSAPI"], metavar="", help="SASL mechanism for authentication. SASL automatically picks the most secure available mechanism - use this option to override.") + p.add_option("--require-connection", action="store_true", help="Raise error if connection can not be established. By default, retries connections that could not be established, which allows events to be printed as brokers start and stop.") + p.add_option("--log-level", action="store", choices=["debug", "info", "warn", "error", "critical"], metavar="", help="Logging level for connections") + options, arguments = p.parse_args(args=argv) if len(arguments) == 0: arguments.append("localhost") + LOG_LEVELS = {'debug': qpid.log.DEBUG, + 'info': qpid.log.INFO, + 'warn': qpid.log.WARN, + 'error': qpid.log.ERROR, + 'critical': qpid.log.CRITICAL} + + if options.require_connection: + manageConnections=False + else: + manageConnections=True + + if options.log_level: + qpid.log.getLogger("qpid.qmf").setLevel(LOG_LEVELS[options.log_level]) + else: + qpid.log.getLogger("qpid.qmf").setLevel(qpid.log.ERROR) + console = EventConsole() - session = Session(console, rcvObjects=False, rcvHeartbeats=options.heartbeats, manageConnections=True) + session = Session(console, rcvObjects=False, rcvHeartbeats=options.heartbeats, manageConnections=manageConnections) brokers = [] try: for host in arguments: brokers.append(session.addBroker(host, None, options.sasl_mechanism)) - try: + while (True): sleep(10) - except KeyboardInterrupt: + + except KeyboardInterrupt: print - sys.exit(0) + return 0 + + except Exception, e: + print "Failed: %s - %s" % (e.__class__.__name__, e) + return 1 + finally: while len(brokers): b = brokers.pop() diff --git a/tools/src/py/qpid-stat b/tools/src/py/qpid-stat index 2a781ad0ef..9061711987 100755 --- a/tools/src/py/qpid-stat +++ b/tools/src/py/qpid-stat @@ -511,7 +511,7 @@ def main(argv=None): return 0 except KeyboardInterrupt: print - except Exception,e: + except Exception, e: print "Failed: %s - %s" % (e.__class__.__name__, e) bm.Disconnect() # try to deallocate brokers diff --git a/tools/src/py/qpid-tool b/tools/src/py/qpid-tool index e7c0231b96..e5f5657dff 100755 --- a/tools/src/py/qpid-tool +++ b/tools/src/py/qpid-tool @@ -30,6 +30,7 @@ from time import strftime, gmtime from qpid.disp import Display from qpid.peer import Closed from qmf.console import Session, Console, SchemaClass, ObjectId +import qpid.log class Mcli(Cmd): """ Management Command Interpreter """ @@ -677,6 +678,8 @@ def Usage(): # Main Program #========================================================= +qpid.log.getLogger("qpid.qmf").setLevel(qpid.log.ERROR) + # Get host name and port if specified on the command line cargs = sys.argv[1:] _host = "localhost" @@ -692,6 +695,7 @@ if _host[0] == '-': disp = Display() + # Attempt to make a connection to the target broker try: data = QmfData(disp, _host) -- cgit v1.2.1