摘要:Alertmanager 只会「调度」,不会「发邮件」——发邮件得靠 MTA。这篇用 Postfix 搭一个本机中继,把告警邮件从虚拟机的 Ubuntu 一路送到你的 QQ 邮箱:main.cf六个参数逐条解释、sasl_passwd授权码文件的写法与权限、发件人一致性这个必踩的坑,以及那个经典的[::1]:25 connection refused。配完,你会第一次收到自己服务器发来的告警邮件。承接上篇:Alertmanager 装好了,告警能在 UI 里看到了,但邮箱里什么都没有。一、Alertmanager 有嘴,但嘴被封住了早上到工位,小林打开 Alertmanager UI,看到昨天触发的HighCpuUsage还挂在那。「周哥,Alertmanager 能看到告警了,但我邮箱里啥都没有。」「因为Alertmanager 的『嘴』还没通。」老周说。「啥意思?」「Alertmanager 知道要发邮件,但它不会自己发。它得调用一个『邮件传输代理』,也就是MTA。」老周说,「Linux 上最常用的 MTA 就是Postfix。」「那 Alertmanager 和 Postfix 啥关系?」「Alertmanager 把邮件内容交给 Postfix,Postfix 负责把邮件发出去。」老周说,「你可以把 Alertmanager 理解成『写信的人』,Postfix 理解成『邮局』。信写好了,得有人送到邮局,邮局再发出去。」「那为什么不用 Alertmanager 直接连 QQ 邮箱?」「可以,但配置更复杂。」老周说,「Alertmanager 的email_configs直接配smtp_smarthost也能发,但 SMTP 认证、TLS、授权码这些,全得写进alertmanager.yml。用 Postfix 做本地中继,Alertmanager 只需要写localhost:25,剩下的交给 Postfix。以后换邮箱,只动 Postfix 一个文件。」「那我 Mac 上能用 Postfix 吗?」「Mac 也有 Postfix,但 Mac 的 Postfix 配置和 Ubuntu 不一样。」老周说,「而且邮件是在虚拟机里发出去的,跟 Mac 没关系。Postfix 装在 Ubuntu 上。」小林记下:Alertmanager 不直接发邮件,它调用 MTA;Linux 上最常用的 MTA 是 Postfix;Postfix 做本地中继,Alertmanager 只写localhost:25;Postfix 装在 Ubuntu 上。二、安装 Postfixsudoaptupdatesudoaptinstall-ypostfix mailutils安装过程中,弹出一个界面:Postfix Configuration General type of mail configuration: No configuration Internet Site Internet with smarthost Satellite system Local only「选哪个?」小林问。「选Internet Site。」老周说,「然后系统主机名填你的 Ubuntu 主机名,或者直接回车用默认。」小林选了 Internet Site,回车。安装完成。「mailutils是啥?」小林问。「mailutils提供mail命令,用来测试发信。」老周说,「Postfix 本身没有发信客户端,mailutils补上。」「那 Postfix 现在在跑吗?」systemctl status postfix输出:● postfix.service - Postfix Mail Transport Agent Active: active (running)「在跑。」小林说。「验证端口。」sudoss-lntp|grep:25小林原以为会看到一行,结果出来两行:LISTEN 0 100 0.0.0.0:25 0.0.0.0:* users:(("master",pid=1234,fd=13)) LISTEN 0 100 [::]:25 [::]:* users:(("master",pid=1234,fd=13))「诶?它不是应该只监听本机吗?」「这就是Internet Site的默认行为——inet_interfaces = all,所有网卡都听。」老周说,「我们是做本机中继,用不着让外面连进来。收窄一下。」sudopostconf-e'inet_interfaces = 127.0.0.1'sudosystemctl restart postfix再验证:sudoss-lntp|grep:25输出:LISTEN 0 100 127.0.0.1:25 0.0.0.0:* users:(("master",pid=1234,fd=13))「127.0.0.1:25,现在只有本机能连了。」老周说,「Alertmanager 和 Postfix 在同一台机器,localhost:25就够了。」小林记下:安装:sudo apt install postfix mailutils;配置类型:Internet Site(但要手动收窄监听范围);默认监听:all(所有网卡),改成127.0.0.1(只本机);mailutils提供mail测试命令。三、配置 QQ 邮箱 SMTP 中继「现在配 QQ 邮箱。」老周说,「你先去 QQ 邮箱开 SMTP 服务,拿授权码。」「授权码?不是密码?」「QQ 邮箱
阅读完成 · 觉得有帮助?