Shell Script to check Weblogic Admin and Managed Servers

In this post I am sharing a script to check the Weblogic and Managed Server status Using Shell script and WLST script.




Assumptions:
Understanding for Shell script
UnderStand Fusion MiddbleWare


Script:

This script is tested in EBS 12.2 Environment. You can change values as per your environment.

#!/bin/bash
#set -x
. /u01/install/APPS/EBSapps.env run
# Set WebLogic Environment Variables
export MW_HOME=/u01/install/APPS/fs1/FMW_Home
export WL_HOME=$MW_HOME/wlserver_10.3
export DOMAIN_HOME=$MW_HOME/user_projects/domains/EBS_domain
export PATH=$PATH:$WL_HOME/common/bin

# WebLogic Admin Server credentials
ADMIN_URL="t3://apps.example.com:7001"
USERNAME="weblogic"
PASSWORD="welcome1"

# Temporary file to store WLST script
WLST_SCRIPT="/tmp/check_wls_status.py"

# Temporary file to store WLST script
WLST_SCRIPT="/tmp/check_wls_status.py"

# Colors for output
RED='\033[0;31m'
NC='\033[0m' # No Color

# Function to create WLST script for checking server status
create_wlst_script() {
  cat <<EOF > $WLST_SCRIPT
connect('$USERNAME', '$PASSWORD', '$ADMIN_URL')
domainRuntime()

# Function to check the status of a server
def check_server_status(serverName):
    cd('/ServerLifeCycleRuntimes/' + serverName)
    state = cmo.getState()
    if state == "RUNNING":
        print('Server: ' + serverName + ' - State: ' + state)
    else:
        print('DOWN: ' + serverName + ' - State: ' + state)

# List of servers to check
servers = cmo.getServerLifeCycleRuntimes()
for server in servers:
    serverName = server.getName()
    check_server_status(serverName)

disconnect()
exit()
EOF
}

# Function to execute WLST script and process the output
execute_wlst_script() {
  $WL_HOME/common/bin/wlst.sh $WLST_SCRIPT | while IFS= read -r line; do
    if [[ $line == DOWN:* ]]; then
      echo -e "${RED}${line}${NC}"
    else
      echo "$line"
    fi
  done
}

# Main function to monitor WebLogic servers
monitor_weblogic() {
  echo "Monitoring WebLogic Admin and Managed Servers..."
  create_wlst_script
  execute_wlst_script
  rm -f $WLST_SCRIPT
}

# Call the main function
monitor_weblogic


Execution of Script:

sh monitor_weblogic.sh 








Please do like and subscribe to my youtube channel: https://www.youtube.com/@foalabs If you like this post please follow,share and comment