WLST

Changing WebLogic’s WebServer Log Settings using WLST

Hi,

Jay SenSharma

Jay SenSharma

Based on the  Comment/Query of one of our Magic Subscriber “Kimjim” (http://middlewaremagic.com/weblogic/?p=1473#comment-4594) , We are going to see how we can edit the WebServer Logging like displaying the Client IP Adddress in the access Log of a Server by adding (Extended Loging Formats  c-ip) and changing the various attribute of Logging in one go.

This script also demonstrates how we should use the edit() , activate() and startEdit()  kind of functions inside the WLST Script if we are migrating our WLST Scripts from WebLogic 8.1  because in WebLogic 8.1 the change control (Lock & Edit)  feature was  not available in the Admin Console.

Step1). Write the following WLST Script somewhere in your file system:
“changeLogFormats.py”

connect('weblogic','weblogic1','t3://localhost:7001')
domainConfig()
managedServers=cmo.getServers()
print 'Found ' + `len(managedServers)` + ' managed servers'

for managedServer in managedServers:
	try:
		edit()
		startEdit()
		serverName = managedServer.getName()
		print 'tt==================='+serverName+'===================='
		cd('/Servers/'+serverName+'/WebServer/'+serverName+'/WebServerLog/'+serverName)
		cmo.setLogTimeInGMT(false)
		cmo.setLogFileFormat('common')
		cmo.setELFFields('c-ip date time time-taken bytes cs-method cs-uri sc-status')
		activate()
		print ' '
		print ' '
	except java.lang.Exception, ex:
		print 'Exception on Changing the Log Attributes: ' + ex.toString()

Step2). Now we need to set  the CLASSPATH and PATH environment variables using setWLSEnv.sh or setDomainEnv.sh script.

Step3). Run the WLST Script java WebLogic.WLST changeLogFormats.py

java weblogic.WLST  changeLogFormats.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'base_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to serverRuntime tree. This is a read-only tree with DomainMBean as the root.
For more help, use help(domainConfig)

Found 2 managed servers

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
		===================AdminServer====================
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

Already in Edit Tree

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
		===================MS1====================
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

.
.
Thanks

Jay SenSharma


How to run WLST script using Cronjob ?

Ravish Mody

One of our subscriber middleware_guy was having issues to run a WLST script using Cronjob and wanted us to help him out with it to solve his problem by providing him a detailed steps or the script which would do the same for him, hence we thought that it would be better if we create an article on this issue so that everyone can take the advantage and can run any WLST script using cronjob.

The requirement was to run the WLST script every hour so that they can get some information from the running Weblogic server, we had suggested to use the cronjob to this part of the job, as running WLST script continually in the background takes lot of usage and we have been reported that this causes high CPU issue as well, thus using cronjob would be a best choice in this situation. For the new guys the question would come….

What is a Cronjob ?

Cronjob is a a job scheduler which is used for time based scheduler which comes with all Unix based operating systems and one of the best tool for the all administrators. You can use cronjob to run any commands or scripts periodically in a certain time based or dates wise manner which would automate the job of an administrators like taking backup of the logs every week, clean up the logs which are older then 30 days etc.

To use cronjob there are predefined scheduling definitions which has to be used as show in the diagram, which would give you a better picture what all things are needed to activate a cronjob.

* * * * * [command/script to get executed]

┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week [0 to 7] (where Sunday=0 or 7)
│    │    │    └────────── month [1 to 12]
│    │    └─────────────── day of month [1 to 31]
│    └──────────────────── hour [0 to 23]
└───────────────────────── min [0 to 59]

Let’s take an example, as per the requirement middleware_guy wanted that the WLST script should be called every hour, so to get that job done following would be the command which has to go in the “crontab” file.

0 * * * * [script]

This would make sure that the given script would run every hour, however to run a WLST script we would have to make sure that the weblogic environment is set properly so that it can call the WLST script or else it would get an exception. So the below shell script would do that job to sent the weblogic environment and then call the WLST script as well. Create a file called “WLST.sh” and put the below code in it

#!/bin/bash

# ************* Setting the Environment ***********************
DOMAIN_HOME="/home/testusr/WLS103/user_projects/domains/Domain_7001"
. ${DOMAIN_HOME}/bin/setDomainEnv.sh $*

echo "Environment has been set....."

# ************* Changing the directory where all the related files are needed ***********************
cd home/testusr/WLS/App_State

echo "Calling the PYTHON script....."

# ************* Calling the WLST script  *****************
java weblogic.WLST application_status.py

NOTE:

Make sure that all the files to run the python script and the WLST.sh file should be in the same directory folder so that you would not get filenotfound error.

Hence below line would be given in the crontab file to run the WLST script given in the WLST.sh file

0 * * * * /home/testusr/WLS/App_State/WLST.sh >> home/testusr/WLS/App_State/out.log

Same way if you want to run your script in every 15 minutes (like 1:15, 1:30,1:45,2:00,2:15…) then do the following:

00,15,30,45  *   *  *  * /home/testusr/WLS/App_State/WLST.sh >> home/testusr/WLS/App_State/out.log

Regards,
Ravish Mody


Sending Email Alert for Application Current State

Ravish Mody

Yesterday evening one of your subscriber middleware_guy using comment asked us how can he can get a regular update say every 1 hour get an email showing the current state of an applications which are not in prepared or failed states, hence we updated our previous script which was written by Jay called Getting Application States using WLST to get the above requirement.

We have just added the two logic in this script which are the email and condition loop which would make sure that only shoot an email when the applications are not in Active states, however you can still modify this to fulfill your requirement and regarding the check every 1 hour I would suggest to use the Cron-Job utility to call this script.

Update:

I have updated the script to show on which server is the applications are be targeted, as this script automatically gets all the application state which are been deployed on all the servers in that domain, without giving any details of the managed servers in domain.

Steps to Create an Email Alert For Application Current State

Step1) Create a Directory somewhere in your file system like : “C:WLST”

Step2) Write a Properties file “domains.properties” inside “C:WLST” like following:

admin.url=t3://localhost:7001
username=weblogic
password=weblogic

Step-3) Create a WLST Script somewhere in your file system with some name like “application_status.py” inside “C:WLST” contents will be something like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################
from java.io import FileInputStream
import java.lang
import os
import string

propInputStream = FileInputStream("domains.properties")
configProps = Properties()
configProps.load(propInputStream)

ServerUrl = configProps.get("admin.url")
UserName = configProps.get("username")
Password = configProps.get("password")

#############  This method would send the Alert Email  #################
def sendMailString():
	os.system('/bin/mailx -s  "ALERT: Applications are currently NOT in ACTIVE state !!! " ABCD@comapny.com < currentAppState_file')
	print '*********  ALERT MAIL HAS BEEN SENT  ***********'

redirect('wlst.log','false')
connect(UserName,Password,ServerUrl)
cd ('AppDeployments')
myapps=cmo.getAppDeployments()
print '=============================================='
print 'Following Applications are not in STATE_ACTIVE'
print '=============================================='
for appName in myapps:
	domainConfig()
	cd ('/AppDeployments/'+appName.getName()+'/Targets')
	mytargets = ls(returnMap='true')
	domainRuntime()
	cd('AppRuntimeStateRuntime/AppRuntimeStateRuntime')
	for targetinst in mytargets:
		currentAppState=cmo.getCurrentState(appName.getName(),targetinst)
		if currentAppState != "STATE_ACTIVE":
			writeInFile ='Applicaiton = "'+ appName.getName() +'"   //    Targeted Server = "'+str(mytargets)+'"   //     Current STATE = "'+ currentAppState +'"'
			print '', writeInFile
			cmd = "echo " + writeInFile + " >> currentAppState_file"
			os.system(cmd)
print '=============================================='
print''
sendMailString()
cmd = "rm -f wlst.log currentAppState_file"
os.system(cmd)

Step-4) Open a command prompt and then run the “setWLSEnv.cmd” or “setWLSEnv.sh” to set the CLASSPATH and PATH variables. Better you do echo %CLASSPATH% or echo $CLASSPATH to see whether the CLASSPATH is set properly or not. If you see an Empty Classpath even after running the “setWLSEnv.sh” then please refer to the Note mentioned at Step3) in the Following post: http://middlewaremagic.com/weblogic/?page_id=1492 Step-5) Now run the WLST Script in the same command prompt using the following command:

java   weblogic.WLST  application_status.py

You will see the following kind of results in the command prompt

$java weblogic.WLST application_status.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

==============================================
Following Applications are not in STATE_ACTIVE
==============================================
 Applicaiton = "Mysite"   //    Targeted Server = "[AdminServer]"   //     Current STATE = "STATE_PREPARED"
 Applicaiton = "SessionTest.war#VesionB"   //    Targeted Server = "[Cluster]"   //     Current STATE = "STATE_NEW"
 Applicaiton = "_appsdir_ClassLoaderTestEAR_ear"   //    Targeted Server = "[AdminServer]"   //     Current STATE = "STATE_PREPARED"
 Applicaiton = "MDB"   //    Targeted Server = "[MS-2]"   //     Current STATE = "STATE_NEW"
 Applicaiton = "FirstEARApplication"   //    Targeted Server = "[MS-1]"   //     Current STATE = "STATE_NEW"
 Applicaiton = "SecondEARApplication"   //    Targeted Server = "[MS-1]"   //     Current STATE = "STATE_NEW"
==============================================

*********  ALERT MAIL HAS BEEN SENT  ***********

NOTE: This script is using mailx (i.e. but Windows box does not have mailx utility) so please do check if your mailx is configured properly or else script would run properly but the mail would not be sent.


Regards,

Ravish Mody


Copyright © 2010-2012 Middleware Magic. All rights reserved. |