I was prepared some of industry most reffered WLST monitoring scripts... they are running well and good... Now the need is to store the result of this monitoring script into a FILE. How to make this? how to redirect this output... normally output is coming from print command in the jython/python. I was searching in google for this output storing to a file from WLST not really good outcomes... then I thought better I choose file style as in C programming allows Standard Output to a files with file pointers. This found a jython tutorial which is already available in online...
![]() |
WLST File input output operations |
Standard Out and Error
Standard output and standard error (commonly abbreviated stdout and stderr) are pipes that are built into every unix-like platform. The stdout (fileObject) is defined in the Python standard sys module, and it is a stream object. Calling its write() function which will print out whatever string you pass as argument to it, then return the length of the output. In fact, this is what the print function really does; it adds a carriage return to the end of the string that you’re printing, and calls sys.stdout.write function.
############################################## # This program is to illustrate sys fileojects # Standard Output stdout # Standard Error stderr ############################################## print "Normal print..." for i in range(3): print "WLST is awesome!!!" import sys print "sys.stdout...." for i in range(5): l=sys.stdout.write('WLST By Examples\n') print "sys.stderr...." for i in range(4): errline=sys.stderr.write('WLST in 12.1.3 Wonderful!! ')Here it shows the difference of print, write function with/without carriage return example.
$ wlst fileObj_sys.py Initializing WebLogic Scripting Tool (WLST) ... Welcome to WebLogic Server Administration Scripting Shell Type help() for help on available commands Normal print... WLST is awesome!!! WLST is awesome!!! WLST is awesome!!! sys.stdout.... WLST By Examples WLST By Examples WLST By Examples WLST By Examples WLST By Examples sys.stderr.... WLST in 12.1.3 Wonderful!! WLST in 12.1.3 Wonderful!! WLST in 12.1.3 Wonderful!!In the simplest case, sys.stdout and sys.stderr send their output to the same place. In Python, standard output, standard error does not add carriage returns for you. If you want carriage returns. you’ll need to write manually include the carriage return characters. sys.stdout and sys.stderr are stream objects, but they are write-only. Here important fact is that when you attempting to call their read() method will always raise an IOError.
Redirecting output with file descriptor is fine worked good...
now one more thing... need a timestamp for everytime when you run the monitoring script...
Solution again available in the same tutorial...
jython is going to support Java internally... Java's Date utility gives you the facility to have a time stamp redirect this print to the same file descriptor ... your job is done!!
How do I test a simple Jython script at Jython prompt?
You can use the following lines to invoke Jython Shell on your WebLogic environment. This is possible because WLST is based on Jython Shell, obviously you can get a Jython prompt in the WebLogic environment.
Normally you can get with
$ java org.python.util.jython
Otherwise use the following way to skip caching:
$ java -Dpython.cachedir.skip=true org.python.util.jython
fileObj = open('test.txt', 'w') writeList=["I am an Indian", "I love my India", \ "I want to go to USA", "I want to earn dollars", \ "I want to die in India"] for i in writeList: print >>fileObj, i fileObj.close() # Default file mode is read (r) fileObj = open('test.txt') # We can move fileObj position here it is 0 fileObj.seek(0) for lines in fileObj.readlines(): print lines fileObj.close()Creates test.txt with lines in list and then will read lines from same file.
wls:/offline> execfile('filereadWrite.py') I am an Indian I love my India I want to go to USA I want to earn dollars I want to die in India
The file attributes
Following table have the list of the file object attributes. Which we can directly access them public.Attribute | Description |
---|---|
file.closed | Returns true if file is closed, false otherwise. |
file.mode | Returns access mode with which file was opened. |
file.name | Returns name of the file. |
# This program illustrates the file attributes f=open('fileattr.py') print "mode:",f.mode print "name:",f.name if f.closed: print f.name, " is closed" else: print f.name," is not closed"Its execution gives the output as follows
wls:/offline> execfile('fileattr.py') mode: r name: fileattr.py fileattr.py is not closed
Testing
with-as operation on Files
Following example shows how to use with
How do I test a simple Jython script at Jython prompt?
You can use the following lines to invoke Jython Shell on your WebLogic environment. This is possible because WLST is based on Jython Shell, obviously you can get a Jython prompt in the WebLogic environment. Normally you can get with
$ java org.python.util.jython
Otherwise use the following way to skip caching:
$ java -Dpython.cachedir.skip=true org.python.util.jython
Remember that, WebLogic 9.2 supports Jython 2.1 featured shell, The new Oracle Fusion middleware, WebLogic 11g supports Jython 2.2 features.
In many capacity planning analysis you need the statistcs of day or hour by hour etc., These statistics can be for JVM monitoring or for JMS or JDBC DataSource or Thread Count of a WebLogic domain.
Simply printing to standard output you can redirect them to your desired filenames which you specify in the Jython script. To do print to a file use:
f = open("JVMStat.txt", "w") # JVM monitoasring Script loop print >>f, "JVM Status Line here" f.close()And to add to the end of a existing file use:
f = open("JVMStat.txt", "a") print >>f, "Added lines comes here" f.close()
Hope this will give you hint for your need in WLST and file usage. For any issue keep writing back to me.
Enjoy with Jython!!! in WLST !!!