Setup load Alerts on your dedicated server or VPS

A customer of ours wanted to get emails from his server, if the load ever rose above a certain number.

Here’s a script I wrote up to accomplish this:

EMAIL="your email 1"
SUBJECT="Alert $(hostname) load average is $L05"
TEMPFILE="/tmp/$(hostname)"
TOPLOAD="10"
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 $TOPLOAD
then
mail -s "$SUBJECT  $L05" "$EMAIL" < $TEMPFILE
fi
rm -f $TEMPFILE
[/bash]

Add this to the root crontab on the server by running:
[bash]echo "* * * * * /root/loadalert >/dev/null 2>&1" >> /var/spool/cron/root

Now we need to restart crond to pick up the change:

/etc/init.d/crond restart

I hope this helps someone out there!


Warning: PHP Startup: Unable to load dynamic library '/opt/cpanel/ea-php56/root/usr/lib64/php/modules/htscanner.so' - /opt/cpanel/ea-php56/root/usr/lib64/php/modules/htscanner.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: Module 'ctype' already loaded in Unknown on line 0

Warning: Module 'curl' already loaded in Unknown on line 0

Warning: Module 'dom' already loaded in Unknown on line 0

Warning: Module 'ftp' already loaded in Unknown on line 0

Warning: Module 'gd' already loaded in Unknown on line 0

Warning: Module 'json' already loaded in Unknown on line 0

Warning: Module 'mbstring' already loaded in Unknown on line 0

Warning: Module 'mysqlnd' already loaded in Unknown on line 0

Warning: Module 'mysql' already loaded in Unknown on line 0

Warning: Module 'mysqli' already loaded in Unknown on line 0

Warning: Module 'PDO' already loaded in Unknown on line 0

Warning: Module 'pdo_mysql' already loaded in Unknown on line 0

Warning: Module 'pdo_sqlite' already loaded in Unknown on line 0

Warning: Module 'Phar' already loaded in Unknown on line 0

Warning: Module 'posix' already loaded in Unknown on line 0

Warning: Module 'SimpleXML' already loaded in Unknown on line 0

Warning: Module 'sqlite3' already loaded in Unknown on line 0

Warning: Module 'tokenizer' already loaded in Unknown on line 0

Warning: Module 'xml' already loaded in Unknown on line 0

Warning: Module 'wddx' already loaded in Unknown on line 0

Warning: Module 'xmlreader' already loaded in Unknown on line 0

Warning: Module 'xmlwriter' already loaded in Unknown on line 0

Warning: Module 'xsl' already loaded in Unknown on line 0