Search This Blog

Showing posts with label WebLogic 12c. Show all posts
Showing posts with label WebLogic 12c. Show all posts

Sunday, February 12, 2023

Server State using WLST

Hello! Dear Automation focused engineer we have this post about how to work simple server status list for a WebLogic domain. This logic can be build and executed for huge number of WebLogic or FMW Servers in Produciton helps to view all at a time hostnames, their corresponding states.   

Why we needWebLogic Server State? What all those states?

While trouble shooting Middleware/FMW administrator need to check the status of all the WebLogic server instances. This is the basic need when the all the servers are in bounced for production code move. This same script can be applicable for the pre-production or staging environment too. WLST provides the built-in methods, which gives the status of the Server instance or servers in a Cluster. Here we will deal with individual instance wise data.

WebLogic Server Life cycle state diagram

There are several ways to list out the servers. The simple way here you go with interactive way...
In the following example 
  1. We are collecting all the list of servers present in the WebLogic domain
  2. state function applied on each server as item  passed to it
  3. repeat this step 2 until all server list ends
 
wls:/demodomain/serverConfig> x=ls('Servers',returnMap='true')
dr--   demoadm
dr--   demoms1
dr--   demoms2

wls:/demodomain/serverConfig> x
[demoadm, demoms1, demoms2]
wls:/demodomain/serverConfig> for i in x:
... state(i,'Server')
...
Current state of "demoadm" : RUNNING
Current state of "demoms1" : SHUTDOWN
Current state of "demoms2" : SHUTDOWN

Cluster listing
wls:/demodomain/serverConfig> c=ls('Clusters',returnMap='true')
dr--   clstr01

wls:/demodomain/serverConfig> c
[clstr01]
wls:/demodomain/serverConfig> state(c[0],'Cluster')

There are 2 server(s) in cluster: clstr01

States of the servers are
demoms1---SHUTDOWN
demoms2---SHUTDOWN

ServerLIfecycleRuntime Mbean tree


Using above shown MBean hierarchy we can fetch the all WebLogic domain server instance's states. If your production WebLogic domain consists of two digit (eg. 60 instances) or three digit number (eg. 120 instances) of managed server then, it is difficult to see all server’s state at once. Weblogic Administration console is unable to show all the servers in the domain on a single page. Navigating in between also a time eating process so think! think better way!! WLST has the solution.

To get the status of all servers in the domain can be obtained with the following steps
  1. Connect to the WebLogic Admin Server
  2. Fetch the Managed server list from the domainRuntime MBean
  3. Iterate the loop and get the state of each Managed Server with ServerLifeCycle Runtime MBean
  4. Repeat if required the step 3 as per the user input to Continue...
  5. Finally if all desired output is visible then disconnect from the AdminServer and exit.

################################################## 
# This script is used to check the status of all WL instances including the admin
###########################################################

def conn():
    UCF='/path/.AdminScripts/userConfigFile.sec'
    UKF='/path/.AdminScripts/userKeyFile.sec'
    admurl = "t3://hostname:wlport"

    try:
        connect(userConfigFile=UCF, userKeyFile=UKF, url=admurl)
    except ConnectionException,e:
        print '\033[1;31m Unable to find admin server...\033[0m'
        exit()

def ServrState():
    print 'Fetching state of every WebLogic instance'
#Fetch the state of the every WebLogic instance
    for name in serverNames:
        cd("/ServerLifeCycleRuntimes/" + name.getName())
        serverState = cmo.getState()
        if serverState == "RUNNING":
            print 'Server ' + name.getName() + ' is :\033[1;32m' + serverState + '\033[0m'
        elif serverState == "STARTING":
            print 'Server ' + name.getName() + ' is :\033[1;33m' + serverState + '\033[0m'
        elif serverState == "UNKNOWN":
            print 'Server ' + name.getName() + ' is :\033[1;34m' + serverState + '\033[0m'
        else:
            print 'Server ' + name.getName() + ' is :\033[1;31m' + serverState + '\033[0m'
        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:
        ServrState()

if __name__== "main":
    redirect('./logs/Server.log', 'false')       
    conn()
    serverNames = cmo.getServers()
    domainRuntime()
    ServrState()

Smart Script

Recently I have online discussion with Dianyuan Wang, state of the Managed servers can be obtained with state() command. This function can be used in two ways: 
  • To get individual managed server status you need to pass arguments as managed server name, type as 'Server'. 
  • Other one is to get individual Cluster wise status. 
This can be achieved by passing two arguments cluster name and type as 'Cluster'. The following script will be illustrate the second option, which I found that shorten code that gives same script outcome as above script. It could be leverage your scripting thoughts it is like a plain vanilla form as shown below:

Note: Hope you follow the WLST Tricks & tips
try:
    connect(url = "t3://adminhostname:adminport")
except:
    print "Connection failed"
state('appclstr','Cluster')
state('web1clstr','Cluster')

...
state('webNclstr','Cluster')
-->

Extra Stroke of this new script is that prints how many servers available in each given cluster.

Server state with redirecting, re in WLST

Wang mailed me his situation clarity of explanation why he chosen state command. And how he resolved with Python script tricks here. Its a great learning for me so sharing with you when I saw same question in StackExchange today(28 April 2014) after 3 years!! "The reason I do not use (for now) domainConfig is because some how few Weblogic domains are not in a good state, and when I run the domainConfig command, it complains that it is not enabled. Hence the alternative way I've selected here is using state command. But it don't return the state. It always return None. But it prints out the state of the server. Here you go, Better way is capture that printing output to a file using WLST command redirect-stopRedirect and then, use the Python regular expression to extract the state of each server. The following is the Python snippet how I use the redirect:
 
# Fill your own connection details 
  serverlist=cmo.getServers()   
  for s in serverlist:   
    server_nm = s.getName()
    urldict[server_nm]='t3s://'+s.getListenAddress()+':'+str(s.getAdministrationPort())   
    #domainRuntime()
    #cd('ServerLifeCycleRuntimes/'+server_nm)
    fileName='/tmp/myserver_state.txt'
    redirect(fileName)
    state(server_nm,'Server')
    stopRedirect()
    f = open(fileName)
    try:
      for line in f.readlines():
        if re.search('Current state',line):
          status[server_nm]=line
    except:
      continue
 
  Ks = status.keys()
  for s in Ks:   
    if re.search('RUNNING',status[s]):
		try:
		connect(username,password,urldict[s])
		except:
		continue
		cd("/Servers/" + s)  


...
best regards! Dianyuan Wang

Here I request you please write back your experiencing with this posting looking ahead for your issues/ suggestions as comments.

Wednesday, July 26, 2017

Logging configuration for domain and WebLogic server

Why we need Logging?
To understand in a basic level, when we run the WebLogic Servers we don't know what is happening inside the process.  It is kind of black box to us. To know what is happening inside the domain or server side to get more details we have logging mechanism enabled in WebLogic domains.


WebLogic Logging works for multiple hierarchical Subsystem. At the top level it is already enabled and we can use the existing setup. But for some reasons each project will have separate logging needs.

When we can use this?
If there are multiple Testing logging configuration at different levels:
  1. domain level logs - global status 
  2. Server level logs - admin or managed server status
  3. Resource level logs - JDBC, JMS, application status
We can set the common attributes of JMX MBean reset with new values which will work for your project needs.

While creating this snippet we have tried to execute it on WebLogic 11g [10.3.6] and WebLogic 12c.

########################################################################
# Python Script file: logrotate.py
# Author: Pavan Devarakonda
# Date : 25-Jul-2017
########################################################################
loadProperties('demodomain.properties')
# Functions
def editMode():
   edit()
   startEdit()

def saveActivate():
   save()
   activate()
   print '###### Completed configuration of domain logs ##############'

def exitDisconnect():
        disconnect()
        exit()

def connectToAdmin():
    connect(username,password,url)

def domainlog_rotation(domainname,FC,MinSize):
        """
        It will take the three parameters domainpath ,domainname is string values
        and FileCount -  FC and File MinSize - MinSize are integer values these values loaded from properties
        so they need to type cast to integers
        """
        cd('/Log/'+domainname)
        cmo.setRotationType('bySize')
        cmo.setRotateLogOnStartup(true)
        cmo.setFileCount(int(FC))
        cmo.setNumberOfFilesLimited(true)
        cmo.setFileMinSize(int(MinSize))
        cmo.setLogFileRotationDir(domainpath+domainname+'/RotationDir')
        save()

def ServerLogRotation(S,SFC,SMinsize):
        """
        This module will rotate the AdminServer or Managed Server loggger settings
        It takes three params - S as name of the server, SFC - Server FileCount, SMinsize - Server FileMinSize
        """
        print S+ ' server log  setting...'
        cd('/Servers/'+S+'/Log/'+S)
        cmo.setRotationType('bySize')
        cmo.setRotateLogOnStartup(true)
        cmo.setFileCount(int(SFC))
        cmo.setNumberOfFilesLimited(true)
        cmo.setFileMinSize(int(SMinsize))
        cmo.setLogFileRotationDir(domainpath+domainname+'/RotationDir +S')

def printline(): print '#'*50

def main():
        printline(); print 'Starting domain logs Configurations '; printline()
        connectToAdmin()
        editMode()
        domainlog_rotation(domainname,FC,MinSize)
        printline(); print 'Starting Server logs configurations' ; printline()
        cd('/')
        for server_name in ls('Servers',returnMap='true'):
                if server_name == 'AdminServer':
                        ServerLogRotation(server_name,ASFC,ASMinsize)
                else:
                        ServerLogRotation(server_name,MSFC,MSMinsize)
        saveActivate()
        exitDisconnect()

main()

Please double check all indentation blocks.
The properties file used for sample tests is as follows:

#WebLogic console connection parameters
username=weblogic
password=welcome1
url=t3://192.168.33.102:8001
domainname=demo_domain

# Domain logger configurations
FC=4
MinSize=5
domainpath=/u01/oracle/domains/

# ManagedServer logger configurations
MSFC=3
MSMinsize=2

# AdminServer logger configurations
ASFC=5


Sample execution output as follows:
WebLogic Logger enabled and configuration using WLST scrpt execution.

Friday, January 25, 2013

JMS Server Configuration using WLST

Most of the message orient middleware architecture designs, while preparing the proof of concept for a new business domain they need to do multiple trails and errors. Configuring for such system resource task can be simplified with Python script. is one of the phases on other hand is setting up the thresholds and quota is next phase.
WebLogic  -  JMS Servers with File Store

Before entering into the scripting let us have brief JMS details, the basic types of JMS message communications are two:

  •  Point to Point (PTP) 
  • Publisher/Subscriber (Pub/Sub)
WLST JMS Server configuration
Usually JEE development lead or architect decides which kind of messaging could be suitable for the application. Once you got the “Sign-off” for the communication mechanism, the Middleware admin will be configuring the JMS system resources.
I like Jeff West video presentation about JMS Servers and their usage with uniform distributed Queue, Topic and newly introduced Partitioned distribution. For your reference embedding the video here.


Initially, we need a JMS persistence store configuration using WLST script, that enables you to configure as many JMS servers and persistence stores as required for an application deployment. The persistence store can be created with File Store or JDBC store options. As per your domain requirement you can specify the total number in the properties file. Suppose Architect team decided to use only File Stores then we can set 0 to JDBC total store so that the loop will be disabled for that.


What we do for JMS configuration using WLST?

The base JMS configuration is going to involve the following: a. Persistence store creation with Files: For each managed server where JMS servers configured there we need to create a File store. As best practice we create a dedicated folder where all the filestores can be stored per machine. Use the same directory structure for all machines where the filestores configured. b. Persistence store with JDBC: This we can use when your JMS message persistence requires huge message sizes. c. JMS server : we need to configure as many JMS servers as managed servers involve in JMS messaging
from java.util import Properties
from java.io import FileInputStream
from java.io import File
from java import io
from java.lang import Exception
from java.lang import Throwable
import os.path
import sys

def createFlstr(fstr_name,dir_name,target_name):
        cd('/')
        fst = create(fstr_name, "FileStore")
        cd('/FileStores/'+fstr_name)
        cmo.setDirectory(dir_name)
        fst.addTarget(getMBean("/Servers/"+target_name))

def createJDstr(jstr_name,ds_name,target_name,prefix):
        cd('/')
        jst = create(jstr_name, "JDBCStore")
        cd('/JDBCStores/'+jstr_name)
        cmo.setDataSource(getMBean('/SystemResources/'+ds_name))
        cmo.setPrefixName(prefix)
        jst.addTarget(getMBean("/Servers/"+target_name))

def createJMSsrvr(jms_srv_name,target_name,persis_store,page_dir, thrs_high, thrs_low, msg_size):
        cd('/')
        srvr = create(jms_srv_name, "JMSServer")
        cd('/Deployments/'+jms_srv_name)
        srvr.setPersistentStore(getMBean('/FileStores/'+persis_store))
#       srvr.setPersistentStore(getMBean('/JDBCStores/'+persis_store))
        srvr.setPagingDirectory(page_dir)
        srvr.addTarget(getMBean("/Servers/"+target_name))
        srvr.setBytesThresholdLow(long(thrs_low))
        srvr.setBytesThresholdHigh(long(thrs_high))
        srvr.setMaximumMessageSize(long(msg_size))

envproperty=""
if (len(sys.argv) > 1):
    envproperty=sys.argv[1]
else:
    print "Environment Property file not specified"
    sys.exit(2)
propInputStream=FileInputStream(envproperty)
configProps=Properties()
configProps.load(propInputStream)

adminUser=configProps.get("adminUser")
adminPassword=configProps.get("adminPassword")
adminURL=configProps.get("adminURL")

connect(adminUser,adminPassword,adminURL)

edit()
startEdit()

#=============# JMS SERVER and PERSISITENT STORE CONFIGURATION #=============#
total_fstore=configProps.get("total_fstore")
#total_jstore=configProps.get("total_jstore")
total_jmssrvr=configProps.get("total_jmssrvr")

j=1
while (j <= int(total_jmssrvr)):
        jms_srv_name=configProps.get("jms_srvr_name"+ str(j))
        trg=configProps.get("jms_srvr_target"+ str(j))
        persis_store=configProps.get("jms_srvr_persis_store_name"+str(j))
        page_dir=configProps.get("jms_srvr_pag_dir"+str(j))
        thrs_high=configProps.get("jms_srvr_by_threshold_high"+str(j))
        thrs_low=configProps.get("jms_srvr_by_threshold_low"+str(j))
        msg_size=configProps.get("jms_srvr_max_msg_size"+str(j))
        createFlstr(persis_store,page_dir,trg)
        createJMSsrvr(jms_srv_name,trg,persis_store,page_dir,thrs_high,thrs_low,msg_size)
        j = j+1
#==========================================================================================#
save()
activate()
To execute this script you need to workout on your properties file, indentation in the script.
$ java weblogic.WLST jms_servers.py jms_servers.properties

Generic advantage of this Script

Here most important thing is that when you wish that the persistance store could be a filestore then, it requires file path, if you are giving in the properties file assign absoulute path.
Sample properties file here
adminUser=weblogic
adminPassword=welcome1
adminURL=t3://192.168.1.106:8100
total_fstore=2
total_jmssrvr=2

jms_srvr_name1=jms_ms1
jms_srvr_target1=ms1
jms_srvr_persis_store_name1=jms_ms1_fs
jms_srvr_pag_dir1=/home/wlsdomains/demodomain/fs
jms_srvr_by_threshold_high1=10
jms_srvr_by_threshold_low1=5
jms_srvr_max_msg_size1=512

jms_srvr_name2=jms_ms2
jms_srvr_target2=ms2
jms_srvr_persis_store_name2=jms_ms2_fs
jms_srvr_pag_dir2=/home/wlsdomains/demodomain/fs
jms_srvr_by_threshold_high2=10
jms_srvr_by_threshold_low2=5
jms_srvr_max_msg_size2=512

Wednesday, May 19, 2010

WebLogic Scripting Tool (WLST) Overview

-->
There are many newbies into the WebLogic stream ( Development/ Administration). Most of them are novice to WLST. I thought let me found a way to give you best links available on the net.

After being in the development team one of my buddy asked me "your blogs are very nice, I want to learn WLST. But where to start? " My answer for all my blog readers who want to encourage newbies in WLST can pass this post. Currently WLST is supporting Application Servers are listed as follows:
  • WebLogic 8.1
  • BEA WebLogic 9.0
  • BEA WebLogic 9.1
  • BEA WebLogic 9.2
  • BEA WebLogic 10.0
WLST is first initiated in the BEA System Inc., days so it is supported since WebLogic 8.1 onwards with latest service packs till WebLogic version 10.0. After Oracle acquisition following are the versions:

  • Oracle WebLogic 10g R3
  • Oracle WebLogic 11gR1
  • Oracle WebLogic 11gR1 PatchSet 1 (10.3.1)
  • Oracle WebLogic 11gR1 PatchSet 2 (10.3.2)
  • Oracle WebLogic 11gR1 PatchSet 3 (10.3.3)
  • Oracle WebLogic 12c

WLST Features indentation - Clear Sytax


The WLST programming structure follows strict syntax indentation and it is the same structure as in Python programming. As per my experiances with WLST it can be divided into four parts
  1. Importing section
  2. Global variables
  3. Definations - class/function
  4. Main program uses above sections

Import section for Reuse
importing WLST libraries includes Python standard libraries and thousands of new Jython libraries also included. You can have Java packages as module imports which gives you flexible programming.

While writing a WLST script, the file can be store with the extension .py are called python modules. We can reuse the external language such as Java methods into our WLST script you can use import statement for this. Here is the syntaxes that you can use.

import module
from module import submodule
from . import submodule

You can write a WLST script that uses command line aruguments using sys module sys.args.

WLST Datatypes - Global variables

In WLST you can define the global variables on the top of the program that can be used across all the functions defined in the script. On the other hand you can define the variables in the function definitions that can be treated as local to the function, Jython programming language supported datatypes all can be used it supports
  • Base Datatypes: integer, float, long, char, str, bool
  • Collection Types: list, dictionary, tuple, sequances, maps etc
  • Object types: Python objects and Java package objects
Dynamic typing
In WLST supports Python Dynamic typing, we can use same variable for storing different type of data as shown below.
wls:/offline> # VAR CAN CHANGE INTO MANY TYPES
wls:/offline> var=10
wls:/offline> print var
10
wls:/offline> var='Busy in WLST'
wls:/offline> print var
Busy in WLST
wls:/offline> var=[1,2,3]
wls:/offline> print var
[1, 2, 3]

Class in WLST

Object oriented scripting can be defined and can be reusable with creation of objects.
 
class CLASSNAME:
 def __init__():   #like C++ constructor
  self.name
  do something initially
 def function1():
  do some task 
Let’s experiment with class in WLST. In a class we can define attributes and we can assign values to them. And we define functions related to that class that you will make.
wls:/offline> class test:
...     u,p='weblogic','vybhav@28'
...
After this we are able to create instances of the class test. Let us create t as a instance of test class.
wls:/offline> t=test()
wls:/offline> print t.u,t.p
weblogic vybhav@28
There is a difference between class attribute and instance attributes. Let us know about it, The variables u, and p of the instance can be modified by assignment:
wls:/offline> t.u='vasuakka'
wls:/offline> t.u
'vasuakka'
In u attribute value was changed into ‘vasuakka’ instead of ‘weblogic’.

Function definitions in WLST

How to define a Function in WLST? It is simple it starts with keyword ‘def’, you need to give a name of the function followed by number of argument and a colon. After this you need to give a tab space indentation that indicates the function block. You can add text to tell about what this function will do. Then you can add number of WLST statements or control flow statements. At the bottom of the function you can return a value or you leave that line, it is optional.
Now, let’s experiment with function definition for conversion of Kilobytes to megabytes. The logic of this function is that we can get the megabyte value when you pass the argument as kilobyte this will be divided by 1024.
wls:/offline> def kb2MB(kb):
...     mb=kb/1024
...     return mb
...
wls:/offline> print kb2MB(2048)
2

WLST Operators

You WLST commands statements not only JMX accessing functions we need to use some of the Python programming control flows in it. To use the control flow you must know about what kind of operations you can do in WLST. So let us see some of them are:

Comparison operators
This opertators we can use in regular if statements or while statements
==, >, <, <=, =>

Logical Operators
In WLST the variable can be empty or zero values are treated as "", 0, None, 0.0, [], and {}. So you need to be careful when you define a variable and moved the values of variables.

How WLST script Execution works?

Here I am going to explain with the following picture, how the Python script will be interpreted and internally compiled to java code and then Byte code so that it can be executable on JVM.
WLST Execution process
The source code for the WLST script is written in Python code. The WLST program file must have the extension as .py which is the source code consisting a set of Jython instructions for WebLogic domain. This source code is going to use a JVM machine when it is instructed to RUN the script; its invocation internally generates Java byte code to interpret with the JVM to produce the desired output. You might wonder by seeing this Chines WLST link, but it is worthy to see in google chrome. Because the web-page is from china and entire page is in chaineese language. If you have Google Chrome it will give you translate on top of the page you can choose your choice of language and have knowledge on WLST :) Here I am sharing the wonderful presentation about WLST overview. Check this following SlideShare Presentation: it's pretty informative, it was delivered by James Bayer who is the technical expert from Oracle. Especially I more impressed myself to see the 47 slide saying about THIS blog as one of the web reference.
Thankyou for visiting this post, feel free to post your comments after viewing these video links.

Popular Posts