Search This Blog

Oracle Fusion Middleware Administration Course

Oracle Fusion Middleware Administration Course
OFMW Admin Course

Thursday, December 25, 2014

File IO in WLST: How to store output of WLST/Jython?

Hey WLST scripting enthos Guys!!

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!!
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.
AttributeDescription
file.closedReturns true if file is closed, false otherwise.
file.modeReturns access mode with which file was opened.
file.nameReturns 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 !!!

7 comments:

  1. Hi Pavan,

    i want to store the below script output into excel file. But i couldn't able to do it. Please help me here.

    #=======================================================
    # This script will monitor the JDBC CONNECTION POOL
    #=======================================================
    connect('user', 'pwd' ,'t3://localhost:7001')
    try:
    poolrtlist=adminHome.getMBeansByType('JDBCConnectionPoolRuntime')
    print ' '
    print ' '
    print 'JDBC CONNECTION POOLS'
    print ' '
    print 'Name Max Active Active WaitSecs Waiting State'
    print ' capacity Current HighCnt HighCnt Count'
    for poolRT in poolrtlist:
    pname = poolRT.getName()
    pmaxcapacity = poolRT.getAttribute("MaxCapacity")
    paccc = poolRT.getAttribute("ActiveConnectionsCurrentCount")
    pachc = poolRT.getAttribute("ActiveConnectionsHighCount")
    pwshc = poolRT.getAttribute("WaitSecondsHighCount")
    pwfccc = poolRT.getAttribute("WaitingForConnectionCurrentCount")
    pstate = poolRT.getAttribute("State")
    print '%10s %7d %7d %7d %7d %7d %10s' % (pname,pmaxcapacity,paccc,pachc, pwshc,pwfccc,pstate)
    print ' '
    redirect('./jdbc.xls')
    except:
    print 'Error:'
    dumpStack()
    pass
    disconnect()

    ReplyDelete
  2. Hi

    Use file pointer (f) ...
    before print string.

    print '%10s %7d %7d %7d %7d %7d %10s' % (pname,pmaxcapacity,paccc,pachc, pwshc,pwfccc,pstate)

    Converting a csv file to excel is already written by Sujit Pal.

    http://sujitpal.blogspot.com/2007/02/python-script-to-convert-csv-files-to.html
    For any doubt reply/mail

    ReplyDelete
  3. Hi Pavan,

    I was trying to create csv file for the datasource details that I fetched from the Weblogic Server like ds name, targets, db url, etc. I did following:-

    fileWriter=open('DatasourceDetails.csv','wb')
    print>>fileWriter, 'Data Source',',','Target',',','DB URL'

    and iterated over datasources and wrote print>>fileWriter, dsName,',',targetServerList,',',dbURL

    But, I do not find this method a clean one for creating csv file. And I think WLST doesn't supports csv module available with Python/Jython.

    ReplyDelete
  4. hi pavan sir,

    actually i wanna store the output of status server script to csv file in another datasource , how to do it? can u pls help.
    IF U PROVIDE ANY SAMPLE SCRIPT WILL BE VERY HELPFUL
    thanks in advance

    ReplyDelete
  5. @ Zia,

    You can get the server state using the http://wlstbyexamples.blogspot.in/2010/02/server-state-using-wlst.html post. The output you can customize according to your needs then you can move the stats into the file using fileObject using write or print methods explained in the above post. Hope you will try it out and share your experience.

    ReplyDelete
  6. Hello Sir,
    I want to get all the user partitions including "default" under the soa_infra and store it to a variable. Please let me know how it can be done using jython through WLST. I am using weblogic 10.3.6.
    Thanks in advance.

    ReplyDelete
  7. BlueHost is definitely the best hosting company for any hosting services you might need.

    ReplyDelete

Please write your comment here

Popular Posts

Oracle WebLogic Administration Course

Oracle WebLogic Administration Course
WebLogic Training

Followers