UNIX 系統日誌

Unix 系統有一個非常靈活和強大的日誌系統,它可以讓您記錄幾乎任何你能想像和操作日誌,以獲取所需要的資訊。

許多版本 UNIX 提供了一個通用的日誌工具,稱為:syslog。個別程式需要有記錄的資訊發送到 syslog 資訊。

Unix的系統日誌主機配置,統一的系統日誌記錄工具。該系統採用一個集中的系統運行程式  /etc/syslogd 或 /etc/syslog 日誌記錄進程。.

系統日誌的操作是相當簡單的。程式日誌條目發送到syslogd,參考的配置檔在/etc/syslogd.conf 或 /etc/syslog,找到一個匹配時,所需的日誌檔寫入日誌消息。

有四個基本的 syslog 條款,你應該明白:

Term 描述
Facility The identifier used to describe the application or process that submitted the log message. Examples are mail, kernel, and ftp.
Priority An indicator of the importance of the message. Levels are defined within syslog as guidelines, from debugging information to critical events.
Selector A combination of one or more facilities and levels. When an incoming event matches a selector, an action is performed.
Action What happens to an incoming message that matches a selector. Actions can write the message to a log file, echo the message to a console or other device, write the message to a logged in user, or send the message along to another syslog server.

系統日誌設備:

這裏是可用的設備選擇。並非所有的設施都存在於所有版本的UNIX。

設備 描述
auth Activity related to requesting name and password (getty, su, login)
authpriv Same as auth but logged to a file that can only be read by selected users
console Used to capture messages that would generally be directed to the system console
cron Messages from the cron system scheduler
daemon System daemon catch-all
ftp Messages relating to the ftp daemon
kern Kernel messages
local0.local7 Local facilities defined per site
lpr Messages from the line printing system
mail Messages relating to the mail system
mark Pseudo event used to generate timestamps in log files
news Messages relating to network news protocol (nntp)
ntp Messages relating to network time protocol
user Regular user processes
uucp UUCP subsystem

Syslog優先順序:

該系統記錄的優先順序總結在下面的表中:

優先順序 描述
emerg Emergency condition, such as an imminent system crash, usually broadcast to all users
alert Condition that should be corrected immediately, such as a corrupted system database
crit Critical condition, such as a hardware error
err Ordinary error
warning Warning
notice Condition that is not an error, but possibly should be handled in a special way
info Informational message
debug Messages that are used when debugging programs
none Pseudo level used to specify not to log messages.

設備和級別的組合,讓你辨識記錄和資訊。

由於每個程式盡職地發送它的消息的系統記錄器,記錄器作出決定什麼來跟蹤和丟棄的基礎上在選擇器中定義的級別。

當你指定一個級別,系統將跟蹤所有在這一水準較高。

/etc/syslog.conf 檔:

/etc/syslog.conf 檔控制,記錄消息的位置。一個典型的 syslog.conf  檔可能看起來像這樣:

*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice           /var/log/messages
lpr.info                     /var/log/lpr.log
mail.*                       /var/log/mail.log
ftp.*                        /var/log/ftp.log
auth.*                       @prep.ai.mit.edu
auth.*                       root,amrood
netinfo.err                  /var/log/netinfo.log
install.*                    /var/log/install.log
*.emerg                      *
*.alert                      |program_name
mark.*                       /dev/console

檔的每一行包含兩個部分:

  • 消息選擇器,指定哪種要記錄的消息。例如,所有的錯誤消息或內核的所有調試資訊。

  • 應該做些什麼消息,說一個動作域。例如,把它放在一個檔或消息發送到用戶的終端上。

以下是對上述配置的顯著點:

  • 消息選擇器有兩個部分組成:設備和優先順序。例如,kern.debug的選擇由內核(設施)產生的所有調試消息(優先順序)。

  • 消息選擇kern.debug的選擇所有優先順序大於調試。

  • 設施或優先的地方中的星號表示“所有”。例如,*. 調試是指所有調試資訊,而  kern.*  指由內核生成的所有消息。

  • 您還可以使用逗號指定多個設備。兩個或多個選擇可以組合在一起使用分號。

日誌操作:

action字段指定的五個動作之一:

  1. 日誌消息發送到一個檔或設備。例如,/var/log/lpr.log 或 /dev/console.。

  2. 發送一條資訊給用戶。您可以指定多個用戶名(如根,amrood)用逗號將它們分隔開。

  3. 發送一條資訊給所有用戶。在這種情況下,“動作”字段中包含一個星號(例如,*)。

  4. 管道消息的程式。在這種情況下,程式被指定後,UNIX管道符號(|)。

  5. 將消息發送到另一臺主機上的系統日誌。在這種情況下,行動領域包括主機名,前面有一個at符號(例如,@ xuhuhu.com)

logger命令:

UNIX 提供了命令logger ,這是一個非常有用的命令處理系統日誌。 logger 命令記錄消息發送到syslogd守護進程,從而引發系統日誌。

這意味著我們可以在命令行檢查隨時syslogd 守護進程,它的配置。 logger 命令提供系統日誌檔,在命令行添加一行條目的方法。

該命令的格式是:

logger [-i] [-f file] [-p priority] [-t tag] [message]...

下麵是詳細的參數:

選項 描述
-f filename Use the contents of file filename as the message to log.
-i Log the process ID of the logger process with each line.
-p priority Enter the message with the specified priority (specified selector entry); the message priority can be specified numerically, or as a facility.priority pair. The default priority is user.notice.
-t tag Mark each line added to the log with the specified tag.
message The string arguments whose contents are concatenated together in the specified order, separated by the space

您可以使用聯機幫助幫助檢查完成此命令的語法。

日誌切換:

日誌檔的增長傾向非常快,消耗大量的磁片空間。要啟用日誌切換,大多數發行版使用 newsyslog 或 logrotate 工具。

這些工具應該被稱為使用cron守護程式在頻繁的時間間隔。檢查newsyslog 或 logrotate的更多詳細資訊的手冊頁。

重要的日誌位置

所有的系統應用程式創建日誌檔在 /var/log 和其子目錄。這裏有幾個重要的應用程式及其日誌目錄:

應用程式 目錄
httpd /var/log/httpd
samba /var/log/samba
cron /var/log/
mail /var/log/
mysql /var/log/


上一篇: UNIX 系統性能 下一篇: UNIX 信號和陷阱