Search This Blog

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 instances go on overload and crash or it could reach to a non 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 confidently control that could happen  in that WebLogic domain.


Python network library smtplib in WLST

WLST supports multiple the network libraries such as

  1. ftplib 
  2. poplib 
  3. imaplib 
  4. smtplib 
  5. telnetlib 
these are  network capabilities in their process, scripting we need to import the libraries and you can use the built-in functions. Before using this smtp libraries you need to check the SMTP service enabled on the machine.

Here I am trying to send the 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.

-->

Assuming that your machine have SMTP mail service must enabled. Check before creating this script on the box. Here in the following script you can replace the 'To' address value given as  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 make you more comfortable here you need to update few lines 7, 8 10, 27, and 40. Hope you have idea what need to replace in these lines! for your environment.

Cronjob schedule

If you need this script need to run for every 30 mins you can schedule the following line into a shell script. map the shell script to crontab by giving the -e option. If got any trouble please 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
  2. http://uthcode.sarovar.org/pyconindia2010/presentation.html

11 comments:

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

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

    ReplyDelete
    Replies
    1. Hi Ashok,

      I am also looking for how to run similar script in Windows environment. Could you please help me if u have already achieved it. Thanks in advance.

      Regards,
      Vikram

      Delete
  3. @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.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. 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

    ReplyDelete
  6. Hitesh,

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. Error: get() cannot be applied at this point of path
    Error: get() cannot be applied at this point of path
    Problem invoking WLST - Traceback (innermost last):
    File "/app_data/scripts/monitoring/monitor_soa_servers.py", line 56, in ?
    File "", line 586, in redirect
    File "", line 552, in raiseWLSTException
    WLSTException: Error occurred while performing redirect : Could not find the file to redirect wlst output to: ./logs/status.log : ./logs/status.log (No such file or directory)
    Use dumpStack() to view the full stacktrace :

    ReplyDelete
  9. Hi Dear,

    we are facing below error, so please can you check.

    D:\UpGrade_WLS\Oracle\Middleware\user_projects\domains\base_domain>java weblogic.WLST startemil.py

    Initializing WebLogic Scripting Tool (WLST) ...

    Welcome to WebLogic Server Administration Scripting Shell

    Type help() for help on available commands

    Error: get() cannot be applied at this point of path
    Error: get() cannot be applied at this point of path
    Problem invoking WLST - Traceback (innermost last):
    File "D:\UpGrade_WLS\Oracle\Middleware\user_projects\domains\base_domain\startemil.py", line 56, in ?
    File "", line 682, in redirect
    File "", line 648, in raiseWLSTException
    WLSTException: Error occured while performing redirect : Could not find the file to redirect wlst output to: .\logs\status.log : .\logs\status.log (The system cannot find the path specified)
    Use dumpStack() to view the full stacktrace

    ReplyDelete
  10. I'm receiving an error while executing this script.
    Could someone please assist me on this ?

    >>>>Error:

    Initializing WebLogic Scripting Tool (WLST) ...

    Welcome to WebLogic Server Administration Scripting Shell

    Type help() for help on available commands

    Error: No domain or domain template has been read.
    Problem invoking WLST - Traceback (innermost last):
    File "/home/WLST/statusMail.py", line 7, in ?
    TypeError: cannot concatenate 'str' and 'NoneType' objects


    >>>>> Following is the Line 7 of /home/WLST/statusMail.py :

    URL='t3://'+get('ListenAddress')+':'+str(get('ListenPort'))

    ReplyDelete

Please write your comment here

Popular Posts