logwatch 是 linux 上的一個日誌分析工具,可以利用 cron 天天執行日誌分析工做,並將日誌發送到指定用戶的郵箱(具體配置方法請看個人另外一篇博客 http://my.oschina.net/JerryBaby/blog/176826)。 前端
我手上一共用 3 臺服務器,兩臺 CentOS,一臺 gentoo 我的電腦。我都爲它們配置了 logwatch。個人作法是,將 logwatch 加入 cront 任務,天天晚上 8 點給 root 發送一封郵件,內容是昨天的系統日誌。可是個人 gentoo 卻怎麼也收不到郵件。整整折騰了兩天,終於知道了原來是 mail 服務沒有配置好。 linux
先來說解一下 mail 服務的幾個概念。 express
郵件傳輸代理。最典型的就是 linux 系統上的 postfix 和 sendmail 以及我要介紹的 gentoo 上的 nullmailer。它的功能是未來自客戶端或者是其餘 MTA 的郵件收下(使用 SMTP 協議);傳遞目的地不是本主機的郵件。 windows
知道了這兩個概念以後,來解釋一下 logwatch 的工做原理。默認 logwatch 是加入 cron 服務的,因此在 /etc/cron.daily 目錄下,有一個名爲 00-logwatch 的腳本(不一樣的系統名稱可能不一樣),cron 服務會根據設定天天自動執行這個目錄下的腳本。那麼 logwatch 是怎樣實現發送郵件給 root 的呢?咱們來看一下 00-logwatch腳本的內容。 服務器
從上面的內容能夠看出:gentoo 系統是調用 logwatch.pl 腳本執行命令;CentOS 1 是調用 logwatch 腳本執行命令;CentOS 2 也是調用 logwatch 腳本執行命令。 工具
如今還看不出 logwatch 的原理,可是我分別查看了一下 logwatch.pl 和 logwatch 腳本,它們默認都會讀取 /usr/share/logwatch/default.conf/logwatch.conf 文件來執行 logwatch。關於 logwatch 如何收集日誌我也沒有深究。至於它如何發送郵件,請往下看。
post
在 logwatch.conf 文件裏,有這樣一個設定
google
mailer = "/usr/sbin/sendmail -t"
這個設定項規定了 logwatch 發送郵件的發送者是 /usr/sbin/sendmail 腳本。
spa
到此,應該都明瞭了。 cron 天天執行 00-logwatch 腳本,00-logwatch 腳本又調用 logwatch.pl 或 logwatch 命令,而後利用 sendmail 命令來發送日誌郵件到 root 用戶的郵箱。 .net
這裏又有一個最容易讓人混亂的地方,那就是 sendmail。
不是說 sendmail 是 linux 下的 MTA 嗎?我只能說:此 sendmail 非彼 sendmail。
linux 系統要想可以讀取郵件,必須安裝上 MUA。要想轉發郵件給 root 用戶,必需要安裝上 MTA。系統上最多見的 MUA 是 mailx,也就是常常使用的 mail 命令。而最多見的 MTA 就是 sendmail 和 postfix。還有幾乎沒有人使的 nullmailer(可是我使了 ^^)。
那麼剛纔說的,logwatch.pl 腳本調用的 sendmail 命令是什麼呢。它並非 MTA-sendmail。只是一個 MTA 的前端。也就是說,postfix 會提供一個名爲 sendmail 的前端,nullmailer 也會提供一個名爲 sendmail 的前端,而 sendmail(MTA)也會提供一個名爲 sendmail 的前端。這些我也是從 man 手冊得來的結論。請看
個人 gentoo 使用的 MTA 是 nullmailer,CentOS 1 使用的 MTA 是 postfix,CentOS 2 使用的 MTA 就是 sendmail。從上面的圖可以看出區別。
關於 postfix 和 sendmail 的配置,在這裏我就再也不多說了,google 上有不少詳細的配置。我重點是要說一下這個 nullmailer。
在 google 排名前 20 的文章我幾乎都看過了,可是幾乎沒有一篇可以解釋得很清楚這個 nullmailer 到底該如何配置,到如今爲止我仍是不太明白究竟增樣配合適。不過我仍是要用它,身爲一個 geek(雖然是個水貨),就要不斷嘗試新的東西嘛。
如今 gentoo 安裝 logwatch 的時候,默認的安裝的就是這個 MTA
這是 gentoo 關於用 nullmailer 替代 ssmtp 的一些討論http://www.gossamer-threads.com/lists/gentoo/dev/265171
並且這個 nullmailer 和 postfix 在 gentoo 上仍是兩個衝突的包
官方的定義是:nullmailer is a simple and secure relay-only mail transport agent.
nullmailer 的主要配置文件在 /etc/nullmailer 目錄下面。它提供 3 個命令:nullmailer-queue,nullmailer-inject,nullmailer-send。
nullmailer-queue:從標準輸入讀取郵件到隊列(/var/nullmailer/queue/)。
nullmailer-inject:從標準輸入讀取郵件並加入 RFC8822 格式的頭部放入隊列。
nullmailer-send:檢查郵件隊列,若是存在郵件,就發送。
最主要的配置文件是 /etc/nullmailer/remove,它規定了將郵件轉發到何處,這裏我只提供一個將系統日誌發送到我 gmail 的配置
smtp.gmail.com smtp --port=587 --auth-login --user=you@gmail.com --pass=Yourpassword --starttls確實可以發送到個人 gmail
鑑於本身能力有限,但願對於 nullmailer 熟悉的小夥伴們可以不吝賜教。