2
2

More than 1 year has passed since last update.

rsyslog(/etc/rsyslog.conf)のデフォルト内容読解(Cent7.7)

Last updated at Posted at 2022-08-20

image.png
rsyslogの/etc/rsyslog.confのデフォルト状態について読み解いてみました。
公式URL等なども併せて載せていますので原文が気になる方はそちらも確認ください。

■環境
CentOS Linux release 7.7.1908 (Core)
rsyslog-8.24.0-38.el7.x86_64

なお、当記事の内容はRHEL/Cent7標準の従来記述方式となります。
RHEL/Cent8からはRainerScript形式と記述が異なりますため注意ください。


コンフィグのパートに沿って以下分類で読み解いていきます。
①MODULES
②GLOBAL DIRECTIVES
③RULES
④begin forwarding rule

/etc/rsyslog.conf全体(折り畳み)
/etc/rsyslog.conf
# rsyslog 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 ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# 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

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### 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                /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                                                 :omusrmsg:*

# 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.
#$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 ###

①MODULES

このパートはロードするモジュールを記述しています。
(なお、記載外でもロードされるデフォルトモジュールもあります)

/etc/rsyslog.conf(MODULES)
#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514

$ModLoad imuxsock
rsyslogのInputModule。旧来のunix socket file経由(systemd)のログ受信を有効にする。

$ModLoad imjournal
rsyslogのInputModule。現行のjournald経由のログ受信を有効にする。

#$ModLoad imklog
rsyslogのInputModule。ファシリティkernを受信するがコメントアウトされ無効となっている。journal(imjournal)で受け取っているため無効推奨。

#$ModLoad immark
rsyslogのInputModule。rsyslogが動作していることを示すrsyslogd:-- MARK --を20分おきに発報する。(/var/log/messages)。運用時は結構邪魔なので無効推奨。

https://www.rsyslog.com/doc/v8-stable/configuration/modules/imklog.html
https://www.rsyslog.com/doc/master/configuration/modules/immark.html
https://milestone-of-se.nesuke.com/l7protocol/syslog/rsyslog-summary/
https://boscono.hatenablog.com/entry/2015/12/30/095620



#$ModLoad imudp
UDP経由でのログを受け取る。デフォルトはコメントアウトで無効化されている。

#$UDPServerRun 514
UDP経由でのログを受け取る際のポート番号を指定する。デフォルトはコメントアウトで無効化されている。有効にする場合は上記の#$ModLoad imudpも有効にすること。
なお、リッスンIPアドレスを固定する際は$UDPServerAddress 192.168.0.1などが利用できる。

#$ModLoad imtcp
TCP経由でのログを受け取る。デフォルトはコメントアウトで無効化されている。

#$InputTCPServerRun 514
TCP経由でのログを受け取る際のポート番号を指定する。デフォルトはコメントアウトで無効化されている。有効にする場合は上記の#$ModLoad imtcpも有効にすること。
なお、$TCPServerAddressは無い模様。

https://www.rsyslog.com/doc/v8-stable/configuration/modules/imudp.html
https://www.rsyslog.com/doc/v8-stable/configuration/modules/imtcp.html



②GLOBAL DIRECTIVES

このパートは名前の通り、全体に関わる制御内容を記述しています。

/etc/rsyslog.conf(GLOBAL DIRECTIVES)
#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# 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

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state

$WorkDirectory /var/lib/rsyslog
ワークディレクトリの指定。キューなどもこのディレクトリに保存される。

$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
ログのタイムスタンプ形式を変更する。

"RSYSLOG_TraditionalFileFormat" → "OCT 10 09:01:01"
"RSYSLOG_FileFormat"      → "2018-10-10T10:37:53.063083+09:00"

#$ActionFileEnableSync on
ファイルへのフラッシュ同期をOnにする。マニュアルによると性能劣化するため、デフォルトのコメントアウト(off)推奨。

$IncludeConfig /etc/rsyslog.d/*.conf
個別設定ファイルの読み取り先を指定。ファイル名はワイルドカード(*)となっており複数ファイルを読み込む。

$OmitLocalLogging on
デフォルト(on)の場合、旧来のunix socket file経由(systemd)のログ受信を停止する。
offにすると受信するが、ModLoad imjournalなどを停止しないとログ内容を二重受信する。

$IMJournalStateFile imjournal.state
Journalに関する永続ファイルの場所指定。imjournalを利用する場合は有効化する(コメントアウトしない)。

https://www.rsyslog.com/doc/master/rainerscript/global.html
https://www.rsyslog.com/doc/v8-stable/configuration/modules/omfile.html
https://milestone-of-se.nesuke.com/l7protocol/syslog/rsyslog-summary/
https://www.rsyslog.com/doc/v8-stable/configuration/global/options/rsconf1_includeconfig.html
https://www.rsyslog.com/doc/v8-stable/configuration/modules/imuxsock.html
https://straypenguin.winfield-net.com/journald.html
https://www.rsyslog.com/doc/master/configuration/modules/imjournal.html



③RULES

(この部分はいろんなサイトで説明があるのであっさりにします)

/etc/rsyslog.conf(RULES)
#### 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                /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                                                 :omusrmsg:*

# 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

このパートは以下のフォーマットに従って指定されたログファイルにログを書き込む記述をしています。
(なお、Serverrityはそれ以上のレベル全てが対象となります)
<Facility>.<Severity> <LogFile>

ワイルドカードが指定可能なので、authpriv.*はFacility:authpriv.*かつ全てのServerityを/var/log/secureに書き込み、*.emergは全てのFacilityかつServerity:emerg以上をomusrmsg:*に書き込んだりすることになります。

ログファイルの冒頭に-を付記すると非同期書き込みモードになります。逆に何もないと同期書き込みモードになります。

また:omusrmsg:*はOutput Module User Messageとしてユーザコンソールに表示を吐き出します。

https://www.infraexpert.com/study/syslog1.html
https://milestone-of-se.nesuke.com/l7protocol/syslog/rsyslog-summary/
https://www.server-world.info/query?os=CentOS_Stream_8&p=rsyslog



④begin forwarding rule

このパートは別のサーバにログを転送する際の設定を記述しています。

/etc/rsyslog.conf(begin forwarding rule)
# ### 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.
#$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 ###

#$ActionQueueFileName fwdRule1
【TCP転送時】転送先サーバが停止している場合のキューファイル名。ディレクトリは$WorkDirectoryを使用する。デフォルトはコメントアウトで無効。

#$ActionQueueMaxDiskSpace 1g
【TCP転送時】転送先サーバが停止している場合のキューファイルサイズ。デフォルトはコメントアウトで無効。

#$ActionQueueSaveOnShutdown on
【TCP転送時】転送先サーバが停止している場合のキューファイルをシャットダウンに保存する。デフォルトはコメントアウトで無効。

#$ActionQueueType LinkedList
【TCP転送時】転送先サーバが停止している場合のキュータイプを選択する。デフォルトはコメントアウトで無効。

#$ActionResumeRetryCount -1
【TCP転送時】転送先サーバが停止している場合のリトライ。0でリトライ、-1で無効。デフォルトはコメントアウトで無効。

https://www.rsyslog.com/doc/master/rainerscript/queue_parameters.html
https://www.rsyslog.com/doc/master/concepts/queues.html
https://l-w-i.net/t/rsyslog/conf_001.txt



#*.* @@remote-host:514
ログをrsyslogで転送するサーバとプロトコルを指定します。デフォルトではコメントアウトで無効。

なお、以下のようにユニークに設定可能です。
  cron.info @@192.168.0.1:514
  →Facility:cronかつServerity:info以上を192.168.0.1にTCP514で転送

  cron.info @192.168.0.1:514
  →Facility:cronかつServerity:info以上を192.168.0.1にUDP514で転送
  (@@はTCP、@はUDPでの転送になります)

https://www.rsyslog.com/sending-messages-to-a-remote-syslog-server/
https://zaki-hmkc.hatenablog.com/entry/2020/03/26/062801
https://www.secuavail.com/kb/tech-blog/tb-210521-01-2/



後記

RHEL/Cent8版(RainerScript)も余裕があったら書く・・・

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2