Welcome to UltratechHost Forum, Guest. Please take the time to register if you haven't, or click

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

[-]
Tags
shell script to monitor load average on a linux server

Shell Script to Monitor Load Average on a Linux server
#1
Load Average on a server reflects the current state of the server. Higher the load average, poorer is the server performance hence it is a necessity to monitor the load average on the server. The following shell script monitors the load average on the Linux server and inform the server administrator with the current running processes of the server if the load average is greater than the defined threshold.

Create a file, say, /root/loadmonitor.sh by running the command

Code:
nano loadmonitor.sh

and add the bellow script

PHP Code:
############### START OF THE SCRIPT ###############

    #!/bin/bash

    # Define Variables
    
CUR_TIME=`date +"%A %b %e %r"`
    
HOSTNAME=`hostname`

    
# Retrieve the load average of the past 1 minute
    
Load_AVG=`uptime | cut -d'l' -f2 | awk '{print $3}' | cut -d. -f1`
    
LOAD_CUR=`uptime | cut -d'l' -f2 | awk '{print $3 " " $4 " " $5}' | sed 's/,//'`

    
# Define Threshold. This value will be compared with the current load average.
    # Set the value as per your wish.
    
LIMIT=5

    
# Compare the current load average with the Threshold and
    # email the server administrator if the current load average is greater.
    
if [ $Load_AVG -gt $LIMIT ]
    
then

    
#Save the current running processes in a file
    
/bin/ps -auxf >> /root/ps_output

    
echo "Current Time :: $CUR_TIME>> /tmp/monitload.txt
    
echo "Current Load Average :: $LOAD_CUR>> /tmp/monitload.txt
    
echo "The list of current processes is attached with the email for your reference." >> /tmp/monitload.txt
    
echo "Please Check... ASAP."  >> /tmp/monitload.txt

    
# Send an email to the administrator of the server
    
/usr/bin/mutt -"ALERT!!! High 1 minute load average on '$HOSTNAME'" -/root/ps_output youremail@yourdomain.tld < /tmp/monitload.txt

    fi

    
# Remove the temporary log files
    
/bin/rm -/tmp/monitload.txt
    
/bin/rm -/root/ps_output

    
############### END OF THE SCRIPT ############### 

Note:
Replace youremail@yourdomain.tld with your mail ID

Now, schedule a cronjob to execute the script on per minute basis. Edit the cronjob file

Code:
# crontab -e

or

nano /etc/crontab

and add the line at the end as
Code:
* * * * * /bin/sh /root/loadmonitor.sh

restart the crond service

Code:
# service crond restart
In order to use “mutt” to send emails, you need to install the mutt package on the server. It allows you to send emails with attachments.

Code:
# yum install mutt

Credit: Origional Auther



Dinesh Kumar Mohanty
Ultra Web Solutions®
Bhubaneswar, Odisha, India.

UltratechHost - Your One Stop Offshore & Onshore Hosting Provider
Server Locations : Netherlands, Germany, Russia, Singapore & USA
Service Offers: Shared hosting | Reseller hosting | Cloud server | Dedicated server | VPS Hosting



Forum Jump:


Users browsing this thread: 1 Guest(s)