TSF – Giải pháp IT toàn diện cho doanh nghiệp SMB | HCM

P13 - Giám sát Mail Server Bằng PRTG Nâng Cao

PRTG – P13 Advanced Mail Server Monitoring with PRTG Network Monitor

Giám sát cơ bản là chưa đủ đối với hệ thống mail production.
Để đảm bảo tính ổn định, bảo mật và khả năng gửi nhận mail liên tục, bạn cần một mô hình giám sát mail server bằng PRTG ở mức nâng cao.

Trong bài viết này, chúng ta triển khai monitoring chuyên sâu cho Mailcow chạy trên Docker, sử dụng PRTG SSH Script Advanced Sensor, kiểm tra DNS, blacklist và phát hiện spam theo thời gian thực.

Mô hình này phù hợp cho môi trường doanh nghiệp yêu cầu tính sẵn sàng cao.


🐳 MAILCOW & DOCKER

Mailcow hoạt động dựa trên nhiều container Docker.
Chỉ cần một container quan trọng dừng hoạt động, toàn bộ hệ thống mail có thể bị ảnh hưởng.

Các container cần theo dõi:

  • postfix-mailcow

  • dovecot-mailcow

  • rspamd-mailcow

  • clamav-mailcow

  • nginx-mailcow


#1️⃣ SSH Script Advanced – Docker Containers

🎯 Function

• Kiểm tra các container Mailcow quan trọng có đang chạy hay không

Output:
• 1 = OK
• 0 = FAIL

Threshold:
• ❌ Lower DOWN: 1

📌 Chỉ cần 1 container lỗi → mail có thể ngừng hoạt động.


Step 1: Cấp quyền Docker cho SSH user

 
 
sudo usermod -aG docker bao
 

Sau đó reboot mail server.

Kiểm tra lại:

 
 
docker ps
 

Nếu không còn yêu cầu quyền truy cập, cấu hình đã đúng.


Step 2: Tạo Script kiểm tra

 
 
sudo mkdir -p /var/prtg/scriptsxml
 

Tạo file:

 
 
sudo nano /var/prtg/scriptsxml/check_mailcow_containers.sh
 

Nội dung Script

 
💻
filename.sh
#!/bin/bash

PATTERNS=(
  "postfix-mailcow"
  "dovecot-mailcow"
  "rspamd-mailcow"
  "clamd-mailcow"
  "nginx-mailcow"
)

FAILED=0
FAILED_LIST=""

for p in "${PATTERNS[@]}"; do
  running=$(docker ps \
    --filter "name=$p" \
    --filter "status=running" \
    --format '{{.Names}}')

  if [ -z "$running" ]; then
    FAILED=$((FAILED+1))
    FAILED_LIST="$FAILED_LIST $p"
  fi
done

# Message
if [ "$FAILED" -eq 0 ]; then
  MSG="All mailcow containers are running"
else
  MSG="Container down:$FAILED_LIST"
fi

cat <<EOF
<prtg>
  <result>
    <channel>Mailcow Containers</channel>
    <value>$FAILED</value>
    <unit>Count</unit>
    <limitmaxerror>0</limitmaxerror>
    <limitmode>1</limitmode>
  </result>
  <text>$MSG</text>
</prtg>
EOF

exit 0

Cấp quyền thực thi:

 
 
sudo chmod +x /var/prtg/scriptsxml/check_mailcow_containers.sh
 

Kiểm tra thủ công:

 
 
sudo /var/prtg/scriptsxml/check_mailcow_containers.sh
 

Step 3: Restart Probe Services

Restart PRTG Probe để nhận diện script mới.


Step 4: Add Sensor – SSH Script Advanced

Kết quả:

• 0 → OK
• 1 → Cảnh báo ngay lập tức


Step 5: Kiểm tra thực tế

 
 
sudo docker stop mailcowdockerized-rspamd-mailcow-1
 

Script phải trả về lỗi → PRTG DOWN ngay lập tức.

Khởi động lại:

 
 
sudo docker start mailcowdockerized-rspamd-mailcow-1
 

#2️⃣ SSH Script Advanced – Mail Queue

🎯 Function

• Giám sát mail queue bị kẹt

Channel:
• Queue Count

📌 Queue tăng cao → mail gửi đi bị block hoặc blacklist.


🟢 Case 1 – Phát hiện ngay khi có mail kẹt

Step 1: Tạo Script

 
 
sudo nano /var/prtg/scriptsxml/check_mail_queue.sh
 

Nội dung Script

💻
filename.sh
#!/bin/bash

# Get mail queue count (Postfix)
QUEUE_COUNT=$(postqueue -p 2>/dev/null | grep -c '^[A-F0-9]')

# Fallback if command is faulty
[ -z "$QUEUE_COUNT" ] && QUEUE_COUNT=0

# Message
if [ "$QUEUE_COUNT" -eq 0 ]; then
  MSG="Mail queue empty (outgoing OK)"
else
  MSG="Mail queue pending: $QUEUE_COUNT"
fi

cat <<EOF
<prtg>
  <result>
    <channel>Queue Count</channel>
    <value>$QUEUE_COUNT</value>
    <unit>Count</unit>
    <limitmaxwarning>100</limitmaxwarning>
    <limitmaxerror>500</limitmaxerror>
    <limitmode>1</limitmode>
  </result>
  <text>$MSG</text>
</prtg>
EOF

exit 0

Cấp quyền và test:

 
 
sudo chmod +x /var/prtg/scriptsxml/check_mail_queue.sh
sudo /var/prtg/scriptsxml/check_mail_queue.sh
 

Step 2: Add Sensor


Step 3: Cấu hình Threshold

Khuyến nghị:

LevelValue
⚠️ Warning1
❌ Error50

🟡 Case 2 – Hệ thống nhiều mail

LevelValue
⚠️ Warning20
❌ Error100

🔴 Case 3 – Chỉ phục vụ báo cáo

LevelValue
⚠️ Warning100
❌ Error500

#3️⃣ DNS Sensor (MX Record)

🎯 Function

• Kiểm tra MX Record tồn tại

URL:

 
 
https://dns.google/resolve?name=tsf.id.vn&type=MX
 

Required keyword:

 
 
mail.tsf.id.vn
 

Threshold đề xuất

🔴 Loading Time

StatusValue
⚠️ Warning> 1500 ms
❌ Down> 5000 ms

🟡 Time to First Byte

StatusValue
⚠️ Warning> 800 ms
❌ Down> 3000 ms

#4️⃣ Blacklist Check

🎯 Function

• Kiểm tra IP mail có bị blacklist hay không


Step 1: Tạo Script

 
 
sudo nano /var/prtg/scriptsxml/check_mail_blacklist.sh
 

Nội dung Script

 
💻
filename.sh
#!/bin/bash

IP=$(curl -s https://api.ipify.org)
[ -z "$IP" ] && IP="UNKNOWN"

REVERSED_IP=$(echo $IP | awk -F. '{print $4"."$3"."$2"."$1}')

RBL_LIST=(
  "zen.spamhaus.org"
  "bl.spamcop.net"
  "b.barracudacentral.org"
  "dnsbl.sorbs.net"
  "psbl.surriel.com"
)

COUNT=0
LISTED=""

for RBL in "${RBL_LIST[@]}"; do
  if dig +short ${REVERSED_IP}.${RBL} | grep -qE '^[0-9]'; then
    COUNT=$((COUNT+1))
    LISTED="$LISTED $RBL"
  fi
done

MSG="IP $IP clean"
[ "$COUNT" -gt 0 ] && MSG="IP $IP listed on:$LISTED"

cat <<EOF
<prtg>
  <result>
    <channel>Blacklist Count</channel>
    <value>$COUNT</value>
    <unit>Count</unit>
    <limitmaxwarning>0</limitmaxwarning>
    <limitmaxerror>1</limitmaxerror>
    <limitmode>1</limitmode>
  </result>
  <text>$MSG</text>
</prtg>
EOF

exit 0

Cấp quyền và test:

 
 
sudo chmod +x /var/prtg/scriptsxml/check_mail_blacklist.sh
sudo /var/prtg/scriptsxml/check_mail_blacklist.sh
 

⛔ Không nên scan quá nhanh để tránh bị RBL chặn.


#5️⃣ Sensor – Rejected Incoming Mail (Spam Check)

🎯 Function

• Giám sát mail bị từ chối khi gửi vào hệ thống
• Phát hiện spam attack, virus hoặc policy reject

📌 Rejected Incoming Mail ≠ Mail Queue


Step 1: Tạo Script

 
 
sudo nano /var/prtg/scriptsxml/check_mail_incoming_reject.sh
 

Nội dung Script

 
💻
filename.sh
#!/bin/bash

# Check time
MINUTES=5

# Container rspamd
RSPAMD_CONTAINER="mailcowdockerized-rspamd-mailcow-1"

# Timestamp từ MINUTES phút trước
SINCE_TS=$(date -d "$MINUTES minutes ago" +"%Y-%m-%d %H:%M")

# Count mail incoming reject
REJECT_COUNT=$(docker logs --since "$SINCE_TS" "$RSPAMD_CONTAINER" 2>/dev/null \
  | grep -i "action=reject" \
  | wc -l)

[ -z "$REJECT_COUNT" ] && REJECT_COUNT=0

# Message
if [ "$REJECT_COUNT" -eq 0 ]; then
  MSG="No incoming mail rejected in last ${MINUTES} minutes"
else
  MSG="Incoming rejected: $REJECT_COUNT mails in last ${MINUTES} minutes"
fi

cat <<EOF
<prtg>
  <result>
    <channel>Incoming Reject Count</channel>
    <value>$REJECT_COUNT</value>
    <unit>Count</unit>
    <limitmaxwarning>1</limitmaxwarning>
    <limitmaxerror>10</limitmaxerror>
    <limitmode>1</limitmode>
  </result>
  <text>$MSG</text>
</prtg>
EOF

exit 0

Cấp quyền và kiểm tra:

 
 
sudo chmod +x /var/prtg/scriptsxml/check_mail_incoming_reject.sh
sudo /var/prtg/scriptsxml/check_mail_incoming_reject.sh
 

Step 2: Add Sensor


Step 3: Threshold khuyến nghị

LevelValue
⚠️ Warning1
❌ Error10

🏁 Kết luận

Giám sát nâng cao không chỉ dừng lại ở CPU hay SMTP.
Một hệ thống giám sát mail server bằng PRTG chuyên nghiệp cần bao phủ:

  • Trạng thái Docker container

  • Mail queue

  • MX record DNS

  • Blacklist IP

  • Spam reject

Với cấu hình này, hệ thống Mailcow của bạn được bảo vệ ở mức hạ tầng, dịch vụ và bảo mật.

Đây là mô hình monitoring thực tế dành cho sysadmin muốn chủ động kiểm soát, thay vì chỉ xử lý sự cố khi người dùng phản ánh.

 
 

Tham khảo thêm bài viết cùng chủ đề

P3 – Giải Pháp Mạnh Mẽ Giám Sát IP WAN Với PRTG

P3 – Giải Pháp Mạnh Mẽ Giám Sát IP WAN Với PRTG PRTG – P3 Giám Sát Internet & WAN IP Với PRTG Network Monitor Việc theo dõi kết nối Internet là yếu tố sống còn trong hạ tầng doanh nghiệp. Khi đường truyền WAN gặp sự cố, các dịch...

Read More

P1 – Hướng Dẫn Cài Đặt PRTG Hoàn Chỉnh Windows 10

P1 – Hướng Dẫn Cài Đặt PRTG Hoàn Chỉnh Windows 10 🚀 Cài đặt PRTG trên Windows 10 – Initial Setup & Configuration (P1) Nếu bạn đang bắt đầu triển khai hệ thống giám sát hạ tầng, thì việc Cài đặt PRTG đúng cách là bước nền tảng cực kỳ...

Read More