Friday, May 28, 2010

Mail from WLST when abnormal state for Server

In most of production environments there could be possibility of a WebLogic server instance crash or not RUNNING state due to many reasons. But if we know the state of that WebLogic server instance then we can act immediately for further harm to be predicated and control over the happenings in the domain.

Here I am with notifying alert mail message when one of the WebLogic Managed Server goes to SHUTDOWN state or UNKNOWN state or some other state which is not RUNNING. Phython language provides us SMTP library to send a mail from the machine.



Assuming that your machine have SMTP mail service must enabled. Check before creating this script on the box. Here you can replace pavanwla@MAILSERVER.com with mailing address of your supporting WebLogic Administrators (WLA) list by comma separation.

#======================================================
# Script File : StatusMail.py
# Author      : Pavan Devarakonda
# Updated on  : 29th April 2010
#======================================================
import smtplib
import time

From = "wla@WLSERVER.com"
To   =["pavanwla@MAILSERVER.com"]
Date = time.ctime(time.time())
URL='t3://'+get('ListenAddress')+':'+str(get('ListenPort'))

def getServerNames():
   domainConfig()
   return cmo.getServers()

def mailing(name, stat):
    serverRuntime()
    serverConfig()
    if stat == 'SHUTDOWN':
        Subject = ' major: '
    else:
        Subject= 'Critical:'
    Subject= Subject + 'The Weblogic server instance ' +name + ' is ' + stat

    Text='The Server ' +name +'   in the '+ stat+'  Listening at URL ' + URL
    Msg = ('From: %s\r\nTo: %s\r\nDate: \%s\r\nSubject: %s\r\n\r\n%s\r\n' %(From, To, Date, Subject, Text))
    s = smtplib.SMTP('YOURSMTP.DOMAIN.COM')
    rCode = s.sendmail(From, To, Msg.as_string())
    s.quit()

    if rCode:
        print 'Fail to send message...'

def serverStatus(server):
    cd('/ServerLifeCycleRuntimes/' +server)
    return cmo.getState()

def checkStatus():
    try:
        connect('username','******','t3://adminIP:AdminPort')
        serverNames= getServerNames()
        domainRuntime()
        for name in serverNames:
            print name        
            serverState = serverStatus(name)
            if serverState == "SHUTDOWN": 
                mailing(name, serverState)
            elif serverStat == 'UNKNOWN':
                mailing(name, serverState)
    except:
        mailing('AdminServer','Connection Fail')

if __name__== "main":
    redirect('./logs/status.log', 'false')
    checkStatus()
    print 'done'



To run the above script you need to update few lines 7, 8 10, 27, and 40. Hope you have idea what need to replace in these lines.

If any trouble write back to me :)

To test run this script in UNIX/Windows/MacOS/anyother...
prompt> java weblogic.WLST StatusMail.py

Keep posting your valuable comments and suggestions on this post.

References:
1. http://docs.python.org/library/smtplib.html

7 comments:

sumanth said...

Its very helpful, Bhavani...
U really rocking...

Ashok J2EE said...

Great script 1! Can you Send me how to run this script in windows machine.. ?

Pavan Bhavani Shekhar said...

@Ashok Thanks for your comment.

On Unix Cronjob/at command, similarly in windows you have Schedulers you can configure them by one of Windows schtasks. You can put the above line of code in a .cmd file it is like shell script in UNIX, invoked as per the scheduled time you assigned in schtasks.
Hope this link:http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true might help you to automatic calling your script in Windows.

Hitesh Mishra said...
This comment has been removed by the author.
Unknown said...

Hi,

Can you plesae let me know what needs to be updated on lines 7 & 8. I am getting error as below:

Problem invoking WLST - Traceback (innermost last):
(no code object) at line 0
File "D:\statusmail.py", line 2
import smtplib
^
SyntaxError: invalid syntax

Pavan Bhavani Shekhar said...

Hitesh,

http://wlstbyexamples.blogspot.in/2010/05/mail-from-wlst-when-abnormal-state-for.html

Pavan Bhavani Shekhar said...

Hitesh,

There was a earlier post about Errors & Exceptions in Python that makes you trouble in WLST scripting.

As per your error it could be happen due to indentation problem.

Read more: WLST errors and Exceptions

Search This Blog

Loading...

My Favorites