ส่ง Log Report อัตโนมัติด้วย Logwatch บน VPS

ส่ง Log Report อัตโนมัติด้วย Logwatch บน VPS

มิถุนายน 2026 · อ่าน 6 นาที

Logwatch อ่าน log files ทุกตัวบน server ทุกวันและส่ง email report สรุปสิ่งที่เกิดขึ้น ทั้ง SSH login attempts, Nginx error, sudo usage, disk warning และอื่นๆ ทำให้ sysadmin เห็นความผิดปกติโดยไม่ต้อง SSH เข้าดู log ด้วยตัวเองทุกวัน เป็น minimum viable monitoring ที่ทุก VPS ควรมี

ติดตั้ง Logwatch และ Mail Transfer Agent

sudo apt update
sudo apt install logwatch mailutils postfix -y

# ตอน postfix ถาม configuration:
# เลือก "Internet Site" (ถ้าต้องการส่ง email จาก server)
# หรือ "Satellite system" (ถ้า relay ผ่าน SMTP ภายนอก)

# ตรวจ logwatch
logwatch --version

ตั้ง Logwatch Config

sudo nano /etc/logwatch/conf/logwatch.conf
# Email ปลายทาง
MailTo = [email protected]
MailFrom = [email protected]

# ระดับรายละเอียด: Low, Med, High
Detail = Med

# ช่วงเวลา: Today, Yesterday, All
Range = Yesterday

# Output: mail, stdout, file
Output = mail

# Service ที่ต้องการ (ทั้งหมด = All)
Service = All

# ยกเว้น service บางตัว
Service = "-zz-sys"

ทดสอบส่ง Report ทันที

# ส่ง report ไปที่ email
sudo logwatch --mailto [email protected] --detail Med --range yesterday

# ดูใน stdout แทน email
sudo logwatch --output stdout --detail Med --range yesterday

ตั้ง Relay ผ่าน Gmail/SMTP

sudo nano /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
sudo nano /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 [email protected]:your_app_password

sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd
sudo systemctl restart postfix

ตั้ง Cron ส่งรายวัน

# Logwatch มักตั้ง cron อัตโนมัติ
cat /etc/cron.daily/00logwatch

# ถ้าไม่มี สร้างเอง:
sudo nano /etc/cron.d/logwatch
# ส่งทุกวันเวลา 07:00
0 7 * * * root /usr/sbin/logwatch --mailto [email protected] --detail Med
Tip: ตั้ง Detail เป็น High สำหรับ server ที่น่าเป็นห่วง และ Low สำหรับ stable server ที่ต้องการแค่ summary report ไม่ต้องการ noise

ตรวจสอบว่า Logwatch ทำงานถูกต้อง

# ทดสอบ output เป็น text
sudo logwatch --output stdout --detail Low --range today

# ตรวจ postfix queue
mailq

# ดู mail log
sudo tail -f /var/log/mail.log

# ทดสอบส่ง email
echo "test" | mail -s "Test from VPS" [email protected]
Tip: ถ้า email ไม่มาถึง ตรวจ spam folder ก่อน และดู /var/log/mail.log หา error SMTP authentication failed หรือ connection refused

แก้ปัญหาที่พบบ่อย

Email ไม่ส่งออก (postfix queue ค้าง)

ตรวจ postfix queue และ force delivery:

mailq
sudo postqueue -f
sudo postfix flush
sudo tail -50 /var/log/mail.log

Gmail ปฏิเสธ SMTP authentication

ต้องใช้ App Password แทน Google account password เพราะ 2FA:

# Google Account > Security > 2-Step Verification > App passwords
# สร้าง App Password สำหรับ "Mail" > "Linux computer"
# ใช้ password ที่ได้ใน sasl_passwd

Logwatch report ว่างเปล่า (ไม่มีข้อมูล)

Log files อาจใช้ systemd journal แทน traditional syslog ต้องตั้งให้ journal เขียนออกเป็น file:

sudo nano /etc/systemd/journald.conf
[Journal]
Storage=persistent

sudo systemctl restart systemd-journald

Report มีข้อมูล SSH failed login จำนวนมากผิดปกติ

นี่คือสัญญาณว่ามี brute force attack ควรติดตั้ง fail2ban ทันที:

sudo apt install fail2ban -y
sudo systemctl enable --now fail2ban

คำถามที่พบบ่อย

Logwatch กับ Graylog/ELK Stack ต่างกันอย่างไร?

Logwatch เป็น simple daily digest email ไม่มี real-time alerting หรือ dashboard Graylog/ELK เป็น enterprise logging platform ที่ทำ real-time analysis Logwatch เหมาะสำหรับ single VPS ที่ต้องการ minimum overhead

Logwatch อ่าน log อะไรบ้าง?

อ่าน /var/log/syslog, auth.log, nginx/apache access/error, mail.log, dpkg.log, kernel, cron, sudo และ service-specific logs ผ่าน service scripts ใน /usr/share/logwatch/scripts/services/

เพิ่ม custom log analysis ได้ไหม?

ได้ สร้าง custom service script ใน /etc/logwatch/scripts/services/ เขียน Perl script ที่รับ log lines จาก stdin และ output summary ไปยัง stdout Logwatch จะ include ใน daily report อัตโนมัติ