Can't see the forest for the trees - when reading file, only processing first line

News arobert at townisp.com
Thu Apr 13 13:12:37 EDT 2006


Hi Everyone,


The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.

The program functions when it gets options from the command line.

It also works when pulling the options from a file.

My issue is that it only processes the first line of the file.

Does anyone know why this might be the case?

The use_file() function should be returning each file line.

Any help you can provide on this would be greatly appreciated.

Thanks


#!/usr/bin/python
#
# Programmer: Andrew Robert
#
# Function: Generic method to extract information from a queue manager
#           This script is intended to be run from the command line to
return values
#           based on supplied keys
#
#           Depending on whether the -d command switch is used or not,
the output of this script
#           will go to the screen or be e-mailed
#
#           The e-mailer function included in this program is set use
the generic smtp library
#           instead of sendmail or procmail.
#
#           This is a design decision since the program is intended
primarily to run
#           on a Windows platform.
#
#


#
#
#
#
def usage():
	print
"""
usage: test_orig.py [options]

options:
  -h, --help            show this help message and exit
  -mQMANAGER, --qmanager=QMANAGER
                                        Queue Manager to inquire against
  -sHOST, --server=HOST
                                        Host the que manager resides on
  -cCHECK, --check=CHECK
                                        Test object if it is
less/equal/greater
  -pPORT, --port=PORT                   Port queue manager listens on
  -oOBJECT, --object=OBJECT
                                        Queue object being inquired on
  -kKEY, --key=KEY                      object attribute to be inquired
about
  -tMTO, --to=MTO                       e-mail address the report will go to
  -q, --quiet                           optional - just returns the value
  -fFILE, --file=FILE   Pull command strings from FILE
  -d, --display                         optional - use sends output to
e-mail
 """


#
# Trap signal interrupts if they occur
#
def signal_handler(signal, frame):
        print 'Signal trapped %s' % signal
        sys.exit(0)


#
# Read a file into a list
#
def use_file():
	myfile = open(options.filename)
	while True:
		line=myfile.readline()
		print line
		if len(line) == 0:
	                myfile.close()
			break
		else:
			return line
	myfile.close()	
		


def check_params():
    if options.report is not None:
	    if options.mto is None:
		    print "\nAsked to send via e-mail but no destination address supplied"
		    usage()
		    sys.exit(1)
    if options.key is None:
	    print"\n\nPlease enter a valid key to inquire upon.\n\nPlease check
your options and try again"
	    usage()
	    sys.exit(1)

    if ( options.port <="1414" ) or ( options.port >="1419" ):
	    print "\n\nAn invalid port number was used. \nValid port numbers
are 1415,1416,1417, and 1418."
	    print "\nPlease check the port number and try again"
	    usage()
	    sys.exit(1)

#
#
# Test return value
#
def testval(inc,value):
	vallen = int(value)
	if vallen > inc :
                return "\nless than test value\n"
	elif vallen < inc :
		return "\nexceeds test value\n"
	else:
		return "\nequal to test value\n"


# Validate e-mail addresses based on lexical rules set forth in RFC 822
# Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by
# John Viega and Matt Messier (O'Reilly 2003)
#
# If the supplied email address is syntactically valid, isAddressValid()
# will return 1; otherwise, it will return 0.
#
def isAddressValid(addr):
	#	
        # First we validate the name portion (name at domain)
        #
        c = 0
        while c < len(addr):
            if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c
- 1] == '"'):
                c = c + 1
                while c < len(addr):
                    if addr[c] == '"': break
                    if addr[c] == '\\' and addr[c + 1] == ' ':
                        c = c + 2
                        continue
                    if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0
                    c = c + 1
                else: return 0
                if addr[c] == '@': break
                if addr[c] != '.': return 0
                c = c + 1
                continue
            if addr[c] == '@': break
            if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
            if addr[c] in rfc822_specials: return 0
            c = c + 1
        if not c or addr[c - 1] == '.': return 0

        # Next we validate the domain portion (name at domain)
        domain = c = c + 1
        if domain >= len(addr): return 0
        count = 0
        while c < len(addr):
            if addr[c] == '.':
                if c == domain or addr[c - 1] == '.': return 0
                count = count + 1
            if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
            if addr[c] in rfc822_specials: return 0
            c = c + 1

        return count >= 1

#
# Test user supplied keys to ensure they are valid
#
def test_valid_keys(key):
    if valid_keys.has_key(key):
	    return
    else:
	    print '\n\t\t\tInvalid key',key,'\n\n\n'
	    usage()
	    sys.exit(1)

#
# Test user supplied keys to ensure they are numeric
#
def test_numeric(key):
    if numeric_keys.has_key(key):
	    return
    else:
	    print '\n\t\t\tString values cannot be numerically tested',key,'\n\n\n'
	    usage()
	    sys.exit(1)

#
# SMTP mailer function
#
def mail(serverURL=None, sender='', to='', subject='', text=''):
    import smtplib

    """
    Usage:
    mail('my.mailserver.com', 'sourceaddress at wherever.com',
'targetaddress at wherever.com', 'test', 'This is a test')
    """

    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
    message = headers + text
    mailServer = smtplib.SMTP(serverURL)
    check_valid=isAddressValid(to)
    if check_valid==1 :
	    mailServer.sendmail(sender, to, message)
	    mailServer.quit()
    else:
	    print "\n\nDestination e-mail address %s is invalid. \n\n\tPlease
check and run again" % to
	    usage()
	    sys.exit(1)


def lookup( qmgr=' ', command='', object=''):
        import pymqi, pymqe, CMQC
	#
	# Connect to remote queue manager via client connection
        #
	m        = pymqi.QueueManager(None)
	try:
		m.connectTCPClient(qmgr,pymqi.cd(),"SYSTEM.ADMIN.SVRCONN",command)
        except pymqi.MQMIError, a:
		print '\n\nScript error detected:\n', a
		sys.exit(1)	

        #
        # Inquire about the passed queue object and return value
        #
        q   = pymqi.Queue(m, object)
        try:
                arg = 'q.inquire(CMQC.'+valid_keys[vt[count]]+')'	
		return eval(arg)
        except pymqi.MQMIError, a:
		print '\n\nScript error detected:\n', a
		sys.exit(1)	


if __name__ == '__main__':

    import sys, string, exceptions, signal
    from optparse import OptionParser

    #
    # Mail gateway and rfc 822 variables
    #
    mserver  = 'harrier.mfs.com'
    mfrom    = 'dlmqsupport at mfs.com'
    rfc822_specials = '()<>@,;:\\"[]'
    count    =0
    text     =""

    #
    # Setup signal handlers to detect if program is
    # aborted, terminated, or Ctrl-C'ed out of
    #
    signal.signal(signal.SIGABRT,signal_handler)
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM,signal_handler)

    #
    # Parse command line options and automatically build help/usage display
    #
    parser = OptionParser()
    parser.add_option("-m","--qmanager", dest="qmanager",
                    help="\t\tQueue Manager to inquire against"),
    parser.add_option("-s","--server", dest="host",
                    help="\t\tHost the que manager resides on"),
    parser.add_option("-c","--check",  dest="check",
                    help="\t\tTest object if it is less/equal/greater"),
    parser.add_option("-p","--port", type="string", dest="port",
                    help="\t\tPort queue manager listens on"),
    parser.add_option("-o","--object", dest="object",
                    help="\t\tQueue object being inquired on"),
    parser.add_option("-k", "--key",dest="key",
                    help="\t\tobject attribute to be inquired about"),
    parser.add_option("-t","--to", type="string",dest="mto",
                    help="\t\te-mail address the report will go to"),
    parser.add_option("-q","--quiet", action="store_false",dest="quiet",
                    help="\t\toptional - just returns the value"),
    parser.add_option("-f", "--file",
                    action="store", type="string", dest="filename",
                    help="Pull command strings from FILE", metavar="FILE")
    parser.add_option("-d","--display", action="store_false",dest="report",
                    help="\t\toptional - use sends output to e-mail")
    (options, args) = parser.parse_args()

    if options.filename is not None:
	    line = use_file()
	    (options,args) = parser.parse_args(line.split())
	    check_params()
    else:
	    check_params()
	
    #
    # Key dictionary
    #
    valid_keys={'AlterationDate':'MQCA_ALTERATION_DATE',
    'AlterationTime':'MQCA_ALTERATION_TIME',
    'BackoutRequeueName':'MQCA_BACKOUT_REQ_Q_NAME',
    'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
    'BaseQName':'MQCA_BASE_Q_NAME',
    'ClusterDate':'MQCA_CLUSTER_DATE',
    'ClusterName':'MQCA_CLUSTER_NAME',
    'ClusterNamelist':'MQCA_CLUSTER_NAMELIST',
    'ClusterQType':'MQIA_CLUSTER_Q_TYPE',
    'ClusterTime':'MQCA_CLUSTER_TIME',
    'CreationDate':'MQCA_CREATION_DATE',
    'CreationTime':'MQCA_CREATION_TIME',
    'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
    'DefBind':'MQIA_DEF_BIND',
    'DefInputOpenOption':'MQIA_DEF_INPUT_OPEN_OPTION',
    'DefPersistence':'MQIA_DEF_PERSISTENCE',
    'DefPriority':'MQIA_DEF_PRIORITY',
    'DefinitionType':'MQIA_DEFINITION_TYPE',
    'DistLists':'MQIA_DIST_LISTS',
    'HardenGetBackout':'MQIA_HARDEN_GET_BACKOUT',
    'HighQDepth':'MQIA_HIGH_Q_DEPTH',
    'InhibitGet':'MQIA_INHIBIT_GET',
    'InhibitPut':'MQIA_INHIBIT_PUT',
    'InitiationQName':'MQCA_INITIATION_Q_NAME',
    'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
    'MaxQDepth':'MQIA_MAX_Q_DEPTH',
    'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE',
    'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
    'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
    'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
    'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
    'ProcessName':'MQCA_PROCESS_NAME',
    'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
    'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
    'QDepthLowEvent':'MQIA_Q_DEPTH_LOW_EVENT',
    'QDepthLowLimit':'MQIA_Q_DEPTH_LOW_LIMIT',
    'QDepthMaxEvent':'MQIA_Q_DEPTH_MAX_EVENT',
    'QDesc':'MQCA_Q_DESC',
    'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',
    'QMgrName':'MQCA_CLUSTER_Q_MGR_NAME',
    'QName':'MQCA_Q_NAME',
    'QNames':'MQCACF_Q_NAMES',
    'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
    'QServiceIntervalEvent':'MQIA_Q_SERVICE_INTERVAL_EVENT',
    'QType':'MQIA_Q_TYPE',
    'RemoteQMgrName':'MQCA_REMOTE_Q_MGR_NAME',
    'RemoteQName':'MQCA_REMOTE_Q_NAME',
    'RetentionInterval':'MQIA_RETENTION_INTERVAL',
    'Scope':'MQIA_SCOPE',
    'Shareability':'MQIA_SHAREABILITY',
    'TimeSinceReset':'MQIA_TIME_SINCE_RESET',
    'TriggerControl':'MQIA_TRIGGER_CONTROL',
    'TriggerData':'MQCA_TRIGGER_DATA',
    'TriggerDepth':'MQIA_TRIGGER_DEPTH',
    'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY',
    'TriggerType':'MQIA_TRIGGER_TYPE',
    'Usage':'MQIA_USAGE',
    'XmitQName':'MQCA_XMIT_Q_NAME'}

    #
    # Keys that can be numerically compared against
    #
    numeric_keys={'AlterationDate':'MQCA_ALTERATION_DATE',
    'AlterationTime':'MQCA_ALTERATION_TIME',
    'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
    'DefPriority':'MQIA_DEF_PRIORITY',
    'ClusterDate':'MQCA_CLUSTER_DATE',
    'ClusterTime':'MQCA_CLUSTER_TIME',
    'CreationDate':'MQCA_CREATION_DATE',
    'CreationTime':'MQCA_CREATION_TIME',
    'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
    'HighQDepth':'MQIA_HIGH_Q_DEPTH',
    'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
    'MaxQDepth':'MQIA_MAX_Q_DEPTH',
    'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE',
    'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
    'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
    'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
    'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
    'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
    'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
    'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',	
    'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
    'TriggerDepth':'MQIA_TRIGGER_DEPTH',
    'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY'}


    host_port  = "%s(%s)" % (options.host.lower(), options.port)
    vt = options.key.split(',')

    for i in vt:
	    test_valid_keys(vt[count])
	    inq = lookup( options.qmanager.upper(), host_port, options.object)
	    if options.quiet is False:
		    text=text+str(inq)
	    else:
		    text = text+"\n%s for %s object %s is %s" % ( vt[count],
options.qmanager.upper(), options.object,inq)

            if options.check is not None:
		    ct = options.check.split(",")
		    test_numeric(vt[count])
		    value_test=testval(inq,ct[count])
		    text=text+" "+value_test
	    count=count+1


    if   options.report is None:
	    print text
	    sys.exit(0)
    else:
	    subject = "Report for queue manager %s on server %s " % (
options.qmanager.upper() , options.host.lower() )
	    mail( mserver, mfrom, options.mto, subject, text )
	    sys.exit(0)



More information about the Python-list mailing list