Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

全鏈路監控與告警系統:打造企業級 Mail Platform 的監控中心

Posted on 2025-11-212025-11-21 by Rico

【Mail Server 系列文:第 16 篇】

在完成郵件系統架構、部署、郵件過濾、郵件歸檔、全文搜尋、HA、營運維護等所有主體系統後,本篇將介紹最後一塊關鍵拼圖:

如何替整套郵件平台建立 “企業級監控與告警系統”

一個郵件系統的可用性不僅取決於架構本身,更取決於:

  • 是否能即時發現異常
  • 是否能量化郵件環境的健康狀態
  • 是否能預測風險(磁碟滿、佇列暴增、CPU 100%)
  • 是否能避免用戶抱怨「收不到信」才發現問題

因此,本篇是從維運角度出發,完整介紹如何打造:

✔ 全鏈路監控(Full Stack Monitoring)
✔ 即時告警(Alerting)
✔ 事件追蹤(Tracing)
✔ 報表分析(Reporting)
✔ 適用於 Docker + Mail Stack 的監控架構


1. 郵件平台要監控哪些項目?(完整清單)

一套完整郵件平台包含:

  • Postfix(SMTP)
  • Dovecot(IMAP/POP3、LMTP)
  • Amavis / ClamAV / SpamAssassin
  • MariaDB / Galera
  • Roundcube
  • Piler
  • ManticoreSearch
  • Apache Reverse Proxy
  • Docker 主機、容器、磁碟

因此監控面向分成六大類:


① SMTP / Postfix 監控

指標說明
mail queue 數量佇列暴漲 = 郵件堵塞
defer / bounced 比例大量退信可能代表 DNS、連線、黑名單問題
送信延遲連線緩慢、對端拒絕、流量瓶頸
inbound/outbound TPS了解負載趨勢
reject 率若異常升高可能是 Spam 攻擊或 DNS 錯誤
TLS 啟用比例評估加密郵件占比

② Dovecot 監控(IMAP/POP3、登入)

指標說明
登入成功/失敗次數可偵測暴力登入攻擊
IMAP/LMTP 連線數過高代表資源耗盡
I/O 延遲代表儲存設備瓶頸
mailbox locking 錯誤代表 shared storage 問題
Auth 伺服器回應時間LDAP/MariaDB 延遲

③ Amavis / ClamAV / SpamAssassin

指標說明
ClamAV freshness病毒庫是否更新
Spam 命中率若突然暴跌 → SA 服務壞了
Amavis Process / Queue通常 Amavis 卡住會直接堵住整個 SMTP
CPU / RAM 使用量SpamAssassin 在高負載時非常吃 CPU

④ MariaDB / Galera Cluster

指標說明
主從延遲影響 PostfixAuth、Roundcube
Galera 状态Flow Control、节点健康、同步延遲
查詢時間大量 Slow Query 會拖垮所有系統
DB 大小piler 資料庫會隨時間成長
連線數是否有 connection leak

⑤ Piler + ManticoreSearch

指標說明
Manticore Search 延遲直接影響全文搜尋體驗
Store 目錄容量歸檔成長是永遠不停的
Indexer 延遲RT index 是否正常同步
Piler queue depth大量 backlog 代表寫入或儲存有問題
搜尋錯誤率字詞解析、index missing、schema error

⑥ 主機資源 / Docker 層級監控

指標說明
CPU / RAM / Load防止 OOM 影響所有容器
Disk I/ONFS/ZFS 停頓會造成 IMAP delay
網路延遲與 Dovecot、Postfix、DB 都密切相關
Docker Container 健康狀態Healthcheck / restart loop
Filesystem 用量磁碟滿會造成郵件系統崩潰

2. 推薦的完整監控架構(企業級)

以下為我建議的參考架構:

┌───────────────────────────────┐
│        Grafana Dashboard       │←── 呈現所有視覺化報表
└───────────────┬───────────────┘
                │
       Prometheus Server
                │
┌───────────────┼───────────────────────────────────────────┐
│               │                                           │
Exporter:   Postfix Exporter                     Node Exporter(主機)
            Dovecot Exporter                     Blackbox Exporter
            MariaDB Exporter                     Docker Exporter
            ClamAV Exporter                      Custom Piler & Manticore Exporter
└───────────────┴───────────────────────────────────────────┘

3. 要安裝哪些 Exporter?(含推薦)

以下是我整理過後的最佳組合:


3.1 Postfix Exporter

可監控:

  • Queue count
  • Reject / Bounce
  • Defer
  • 連線數
  • TLS 啟用率

推薦:

knyar/nginx-lua-prometheus-style postfix exporter

3.2 Dovecot Exporter

可監控:

  • Login fail rate
  • IMAP/POP3 連線數
  • Auth 成功率
  • 處理延遲

專用 exporter:

gh `/klen/dovecot_exporter`

3.3 ClamAV Exporter

監控:

  • 病毒庫更新時間
  • ClamAV process 健康
  • 掃描統計

3.4 MariaDB Exporter

Prometheus 官方提供:

prom/mysqld_exporter

3.5 Node Exporter

CPU、Memory、Disk、IO、Network
→ 必裝。


3.6 Blackbox Exporter

監控:

  • SMTP TLS
  • IMAP TLS
  • HTTPS(webmail、piler)

3.7 Docker Exporter

確認每個容器是否:

  • CrashLoop
  • OOMKilled
  • Restarting
  • Unhealthy

3.8 Custom Exporter for Piler & Manticore

建議自建:

  • index 延遲
  • 搜尋 latency
  • store usage
  • piler backlog

我可以幫你寫 custom exporter(如你需要)。


4. 建立 Grafana 視覺化儀表板(Dashboard)

我建議至少建立以下儀表板:


Dashboard A:郵件系統總覽(Mail System Overview)

包含:

  • inbound/outbound TPS
  • queue count
  • smtp reject ratio
  • dovecot login errors
  • DB 延遲
  • Piler index lag
  • Manticore search latency

適合主管或維運快速查看健康狀態。


Dashboard B:SMTP 深度監控(Postfix Deep View)

  • 每分鐘 SMTP 流量
  • 每項 restriction 的 reject 數量
  • TLS 搭配比例
  • domain 別寄送量
  • top sender domain / spam source

Dashboard C:Dovecot 深度監控(IMAP/LMTP)

  • login fail rate
  • auth response time
  • LMTP 投遞失敗統計
  • folder access 數量
  • I/O 讀寫延遲

Dashboard D:Piler / Manticore(Archive Search Dashboard)

  • 每分鐘 indexing rate
  • search latency
  • store free space
  • RT index memory consumption
  • index fragmentation

Dashboard E:主機資源 / Docker

  • CPU / Load
  • Memory usage
  • Disk I/O
  • Network latency
  • Container health

5. 告警(Alerting)規則建議

為了避免 false positive,我設計一套企業級穩定的告警規則。


郵件系統核心告警(務必啟用)

(1)Postfix Queue > 500 封(10 分鐘)

可能原因:
- DNS 錯誤
- SMTP 過載
- Amavis 卡住

(2)Dovecot 登入失敗率 > 30%

可能是暴力攻擊。


(3)ClamAV 24 小時未更新

病毒庫過期。


(4)Manticore Search Latency > 500 ms

搜尋體驗下降。


(5)Piler backlog > 1000 筆(延遲處理)

可能是 I/O 問題。


(6)MariaDB 查詢延遲 > 200 ms

會影響:

  • SMTP 認證
  • Roundcube 登入
  • Dovecot Auth
  • piler

(7)磁碟使用量 > 85%

尤其是:

/var/vmail
/var/piler/store

(8)Docker container unhealthy

例如:

amavis unhealthy
manticore restarting
postfix no listening port

6. SMTP / IMAP 外部探測監控(非常重要)

使用 Blackbox exporter:

定時探測:

smtp_starttls://mail.it.demo.tw:25
smtp_auth://mail.it.demo.tw:587
imap_starttls://mail.it.demo.tw:143
https://webmail.it.demo.tw
https://archive.it.demo.tw

可以測試:

  • 是否能建 TLS
  • 是否能正常握手
  • 是否有憑證過期
  • Roundcube / Piler 是否正常回應

7. 統一事件通知(Alert Channel)

建議:

  • Microsoft Teams
  • Slack
  • Telegram Bot
  • Email(當然也可以,但不建議作為唯一方式)

推薦用 Alertmanager 進行整合。


8. 實際部署建議

目前架構:

  • Docker-based(多容器)
  • nginx/apache 作為 Reverse Proxy
  • postfix + dovecot + amavis
  • piler(archive)
  • Manticore(全文搜尋)
  • MariaDB 單點(可未來升級 Galera)
  • 防火牆有 DOCKER-USER 自訂規則

建議部署:

docker host 上安裝:
- node_exporter
- docker_exporter

在 mail stack 專案內增加:
- postfix_exporter
- dovecot_exporter
- clamav_exporter
- mysqld_exporter
- blackbox_exporter

在管理主機部署:
- prometheus
- grafana
- alertmanager

結語:郵件系統若無監控,就不算真正進入生產環境

部署完成只是第一步。
真正的營運價值來自:

  • 能提前預測異常
  • 能第一時間收到告警
  • 能診斷問題
  • 能了解流量、趨勢、安全異常
  • 能避免影響使用者

本篇讓整套自建郵件系統具備了 生產等級的監控能力。

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme