syslog 格式學習

完整的syslog消息由3部分組成,分 別是PRI、HEADER和MSG。大部分syslog都包含PRI和MSG部分,而HEADER可能沒有。

下面是一個syslog消息:
<30>Oct 9 22:33:20 hlfedora auditd[1787]: The audit daemon is exiting.
其中“<30>”是PRI部分,“Oct 9 22:33:20 hlfedora”是HEADER部分,“auditd[1787]: The audit daemon is exiting.”是MSG部分。

 

1、PRI部分

    PRI部分由尖括號包含的一個數字構成(即Priority,取值0~191)。這個數字包含兩個信息:Facility和Level。可以把Facility看做是創建日誌消息的實體(例如是計算機內核還是用戶程序),把Level看做是日誌真正的級別(非爲8個等級,後邊會給出)。其中,Priority=Facility * 8 + Level。

The list of Facilities available:
0 kernel messages
1 user-level messages
2 mail system
3 system daemons
4 security/authorization messages
5 messages generated internally by syslogd
6 line printer subsystem
7 network news subsystem
8 UUCP subsystem
9 clock daemon
10 security/authorization messages
11 FTP daemon
12 NTP subsystem
13 log audit
14 log alert
15 clock daemon
16 local use 0 (local0)
17 local use 1 (local1)
18 local use 2 (local2)
19 local use 3 (local3)
20 local use 4 (local4)
21 local use 5 (local5)
22 local use 6 (local6)
23 local use 7 (local7)

Facility的定義如下,可以看出來syslog的Facility是早期爲Unix操作系統定義的,不過它預留了User(1),Local0~7 (16~23)給其他程序使用。

The list of severity Levels:
0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 Informational: informational messages
7 Debug: debug-level messages

A detailed explanation of the severity Levels:
DEBUG:
Info useful to developers for debugging the app, not useful during operations
INFORMATIONAL:
Normal operational messages - may be harvested for reporting, measuring throughput, etc - no action required
NOTICE:
Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required
WARNING:
Warning messages - not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time
ERROR:
Non-urgent failures - these should be relayed to developers or admins; each item must be resolved within a given time
ALERT:
Should be corrected immediately - notify staff who can fix the problem - example is loss of backup ISP connection
CRITICAL:
Should be corrected immediately, but indicates failure in a primary system - fix CRITICAL problems before ALERT - example is loss of primary ISP connection
EMERGENCY:
A "panic" condition - notify all tech staff on call? (earthquake? tornado?) - affects multiple apps/servers/sites...

在生成的日誌消息中,Priority=Facility * 8 + Level,

                  例如:   15   =   1      * 8 +    7

在日誌服務器對該消息的級別進行解析時,Facility = Priority / 8;  Level = Priority % 8;

                                        例如:    1     =    15    /  8;    7    =      15   % 8;

2、HEADER部分
HEADER部分包括兩個字段,時間和主機名(或IP)。
時間緊跟在PRI後面,中間沒有空格,格式必須是“Mmm dd hh:mm:ss”,不包括年份。“日”的數字如果是1~9,前面會補一個空格(也就是月份後面有兩個空格),而“小時”、“分”、“秒”則在前面補“0”。月份取值包括:
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
時間後邊跟一個空格,然後是主機名或者IP地址,主機名不得包括域名部分。
HEADER部分後面跟一個空格,然後是MSG部分。
有些syslog中沒有HEADER部分。這個時候MSG部分緊跟在PRI後面,中間沒有空格。

3、MSG部分
MSG部分又分爲兩個部分,TAG和Content。其中TAG部分是可選的。
在 前面的例子中(“<30>Oct 9 22:33:20 hlfedora auditd[1787]: The audit daemon is exiting.”),“auditd[1787]”是TAG部分,包含了進程名稱和進程PID。PID可以沒有,這個時候中括號也是沒有的。
進程PID有時甚至不是一個數字,例如“root-1787”,解析程序要做好容錯準備。

TAG後面用一個冒號隔開Content部分,這部分的內容是應用程序自定義的。

4、RFC3195
BSD syslog協議使用UDP協議在網絡中傳遞,然而UDP是一個不可靠的協議,並且syslog也沒有要求接收方有所反饋。爲了解決這個問題,RFC又定義了一個新的規範來可靠的傳遞syslog消息,它使用TCP協議:
http://www.ietf.org/rfc/rfc3195.txt
不過大多數情況下,使用UDP發送不需要確認的syslog消息,已經能夠滿足要求了,並且這樣做非常簡單。因此到目前爲止,RFC3195的應用還是很少見的。

 

Kiwi Syslog Server 是一個免費的Windows平臺上的syslog守護進程。它接收,記錄,顯示和轉發系統日誌,如路由器,防火牆,交換機,Unix主機和其他功能的設備主機的syslog消息。有許多可供自定義的選項。其特點包括PIX防火牆日誌記錄,Linksys的家庭防火牆日誌,SNMP陷阱和TCP的支持,有能力進行篩選,分析和修改信息,並透過VBScript或JScript引擎執行動作。註冊版有附加功能。http://www.cr173.com/soft/20677.html

 

感謝引文:

在2001年定義的RFC3164中,描述了BSD syslog協議:http://www.ietf.org/rfc/rfc3164.txt

引文:http://areyouok.iteye.com/blog/251590

http://blog.csdn.net/donhao/article/details/5592204

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章