Wednesday, August 10, 2022

Why and How to Monitor a Servers in WebLogic using a script?

  Why ?

In WebLogic admin console you can see the list of servers and it is health/state , if you don’t have a monitoring tools that is connected to WebLogic and check these servers ( like OEM with WebLogic pack) you have to write a script to get this status in the file for example. Most monitoring tool will be able to be configured with the file or shell script and report an alert in certain condition.

How ?

Below are steps and a sample code to monitor certain servers in WebLogic, WebLogic script use a jython which mostly like a python programing language, tab is considered in the jython code:

1- Create a shell script .sh file with executing permission to call the WebLogic file and store the result in a text file , this can be run in a crontab for example every 5 minutes to be like an archived logs or appended somewhere in a log file:

#!/bin/bash

now=$(date +%d-%m-%Y_%H-%M)

/oracle/Middleware/Oracle_Home/oracle_common/common/bin/wlst.sh /export/home/oracle/get_wls_serverstate_all.py &>/export/home/oracle/weblogic_status.txt

cp -p /export/home/oracle/weblogic_status.txt /export/home/oracle/archive_weblogic_status/weblogic_status_${now}.txt

2- Create a jython code file to monitor and exclude certain servers as below example:

redirect('/dev/null', 'false')

connect('weblogic','<password>','t3://<serverIP>:7001')

import datetime

servers = cmo.getServers()

domainRuntime()

 

for server in servers:

        if server.getName() not in ["",""]:

                try:

                        cd('/ServerRuntimes/' + server.getName());

                        currentState = get('OverallHealthState').getState()

                        now = datetime.datetime.now()

                        current_time = now.strftime("%d-%b-%y_%H:%M:%S")

                        dump_file = '/export/home/oracle/threadDump/'+current_time+server.getName()+'-ThreadDump.log'

                        if currentState == 0:

                                print server.getName() + ': ' + get('State') + ': HEALTH_OK'

                        elif currentState == 1:

                                try:

                                        threadDump('true',dump_file,server.getName())

                                        messg1 = 'ThreadDump has been generated on:'+dump_file

                                except WLSTException, e:

                                        messg1 = 'Exectpiont occur in threadDump'

                                print server.getName() + ': ' + get('State') + ': HEALTH_WARN :'+messg1

                        elif currentState == 2:

                                try:

                                        threadDump('true',dump_file,server.getName())

                                        messg1 = 'ThreadDump has been generated on:'+dump_file

                                except WLSTException, e:

                                        messg1 = 'Exectpiont occur in threadDump'

                                print server.getName() + ': ' + get('State') + ': HEALTH_CRITICAL :'+messg1

                        elif currentState == 3:

                                try:

                                        threadDump('true',dump_file,server.getName())

                                        messg1 = 'ThreadDump has been generated on:'+dump_file

                                except WLSTException, e:

                                        messg1 = 'Exectpiont occur in threadDump'

                                print server.getName() + ': ' + get('State') + ': HEALTH_FAILED :'+messg1

                        elif currentState == 4:

                                try:

                                        threadDump('true',dump_file,server.getName())

                                        messg1 = 'ThreadDump has been generated on:'+dump_file

                                except WLSTException, e:

                                        messg1 = 'Exectpiont occur in threadDump'

                                print server.getName() + ': ' + get('State') + ': HEALTH_OVERLOADED :'+messg1

                        else:

                                try:

                                        threadDump('true',dump_file,server.getName())

                                        messg1 = 'ThreadDump has been generated on:'+dump_file

                                except WLSTException, e:

                                        messg1 = 'Exectpiont occur in threadDump'

                                print server.getName() + ': ' + get('State') + ': UNKNOWN_HEALTH_STATE (' + str(currentState) + ') :'+messg1

                except WLSTException, e:

                        print server.getName()+': SHUTDOWN: Not_Running'

 

disconnect()

3- To use a crontab for example :

####

#Weblogic status

####

0,5,10,15,20,25,30,35,40,45,55 * * * * /export/home/oracle/check_weblogic_all.sh

 


No comments:

Post a Comment

Why and How to install Grid 19c on RHEL 8?

  Why ? Simply we will be requested to install Oracle Grid RAC DB on Redhat RHEL 8, below is my note for this installation . How ? 1-  OS in...