rsyslog 的預設行為是將追蹤附加到現存的記錄檔.
現在,我已經看到(CentOs、Scientific Linux)當 rsyslog 已經運行時,您刪除日誌檔案(例如專用於記錄應用程式追蹤的日誌檔案),然後執行您的應用程式 rsyslog將不會建立一個日誌文件,不會記錄任何痕跡。
是否有一個設定選項可以告訴 rsyslog 在附加追蹤之前建立一個日誌檔案(如果不存在)?
筆記:執行 aservice rsyslog restart
將強制建立一個空日誌檔案。
rsyslog.conf(沒有添加任何東西)
# rsyslog v5 configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog # provides kernel logging support (previously done by rklogd)
#$ModLoad immark # provides --MARK-- message capability
$SystemLogRateLimitInterval 1
$SystemLogRateLimitBurst 50000
# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514
# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.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!
*.info;mail.none;authpriv.none;cron.none;local1.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
# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
答案1
從rsyslog的POV來看,刪除的日誌檔案仍然存在。這是因為 rsyslog 沒有寫入檔案名,而是寫入為日誌檔案開啟的檔案句柄。
Unix 系統實際上不會刪除文件,直到沒有進程打開該文件的句柄。這意味著,在關閉所有開啟的檔案句柄之前,不會釋放已刪除檔案使用的磁碟空間。這也意味著任何開啟已刪除檔案的檔案句柄的進程都可以繼續讀取和/或寫入該檔案。
向 rsyslog發送 HUP 信號(例如通過pkill -HUP rsyslog
或/etc/init.d/rsyslog rotate
)告訴它關閉所有打開的文件,重新加載其配置文件,並重新打開所有日誌文件進行寫入(如有必要,創建它們)。
重新啟動 rsyslogd 也可以。
請注意,這是一個功能,而不是錯誤,具有一些有用的含義- 例如,這就是為什麼rsyslog 即使在輪換(即重命名/mv-ed)之後仍不斷寫入同一日誌文件,直到rsyslog 收到HUP訊號.這意味著日誌處理腳本和實用程式不必非常小心計時 - 它們只需輪換所有日誌,向 rsyslog 發送 HUP,一切都會繼續工作,不會丟失日誌資料。
順便說一句,rsyslog 不發生這種情況的唯一方法是在每次寫入(或至少呼叫sync()
)時關閉並重新開啟每個日誌檔案。表現將會很糟糕。
答案2
$檔案建立模式
這個選項沒有達到你想要的效果嗎?$檔案建立模式?
摘抄
$FileCreateMode 0600
This sample lets rsyslog create files with read and write access only for the
users it runs under.
The following sample is deemed to be a complete rsyslog.conf:
$umask 0000 # make sure nothing interferes with the following definitions
*.* /var/log/file-with-0644-default
$FileCreateMode 0600
*.* /var/log/file-with-0600
$FileCreateMode 0644
*.* /var/log/file-with-0644
文件輸出模組
根據 rsyslog 文檔,文件輸出模組的 File 參數可用於執行此操作。
摘抄om檔模組
文件
如果檔案已存在,則會將新資料附加到其中。現有資料不會被截斷。如果該文件尚不存在,則會建立該文件。只要 rsyslogd 處於活動狀態,檔案就會保持開啟。這與外部日誌檔案輪換衝突。為了在輪換後關閉文件,請在文件輪換後向 rsyslogd 發送 HUP 訊號。
向系統日誌發送 HUP 訊號
我認為最終您需要“觸發”rsyslog 來執行此操作。我認為這不會自動達到您想要的效果。因此,您可以給它一個 HUP 訊號,以在刪除日誌檔案後觸發日誌檔案的重新建立。
$ sudo pkill -HUP rsyslog
/var/log/messages
這樣做會在我的日誌檔案中建立以下訊息:
Sep 26 15:16:17 grinchy rsyslogd: [origin software="rsyslogd" swVersion="4.6.3" x-pid="1245" x-info="http://www.rsyslog.com"] rsyslogd was HUPed, type 'lightweight'.
Sep 26 15:16:44 grinchy rsyslogd: [origin software="rsyslogd" swVersion="4.6.3" x-pid="1245" x-info="http://www.rsyslog.com"] rsyslogd was HUPed, type 'lightweight'.