一. syslog簡介
syslog是一種工業标準的協定,可用來記錄裝置的日志。在UNIX系統,路由器、交換機等網絡裝置中,系統日志(System Log)記錄系統中任何時間發生的大小事件。管理者可以通過檢視系統記錄,随時掌握系統狀況。UNIX的系統日志是通過syslogd這個程序記錄系統有關事件記錄,也可以記錄應用程式運作事件。通過适當的配置,我們還可以實作運作syslog協定的機器間通信,通過分析這些網絡行為日志,藉以追蹤掌握與裝置和網絡有關的狀況。
功能:記錄至系統記錄。
二 . syslog服務與配置
安裝syslog軟體包
軟體包名稱為:rsyslog-5.8.10-10.el6_6.x86_64
我用的是centos系統,配置的有第三方的yum源,我直接yum install直接裝就OK了
裝完後用 rpm -qa | grep syslog下看是否已安裝
2.syslog日志系統可以根據程式詳細資訊的不同定義不同的日志級别。
3.Linux上的日志系統分為:syslog和syslog-ng(syslog日志系統的更新版)
4.syslog服務:
syslog服務程序分兩個,分别是:syslogd(系統,非核心産生的日志)和klogd(專門記錄核心産生的日志)
kernel-->實體終端(/dev/console)-->/var/log/dmesg
/var/log/dmesg檔案可以使用dmesg指令和cat檢視檔案内容
日志滾動(日志切割):
所謂的滾動是指曆史資訊所儲存的日志,如;messages檔案日志會越來越大等到了某一段時間,會把messages檔案重新命名為messages.1,系統并重新建立messages檔案,是以叫做日志滾動
/sbin/init
/var/log/messages:系統标準錯誤日志資訊;非核心産生的引導資訊,各子系統産生的資訊
/vat/log/maillog:郵件系統産生的日志資訊
/vat/log/secure:安全相關log
系統自帶的日志切割程式logrotate
logrotate腳本配置檔案
<code>[root@localhost </code><code>cron</code><code>.daily]</code><code># pwd</code>
<code>/etc/cron</code><code>.daily</code>
<code>[root@localhost </code><code>cron</code><code>.daily]</code><code># vim logrotate</code>
滾動日志資訊配置檔案
<code>[root@localhost </code><code>cron</code><code>.daily]</code><code># vim /etc/logrotate.conf</code>
<code># see "man logrotate" for details</code>
<code># rotate log files weekly</code>
<code>weekly </code><code>#每周滾動一次</code>
<code># keep 4 weeks worth of backlogs</code>
<code>rotate 4 </code><code>#隻保留4個切割版本檔案,超過後清除 </code>
<code># create new (empty) log files after rotating old ones</code>
<code>create </code><code>#滾動完之後建立一個空的新的檔案</code>
<code># use date as a suffix of the rotated file</code>
<code>dateext</code>
<code># uncomment this if you want your log files compressed</code>
<code>#compress</code>
<code># RPM packages drop log rotation information into this directory</code>
<code>include </code><code>/etc/logrotate</code><code>.d </code><code>#包括/etc/logrotate.d 下的檔案,手動添加可直接添加到此目錄</code>
<code># no packages own wtmp and btmp -- we'll rotate them here</code>
<code>/var/log/wtmp</code> <code>{</code>
<code> </code><code>monthly </code><code>#按月,自己定義</code>
<code> </code><code>create 0664 root utmp </code><code>#建立檔案并0664權限</code>
<code> </code><code>minsize 1M </code><code>#最新1M</code>
<code> </code><code>rotate 1 </code><code>#保留幾個版本</code>
<code>}</code>
<code>/var/log/btmp</code> <code>{</code>
<code> </code><code>missingok</code>
<code> </code><code>monthly</code>
<code> </code><code>create 0600 root utmp</code>
<code> </code><code>rotate 1</code>
<code># system-specific logs may be also be configured here.</code>
<code>[root@localhost </code><code>cron</code><code>.daily]</code><code>#</code>
syslog配置檔案/etc/rsyslog.conf
注:centos 6 的配置檔案是/etc/rsyslog.conf,centos5的配置檔案是/etc/syslog.conf
5.配置檔案定義格式為facility.priority action
facility是指哪個facility來源産生的日志; priority是指拿個級别的日志 ;action是指産生日志怎麼辦是儲存在檔案中還是其他。。。
facility可以了解為日志的來源或裝置或選擇條件,目前常用的facility有以下幾種:
auth #認證相關的
authpriv #權限,授權相關的
cron #任務計劃相關的
daemon #守護程序相關的
kern #核心相關的
lpr #列印相關的
mail #郵件相關的
mark #标記相關的
news #新聞相關的
security #安全相關的,與auth類似
syslog #syslog自己的
user #使用者相關的
uucp #unix to unix cp相關的
local0到local7 #使用者自定義使用
* #*表示所有的facility
priority(log level)日志優先級的級别,一般分為以下幾種級别(從低到高)
注:級别越低記錄的越詳細
debug #程式或系統的調試資訊
info #一般資訊
notice #不影響正常的功能,需要提醒使用者的重要事件
warning/warn #可能影響系統功能,需要提醒使用者的重要事件
err/error #錯誤資訊
crit #比較嚴重的
alert #必須馬上處理的
emerg/canic #會導緻系統不可用的
* #表示所有的日志級别
none #跟*相反,表示什麼也沒有
action(動作)日志記錄的位置
系統上的絕對路徑 #普通檔案 如:/var/log/XXX
| #管道 通過管道送給其他的指令出來
終端 #終端 如:/dev/console
@host #遠端主機 如@10.1.1.1
使用者 #系統使用者 如:root
* #登入到系統上的所有使用者,一般emerg級别的日志是這樣定義的
定義格式列子:
mail.info /var/log/mail.log #表示将mail相關的,級别為info及info以上級别的資訊記錄到/var/log/mail.log檔案中
auth.=info @10.1.1.1 #表示将auth相關的,級别為info的資訊記錄到10.1.1.1主機上去,前提是10.1.1.1要能接收其主機發來的日志資訊
user.!=error #表示記錄user相關的,不包括error級别的資訊
user.!error #表示user.error相反
*.info #表示記錄所有的日志資訊的info級别
mail.* #表示記錄所有mail相關的所有級别的資訊
*.* #表示記錄所有的日志資訊的所有的日志級别
cron.info;mail.info #多個日志來源可以用“ ; ”隔開
cron,mail.info #與cron.info;mail.info 是一個意思
mail.*;mail.!=info #表示記錄mail相關的所有級别的資訊,但是不包括info級别的資訊
重新開機服務
#service rsyslog reload 注:不重新開機但能讀到配置檔案;不建議重新開機
6.搭建Linux日志伺服器
<code>編輯配置檔案 [root@localhost etc]</code><code># vim /etc/sysconfig/rsyslog</code>
修改配置檔案中SYSLOGD_OPTIONS="-c 5" 添加“ -r選項”即可
目的可以讓伺服器能夠接受用戶端傳來的資料
配置檔案如下
<code>[root@localhost etc]</code><code># vim /etc/sysconfig/rsyslog</code>
<code># Options for rsyslogd</code>
<code># Syslogd options are deprecated since rsyslog v3.</code>
<code># If you want to use them, switch to compatibility mode 2 by "-c 2"</code>
<code># See rsyslogd(8) for more details</code>
<code>SYSLOGD_OPTIONS=</code><code>"-r -c 5"</code>
<code>~</code>
重新開機syslog服務
<code>[root@localhost etc]</code><code># service rsyslog restart</code>
<code>關閉系統日志記錄器: [确定]</code>
<code>啟動系統日志記錄器:-r option only supported </code><code>in</code> <code>compatibility modes 0 to 2 - ignored</code>
<code> </code><code>[确定]</code>
<code>[root@localhost etc]</code><code>#</code>
7.搭建syslog用戶端
在syslog配置檔案中修改/etc/rsyslog.conf
<code>[root@localhost etc]</code><code># vim /etc/rsyslog.conf</code>
<code># Log all kernel messages to the console.</code>
<code># Logging much else clutters up the screen.</code>
<code>#kern.* /dev/console</code>
<code># Log anything (except mail) of level info or higher.</code>
<code># Don't log private authentication messages!</code>
<code>*.info;mail.none;authpriv.none;</code><code>cron</code><code>.none </code><code>/var/log/messages</code>
<code># The authpriv file has restricted access.</code>
<code>authpriv.* </code><code>/var/log/secure</code>
<code># Log all the mail messages in one place.</code>
<code>mail.* @192.168.1.66</code>
<code># Log cron stuff</code>
<code>cron</code><code>.* </code><code>/var/log/cron</code>
<code>.</code>
<code>省略</code>
重新開機syslog用戶端
注:如果以上内容發現錯誤,請告知留言即可,以友善糾正保證内容的準确性!!!
本文轉自 zhangguangyi 51CTO部落格,原文連結:http://blog.51cto.com/bosszhang/1680914