How to Set Up Automated Linux Memory & Swap Monitoring with Email Alerts
Learn step‑by‑step how to install the msmtp email client, configure mutt, use the free command to monitor Linux memory and swap usage, write Bash scripts that log and email the results, and schedule these checks with cron for continuous system health alerts.
1. Install msmtp email client (similar to Foxmail)
Download and extract, then configure and install:
# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make installCreate configuration file ~/.msmtprc with host, user, password, logfile, and set permissions:
# vim ~/.msmtprc
account default
host 126.com
from [email protected]
auth login
user test
password 123456
logfile ~/.msmtp.log
# chmod 600 ~/.msmtprc
# touch ~/.msmtp.log2. Configure mutt to use msmtp
# vim ~/.muttrc
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="memory"
set [email protected]
set envelope_from=yes
set rfc2047_parameters=yes
set charset="utf-8"Test sending an email with attachment:
# echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt [email protected]3. Monitor system memory with free
Use free -m to view memory usage in megabytes.
Extract specific values:
# free -m | grep Mem | awk '{print $4}' # physical free
# free -m | grep - | awk '{print $4}' # buffers/cache free
# free -m | grep Swap | awk '{print $4}' # swap freeCreate a Bash script /scripts/free-mem.sh that records timestamp, memory, buffers, swap values and writes them to /scripts/freemem.txt:
#!/bin/bash
date >> /scripts/date-time.txt
echo Mem-free: `free -m | grep Mem | awk '{print $4}'`M >> /scripts/mem-free.txt
echo buffers/cache-free: `free -m | grep - | awk '{print $4}'`M >> /scripts/buffers-free.txt
echo Swap-free: `free -m | grep Swap | awk '{print $4}'`M >> /scripts/swap-free.txt
paste /scripts/date-time.txt /scripts/mem-free.txt /scripts/buffers-free.txt /scripts/swap-free.txt > /scripts/freemem.txt
chmod a+x /scripts/free-mem.shSend the memory report via email:
#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
today=`date -d "0 day" +%Y年%m月%d日`
echo "这是$IP服务器$today的内存监控报告,请下载附件。" | mutt -s "$IP服务器$today内存监控报告" -a /scripts/freemem.txt [email protected]
chmod a+x /scripts/sendmail-mem.sh4. Monitor swap usage and send alerts when over 80%
Script /scripts/swap-warning.sh calculates free percentage and compares with a 20% threshold:
#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
swap_total=`free -m | grep Swap | awk '{print $2}'`
swap_free=`free -m | grep Swap | awk '{print $4}'`
swap_used=`free -m | grep Swap | awk '{print $3}'`
if ((swap_used != 0)); then
swap_per=0`echo "scale=2;$swap_free/$swap_total" | bc`
swap_warn=0.20
swap_now=`expr $swap_per \> $swap_warn`
if (($swap_now == 0)); then
echo "$IP服务器swap交换分区只剩下 $swap_free M 未使用,剩余不足20%,使用率已经超过80%,请及时处理。" | \
mutt -s "$IP 服务器内存告警" [email protected]
fi
fi
chmod a+x /scripts/swap-warning.sh5. Schedule tasks with cron
# crontab -e
*/10 * * * * /scripts/free-mem.sh
*/10 * * * * /scripts/swap-warning.sh
0 8 * * * /scripts/sendmail-mem.sh
service crond restartSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
