中國廣東省深圳市龍華新區民治街道溪山美地
518131
+86 13113668890
+86 755 29812080
版權 © 2013, 2014 netkiller. all rights reserved.
版權聲明
轉載請與作者聯系,轉載時請務必标明文章原始出處和作者資訊及本聲明。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SYz1yYu1Sei9CXzV2Zh1Wavw1bp5iY1hGdpdmLyVGbsl2a0Vmbvw1LcpDc0RHaiojIsJye.png)
文檔出處:
<a href="http://netkiller.github.io/" target="_top">http://netkiller.github.io</a>
<a href="http://netkiller.sourceforge.net/" target="_top">http://netkiller.sourceforge.net</a>
2014-12-16
摘要
2013-03-19 第一版
2014-12-16 第二版
我的系列文檔
<a href="http://netkiller.github.io/architect/index.html" target="_top">netkiller architect 手劄</a>
<a href="http://netkiller.github.io/developer/index.html" target="_top">netkiller developer 手劄</a>
<a href="http://netkiller.github.io/php/index.html" target="_top">netkiller php 手劄</a>
<a href="http://netkiller.github.io/python/index.html" target="_top">netkiller python 手劄</a>
<a href="http://netkiller.github.io/testing/index.html" target="_top">netkiller testing 手劄</a>
<a href="http://netkiller.github.io/cryptography/index.html" target="_top">netkiller cryptography 手劄</a>
<a href="http://netkiller.github.io/linux/index.html" target="_top">netkiller linux 手劄</a>
<a href="http://netkiller.github.io/debian/index.html" target="_top">netkiller debian 手劄</a>
<a href="http://netkiller.github.io/centos/index.html" target="_top">netkiller centos 手劄</a>
<a href="http://netkiller.github.io/freebsd/index.html" target="_top">netkiller freebsd 手劄</a>
<a href="http://netkiller.github.io/shell/index.html" target="_top">netkiller shell 手劄</a>
<a href="http://netkiller.github.io/security/index.html" target="_top">netkiller security 手劄</a>
<a href="http://netkiller.github.io/www/index.html" target="_top">netkiller web 手劄</a>
<a href="http://netkiller.github.io/monitoring/index.html" target="_top">netkiller monitoring 手劄</a>
<a href="http://netkiller.github.io/storage/index.html" target="_top">netkiller storage 手劄</a>
<a href="http://netkiller.github.io/mail/index.html" target="_top">netkiller mail 手劄</a>
<a href="http://netkiller.github.io/docbook/index.html" target="_top">netkiller docbook 手劄</a>
<a href="http://netkiller.github.io/version/index.html" target="_top">netkiller version 手劄</a>
<a href="http://netkiller.github.io/database/index.html" target="_top">netkiller database 手劄</a>
<a href="http://netkiller.github.io/postgresql/index.html" target="_top">netkiller postgresql 手劄</a>
<a href="http://netkiller.github.io/mysql/index.html" target="_top">netkiller mysql 手劄</a>
<a href="http://netkiller.github.io/nosql/index.html" target="_top">netkiller nosql 手劄</a>
<a href="http://netkiller.github.io/ldap/index.html" target="_top">netkiller ldap 手劄</a>
<a href="http://netkiller.github.io/network/index.html" target="_top">netkiller network 手劄</a>
<a href="http://netkiller.github.io/cisco/index.html" target="_top">netkiller cisco ios 手劄</a>
<a href="http://netkiller.github.io/h3c/index.html" target="_top">netkiller h3c 手劄</a>
<a href="http://netkiller.github.io/multimedia/index.html" target="_top">netkiller multimedia 手劄</a>
<a href="http://netkiller.github.io/perl/index.html" target="_top">netkiller perl 手劄</a>
<a href="http://netkiller.github.io/radio/index.html" target="_top">netkiller amateur radio 手劄</a>
<a href="http://netkiller.github.io/devops/index.html" target="_top">netkiller devops 手劄</a>
目錄
<a href="http://netkiller.github.io/journal/log.html#what">1. 什麼日志歸檔</a>
<a href="http://netkiller.github.io/journal/log.html#why">2. 為什麼要做日志歸檔</a>
<a href="http://netkiller.github.io/journal/log.html#when">3. 何時做日志歸檔</a>
<a href="http://netkiller.github.io/journal/log.html#where">4. 歸檔日志放在哪裡</a>
<a href="http://netkiller.github.io/journal/log.html#who">5. 誰去做日志歸檔</a>
<a href="http://netkiller.github.io/journal/log.html#how">6. 怎樣做日志歸檔</a>
<a href="http://netkiller.github.io/journal/log.html#idp65035024">6.1. 日志格式轉換</a>
<a href="http://netkiller.github.io/journal/log.html#idp65036736">6.1.1. 将日志放入資料庫</a>
<a href="http://netkiller.github.io/journal/log.html#idp65036992">6.1.2. apache pipe</a>
<a href="http://netkiller.github.io/journal/log.html#idp65048496">6.1.3. log format</a>
<a href="http://netkiller.github.io/journal/log.html#idp65048752">6.1.4. 日志導入到 mongodb</a>
<a href="http://netkiller.github.io/journal/log.html#idp65061056">6.2. 日志中心方案</a>
<a href="http://netkiller.github.io/journal/log.html#idp65064160">6.2.1. 軟體安裝</a>
<a href="http://netkiller.github.io/journal/log.html#idp65065552">6.2.2. 節點推送端</a>
<a href="http://netkiller.github.io/journal/log.html#idp65065808">6.2.3. 日志收集端</a>
<a href="http://netkiller.github.io/journal/log.html#idp65066064">6.2.4. 日志監控</a>
歸檔,是指将日志整理完畢且有儲存價值的檔案,經系統整理交日志伺服器儲存的過程。
随時調出曆史日志查詢。
通過日志做資料挖掘,挖掘有價值的資料。
檢視應用程式的工作狀态
日志歸檔應該是企業規定的一項制度(“歸檔制度”),系統建設之初就應該考慮到日志歸檔問題。如果你的企業沒有這項工作或制度,在看完本文後建議你立即實施。
簡單的可以采用單節點伺服器加備份方案。
随着日志規模擴大,未來必須采用分布式檔案系統,甚至涉及到遠端異地容災。
我的答案是日志歸檔自動化,人工檢查或抽檢。
将所有伺服器的日志都彙總到一處,有幾種方法
日志歸檔常用方法:
ftp 定是下載下傳, 這種做法适合小檔案且日志量不大,定是下載下傳到指定伺服器,缺點是重複傳輸,實時性差。
rsyslog 一類的程式,比較通用,但擴充不便。
rsync 定是同步,适合打檔案同步,好于ftp,實時性差。
首先我來介紹一種簡單的方案
将web伺服器日志通過管道處理然後寫入資料庫
處理程式源碼
編譯
簡單用法
進階用法
實時處理日志,首先建立一個管道,尋該日志檔案寫入管道中。
這樣就可以實作實時日志插入。
上面程式稍加修改即可實作hbase, hypertable 本版
apache 日志管道過濾 customlog "| /srv/match >> /tmp/access.log" combined
經過管道轉換過的日志效果
通過定義logformat可以直接輸出sql形式的日志
apache
nginx
但對于系統管理者使用grep,awk,sed,sort,uniq分析時造成一定的麻煩。是以我建議仍然采用正則分解
産生有規則日志格式,apache:
d語言日志處理程式
編譯日志處理程式
用法
處理壓錯過的日志
實時采集日志
上面的方案雖然簡單,但太依賴系統管理者,需要配置很多伺服器,每種應用軟體産生的日志都不同,是以很複雜。如果中途出現故障,将會丢失一部日志。
于是我又回到了起點,所有日志存放在自己的伺服器上,定時将他們同步到日志伺服器,這樣解決了日志歸檔。遠端收集日志,通過udp協定推送彙總到日志中心,這樣解決了日志實時監控、抓取等等對實時性要求較高的需求。
為此我用了兩三天寫了一個軟體,下載下傳位址:https://github.com/netkiller/logging
這種方案并不是最佳的,隻是比較适合我的場景,而且我僅用了兩三天就完成了軟體的開發。後面我會進一步擴充,增加消息隊列傳送日志的功能。
安裝啟動腳本
centos
ubuntu
配置腳本,打開 /etc/init.d/ulog 檔案
配置日志中心的ip位址
然後配置端口與采集那些日志
格式為
1213 目的端口号(日志中心端口)後面是你需要監控的日志,如果日志每日産生一個檔案寫法類似 /tmp/$(date +"%y-%m-%d.%h:%m:%s").log
每日産生一個新日志檔案需要定時重新開機 ulog 方法是 /etc/init.d/ulog restart
配置完成後啟動推送程式
檢視狀态
停止推送
配置接收端口與儲存檔案,打開 /etc/init.d/ucollection 檔案,看到下面段落
格式如下,表示接收來自1213端口的資料,并儲存到/tmp/nginx/access.log檔案中。
如果需要分割日志配置如下
上面配置日志檔案将會産生在下面的目錄中
同樣,如果分割日志需要重新開機收集端程式。
啟動收集端
停止程式
監控來自1217寬口的資料
啟動後實時将最新日志傳送過來