何謂 Syslogd

Syslogd的運作流程

Syslogd的基本組態

Log檔內的儲存格式

使用者自定Log檔內容

組態Log Server及Log Client

 

Syslogd的運作流程

 

為了能讓系統管理人員,清楚掌握系統的運作狀況,大多數的作業系統都會有日誌(Log)的功能,在 RHEL 3.0 系統中是由 sysklogd-1.4.1-12.i386.rpm 這個套件來負責此一功能的,在 Syslogd 系統中共有兩個 daemon分別為 Syslogd 及 Klogd如上圖所示Kernel 所產生的 Log 將會交由 Klogd 來處理,Klogd 在交給 Syslogd 處理而 AP 的部分可能會有兩種做法一是將 Log 交由 Syslogd 處理二是 AP 自行處理如果是 AP 自行處理那麼 AP 將自行將 Log 資訊儲存於 /var/log/ 目錄下的某個檔案 如果是交理 Syslogd 來處理那麼 Syslogd 將會依照 /etc/syslog.conf 的內容來決定那些資訊該儲存於 /var/log/ 目錄下的那一個檔案。

 

Syslogd的基本組態

Syslogd 有兩個設定檔,分別是 /etc/sysconfig/syslog及 /etc/syslog.conf,如下所示:

/etc/sysconfig/syslog

# Options to syslogd
# -m 0 disables 'MARK' messages.
# -r enables logging from remote machines
# -x disables DNS lookups on messages recieved with -r
# See syslogd(8) for more details
SYSLOGD_OPTIONS="-m 0"


# Options to klogd
# -2 prints all kernel oops messages twice; once for klogd to decode, and
# once for processing with 'ksymoops'
# -x disables all klogd processing of oops messages entirely
# See klogd(8) for more details
KLOGD_OPTIONS="-x"

 

在一般的使用下,鮮少需要更動這個檔案,除非有特殊的需求。

 

/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

 

在這個設定檔中, 其語法如下:

  類型.記錄等級 處理方式 

類型可分為: authauthprivcron, daemon,  kernlprmail,  mark,  newssecurity (same as auth),  syslog,  useruucp local0 local7,其中 local0~local7為使用者自訂類型,也可以使用 號來代表所有的類型。
「記錄等級」可分為: debuginfo,  noticewarningwarn (same as warning),  err,  error (same as err),  crit,  alertemerg,  panic (same as emerg),以上各等級所記錄的資訊,由左至右遞減,但系統的緊急程度,由右至左遞減,也可以使用 none 表示全部不要。
「處理方式」可分為: 存入檔案終端機視窗傳給線上的使用者或者傳遞至另外的Linux主機

 

前端的 AP 可以自行定義其產生 Log 類型「記錄等級」,而 /etc/syslog.conf 檔案內則定義著,前端 AP 傳來的 Log 資訊其類型「記錄等級」是什麼,再決定要儲存於何檔案內。

 

範例

*.info ; mail.none ; authpriv.none ; cron.none /var/log/messages

*.info :其中的 * 表示要記錄所有的「大項」, 而 . info表示要記錄 >= info 等級的所有資訊。

mail.none:其中的 mail 表示要記錄 mail 該項記錄,但後面的 .none 表示所有等級的資訊都不要。

所以這一行的意思是說:除了mail、authpriv及cron大項的資訊不需要, 其餘的類型中,大於等於info等級的資訊都要記錄到 /var/log/message這個檔案內

PS:請注意「類型」及「等級」之間的連結符號,共可分為  .   .=  .=! .* 這四種, 其意義如下:

. 代表 >= 的意思

.= 代表  =  的意思

.=! 代表 <> 的意思

.* 代表所有的意思

 

Log檔內的儲存格式

Syslogd 將系統資訊儲存於檔案,其檔案內的格式如下

Sep 19 23:43:28 mail sshd[947]: Accepted password for root from 192.168.18.1 port 3287

其格式共分為四個欄位,分別如下

Sep 19 23:43:28 記錄的時間
mail 產生此 Log 的主機名稱
sshd[947] 產生此 Log 的應用程式名稱
Accepted password for root from 192.168.18.1 port 3287 此一 Log 的內容

 

 

 

使用者自定Log檔內容

由於 Syslogd 會將不同應用程式所產生的 log ,置於相同一個檔案內(如: /var/log/message 就集合了各種不同的 log),如果我們希望將某一個應用程式的 log, 獨立存放於一個檔案內,又該如何指定呢

一般應用程式的設定檔,都可以指定 Log 輸出的 類型,以 sshd  為例,sshd 的設定檔為 /etc/ssh/sshd_config,其內容有一行為SyslogFacility AUTHPRIV,表示 sshd 所產生的 log 將歸類為 AUTHPRIV 的類型,而 /etc/syslog.conf 檔案內指定將 AUTHPRIV 類型的資訊記錄於 /var/log/secure, 因此在預設定狀況下,sshd 所產生的 log 就存在於 /etc/log/secure內。

我們可以試著將 /etc/ssh/sshd_config 內的 SyslogFacility AUTHPRIV」改為 「SyslogFacility local0,也就是將 log 的類型改為 local0,並在 /etc/syslog.conf 檔案內加入一行「 local0.* /var/log/sshd」,接著請重新啟動 sshd 及 syslogd ,並試著使用 ssh 於遠端登入,此時 /var/log/ 內應該就會出現 sshd 這個 log 檔了。

 

組態Log Server及Log Client

如果 Server 眾多時,要管理分散於各地的 Log 也是一件苦差事,所幸 Syslog 系統本身就有 Client Server 的架構,只要經過簡單的組態,即可架構出 Log Server ,並將分散於各地的 Log 回存於 Log Server 之上,接下來就為各位示範 Log Server 及 Log Client 的組態。

 

Log Server

請修放 /etc/sysconfig/syslog 檔案,並將 「SYSLOGD_OPTIONS="-m 0"」改為「SYSLOGD_OPTIONS="-r -m 0"」,然後重新啟動 syslog 服務,如果 Log Server 有使用防火牆,那麼請記得將 udp 的 514 port 打開。

 

Log Client

  請修改 /etc/syslog.conf ,例如原本存於本機內 /var/log/secure 的資訊,要傳回 Log Server 內存放,那麼請修改如下:

修改前:authpriv.* /var/log/secure 修改後:authpriv.* @192.168.1.254

其中 @192.168.1.254 為 Log Server 的 IP,接著 Log Client 主機上,原本存於 /var/log/secure 檔案內的資訊,將會傳回給 Log Server ,而 Log Server 也將是依照 Log 的「類型」,將 log 資訊存放於 /var/log 內的某一個檔案。

也許你會有個疑問,如果有 10 個 Log Client,那 10 個 Log Client 的資訊都擠在 Log Server 的某一個檔案之中,不是更難管理嗎?

事實上一點也不會難以管理,別忘了 Log 格式中的第二個欄位為「產生此 Log 的主機名稱」,因此我們只要使用 grep 指令,並透過 hostname 來搜尋,即可快速分離各 Log Client 的 Log 資訊了。

posted on 2008-12-18 11:55  starspace  阅读(918)  评论(0编辑  收藏  举报

导航