Friday, October 16, 2015

Server & NodeManager Listing

 Following script will display the details on machine Server Name, Process owner, Process ID and NodeManager:

#!/bin/ksh

echo "################################################################################"
echo "# WebLogic Servers"
echo "#  [owner]  [server]  [pid]   "
echo "################################################################################"
ps -ef | grep "[D]weblogic.Name="|while read tmp
do
  owner=`echo $tmp | awk '{ print $1 }'`
  webLogicServer=`echo $tmp | grep -oP "(?<=Dweblogic.Name=)[^ ]+"`
  pid=`echo $tmp | awk '{ print $2 }'`
    echo "$owner $webLogicServer $pid $port"
done|sort|column -t

echo
echo "################################################################################"
echo "# Node Managers"
echo "#       "
echo "################################################################################"
ps -ef | grep "[w]eblogic.NodeManager"|while read tmp
do
  owner=`echo $tmp | awk '{ print $1 }'`
  pid=`echo $tmp | awk '{ print $2 }'`
  port=`echo $tmp | netstat -tlpn 2>/dev/null | grep $pid | awk '{ print $4 }' | tr '\n' ',' | tr ' ' ',' | grep -o ":....," | sort -u | tr -d '\n' | tr -d ':' | sed 's
/,$//'`
  if [ -z "$port" ]; then
    port="null"
  fi
  mw_home=`echo $tmp | grep -oP "(?<=bea.home=)[^ ]+"`
    echo "$owner $mw_home $pid $port"
done|sort|column -t

exit 0

Output will be like this:

./wlsList.sh
################################################################################
# WebLogic Servers
# [owner]  [server]  [pid]
################################################################################
oracle  AdminServer  29385
oracle  MY_server1   1758
oracle  My_server2   2721
oracle  My_server3   4668

################################################################################
# Node Managers
#      
################################################################################
oracle  /opt/admin/fmw  61224  5556

Wednesday, August 19, 2015

Getting State of all Servers in a domain

First you may create secure key files to avoid using plain text uname and password for weblogic.

Step1:

Run WLST --> /opt/admin/fmw/oracle_common/common/bin/wlst.sh

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline>connect('weblogic','welcome1',url='t3://localhost:7001')
wls:/myDomain/serverConfig> storeUserConfig('/tmp/myUserConfigFile','/tmp/myUserKeyFile')
wls:/myDomain/serverConfig> exit()

Step2:

File name: getAllServerStatus.py

import sys
import os

connect(userConfigFile='myUserConfigFile', userKeyFile='myUserKeyFile', url='t3://localhost:7001')
domainRuntime()
cd('ServerRuntimes')

sServers=domainRuntimeService.getServerRuntimes()
for sServer in sServers:
     serverName=sServer.getName();
     print '###' , serverName,'State is:', sServer.getState()

disconnect()
exit()

Step3:

FileName: getAllServerState_wls.sh

#!/bin/ksh

echo "Getting All Servers State......"

/opt/admin/fmw/oracle_common/common/bin/wlst.sh getAllServerStatus.py > /tmp/allServerStatus.log

cat /tmp/allServerStatus.log | grep -i state

exit 0

Example Run:

./getAllServerState_wls.sh

Getting All Servers State......
### AdminServer State is: RUNNING
### my_server1 State is: RUNNING
### my_server2 State is: RUNNING

Tuesday, August 18, 2015

Get Server Status in Weblogic using WLST

First you may create secure key files to avoid using plain text uname and password for weblogic.

Step1:

Run WLST --> /opt/admin/fmw/oracle_common/common/bin/wlst.sh

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline>connect('weblogic','welcome1',url='t3://localhost:7001')
wls:/myDomain/serverConfig> storeUserConfig('/tmp/myUserConfigFile','/tmp/myUserKeyFile')
wls:/myDomain/serverConfig> exit()

Step2:

File name: getServerStatus.py

import sys
import os

sServer=sys.argv[1]

connect(userConfigFile='myUserConfigFile', userKeyFile='myUserKeyFile', url='t3://localhost:7001')
state(sServer)

disconnect()

exit()

Step3:

FileName: getServerState_wls.sh

#!/bin/ksh

usage()
{
 echo "\n!!!!!Usage!!!!!!!"
 echo
 echo "   getServerState_wls.sh "
 echo "\n getServerState_wls.sh myserver1"
 echo
}

if [ $# -ne 1 ]
then
 usage
 exit 2
fi

SERVER=$1

echo "Server= ${SERVER}"
echo

/opt/admin/fmw/oracle_common/common/bin/wlst.sh getServerStatus.py ${SERVER} > /tmp/serverstatus.log
state=`cat /tmp/serverstatus.log | grep -i state`
echo $state


exit 0

Example Run:

 ./getServerState_wls_adp.sh MY_server2
Server= MY_server2


Current state of 'MY_server2' : RUNNING

Wednesday, August 12, 2015

Managed Server Operations without NM

wls_ms_operations.sh

Usage: ./wls_ms_operations.sh {start|stop|restart} {SERVER-NAME}

#!/bin/bash
server_name=$2
export DOMAIN_LOG=/opt/admin/fmw/

stop() {
        echo "Stopping Managed Server(s)..." $server_name
        echo $SUDO_USER "gave stop command for $server_name at" `/bin/date` > $DOMAIN_LOG/shutdown_$server_name.log
        nohup /opt/admin/fmw/domains/myDomain/bin/stopManagedWebLogic.sh $server_name > $DOMAIN_LOG/shutdown_$server_name.log 2> /dev/null &
        echo "Follow the log @ /opt/admin/fmw/domains/myDomain/servers/${server_name}/logs/"

}

start() {

        echo "Starting Managed Server..." $server_name
        echo $SUDO_USER "gave start command for $server_name at" `/bin/date` > $DOMAIN_LOG/startup_$server_name.log
        nohup /opt/admin/fmw/domains/myDomain/bin/startManagedWebLogic.sh $server_name t3://localhost:7001 2> /dev/null &
        echo "Follow the log @ /opt/admin/fmw/domains/myDomain/servers/${server_name}/logs/"

        exit 1
}

restart() {
        echo "Stopping Managed Server(s)..." $server_name
        echo $SUDO_USER "gave stop command for $server_name at" `/bin/date` > $DOMAIN_LOG/shutdown_$server_name.log
        nohup /opt/admin/fmw/domains/myDomain/bin/stopManagedWebLogic.sh $server_name > $DOMAIN_LOG/shutdown_$server_name.log 2> /dev/null &
        echo "Follow the log @ /opt/admin/fmw/domains/myDomain/servers/${server_name}/logs/"
sleep 10
pid=$(ps auxwww | grep ${server_name}| grep Dweblogic | cut -c 10-15 | tr -d ' ')
if [[ ! -z "$pid" ]]
                then
                        `kill -9 ${pid}`
echo "Killed the process..."
fi
        
echo "Now Starting Managed Server..." $server_name
        echo $SUDO_USER "gave start command for $server_name at" `/bin/date` > $DOMAIN_LOG/startup_$server_name.log
        nohup /opt/admin/fmw/domains/myDomain/bin/startManagedWebLogic.sh $server_name t3://localhost:7001 2> /dev/null &
        echo "Follow the log @ /opt/admin/fmw/domains/myDomain/servers/${server_name}/logs/"
        exit 1

}

case "$1" in

        "start")
                start
                ;;
        "stop")
                stop
                ;;
       "restart")
                restart
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart} {SERVER-NAME}"
                exit 1
                ;;

esac
exit

Tuesday, August 11, 2015

Admin Server Operations

wls_admin_operations.sh

#!/bin/bash

export SERVER_LOG=/opt/admin/fmw/

start() {
        echo "Starting WebLogic Server(s)..."

echo $SUDO_USER "gave start command for Admin Server at" `/bin/date` > $SERVER_LOG/startup.log

nohup /opt/admin/fmw/domains/myDomain/bin/startWebLogic.sh > $SERVER_LOG/startup.log 2>&1

}

stop() {
        echo "Stopping WebLogic Server(s)..."
echo $SUDO_USER "gave stop command for Admin Server at" `/bin/date` > $SERVER_LOG/shutdown.log
nohup /opt/admin/fmw/domains/myDomain/bin/stopWebLogic.sh > $SERVER_LOG/shutdown.log 2>&1

}

restart() {
        echo "Stopping WebLogic Server(s)..."
        echo $SUDO_USER "gave stop command for Admin Server at" `/bin/date` > $SERVER_LOG/shutdown.log
        nohup /opt/admin/fmw/domains/myDomain/bin/stopWebLogic.sh > $SERVER_LOG/shutdown.log 2>&1
        sleep 40
        echo "Starting WebLogic Server(s)..."
        echo $SUDO_USER "gave start command for Admin Server at" `/bin/date` > $SERVER_LOG/startup.log
        nohup /opt/admin/fmw/domains/myDomain/bin/startWebLogic.sh > $SERVER_LOG/startup.log 2>&1
}

case "$1" in
        "start")
                start
                ;;
        "stop")
                stop
                ;;
        "restart")
                restart
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
                ;;
esac

Sunday, February 8, 2015

Clean up Weblogic Server Cache

In many situations we would need to clean up weblogic server cache and we need to be careful while doing this. It's better to have a script which can do our job can help in reducing manual effort/human errors.

Following script can be run using 2 parameters (domain name & server name). This is required because there may be multiple domains in one machine and server name needs to mentioned as we can clean-up any server depending on the need basis.

clearWLCache.sh 


#!/bin/ksh

usage()
{
 echo "\n!!!!!Usage!!!!!!!"
 echo
 echo " clearWLCache.sh "
 echo "\ne.g. clearWLCache.sh myDomain myServer"
 echo
}

if [ $# -ne 2 ]
then
 usage
 exit 2
fi

domain=$1
server=$2
WLS_HOME=/opt/fmw

state=`cut -f1 -d ":" $WLS_HOME/domains/${domain}/servers/${server}/data/nodemanager/${server}.state`
echo "Server state is $state"

if [ $state = RUNNING -o $state = STARTING ]
then
    echo "Server is in $state mode, please shutdown and then use this script "
    exit
else

 if [ -d $WLS_HOME/domains/${domain}/servers/${server} ]
 then
   path=$WLS_HOME/domains/${domain}/servers/${server}
   rm -Rf $path/cache/*
   rm -Rf $path/tmp/*
   echo "Cache clean-up completed successfully"
   exit
 else
   echo ${domain} "or" ${server} "doesn't exists, please check"
   exit
 fi

fi

echo "There was some error, script not executed, please check the command/server name/admin name"
exit

Useful Scripts

Following script is used to clean up the logs by executing the script with 2 parameters (domain name & server name)

cleanLogs.sh

#!/bin/ksh

usage()
{
 echo "\n!!!!!Usage!!!!!!!"
 echo
 echo " cleanLogs.sh "
 echo "\ne.g. cleanLogs.sh mydomain myServer"
 echo
}
if [ $# -ne 2 ]
then
 usage
 exit 2
fi

domain=$1
server=$2
FMW_HOME=/opt/fmw/

#Remove the logs older than 30 days
find $FMW_HOME/user_projects/domains/${domain}/servers/${server}/logs/*_log.* -mtime +30 | xargs rm

#Compress the logs older than 15 days
find $FMW_HOME/user_projects/domains/${domain}/servers/${server}/logs/*access_log.* -mtime +15 | xargs compress

exit