How to Set Up Automated Email Alerts for Linux Memory and Swap Monitoring
Learn step-by-step how to install the msmtp email client, configure mutt for sending mail, use the free command to monitor Linux memory and swap usage, script automated monitoring and alerting, and schedule these tasks with cron for continuous system health checks.
1. Install msmtp email client
Download and extract the source, then compile and install:
# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make installCreate configuration and log files (replace host, user, password as needed):
# 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:
# echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt [email protected]3. Monitor memory with free command
Show memory usage in megabytes:
# free -m
total used free shared buffers cached
Mem: 3952 3414 538 0 168 484
-/+ buffers/cache: 2760 1191
Swap: 8191 86 8105Extract specific free values:
# free -m | grep Mem | awk '{print $4}'
# free -m | grep - | awk '{print $4}'
# free -m | grep Swap | awk '{print $4}'4. Script to record memory statistics
# vim /scripts/free-mem.sh
#!/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.sh
# /scripts/free-mem.sh5. Email the memory report
# vim /scripts/sendmail-mem.sh
#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f2 -d ":" | cut -f1 -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.sh6. Warn when swap usage exceeds 80%
# vim /scripts/swap-warning.sh
#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f2 -d ":" | cut -f1 -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.sh7. 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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
