Operations 8 min read

Automate Linux Email Alerts and Memory Monitoring with msmtp, mutt, and Bash

This guide walks through installing msmtp, configuring mutt to send emails, using the free command to monitor memory and swap, creating Bash scripts for logging and alerting, and scheduling the tasks with cron for continuous system health reporting.

ITPUB
ITPUB
ITPUB
Automate Linux Email Alerts and Memory Monitoring with msmtp, mutt, and Bash

1. Install msmtp

# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make install

Download the source from http://downloads.sourceforge.net/.../msmtp-1.4.16.tar.bz2 and compile it with the commands above.

2. Create msmtp configuration and log file

# vim ~/.msmtprc
account default
host 126.com
from [email protected]
auth login
user test
password 123456
logfile ~/.msmtp.log

# chmod 600 ~/.msmtprc
# touch ~/.msmtp.log

This file tells msmtp how to connect to the SMTP server and where to write logs.

3. 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"

Most Linux distributions already have mutt installed; the above settings make mutt invoke msmtp for sending mail.

4. Test email sending

# echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt [email protected]

5. Monitor memory with the free command

# free -m
# free -m | grep Mem | awk '{print $4}'   # physical memory free (M)
# free -m | grep - | awk '{print $4}'    # buffer cache free (M)
# free -m | grep Swap | awk '{print $4}' # swap free (M)

6. Bash script to record memory statistics

# vim /scripts/free-mem.sh
#!/bin/bash
# Record current time
date >> /scripts/date-time.txt
# Record free values
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
# Combine into one file
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

7. Script to email the memory report

# vim /scripts/sendmail-mem.sh
#!/bin/bash
# Get server IP
IP=`ifconfig eth0 | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "`
# Current date
today=`date -d "0 day" +%Y年%m月%d日`
# Send email with attachment
echo "这是${IP}服务器${today}的内存监控报告,请下载附件。" \
| mutt -s "${IP}服务器${today}内存监控报告" -a /scripts/freemem.txt [email protected]
chmod a+x /scripts/sendmail-mem.sh

8. Swap usage warning script (alert when usage >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=$(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.sh

9. Schedule tasks with cron

# crontab -e
*/10 * * * * /scripts/free-mem.sh
*/10 * * * * /scripts/swap-warning.sh
0 8 * * * /scripts/sendmail-mem.sh
# service crond restart

These entries run memory collection and swap warning every ten minutes and send a daily summary at 08:00.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxcronMemory MonitoringBash Scriptingmsmtpmutt
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.