In this post and this, we know we can use several methods to check the system load average for Linux OS. We also know that, similar to this post (get a email when VPS is rebooted), we can setup a crontab job that runs at the given interval.
So, we can get a email to alert us that the system load average is high (exceeded a threshold).
1 | */2 * * * * /var/www/checkload.sh > /dev/null 2>&1 |
*/2 * * * * /var/www/checkload.sh > /dev/null 2>&1
The above runs every 2 minutes, so we need to create this checkload.sh using your favorite text editor.
1 2 3 4 5 6 7 8 9 | #!/bin/bash load=`echo $(cat /proc/loadavg | awk '{print $1}') \> 3 | bc -l` if [ "$load" -ne 0 ]; then echo "Your Server Load Alert Needs Attention! " | mail -s "System Load Alert $load" mail@helloacm.com echo "Alert email sent to [email protected]" fi echo "System Load $(cat /proc/loadavg)" |
#!/bin/bash load=`echo $(cat /proc/loadavg | awk '{print $1}') \> 3 | bc -l` if [ "$load" -ne 0 ]; then echo "Your Server Load Alert Needs Attention! " | mail -s "System Load Alert $load" [email protected] echo "Alert email sent to [email protected]" fi echo "System Load $(cat /proc/loadavg)"
We use awk to extract the first number (similar as the split function) and pipe in the calculator bc if it is larger than 3 (changed this accordingly). If the load average is more than 3, then the result will be 1 (-ne 0). Then we send a email.
The VPS I am using is from QuickHOSTUK, which has 4 cores, setting the load average threshold to 3 should be reasonable for now.
PS: You can use api/cat-proc/?file=loadavg API from this page to run the command cat /proc/loadavg to see the load average of the VPS current blog is on.
This online utility helps configuration of the crontab.
We can integrate this script with showing the last requests so that we can get a rough understanding of what are causing the trouble to the server: How to Get the Last Requests to Apache2 Server?
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Check System Load Average under Linux?
Next Post: Speed Up Website By Caching Static Resources using Apache2 Cache Control (.htaccess)
Thanks for this nice script. It seems to do what I want. The only thing that isn’t working for me is – $load isn’t printed in the email. Any idea how to fix? I tried single quoting ‘$load’ – but it didn’t change the behavior.
Have you fixed this? the $load should be printed to console using the BASH echo. You can pass this value to your Linux script which sends email. Let me know if you can’t get it working. Thanks!