Search This Blog

Sunday, November 18, 2012

Input and Output in WLST

In WLST we can have same kind of command line inputs and outputs as in Python and Jython languages. This can be useful when it is required to prepare a huge monitoring report using WLST or when you work for hundreds of servers in a domain such as in cloud where Middleware as a service (MAAS) and you need to find an automation task with WLST scripts.

Inputs in WLST

To read simple string of values we can use raw_input() built in function or sys.stdin.readline() method. For reading different data types such as number values we can use input() function, which internally calls raw_input() function and apply eval() to convert the input value to number values.
wls:/offline> x=raw_input('Enter a number:')
Enter a number:4009
wls:/offline> print x*2
40094009
wls:/offline> y=input('Enter a number: ')
Enter a number: 400
wls:/offline> print y*3
1200
Let’s experiment with Boolean type of variable in WLST
wls:/offline> b=input('Do you love WLST?')
Do you love WLST?True
wls:/offline> print b
1
wls:/offline> b=input('Do you love WLST?')
Do you love WLST?False
wls:/offline> print b
0
Here you enter as True or False but, the Boolean variables are stores on WLST SHELL as either zero or one. This is somewhat confusing you with regular Boolean types in other languages or UNIX it is opposite. One represents True value, zero represents False. So take care while you scripting for the Boolean type variables.
When you have the option to select the data in string format then go for raw_input() built-in function. This is much better way when situation demands your script to enter name of the WebLogic server instance or some questions to repeat the script task. On the other hand input() will be good option when you have to select multiple options for monitoring geographical sites to choose one of them with a numbered input.

Reading variable in WLST with stdin

We have one more option to read the values from the WLST command prompt that is using sys module. We can use sys.stdin.readline() method to read the input and assign it to a variable. This will fetches only text data in to the variable, so you use this when you want to input the strings.
wls:/offline> serv=sys.stdin.readline()
managed3_server
wls:/offline> serv
'managed3_server\n'
wls:/offline> print serv
managed3_server

Output in WLST

An output can be displayed using print command as we have seen many statements in WLST already. Although, we cover few basics of print statement, which accepts string as an argument.
  • Simple string to display
    print ‘Welcome to WLST!!!’
    
  • To display the string and variables combinations in Java Style concatenation that is using ‘+’ operator
    print  ‘Server Status ’+ stat
    
  • To display only WLST script variables then you can use the comma separated variables you can use the combination of string in the same line too
    print x, y
    
  • Now, we will see the good old C style formatting for the output, here we use % symbol followed by paranthesized list of variables that will be substituted in place of embedded conversion types (such as string - s, decimal - d, float - f) in the order.
    print ‘%14s %d %f’ %(server, freeJvm, throughput)
    
In WLST we can use the print statement for displaying the data from a variable or strings objects. We can display the combination of variables in the same line. This is possible with comma separation or simply we can use Java style of concatenation using plus symbol.

Obtain Environment variables in WLST

WLST supports the Jython capability that obtaining the environment variables from the System. To do this we need to import the os module (operating system) and use the environ dictionary variable.

wls:/offline> import os
wls:/offline> os.environ
{'WL_HOME': 'C:\\wls12c\\wlserver',  …}

You can pull out your required values of environment variable as you access the dictionary variable. Example you can retrieve WL_HOME or JAVA_HOME values as show below.

wls:/offline> os.environ["WL_HOME"]
'C:\\wls12c\\wlserver'

wls:/offline> os.environ["JAVA_HOME"]
'C:\\Java\\jdk1.6.0_34'

This WLST trick can be most often used; when you are configuring resources are creating WebLogic domains. Remember, This WLST trick will works on UNIX platform as well.



Friday, June 22, 2012

WLST JMS monitoring stats to CSV file

Intro: We got a mail from Richard who wish to write a WLST script to monitor a JMS Runtime. The Queue performance details wish to collect.

WLST script to monitor JMS and store the attribute values when there is Production live available or Performance test run  going on with multiple user load collect the statistcs and send that into a separate CSV file. Just to give little idea on CVS, A comma-separated values (CSV) file are also referred as a flat files, ascii files, and it is a spreadsheet convertible files. Richi want the logic to storing the statistics to a .csv file. Thought that it will be helpful for anyone who may have similar thoughts. We got the script idea from Richi that consisting the following prototype.

Writing to a CVS file has been simplified without using writer.csv, instead commas have been used and the file extension is hard coded as .csv, once you open the file you will see a prompt where in you need to select comma as the delimeter so that the values of jms statistics will be sorted accordingly, you can modify the script as per your need.

The script flow will be as mentioned below
  1. Open a file with write access.
  2. Get the RUNNING servers of the domain from domainRuntimeService.
  3. Print the headers of JMS counters.
  4. Get the details of JMS servers using JMSRuntime and JMSServers functions.
  5. Using a loop get the jms server destinations.
  6. Using a loop get the details of the parameters like ConsumersCurrentCount, ConsumersHighCount etc of the destinations and store those to variables.
  7. Print the variables.
  8. Close the file.
##################################################
#Title: WLST script to monitor JMS runtime and saving the values to CSV file
#Author: Sunil Nagavelli/Pavan Bhavani Shekar Devarakonda
#
###################################################
import sys


file1=open('JMS.csv','w+')

servers = domainRuntimeService.getServerRuntimes();

print>>file1, " ConsumersCurrentCount: ", ",", " ConsumersHighCount: ", ",", " DestinationType: ", ",", " BytesHighCount: ",
",", " BytesPendingCount: "

if (len(servers) > 0):
  for server in servers:
    jmsRuntime = server.getJMSRuntime();
    jmsServers = jmsRuntime.getJMSServers();
    for jmsServer in jmsServers:
      destinations = jmsServer.getDestinations();
      for destination in destinations:

        CCC = destination.getConsumersCurrentCount()
        CHC = destination.getConsumersHighCount()
        DT  = destination.getDestinationType()
        BHC = destination.getBytesHighCount()
        BPC = destination.getBytesPendingCount()

        print>>file1,CCC, ",", CHC, ",", DT,  ",", BHC, ",", BPC

print "values have been captured successfully into the csv file"
       
file1.close()

Sunday, June 10, 2012

JDBC Monitoring

JDBC Monitoring with LWLST script
One fine morning we (WLA support Team) got an assignment, The summary of the assignment was to find "How the WebLogic server instance performing for a DataSource?". WebLogic 9.x onwards a DataSource is associated with a ConnectionPool (a pool of connections to the DB). If we monitor ConnectionPool, inturn it is nothing but monitoring a DataSource.

Of-course the task is not that easy, I have gone through various WebLogic forums to find a appropriate solution for this task.

Oracle WebLogic provides two ways to Monitor a Datasource
1. Monitoring Datasource server wise
2. Testing the Connection Pool

Here I am publishing the same script which Madan Noru/Satya prepared in the old bea fourms or ObjectMix forums. Only one thing is difference is that displaying pattern, I had created a separate header, so that output looks good to see in a table form. To make this possible I have used C-Style print command from Python Language. This format you can change as per your screen display size.

The script will retrieve the JDBC Connection Pool MBean using adminHome, which is deprecated object in WLST. The output of the script will gives you the values of the following attributes:
  • DataSource Name
  • Maximum Capacity of the Connection Pool at Run-time
  • Active Connections Current Count
  • Active Connections High Count
  • Wait Seconds High Count
  • Waiting For Connection Current Count
  • State of the Connection Pool

#=======================================================
# This script will monitor the JDBC CONNECTION POOL
# more details on this script contact: Pavan Devarkonda
#=======================================================
connect("username","passwd","t3://AdminIP:AdminPort")
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 ' '
except:
  print 'Error:'
  dumpStack()
  pass
disconnect()

In Year 2012 revisting the same script

We have trouble in the produciton environment with JDBC Connection pool overloaded. All the ActiveConnectionCount reaching the MaxCapacity of the Connection pool. As a temprory workaround we need to reset the Connection pool for that movement. Permanent cure is tuning the Connection pool. For both solutions we need active monitoring the JDBC Connection pool.
To monitor this we have revisited the same script. Now adding more saus to the script we need to find that ActiveConnectionCount reaching 85% as threshold limit. Once it reaches 85 or greater then monitoring tool like HP OV/some other tool will reads log file generated by the script and then sends warns message to supporting Middleware Admin when threshold crossed. If you have sendmail service on your machine you can send message fromt he script itself.
To do this we had manupulated the above script according to the requirements
  1. We have specified DataSource that are causing this trouble
  2. Used separate properties file for user credentials.
  3. The output of script is redirected to a file
  4. Managed Server name opposite to the monitor values it is tricky but resolved The new script is

#=======================================================
# This script will monitor the JDBC CONNECTION POOL
# Date :  18 Apr 2012
#=======================================================
import sys
import time
try:
        loadProperties('./DBcheck.properties')
        fp=open('ActiveConn.log','a+')
        Date = time.ctime(time.time())
        admurl='t3://'+admAdrs+':'+admPort
        connect(userConfigFile=UCF, userKeyFile=UKEY, url=admurl)
        poolrtlist=adminHome.getMBeansByType('JDBCConnectionPoolRuntime')
        print ' '
        print 'JDBC CONNECTION POOLS'
        print>>fp, '================================================ '

 for poolRT in poolrtlist:
                pname = poolRT.getName()
                pmaxcapacity = poolRT.getAttribute("MaxCapacity")
                paccc = poolRT.getAttribute("ActiveConnectionsCurrentCount")
        
  if pname == 'myDataSource1' or pname == 'myDS2'' or pname == 'myDS3':
                        server= str(poolRT).split(',')[2].split('=')[1]
                        p=(paccc /(pmaxcapacity * 1.0)) * 100 
                        if p >= 85:
                                print >>fp, 'WARNING: The Active connections are Greater than Threshold 85%'
                        print>>fp, '%24s %15s %18s %7d %7d' % (Date,server,pname,pmaxcapacity,paccc)

except Exception, e:
        sys.stderr.write('ERROR: %s\n' % str(e))
        print 'Error:', e
        dumpStack()
        pass
fp.close()
disconnect()


-->

Post script actions

  1. Prepare a Shellscript that will let you know howmuch time it will counsume to run py script.
    clear
    date
    $JAVA_HOME/bin/java weblogic.WLST /path/urscript/jdbcmon.py
    date
    
  2. Schedule a scheduler to run this script autosys or crontab that invokes above shell script.
  3. Configure a monitoring tool frequently lookup the logs and send alert messages such as HP OVO or smpt mailing also fine.
What do you think about this new version of script? Write back your comments and suggestions to deliver better. Refernce Object Mix discussion:
http://objectmix.com/weblogic/549153-weblogic-monitoring-script-wlst-2.html -->

Popular Posts