Bash script to check server load and notify by email

Bash script to check server load and notify by email

Shell scripts are always to handy to do some tasks on a Linux-based server for SysAdmins, they can automate everything they do frequently through a shell script. Here I’m going to share a simple script that I got from Muhammed Fasal to monitor server load on a Linux-based system and alert us via email if it exceeded a triggering value. This will definitely help us to investigate the server load issues as soon as possible without any paid monitoring addons, I will enhance this more soon to include a few other information which will help us to troubleshoot the load issue on a server.

#!/bin/bash
#
# Date: Jan 18th 2017
# Author: Muhammed Fasal (https://fasal.cf)
# Bash script for monitoring server load and alert us via Email if it exceeds a certain value
#
# Copyright (C) 2017 Muhammed Fasal
#
#variable for triggering
trig=5.00

#Get current load on the server
load=`cat /proc/loadavg | awk '{print $1}'`

#Check both
if [[ $load > $trig ]];
then
echo -e "\n\nYour server load is High. Check Immediatly\n\nServer load : $load " | mail -s "High Server Load [$load]" user@example.com
fi

You can adjust triggering value there on ‘trig‘ variable, default would be 5 also replace ‘user@example.com‘ with your email address where you wish to receive the alert. Save this on to a file check_load.sh, or you can download this directly from here. Once you have saved/downloaded the file there, create below cron job on the server to monitor the server.

*/2   *   *   *   *  /bin/bash   /path/to/the/folder/check_load.sh

The above crob job will monitor the server on every 2 minutes, and alert us if there any issue with the server load.

If you run into any issues, please use the comment below and let me know your thoughts or ideas for improvement. Thanks!

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.