http://nixcraft.com/shell-scripting/923-cpu-usage-shell-script-monitoring-cpu-usage.html
http://itmission.org/Main/Load-alert-bash-script-for-linux-servers
Here is the script if 15 minutes load goes >= 6.0.
Code:
—————————————————————–
#!/bin/bash
# Set up limit below
NOTIFY="6.0"
FTEXT='load average:'
# 15 min
F15M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3)"
# compare it with last 15 min load average
RESULT=$(echo "$F15M > $NOTIFY" | bc)
# if load >= 6.0 create a file /tmp/file.txt
if [ "$RESULT" == "1" ]; then
echo 'LOAD ISSUE'>/tmp/file.txt
fi
—————————————————————
Load alert bash script for Linux Servers
Advanced Server Load Alert Script With Process Status.
File for the script vi /root/loadalert
Get the process status report through e-mail for server load, you can try to add additional info to the code, to get more info through e-mail.
————————————————————–
#!/bin/bash
#Wednesday, December 06 2006
EMAIL="your email 1"
EMAIL1="your email 2"
SUBJECT="Alert $(hostname) load average is $L05"
TEMPFILE="/tmp/$(hostname)"
echo "Load average Crossed allowed limit." >> $TEMPFILE
echo "Hostname: $(hostname)" >> $TEMPFILE
echo "Local Date & Time : $(date)" >> $TEMPFILE
echo "| Uptime status: |" >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
/usr/bin/uptime >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
echo "| Top 20 CPU consuming processes: |" >> $TEMPFILE
ps aux | head -1 >> $TEMPFILE
ps aux --no-headers | sort -rn +2 | head -20 >> $TEMPFILE
echo "| Top 10 memory-consuming processes: |" >> $TEMPFILE
ps aux --no-headers| sort -rn +3 | head >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
echo "| Memory and Swap status: |" >> $TEMPFILE
/usr/bin/free -m >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
echo "| Active network connection: |" >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
/bin/netstat -tnup | grep ESTA >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
echo "| Disk Space information: |" >> $TEMPFILE
echo "-------------------------------------------" >> $TEMPFILE
/bin/df -h >> $TEMPFILE
echo "-----------------THE END-------------------" >> $TEMPFILE
L05="$(uptime|awk '{print $(NF-2)}'|cut -d. -f1)"
if test $L05 -gt 5
then
mail -s "$SUBJECT $L05" "$EMAIL" < $TEMPFILE
mail -s "$SUBJECT $L05" "$EMAIL1" < $TEMPFILE
fi
rm -f $TEMPFILE
---------------------------------------------------------------------
* Give exec permission
chmod +x /root/loadalert
* Now add below in roots cron file /var/spool/cron/root
* * * * * /root/loadalert >/dev/null 2>&1
* Restart crond
/etc/init.d/crond restart
* Check log for errors:
/var/log/cron
* Check Mail log
/var/log/exim_mainlog
if running exim.