WLST script to monitor WebLogic status, heap, JDBC, and JMS
Ahmed Aboulnaga -
Do you want a custom script to send out an email that reports on your Oracle WebLogic Server status, heap, data source, and JMS information?
If you want the an equivalent of the output below, all you need is a single crontab entry, a single bash script, and a single WLST Python script, all included below.
Basic scripting and WLST knowledge is required. The scripts can be customized to your liking.
#!/bin/bash
##############################################
# Weblogic Server Monitoring Script
# Author: Ahmed Aboulnaga
# Date: 2020-03-16
##############################################
#----------------------------------------
# Variables
#----------------------------------------
ENV=PROD12C
ORACLE_HOME=/u01/app/oracle/middleware
SCRIPT_PATH=/home/oracle/scripts/wlst
SERVERS=soadev1
PORT=7001
EMAILS="ahmed@revelationtech.com,ahmed.aboulnaga@revelationtech.com"
#----------------------------------------
# Set environment
#----------------------------------------
source ${ORACLE_HOME}/wlserver/server/bin/setWLSEnv.sh 2>&1 > /dev/null
#----------------------------------------
# Loop through server list
#----------------------------------------
for serv in ${SERVERS}
do
#----------------------------------------
# Run WLST script
#----------------------------------------
echo "********************************************************";
echo " Running Server status report for :${serv} ";
echo "********************************************************";
${ORACLE_HOME}/common/bin/wlst.sh ${SCRIPT_PATH}/monitor_all_servers.py ${serv} ${port}
echo '
********** END OF REPORT **********
' >> ${SCRIPT_PATH}/monitorstatus.html
#----------------------------------------
# Set email subject
#----------------------------------------
grep "yellow" ${SCRIPT_PATH}/monitorstatus.html >> /dev/null
if [ $? == 0 ]; then
ALERT_CODE="[WARNING]"
fi
grep "red" ${SCRIPT_PATH}/monitorstatus.html >> /dev/null
if [ $? == 0 ]; then
ALERT_CODE="[CRITICAL]"
fi
grep "green" ${SCRIPT_PATH}/monitorstatus.html >> /dev/null
if [ $? == 0 ]; then
ALERT_CODE="[GREEN]"
fi
ENVIRONMENT=`echo $ENV | tr '[:lower:]' '[:upper:]'`
#----------------------------------------
# Send email
#----------------------------------------
CONTENT=${SCRIPT_PATH}/monitorstatus.html
SUBJECT="${ALERT_CODE} - SOA ${ENVIRONMENT} ENVIRONMENT STATUS REPORT "
( echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $CONTENT )| /usr/sbin/sendmail $EMAILS
done
rm ${SCRIPT_PATH}/monitorstatus.html
monitor_all_servers.py:
##############################################
# Weblogic Server WLST Script
# Author: Ahmed Aboulnaga
# Date: 2020-03-16
##############################################
import os
import sys
# Username and password
uname = 'weblogic'
pwd = 'welcome1'
# Get server name
parServerName = sys.argv[1]
# Connect to server
url = 't3://' + parServerName + ':7001'
connect(uname, pwd, url)
# Write output to HTML file
fo = open("/home/oracle/scripts/monitorstatus.html", "wb+")
#----------------------------------------
# Report Server Status
#----------------------------------------
fo.write('
')
fo.write('\n
SERVER STATUS REPORT: ' + url + '
\n\n')
def getRunningServerNames():
domainConfig()
return cmo.getServers()
serverNames = getRunningServerNames()
domainRuntime()
def healthstat(server_name):
cd('/ServerRuntimes/' + server_name + '/ThreadPoolRuntime/ThreadPoolRuntime')
s = get('HealthState')
x = s.toString().split(',')[2].split(':')[1].split('HEALTH_')[1]
return x
serverNames = domainRuntimeService.getServerRuntimes()
getRunningServerNames()
domainRuntime()
# Create table for Server Report Status
fo.write('
')
fo.write('
SERVER STATUS
')
fo.write('
Server Name
Status
Health
')
rowNum = 0;
for name in serverNames:
status = str(name.getState())
health = healthstat(name.getName())
# Alternate Report Row Color
if rowNum % 2 == 0:
rowColor = '#D7DEEC'
else:
rowColor = '#F4F6FA'
# Change cell color based on status returned
hcolor = 'green'
if health != 'OK':
if health == 'WARN':
hcolor = 'yellow'
else:
hcolor = 'red'
else:
hcolor = 'green'
if status != 'RUNNING':
if status == 'WARNING':
fo.write('
')
# Calling printHeapDetails with arguments
# Create Table for Heap Details
fo.write('
')
fo.write('
SERVER HEAP SIZE REPORT
')
fo.write('
Managed Server
HeapFreeCurrent
HeapSizeCurrent
HeapFreePercent
')
servers = domainRuntimeService.getServerRuntimes();
rowNum = 0;
for server in servers:
# Alternate Report Row Color
if rowNum % 2 == 0:
rowColor = '#D7DEEC'
else:
rowColor = '#F4F6FA'
printHeapDetails(server.getName())
# Increment Row Color
rowNum += 1
fo.write('
')
#----------------------------------------
# Report JDBC Status
#----------------------------------------
fo.write('\n
SERVER JDBC RUNTIME INFORMATION
\n\n')
servers = domainRuntimeService.getServerRuntimes();
for server in servers:
jdbcRuntime = server.getJDBCServiceRuntime();
datasources = jdbcRuntime.getJDBCDataSourceRuntimeMBeans();
# Create Table for JDBC Status
fo.write('
')
fo.write('
' + server.getName() + '
')
fo.write('
Data Source:
State
Active Connections
Waiting for Connections
')
rowNum = 0;
for datasource in datasources:
if rowNum % 2 == 0:
rowColor = '#D7DEEC'
else:
rowColor = '#F4F6FA'
if datasource.getState() != "Running":
stateColor = "red"
else:
stateColor = rowColor
if datasource.getActiveConnectionsCurrentCount() > 10:
acColor = "yellow"
if datasource.getActiveConnectionsCurrentCount() > 20:
acColor = "red"
else:
acColor = rowColor
if datasource.getWaitingForConnectionCurrentCount() > 2:
wcColor = "yellow"
if datasource.getWaitingForConnectionCurrentCount() > 5:
wcColor = "red"
else:
wcColor = rowColor
fo.write('
')
#----------------------------------------
# Report JMS Status
#----------------------------------------
fo.write('\n
SERVER JMS STATUS INFORMATION
\n\n')
# Print JMS status for all servers
servers = domainRuntimeService.getServerRuntimes();
for server in servers:
serverName = server.getName();
jmsRuntime = server.getJMSRuntime();
jmsServers = jmsRuntime.getJMSServers();
if not jmsServers:
fo.write('
No JMS Information For ' + serverName + '
\n')
else:
# Create Table for JMS Status
fo.write('
')
fo.write('
JMS Runtime Info for :' + serverName + '
')
fo.write('
SERVER
JMSSERVER
DestinationName
DestinationType
MessagesCurrentCount
MessagesHighCount
ConsumersCurrentCount
ConsumersHighCount
ConsumersTotalCount
')
for jmsServer in jmsServers:
jmsServerName = jmsServer.getName();
destinations = jmsServer.getDestinations();
rowNum = 0;
for destination in destinations:
if destination.getMessagesCurrentCount() >= 0 :
# Alternate Report Row Color
if rowNum % 2 == 0:
rowColor = '#D7DEEC'
else:
rowColor = '#F4F6FA'
fo.write('