WLST script to monitor WebLogic status, heap, JDBC, and JMS

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.

Crontab:

# --------------------------------------------------------------------------------
# 2020-03-16 | Script to monitor WebLogic Server
# --------------------------------------------------------------------------------
0 * * * * /home/oracle/scripts/monitorSOAWLS.sh > /dev/null 2>&1

soamonitor.sh:

#!/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('') fo.write('') 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('') else: fo.write('') else: fo.write(' ') rowNum += 1 fo.write("
SERVER STATUS
Server NameStatusHealth
ALERT!' + name.getName() + ' ' + status + '' + health + '
ALERT!' + name.getName() + ' ' + status + ' ' + health + '
' + name.getName() + ' ' + status + ' ' + health + '


") #---------------------------------------- # Report Heap Details #---------------------------------------- # Definition to print a running servers heap details def printHeapDetails(server_name): domainRuntime() cd('/') cd('ServerRuntimes/' + server_name + '/JVMRuntime/' + server_name) hf = float(get('HeapFreeCurrent')) / 1024 hs = float(get('HeapSizeCurrent')) / 1024 hfpct = float(get('HeapFreePercent')) hf = round(hf / 1024, 2) hs = round(hs / 1024, 2) cellcolor = rowColor if hfpct <= 20 and server_name != 'AdminServer': if hfpct <= 10: cellcolor = 'red' else: cellcolor = 'yellow' else: cellcolor = rowColor fo.write('' + server_name + '  ' + `hf` + 'MB  ' + `hs` + 'MB  ' + `hfpct` + '%  ') # Calling printHeapDetails with arguments # Create Table for Heap Details fo.write('') fo.write('') fo.write('') 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('
SERVER HEAP SIZE REPORT
Managed ServerHeapFreeCurrentHeapSizeCurrentHeapFreePercent


') #---------------------------------------- # 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('') fo.write('') 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(''); rowNum += 1 fo.write('
' + server.getName() + '
Data Source:StateActive ConnectionsWaiting for Connections
' + datasource.getName() + ' ' + datasource.getState() + ' ' + repr(datasource.getActiveConnectionsCurrentCount()) + '  ' + repr(datasource.getWaitingForConnectionCurrentCount()) + ' 


') #---------------------------------------- # 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('') fo.write('') 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('') rowNum += 1 fo.write('
JMS Runtime Info for :' + serverName + '
SERVERJMSSERVERDestinationNameDestinationTypeMessagesCurrentCountMessagesHighCountConsumersCurrentCountConsumersHighCountConsumersTotalCount
' + serverName + '   ' + jmsServerName + '   ' + str(destination.getName()) + '  ' + str(destination.getDestinationType()) + '   ' + str(destination.getMessagesCurrentCount()) + '   ' + str(destination.getMessagesHighCount()) + '   ' + str(destination.getConsumersCurrentCount()) + '  ' + str(destination.getConsumersHighCount()) + '  ' + str(destination.getConsumersTotalCount()) + ' 


') fo.write('
') #---------------------------------------- # Exit WLST #---------------------------------------- exit()