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