在 Ubuntu 上监控邮件服务器,通常取决于你使用的 MTA(邮件传输代理),如 Postfix、Exim、Sendmail,以及是否包含 Dovecot(IMAP/POP3)。下面给你一套从基础到进阶的完整监控方案,适用于生产环境。
邮件服务器一般包含以下组件:
| 类型 | 示例 | 监控重点 |
|---|---|---|
| MTA | Postfix / Exim | 队列、发送成功率、延迟 |
| MDA | Dovecot | 登录、收信、认证 |
| 反垃圾 | SpamAssassin / Amavis | 垃圾邮件数量 |
| 安全 | Fail2ban | 暴力破解 |
| 系统 | Ubuntu | CPU、内存、磁盘、网络 |
mailq
或
postqueue -p
✅ 重点关注:
Postfix 日志位置:
/var/log/mail.log
实时查看:
tail -f /var/log/mail.log
常见错误:
| 关键词 | 含义 |
|---|---|
| deferred | 邮件延迟 |
| rejected | 被拒 |
| bounce | 退信 |
| timeout | 超时 |
安装:
sudo apt install pflogsumm
生成日报:
pflogsumm /var/log/mail.log
✅ 可看到:
systemctl status postfix
systemctl status dovecot
自动重启(推荐):
sudo systemctl enable postfix dovecot
ss -lntp
常见端口:
| 服务 | 端口 |
|---|---|
| SMTP | 25 |
| SMTPS | 465 |
| Submission | 587 |
| IMAP | 143 |
| IMAPS | 993 |
安装:
sudo apt install monit
示例配置 /etc/monit/conf.d/mail:
check process postfix with pidfile /var/spool/postfix/pid/master.pid
start program = "/bin/systemctl start postfix"
stop program = "/bin/systemctl stop postfix"
if failed port 25 then alert
check process dovecot with pidfile /var/run/dovecot/master.pid
start program = "/bin/systemctl start dovecot"
stop program = "/bin/systemctl stop dovecot"
启动:
sudo systemctl restart monit
✅ 可自动重启 + 邮件/短信告警
监控项包括:
示例监控脚本(队列):
mailq | tail -n 1
sudo apt install fail2ban
检查状态:
fail2ban-client status dovecot
fail2ban-client status postfix
防止成为垃圾邮件跳板:
grep "status=sent" /var/log/mail.log | wc -l
df -h /var/mail
df -h /var/spool/postfix
count=$(mailq | grep -c "^[A-F0-9]")
if [ "$count" -gt 100 ]; then
echo "邮件队列异常:$count" | mail -s "邮件队列告警" admin@example.com
fi
| 工具 | 用途 |
|---|---|
| Grafana + Prometheus | 图表 |
| ELK | 日志分析 |
| Zabbix | 综合监控 |
✅ 必做:
✅ 可选:
你可以告诉我:
我可以直接给你一份 可复制的完整监控脚本或 Zabbix 模板。