A Shell Script to Send Email Alert When Memory Gets Low

[‘

n

A powerful aspect of Unix/Linux shell programs such as bash, is their amazing support for common programming constructs that enable you to make decisions, execute commands repeatedly, create new functions, and so much more. You can write commands in a file known as a shell script and execute them collectively.

n

This offers you a reliable and effective means of system administration. You can write scripts to automate tasks, for instance daily back ups, system updates etc; create new custom commands/utilities/tools and beyond. You can write scripts to help you keep up with what’s unfolding on a server.

n

One of the critical components of a server is memory (RAM), it greatly impacts on overall performance of a system.

n

In this article, we will share a small but useful shell script to send an alert email to one or more system administrator(s), if server memory is running low.

n

This is script is particularly useful for keeping an eye on Linux VPS (Virtual Private Servers) with small amount of memory, say of about 1GB (approximately 990MB).

n

Testing Environment Setup

n

    n

  1. A CentOS/RHEL 7 production server with mailx utility installed with working postfix mail server.
  2. n

n

This is how the alertmemory.sh script works: first it checks the free memory size, then determines if amount of free memory is less or equal to a specified size (100 MB for the purpose of this guide), used as a bench mark for the least acceptable free memory size.

n

If this condition is true, it will generate a list of the top 10 processes consuming server RAM and sends an alert email to specified email addresses.

n

Note: You will have to make a few changes to script (especially the mail sender utility, use the appropriate flags) to meet your Linux distributions requirements.

n

Shell Script to Check Server Memory

n

#!/bin/bash rn#######################################################################################rn#Script Name    :alertmemory.shrn#Description    :send alert mail when server memory is running lowrn#Args           :       rn#Author         :Aaron Kili Kisingarn#Email          :[emailxa0protected]rn#License       : GNU GPL-3trn#######################################################################################rn## declare mail variablesrn##email subject rnsubject="Server Memory Status Alert"rn##sending mail asrnfrom="[emailxa0protected]"rn## sending mail tornto="[emailxa0protected]"rn## send carbon copy tornalso_to="[emailxa0protected]"rnrn## get total free memory size in megabytes(MB) rnfree=$(free -mt | grep Total | awk '{print $4}')rnrn## check if free memory is less or equals to  100MBrnif [[ "$free" -le 100  ]]; thenrn        ## get top processes consuming system memory and save to temporary file rn        ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txtrnrn        file=/tmp/top_proccesses_consuming_memory.txtrn        ## send email if system memory is running lowrn        echo -e "Warning, server memory is running low!\n\nFree memory: $free MB" | mailx -a "$file" -s "$subject" -r "$from" -c "$to" "$also_to"rnfirnrnexit 0rn

n

After creating your script /etc/scripts/alertmemory.sh, make it executable and symlink to cron.hourly.

n

# chmod +x /etc/scripts/alertmemory.shrn# ln -s -t /etc/cron.hourly/alertmemory.sh /etc/scripts/alertmemory.shrn

n

This means that the above script will be run after every 1 hour as long as the server is running.

n

Tip: You can test if it is working as intended, set the bench mark value a little high to easily trigger an email to be sent, and specify a small interval of about 5 minutes.

n

Then keep on checking from the command line using the free command provided in the script. Once you confirm that it is working, define the actual values you would like to use.

n

Below is a screenshot showing a sample alert email.

n

Linux Memory Email Alert
Linux Memory Email Alert

n

That’s all! In this article, we explained how to use shell script to send alert emails to system administrators in case server memory (RAM) is running low. You can share any thoughts relating to this topic, with us via the feedback form below.

n

‘]