Way to success...

--"Running Away From Any PROBLEM Only Increases The DISTANCE From The SOLUTION"--.....--"Your Thoughts Create Your FUTURE"--.....--"EXCELLENCE is not ACT but a HABIT"--.....--"EXPECT nothing and APPRECIATE everything"--.....

Tuesday, June 7, 2016

Script to Monitor Server CPU Load Average


Pre-requisites to Run the script:

Make sure mailx  is installed on your OS, also check below environements/file is created prior to execute the scripts.

This script is tested on Linux Server.

This script needs to be deployed in database node/server.

#Base location for the DBA scripts
DBA_SCRIPTS_HOME=$HOME/DBA_MON

#OS User profile where database environment file is set
$HOME/.bash_profile

Create Custom .sysenv file for scripts

Download(.sysenv)
Download .sysenv file and save it under $HOME/DBA_MON
$ chmod 777 .sysenv
$ cat $HOME/DBA_MON/.sysenv

export DBA_SCRIPTS_HOME=$HOME/DBA_MON
export PATH=${PATH}:$DBA_SCRIPTS_HOME
export DBA_EMAIL_LIST=kiran.jadhav@domain.com,jadhav.kiran@domain.com
#Below parameter is used in script, whenever there is planned downtime you can set it to Y so there will be no false alert.
export DOWNTIME_MODE=N

Script to Monitor Server CPU Load Average and Send Notification if Utilization is above threshold.



Download(ServerLoadMon.sh)
Download ServerLoadMon.sh and save it under $HOME/DBA_MON/bin/
$ chmod 755 ServerLoadMon.sh

#!/bin/bash

############################################################
# Script Name : ServerLoadMon.sh                           #
#                                                          #
# Description:                                             #
# Script to Monitor Server CPU Load Average and            #
# Send Notification if Utilization is above threshold      #                                                                          
#                                                          #
# Usage : sh <script_name> <Threshold>                     #
# For example : sh ServerLoadMon.sh 10                     #
#                                                          #
# Created by : Kiran Jadhav -(https://h2hdba.blogspot.com) #
############################################################

# Initialize variables

THRESHOLD=$1
HOST_NAME=`hostname | cut -d'.' -f1`
PROGRAM=`basename $0 | cut -d'.' -f1`
export DBA_SCRIPTS_HOME=$HOME/DBA_MON
LOG_DIR=$DBA_SCRIPTS_HOME/logs/$HOST_NAME
OUT_FILE=$LOG_DIR/`echo $PROGRAM`.out
LOG_FILE=$LOG_DIR/`echo $PROGRAM`.log
ERR_FILE=$LOG_DIR/`echo $PROGRAM`.err
LOG_DATE=`date`


# Source the env
. $HOME/.bash_profile
. $DBA_SCRIPTS_HOME/.sysenv

if [ $? -ne 0 ]; then
   echo "$LOG_DATE" > $LOG_FILE  
   echo "Please pass correct environment : exiting the script  \n" >> $LOG_FILE
   cat $LOG_FILE
   exit
fi

if [ -s $OUT_FILE ]; then
 echo "$LOG_DATE" > $LOG_FILE
 echo "Deleting existing output file $OUT_FILE" >> $LOG_FILE
 rm -f $OUT_FILE
 cat $LOG_FILE
fi

if [ $DOWNTIME_MODE = "Y" ]; then
 echo "$LOG_DATE" >> $LOG_FILE
 echo "Host: $HOST_NAME | Instance: $ORACLE_SID is under maintenance: exiting the script" >> $LOG_FILE
 cat $LOG_FILE
 exit
fi

usage()
{
  echo "$LOG_DATE" > $LOG_FILE
        echo "Script to monitor Server Load"  >> $LOG_FILE
        echo "Usage   : sh <script_name> <LoadAvg> " >> $LOG_FILE
  echo "For example : sh $PROGRAM.sh 10" >> $LOG_FILE
        echo
}

if [ $# -lt 1 ]; then
    usage
    echo "Error : Insufficient arguments." >> $LOG_FILE
 cat $LOG_FILE
    exit
fi

LOADAVG=`/usr/bin/uptime|awk '{print $(NF-2)}'|cut -d. -f1`
#echo $LOADAVG
 
if [ "$LOADAVG" -ge "$THRESHOLD" ]; then
 /usr/bin/uptime > $OUT_FILE 
   cat $OUT_FILE| mailx -s "Critical : High Load ( > $THRESHOLD ) average on $HOST_NAME" $DBA_EMAIL_LIST
else 
    echo "$LOG_DATE" > $OUT_FILE
 echo "Server Load is NORMAL" >> $OUT_FILE
fi


Logs and Out files will be generated under $DBA_SCRIPTS_HOME/logs/$HOST_NAME
So make sure to create logs/$HOST_NAME directory under $DBA_SCRIPTS_HOME before executing 
the script.

Once the script is ready, then as per the requirement please schedule it in crontab/OEM.

Execute the Script as below:

Syntax :  sh <script_name> <Threshold>  

$ cd $HOME/DBA_MON/bin
$ sh ServerLoadMon.sh 10
This script will send the notification with details if Server Load Average is above 10.

1 comment:

  1. Hi Kiran,
    Thanks for your scripts but I am not able to run the script as command not found appears. Please help

    ReplyDelete