Register
It is currently Thu Feb 22, 2018 10:38 am

Server Load alert script


All times are UTC


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Oct 12, 2006 11:46 am   
New Member
User avatar

Joined: Thu Oct 12, 2006 9:46 am
Posts: 23
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
Solaris 9/10 -
Debian


Top
 Profile WWW  
 PostPosted: Fri Aug 17, 2012 8:48 am   
New Member

Joined: Fri Aug 17, 2012 8:31 am
Posts: 1
Thank for this script.

I am having one issue.

I get this error

Code:
cat: /home/**/script/tmp/loadreply: No such file or directory
/home/**/script/load.sh: line 85: [: =: unary operator expected


I gave tmp 777 permissions to allow creating of files.

Any suggestions?


Top
 Profile  
 PostPosted: Fri Aug 17, 2012 10:51 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Not sure if this is the cause of the issue or not, but make sure the directories where the files are to be placed exist. You can do that in the following manner, assuming you have access to the parent directory:

mkdir -p /home/sysadm/www /home/sysadm/script/tmp

... and any other directories that I may have missed that need to be created. IF the directories exist and permissions are allowed, there should be no issues. Not sure I'd want to keep 777 access. Instead, manage them from whatever account you're going to run these scripts. If no special account is used, run them from root and have them owned by root with 755 protection mask.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP