USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Server Load alert script

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
sysconfig
New Member


Joined: 12 Oct 2025
Posts: 12

PostPosted: Thu Oct 12, 2025 11:46 am    Post subject: Server Load alert script Reply with quote

Load Average checking script:

Intro
This script will sent you an email when server load is more than 3 and it will also catch the values of top memory and cpu consuming process and network connection so that we can check the server for more information and bad processes at that time.

Quote:
Set this file in cron like:
*/15 * * * * sh /home/sysadm/script/load.sh


/home/sysadm/script - location for this script
/home/sysadm/script/tmp/ - Temporary files

Quote:
# echo "no" > /home/sysadm/script/tmp/loadreply


Code:

Quote:
#!/bin/bash

infofile="/home/sysadm/www/loadinfo.`date +%d-%m-%Y`.txt" # File will store the all status to diagnose the load problem
temp="/home/sysadm/script/tmp/mailinfo" # File will store the formated output when load is high
loadreply="/home/sysadm/script/tmp/loadreply" # File will store formated output when load avg problem is solved
srvrnm=`hostname` # get the hostname of the server [/color]
cur=`uptime | awk '{print $10}' | tr -d , | cut -d. -f1` # Get the current load of the server

if [ $cur -ge 3 ]; then # Condition if the server load is greater then 3 then sent Alert email, you can change this value


#################### Formating Server Statistics to diagnose the server when server load is high ##################

echo " " >> $infofile
echo " " >> $infofile
echo " " >> $infofile
echo "_____________________________________________________________________________________________________" >> $infofile
echo "High Load Average `date`, please find detailed information of it as mentioned below:" >> $infofile
echo "_____________________________________________________________________________________________________" >> $infofile

echo " " >> $infofile
echo " " >> $infofile
echo "------------------" >> $infofile
echo "| Uptime status: |" >> $infofile
echo "------------------" >> $infofile
/usr/bin/uptime >> $infofile ##### Adding uptime status

echo " " >> $infofile
echo "-----------------------------------" >> $infofile
echo "| Top 20 CPU consuming processes: |" >> $infofile
echo "-----------------------------------" >> $infofile
ps aux | head -1 >> $infofile
#ps aux | sort -rn +2 | head -20 >> $infofile
ps aux --no-headers | sort -rn +2 | head -20 >> $infofile #### Top 20 cpu consuming process

echo " " >> $infofile
echo "-----------------------------------" >> $infofile
echo "| Top 10 memory-consuming processes: |" >> $infofile
echo "-----------------------------------" >> $infofile
ps aux | head -1 >> $infofile
ps aux --no-headers| sort -rn +3 | head >> $infofile #### Top 10 memory consuming process

echo " " >> $infofile
echo "---------------------------" >> $infofile
echo "| Memory and Swap status: |" >> $infofile
echo "---------------------------" >> $infofile
/usr/bin/free -m >> $infofile ###### Memory usage status

echo " " >> $infofile
echo "------------------------------" >> $infofile
echo "| Active network connection: |" >> $infofile
echo "------------------------------" >> $infofile
/bin/netstat -tnup | grep ESTA >> $infofile # All active network connectins

echo " " >> $infofile
echo "---------------------------" >> $infofile
echo "| Disk Space information: |" >> $infofile
echo "---------------------------" >> $infofile
/bin/df -h >> $infofile # get the disk status information also
echo " " >> $infofile
echo "______________________________________________________________________________________________" >> $infofile
echo " " >> $infofile
###################################################################

################### Formation for email output ##########################
echo "Hi," > $temp
echo " " >> $temp
echo " " >> $temp
echo "Presently we have noticed high server load on `hostname`." >> $temp
echo " " >> $temp
echo "Please find the status of `hostname` server using below mentioend URL: http://$srvrnm/sysadm/loadinfo.`date +%d-%m-%Y`.txt" >> $temp
echo " " >> $temp
echo " " >> $temp
echo "Thanks," >> $temp
echo " " >> $temp
echo "- Linux SysAdmin." >> $temp
######################################################

mail -s "Alert: Server Load `hostname`" [email protected] < $temp
echo "yes" > $loadreply ## Change value in loadreply file to sent email when problem is resolved

fi

#### Condition to sent email when load problem is resolved by sysadmin ######
if [ `cat $loadreply` = 'yes' ] && [ $cur -le 1 ]; then

################### Formation for email output ##########################
echo " " > $temp
echo "Hi," >> $temp
echo " " >> $temp
echo "The server had high utilization problem and it is recovered from that" >> $temp
echo "Current load of the server is:" >> $temp
echo " " >> $temp
echo " " >> $temp
echo "`uptime`" >> $temp
echo " " >> $temp
echo "Thanks," >> $temp
echo " " >> $temp
echo "- Linux Sysadmin." >> $temp
######################################################

mail -s "Alert: Server Load is under control for `hostname` on `date`" [email protected] < $temp
echo "no" > $loadreply # change the value in laod reply file that problem is solved

fi

############## End of Script #########




_________________
CentOS 4.4 -- Feel like Redhat
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2025 - Usa Linux Users Group
This forum is powered by phpBB. © 2025-2006 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse