做為一位系統管理者,一定要時常注意系統的健康狀態。
前一陣子公司有位同仁的 email 密碼設的太簡單了,所以,被猜到並且被盜用來濫發垃圾信件。Mail 的 log 量又非常的大,所以也不可能每天去檢視所有的紀錄。一直到幾天後,收到有寄出的 email 被退件,而原因就是我們的 mail server 被列在某個黑名單上。我再進一步去查,才發現原來有帳號已經被盜用了好幾天。由於使用的流量/頻寬與時間都沒有太大,所以,從流量上來看,並不能很容易的察覺有異常。
其他系統方面的善後與設定我就不多著墨了。但是,我就開始思考與尋找適合的工具來幫我處理這部份的問題。在努力的尋找下(迷之音,先前不夠努力,Orz),我找到了 pflogsumm!
由於該套件有包含在 Debian 內,所以安裝非常簡單:
apt-get install pglogsumm
安裝好後,先寫一個 bash script,我定的路徑及檔名(/usr/local/sbin/postfix_report.sh):
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
pflogsumm /var/log/你的maillog檔 | formail -c -I"Subject: Mail Statistics" -I"From: pflogsumm@example.com" -I"To: 收件人@example.com" -I"Received: from mx.example.com ([192.168.xxx.xxx])" | sendmail postmaster@example.com
exit 0
以上的 script,請將所有的 domain / email / IP,更改為適當資訊。存檔後再 chmod 即可。
Script 寫好後,現在剩下的就是如何執行,什麼時候執行?
由於資料的來源是 mail log,所以我們必須要檢視一下 mail log 的設定,不同 distribution 的 mail log 的設定方式可能會有些許不同。Debian 使用的是 rsyslog。所以,postfix 的 mail log 是在 /etc/logrotate.d/rsyslog 進行設定:
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
{
rotate 14
daily
missingok
notifempty
compress
delaycompress
sharedscripts
prerotate
/usr/local/sbin/postfix_report.sh
endscript
postrotate
invoke-rc.d rsyslog reload > /dev/null
endscript
}
上面的範例,可以自己的需求來進行調整。而且,直接在 rotate 之前呼叫我的 shell script,會比使用 crontab 來呼叫更為簡單而且乾淨。
以上,所有的設定就完成了。
之後,收到信件的內容大致如下:
Grand Totals
------------
messages
1923 received
3131 delivered
4 forwarded
8 deferred (68 deferrals)
3 bounced
6673 rejected (68%)
0 reject warnings
0 held
0 discarded (0%)
253674k bytes received
372125k bytes delivered
551 senders
297 sending hosts/domains
163 recipients
67 recipient hosts/domains
Per-Day Traffic Summary
date received delivered deferred bounced rejected
--------------------------------------------------------------------
Sep 8 2010 1555 2597 47 3 4597
Per-Hour Traffic Daily Average
time received delivered deferred bounced rejected
--------------------------------------------------------------------
0000-0100 29 52 2 0 137
Host/Domain Summary: Message Delivery
sent cnt bytes defers avg dly max dly host/domain
-------- ------- ------- ------- ------- -----------
2589 315653k 42 2.4 s 62.3 h example.com
Host/Domain Summary: Messages Received
msg cnt bytes host/domain
-------- ------- -----------
586 116367k example.com
Senders by message count
------------------------
156 webmaster@example.com
Recipients by message count
---------------------------
374 example@example.com
Senders by message size
-----------------------
26552k example@example.com
Recipients by message size
--------------------------
39781k example@example.com
message deferral detail
-----------------------
smtp (total: 68)
message bounce detail (by relay)
--------------------------------
gmail-smtp-in.l.google.com[74.125.127.27]:25 (total: 1)
message reject detail
---------------------
cleanup
header (total: 29)
29 Date: Tue, 19 Jan 2038 11:14:07 +0800
message reject warning detail: none
message hold detail: none
message discard detail: none
smtp delivery failures: none
它所整理出來的資訊非常容易閱讀,而且,有異常就很容易發現。系統管理者就可以進一步的去做追查及因應。
參考資料來源:
http://www.howtoforge.com/postfix-monitoring-with-mailgraph-and-pflogsumm-on-debian-lenny