Automate Linux Memory & Swap Monitoring with Email Alerts
This guide walks through installing the msmtp email client, configuring mutt, using the free command to capture memory and swap statistics, writing Bash scripts to log and email the data, and scheduling the tasks with cron so alerts are sent when swap usage exceeds 80 %.
来自网络旧文,原作者不详
1. Install and configure msmtp
Download the msmtp source package, extract it, compile and install it to /usr/local/msmtp using the typical ./configure --prefix=/usr/local/msmtp, make and make install steps.
Create the configuration file ~/.msmtprc with the SMTP server, user name, password and log file, then protect it with chmod 600 and create the log file.
Configure ~/.muttrc to use msmtp as the sendmail program, set the sender address, enable envelope‑from and UTF‑8 charset.
Test the setup with a mutt command that pipes a message body and attaches a file, e.g.
echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt [email protected].
2. Monitor memory with the free command
Run free -m to display memory usage in megabytes. Extract the free values for physical memory, buffers/cache and swap using awk pipelines.
Example commands:
# 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 free3. Script to log memory statistics
Create /scripts/free-mem.sh that records the current timestamp, the three free values, and concatenates them into /scripts/freemem.txt using paste. Make the script executable.
#!/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.txt4. Script to email the memory report
The script /scripts/sendmail-mem.sh extracts the server’s IP address and the current date, then sends freemem.txt as an attachment with a descriptive subject line.
#!/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]5. Swap usage warning script
The script /scripts/swap-warning.sh calculates the percentage of free swap space, compares it with a 20 % threshold (meaning usage >80 %), and sends an alert email when the threshold is exceeded.
#!/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=$(echo "scale=2;$swap_free/$swap_total" | bc)
swap_warn=0.20
swap_now=$(echo "$swap_per > $swap_warn" | bc)
if ((swap_now == 0)); then
echo "$IP 服务器swap交换分区只剩下 ${swap_free}M 未使用,剩余不足20%,使用率已经超过80%,请及时处理。" |
mutt -s "${IP} 服务器内存告警" [email protected]
fi
fi6. Schedule the tasks with cron
Add the following entries to crontab -e so that memory and swap checks run every ten minutes, and the daily summary is sent at 08:00.
*/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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
