Saturday, May 9, 2015

How to Auto start Weblogic Domain through WLST

 

The Following Script checks the health of Weblogic Domain when configured as corn job, it connected to domain admin servers if its up if adminserver is down it will start the admin server before proceeding further. Expectation of this script is Nodemanager is running in all the machines hosting the domain.

 

We need two files one is the environment Properties file and one python script.

<evn>.<properties> file will have following information need to update according to the environment.

# Environment Specific Values need to updated below

LOGS=/home/oracle/logs
WLS_HOME=/u01/app/oracle/product/fmw/wlserver_10.3
DOMAIN_BASE=/u03/app/oracle
JAVA_HOME=/u01/app/oracle/product/fmw/java/jrockit-jdk1.6.0_33-R28.2.4-4.1.0
MW_HOME=/u01/app/oracle/product/fmw
ORACLE_COMMON_HOME=/u01/app/oracle/product/fmw/oracle_common

# Credentials to connect to Domain

admin.url=t3://hostname:port
admin.userName=xxxxxx
admin.password=xxxxx

#NodeManager Details.

NM_user=xxxxxx
NM_pass=xxxxxx
NM_host=localhost
NM_port=5556
NM_type=plain
verboseModeValue=false
Domain_Name=WLSDomain
Domain_Directory=/u03/app/oracle/admin/WLSDomain

# Defining the UserKey values if we need to use the encrypted user and password

UserKeyFile=/u02/oraScripts/Automation/Env/UserKeyFile

UserConfigFile=/u02/oraScripts/Automation/Env/UserConfigFile

# List of Servers in Domain separated by comma

Serverlist=

# Order to Start/Stop Servers

StartServersOrder=

StopServersOrder=

# List of Clusters in the domain

ClusterList=

# Order to Start the servers on VM restart/crash

ClusterStartOrder=

# Order to stop the cluster in the Domain

ClusterStopOrder=

------------------------------

# Script to AutoStart All the instances of the Domain.

from java.io import FileInputStream
import sys
import os
from java.util import Date
import string


Environment = sys.argv[1]
Domain = sys.argv[2]
env = Environment + '.' + Domain + '.properties'
logfile = Environment + '.' + Domain
propInputStream = FileInputStream(env)

#propInputStream = FileInputStream("env.properties")
configProps = Properties()
configProps.load(propInputStream)

adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")
Serverlist=configProps.get("ServerList")
StartServersOrder=configProps.get("StartServersOrder")
StopServersOrder=configProps.get("StopServersOrder")
ClusterList=configProps.get("ClusterList")
ClusterStartOrder=configProps.get("ClusterStartOrder")
ClusterStopOrder=configProps.get("ClusterStopOrder")

UserKeyFile=configProps.get("UserKeyFile")
UserConfigFile=configProps.get("UserConfigFile")

NM_user=configProps.get("NM_user")
NM_pass=configProps.get("NM_pass")
NM_type=configProps.get("NM_type")
NM_port=configProps.get("NM_port")
NM_host=configProps.get("NM_host")
verboseModeValue=configProps.get("verboseModeValue")
Domain_Name=configProps.get("Domain_Name")
Domain_Directory=configProps.get("Domain_Directory")
serverValue='AdminServer'

print 'AutoStart of Domain Script Starting....';
print 'Connecting to Nodemanager ..';

# Function to Connecting to NodeManager

def NMconn():
    try:
        nmConnect(NM_user,NM_pass,NM_host,NM_port,Domain_Name,Domain_Directory,NM_type)
#        nmConnect(UserConfigFile,UserKeyFile,NM_host,NM_port,Domain_Name,Domain_Directory,NM_type)
    except ConnectionException, e:
        print ' Unable to Connect to Node Manager ...'
        exit()

# Function to Autostart the AdminServer

def AdminAutoStart():
    serverStatus = nmServerStatus(serverValue)
    print ' Current ', serverValue ,'Server Status is : ', serverStatus;
    if serverStatus != 'RUNNING' :
        print ' Starting', serverValue ,'Now ..'
        nmStart(serverValue)
        CurrentStatus = nmServerStatus(serverValue)
        print ' Current ', serverValue , 'Status ', CurrentStatus;
    nmDisconnect()
    #exit()


# Function to Connect to Domain

def conn():
    try:
        connect(adminUserName, adminPassword, adminURL)
#        connect(UserConfigFile,UserKeyFile,adminURL)
    except ConnectionException,e:
        print ' Unable to Find Admin Server ...'
        exit()

# Function to Check server state and Initiate Managed Server Start

def ServerAutoStart():
    for server in StartServersOrder.split(','):
        if server != 'AdminServer' :
            ServerState = serverStatus(server)
            if ServerState != "RUNNING" :
                start(server,'Server')
#    disconnect()
#    stopRedirect()
#    exit()

# Function to Check the server status of Managed Server

# Function to Auto Start the Cluster

def ClusterAutoStart():
    for clustername in ClusterStartOrder.split(','):
        clusterstate = printClusterState(clustername)
        if clusterstate != "RUNNING" :
            startCluster(clustername)

# Alert Funciton

def AlertMe():
        for server in StartServersOrder.split(','):
                serverstate = serverStatus(server)
                if serverstate != 'RUNNING':
                        today = Date()
                        stateMessage = 'The Follwoing Server is not Running please check ... ' + server
                        cmd = "echo " + stateMessage + " >> serverState_file"
                        os.system(cmd)
                        sendMail()
                        cmd = "rm -f serverState_file"
                        os.system(cmd)
        disconnect()
        stopRedirect()
        exit()

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

#Fetch the state of the every cluster in the Domain.
   
def ClusterStatus():
    for clustername in ClusterList.split(','):
        ClusterState = printClusterState(clustername)
    disconnect()
    stopRedirect()
    exit()

# Start the clusters in the Domain.

def StartClusters():
    for clustername in ClusterStartOrder.split(','):
        startcluster =startCluster(clustername)
        clusterstate = printClusterState(clustername)
    disconnect()
    stopRedirect()
    exit()

# Stop the Clusters in the Domain

def StopCluster():
    for clustername in ClusterStopOrder.split(','):
        stopcluster =shutdownCluster(clustername)
        clusterstate = printClusterState(clustername)
    disconnect()
    stopRedirect()
    exit()


       
# Print state of cluster

def printClusterState(clustername):
    try:
       state(clustername,"Cluster")
    except Exception, e:
       print 'Error while printing cluster state ',e
       dumpStack()


# Start the Cluster

def startCluster(clustername):
    try:
       start(clustername,"Cluster")
    except Exception, e:
       print 'Error while starting cluster ',e
       dumpStack()

# Stop the Cluster

def shutdownCluster(clustername):
    try:
       shutdown(clustername,"Cluster")
    except Exception, e:
       print 'Error while shutting down cluster ',e
       dumpStack()

# Function to send email.

def sendMail():
    os.system('/bin/mailx -s " ALERT : Check the Server it May not be RUNNING !!! Please Check .." mohanmurali_kandra@optum.com < serverstate_file')
    print ' '
   
# Function to Quit

def quit():
    print '\033{1;35mRe-Run the Script Hit any key ..\033[0m'
    Ans = raw_input("Are you sure Quit from WLST ...(y/n) ")
    if (Ans == 'y'):
        disconnect()
        stopRedirect()
        exit()
    else:
        printClusterState()

# Main Function to declared the flow

if __name__== "main":
    redirect('./logs/'+ logfile +'.Autostart.log', 'false')
    NMconn()
    AdminAutoStart()
    conn()
    serverNames = cmo.getServers()
    domainRuntime()
#    ServerAutoStart()
    ClusterAutoStart()
    ClusterStatus()   
#    AlertMe()   

 

----------------------------------------------------------------

1 comment:

  1. Thank you for this script but when i try
    java weblogic.WLST domain.py


    Problem invoking WLST - Traceback (innermost last):
    File "/home/oracle/WLST/domain.py", line 10, in ?
    IndexError: index out of range: 1

    ReplyDelete