<序言>
我已經寫過兩篇實用型的博文,可以作為參考:
1) 如何將Linux主機設置成syslog服務器;
2) 使用cron和logrotate來管理日志文件。
這篇文章主要介紹日志服務的基礎知識。涉及日志的服務有兩個,在Fedora9中是rsyslog服務和logrotate服務。前者負責寫入日志,后者負責備份和刪除舊日志,以及更新日志文件。
1. rsyslogd 服務
1.1 查看當前rsyslogd服務的狀態
在Fedora 9開始,負責寫入日志信息的服務是rsyslogd,它的配置文件為:/etc/rsyslog.conf。我們檢測一下當前這個服務是不是在運行。
[flagonxia@airhouse etc]$ ps -ef | grep -v grep | grep rsyslog
root 1584 1 0 10:33 ? 00:00:00 rsyslogd -c 3
從上面命令的輸出結果看到rsyslogd執行時使用的參數是-c 3。這個輸入參數在文件/etc/sysconfig/rsyslog中指定。
[flagonxia@airhouse sysconfig]$ less rsyslog
# Options to syslogd
# syslogd options are deprecated in rsyslog v3
# if you want to use them, switch to compatibility mode 2 by "-c 2"
SYSLOGD_OPTIONS="-c 3"
也可以通過另一種方式,查看當前運行級別中,rsyslogd是否運行。
[flagonxia@airhouse sysconfig]$ sudo chkconfig --list rsyslog
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
注意,這里的服務名是rsyslog。
1.2 配置文件/etc/rsyslog.conf
1.2.1 配置文件的基本信息
配置文件/etc/rsyslog.conf中有很多內容,但最主要的是指定需要記錄哪些服務和需要記錄什么等級的信息。
下面是rsyslog.conf的內容。
# /etc/rsyslog.conf
... ...
#### RULES ####
# 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!
A *.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.
B mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
C *.emerg *
# Save news errors of level crit and higher in a special file.
D uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
【注釋】
A:把所有大于info級別的信息都記錄到/var/log/messages中,但不要記錄mail,authpriv和cron服務產生的信息;
B:把mail產生的信息都記錄到/var/log/maillog中
C:把所有服務輸出的“大于”emergy級別的信息顯示給每個在線的人,通過wall工具
D:把uucp和news輸出的大雨crit級別的信息記錄到/var/log/spooler中
1.2.2 信息的等級及其在配置文件中指定的方式
A 七種信息等級
1)info
2)notice
3)warning或warn
4)err或error
5)crit
6)alert
7)emerg或panic:導致系統幾乎要死機
B 信息等級的指定方式
1). xxx: 表示大于xxx級別的信息
2).=xxx:表示等于xxx級別的信息
3).!xxx:表示在xxx之外的等級的信息
2. logrotate服務
2.1 logrotate服務的啟動方式
logrotate是一個日志管理程序,用來把舊的日志文件刪除(備份),并創建新的日志文件,這個過程稱為“轉儲”。我們可以根據日志的大小,或者根據其使用的天數來轉儲。
logrotate的執行由crond服務實現。在/etc/cron.daily目錄中,有個文件logrotate,它實際上是個shell script,用來啟動logrotate。logrotate程序每天由cron在指定的時間(/etc/crontab)啟動。
[flagonxia@airhouse cron.daily]$ less logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
因此,使用ps是無法查看到logrotate的。如果它沒有起來,就要查看一下crond服務有沒有在運行。
[flagonxia@airhouse logrotate.d]$ ps -ef | grep -v grep | grep cron
root 1984 1 0 10:34 ? 00:00:00 crond
2.2 logrotate的配置文件/etc/logrotate.conf
在執行logrotate時,需要指定其配置文件/etc/logrotate.conf。這個文件定義了如何轉儲日志文件的規則。如下:
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
這個配置文件的注釋寫得很清楚,沒有必要再羅嗦了。只想強調下面這行,它的作用包含存放在/etc/logrotate.d目錄下面的配置文件,不可或缺。如果你安裝了一個新的服務,它的日志轉儲的規則可以建立一個專門的配置文件,放在/etc/logrotate.d下面。它其實也因為下面的這句話,在logrotate服務啟動時被讀取。
include /etc/logrotate.d
這里有個例子:/etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
上面的配置對于/var/log/messages, /var/log/secure, /var/log/mailog/ /var/log/spooler, /var/log/boot.log, /var/log/cron都是適用的。
注意,prerotate和postrotate必須和sharescripts...endscript一起用。上面的信息表示日志文件轉儲后,重啟rsyslogd服務。
每個存放在/etc/logrotate.d目錄里的文件,都有上面格式的配置信息。在{}中定義的規則,如果與logrotate.conf中的沖突,以/etc/logrotatate.d/中的文件定義的為準。