在 Ubuntu 上监控邮件服务器,通常取决于你使用的 MTA(邮件传输代理) 类型(如 Postfix、Exim、Sendmail)以及你希望监控的层级(服务状态、队列、日志、性能指标、可用性等)。下面按常见场景 + 工具给你一个完整、可落地的方案。
常见监控目标包括:
/var)systemctl status postfix
systemctl status dovecot
或一次性检查:
systemctl status postfix dovecot
✅ 推荐:写成定时脚本 + 告警
查看队列数量:
mailq | tail -n 1
示例输出:
-- 12 Kbytes in 3 Requests.
告警脚本示例:
count=$(mailq | tail -n 1 | awk '{print $5}')
if [ "$count" -gt 100 ]; then
echo "邮件队列堆积过多"
fi
Postfix 日志位置:
/var/log/mail.log
/var/log/mail.err
实时查看:
tail -f /var/log/mail.log
常见错误关键词:
grep -E "reject|error|bounce|timeout" /var/log/mail.log
✅ 可结合 logwatch 或 rsyslog
sudo apt install prometheus-node-exporter
可监控:
✅ 最推荐方案
Zabbix 可监控:
示例监控项:
net.tcp.port[<ip>,25]安装:
sudo apt install monit
示例配置 /etc/monit/conf.d/postfix:
check process postfix with pidfile /var/spool/postfix/pid/master.pid
start program = "/usr/sbin/service postfix start"
stop program = "/usr/sbin/service postfix stop"
if failed port 25 protocol smtp then alert
✅ 适合单台服务器
swaks(强烈推荐)安装:
sudo apt install swaks
测试发信:
swaks --to user@example.com \
--from monitor@yourdomain.com \
--server localhost
✅ 可写成定时任务 + 失败告警
监控:
/var/log/mail.log
关注:
sasl_method=PLAINsudo apt install fail2ban
监控 SMTP / IMAP 登录尝试。
| 场景 | 推荐方案 |
|---|---|
| 单台服务器 | Monit + 日志 + swaks |
| 中小企业 | Zabbix |
| 生产 / 云环境 | Prometheus + Grafana |
| 安全审计 | Fail2ban + 日志分析 |
# 1. 服务存活
systemctl status postfix dovecot
# 2. 队列
mailq | tail -n 1
# 3. 发信测试
swaks --to test@example.com --server localhost
# 4. 日志异常
tail -n 100 /var/log/mail.log | grep -i error
如果你愿意,可以告诉我:
我可以给你一个完全定制的监控方案 + 配置文件示例。