對于日志管理,老版本的Linux缺省使用Syslog,其配置大致如下所示:
shell> cat /etc/syslog.conf # Log all kernel messages to the console. # Logging much else clutters up the screen. # kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;mail.none;authpriv.none;cron.none /var/log/messages # The authpriv file has restricted access. authpriv.* /var/log/secure # Log all the mail messages in one place. mail.* -/var/log/maillog # Log cron stuff cron.* /var/log/cron # Everybody gets emergency messages *.emerg * # Save news errors of level crit and higher in a special file. uucp,news.crit /var/log/spooler # Save boot messages also to boot.log local7.* /var/log/boot.log
其中涉及兩個概念:Facility和Severity,中文的意思大致是類型和級別。重點說明兩條配置的含義:首先,所有Severity大于等于info的信息都會被保存到「/var/log/messages」中,但是Facility為mail、authpriv、cron的消息例外;其次,所有Facility為mail的消息都會保存到「/var/log/maillog」中,日志文件前面的減號表示的意思是異步寫文件。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
關(guān)于Syslog的內(nèi)容我并不想多說,否則就偏離了主題,大家如果有不清楚的地方,可以參考鳥哥的Linux私房菜。雖然Syslog中規(guī)中矩,但是隨著時間的推移,無論是功能還是性能,它都顯得捉襟見肘,于是出現(xiàn)了:Rsyslog和Syslog-ng,它們都涵蓋SysLog的常用功能,不過在功能和性能上更為出色,至于孰優(yōu)孰劣是個仁者見仁智者見智的問題,鑒于多數(shù)Linux發(fā)行版均選擇了Rsyslog,姑且讓我隨波逐流一次。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
如果說Rsyslog有哪些缺點的話,那么兼容性無疑是很顯眼的一個,不同版本之間的差異比較大,使用時需要格外留意一下。在使用前最好完整的瀏覽幾遍官方文檔,此外網(wǎng)絡(luò)上有一些不錯的文章可供參考,比如:rsyslog研究,rsyslog和logrotate服務(wù),核心概念在這些資料里都有提及,本文就不贅述了,這里著重與快速上手。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
安裝配置
我以CentOS為例,說明如何通過RPM來安裝Rsyslog:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
shell> cd /etc/yum.repos.d/ shell> wget http://rpms.adiscon.com/v8-stable/rsyslog.repo shell> yum install rsyslog
安裝完成后,我們可以查看一下到底都裝了些什么東西:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
shell> rpm -ql rsyslog /etc/logrotate.d/syslog /etc/pki/rsyslog /etc/rc.d/init.d/rsyslog /etc/rsyslog.conf /etc/rsyslog.d /etc/sysconfig/rsyslog ...
如果系統(tǒng)里有Syslog的話,那么在啟動Rsyslog之前,別忘了先關(guān)閉它:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
shell> service syslog stop shell> service rsyslog start
如果運行Rsyslog時出現(xiàn)問題,那么可以通過激活調(diào)試模式來查找原因:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
shell> cat /etc/sysconfig/rsyslog # Options for rsyslogd # Syslogd options are deprecated since rsyslog v3. # If you want to use them, switch to compatibility mode 2 by "-c 2" # See rsyslogd(8) for more details SYSLOGD_OPTIONS="-d -n"
如果你想測試Rsyslog是否工作的話,可以通過系統(tǒng)內(nèi)建的logger命令發(fā)消息;如果你想測試Rsyslog性能如何的話,可以考慮使用官方提供的tcpflood。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
實例演示
在我們動手之前,有必要了解一下Rsyslog的工作流程,說起來非常簡單:首先數(shù)據(jù)通過輸入模塊進入主隊列,然后經(jīng)由過濾條件分解到各個子隊列,最后交給輸出模塊。
理解了Rsyslog的工作流程,我們就可以實例演示了,請聽題:請把多臺Web服務(wù)器上的access日志發(fā)送到統(tǒng)一的App服務(wù)器。實際上新版Nginx可以直接發(fā)Syslog請求。
設(shè)置Web服務(wù)器:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
module(load="imfile") ruleset(name="remote") { action(type="omfwd" Protocol="tcp" Target="<HOST>" Port="<PORT>") stop } input(type="imfile" File="/path/to/web/access.log" Facility="user" Severity="info" Tag="web_access" PersistStateInterval="1" Ruleset="remote")
通過imfile輸入模塊,我們引入文件,并且綁定Ruleset。不過需要說明的是,需要通過配置WorkDirectory來聲明狀態(tài)文件的保存位置,這個狀態(tài)文件被用來記錄掃描日志位置等信息,PersistStateInterval被用來控制狀態(tài)文件的持久化頻率,測試階段,可以把它設(shè)置的小點兒,正式階段,出于效率的考慮,可以把它調(diào)大點兒,但是相應(yīng)的也會出現(xiàn)丟失數(shù)據(jù)的潛在風險,具體設(shè)置多少合適需要結(jié)合自己的情況來斟酌。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
設(shè)置App服務(wù)器:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
module(load="imtcp") template(name="msg" type="string" string="%msg:2:$%\n") ruleset(name="analysis") { action(type="omfile" File="/path/to/access.log" Template="msg") stop } input(type="imtcp" Port="<PORT>" Ruleset="analysis")
通過omfile輸出模塊可以簡單的實現(xiàn)匯總,利用Template指令定義僅記錄msg數(shù)據(jù),此外還使用Property Replacer去除了msg開頭的空格。
如果僅此而已就太無趣了,實際上利用omprog輸出模塊,我們可以玩得更出彩:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
module(load="omprog") ruleset(name="analysis") { action(type="omprog" Binary="/usr/bin/php /path/to/script.php" Template="msg") stop }
數(shù)據(jù)通過管道無縫傳遞給外部程序,可以說賦予了Rsyslog更多的可能性,你可以使用任何熟悉的語言來實現(xiàn),以PHP為例,大致代碼如下所示:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
<?php while (($data = fgets(STDIN)) !== false) { // ... } ?>
一個需要注意的地方是,如果循環(huán)里的業(yè)務(wù)邏輯比較重,那么可能會導(dǎo)致?lián)矶?,此時可以考慮不直接在循環(huán)里處理業(yè)務(wù)邏輯,而是稍作處理,直接轉(zhuǎn)發(fā)給 Gearman 之類的任務(wù)分發(fā)器,稍后有 Gearman 把任務(wù)分發(fā)給相應(yīng)的 Worker 來處理。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
友情提示:如果主配置文件太臃腫的話,可以考慮使用子配置文件:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
# Include all config files in /etc/rsyslog.d/ $IncludeConfig /etc/rsyslog.d/*.conf文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/9623.html
評論