■環境
CentOS Linux release 8.3.2011
rsyslog-8.1911.0-6.el8.x86_64
1.構成
2.送信元の設定
送信元の設定としては、「全てのログを送信先サーバへUDP514で転送する」としたいと思います。
送信元の/etc/rsyslog.conf全体(折り畳み)
# rsyslog configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# or latest version online at http://www.rsyslog.com/doc/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES ####
module(load="imuxsock" # provides support for local system logging (e.g. via logger command)
SysSock.Use="off") # Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
module(load="imjournal" # provides access to the systemd journal
StateFile="imjournal.state") # File to store the position in the journal
#module(load="imklog") # reads kernel messages (the same are read from journald)
#module(load="immark") # provides --MARK-- message capability
# Provides UDP syslog reception
# for parameters see http://www.rsyslog.com/doc/imudp.html
#module(load="imudp") # needs to be done just once
#input(type="imudp" port="514")
# Provides TCP syslog reception
# for parameters see http://www.rsyslog.com/doc/imtcp.html
#module(load="imtcp") # needs to be done just once
#input(type="imtcp" port="514")
#### GLOBAL DIRECTIVES ####
# Where to place auxiliary files
global(workDirectory="/var/lib/rsyslog")
# Use default timestamp format
module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")
# Include all config files in /etc/rsyslog.d/
include(file="/etc/rsyslog.d/*.conf" mode="optional")
#### 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
# ### sample forwarding rule ###
#action(type="omfwd"
# 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.
#queue.filename="fwdRule1" # unique name prefix for spool files
#queue.maxdiskspace="1g" # 1gb space limit (use as much as possible)
#queue.saveonshutdown="on" # save messages to disk on shutdown
#queue.type="LinkedList" # run asynchronously
#action.resumeRetryCount="-1" # infinite retries if host is down
# Remote Logging (we use TCP for reliable delivery)
# remote_host is: name/ip, e.g. 192.168.0.1, port optional e.g. 10514
#Target="remote_host" Port="XXX" Protocol="tcp")
########### ADD ###########
action(type="omfwd" Target="192.168.142.44" Port="514" Protocol="udp")
########### END ###########
2.1 送信元設定
最下行に以下内容を追記しています。
########### ADD ###########
action(type="omfwd" Target="192.168.142.44" Port="514" Protocol="udp")
########### END ###########
なお、最下行に設定する場合は全てのFacilityかつ全てのServerityを送信することになります。
送信ログ対象を絞りたい場合は.と併用する必要があります。
例として、cronおよびinfo以上のログのみを送信したい場合は以下となります。
cron.info action(type="omfwd" Target="192.168.142.44" Port="514" Protocol="udp")
2.2 サービス再起動
サービスを再起動すれば完了です。
(直後からログ転送が開始されますが、UDPのため破棄されます)
systemctl restart rsyslog
3.送信元の設定
送信先の設定としては、「UDPで飛んできたログをLogGroup001と定義し、LogGroup001の対象となるものは指定ディレクトリに出力する」というものです。
3.1 ログ定義、3.2 ルール定義、3.3 UDP受信の3ブロックに分けて記述します。
送信先の/etc/rsyslog.conf全体(折り畳み)
# rsyslog configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# or latest version online at http://www.rsyslog.com/doc/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES ####
module(load="imuxsock" # provides support for local system logging (e.g. via logger command)
SysSock.Use="off") # Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
module(load="imjournal" # provides access to the systemd journal
StateFile="imjournal.state") # File to store the position in the journal
#module(load="imklog") # reads kernel messages (the same are read from journald)
#module(load="immark") # provides --MARK-- message capability
# Provides UDP syslog reception
# for parameters see http://www.rsyslog.com/doc/imudp.html
#module(load="imudp") # needs to be done just once
#input(type="imudp" port="514")
########### ADD ###########
module(load="imudp") # needs to be done just once
input(type="imudp" port="514" ruleset="ruleset001")
########### END ###########
# Provides TCP syslog reception
# for parameters see http://www.rsyslog.com/doc/imtcp.html
#module(load="imtcp") # needs to be done just once
#input(type="imtcp" port="514")
#### GLOBAL DIRECTIVES ####
# Where to place auxiliary files
global(workDirectory="/var/lib/rsyslog")
# Use default timestamp format
module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")
# Include all config files in /etc/rsyslog.d/
include(file="/etc/rsyslog.d/*.conf" mode="optional")
########### ADD ###########
template(name="LogGroup001" type="string"
string="/var/log/remote/%fromhost-ip%/%$year%-%$month%-%$day%.log"
)
########### END ###########
#### RULES ####
########### ADD ###########
ruleset(name="ruleset001"){
*.* action(type="omfile" DynaFile="LogGroup001")
stop
}
########### END ###########
# 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
# ### sample forwarding rule ###
#action(type="omfwd"
# 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.
#queue.filename="fwdRule1" # unique name prefix for spool files
#queue.maxdiskspace="1g" # 1gb space limit (use as much as possible)
#queue.saveonshutdown="on" # save messages to disk on shutdown
#queue.type="LinkedList" # run asynchronously
#action.resumeRetryCount="-1" # infinite retries if host is down
# Remote Logging (we use TCP for reliable delivery)
# remote_host is: name/ip, e.g. 192.168.0.1, port optional e.g. 10514
#Target="remote_host" Port="XXX" Protocol="tcp")
3.1 ログ定義
GLOBAL DIRECTIVESのinclude(file="/etc/rsyslog.d/*.conf" mode="optional")
の下あたり(最下部)に以下を追記します。
ロググループ定義とその保存先ファイルを指定します。
########### ADD ###########
template(name="LogGroup001" type="string"
string="/var/log/remote/%fromhost-ip%/%$year%-%$month%-%$day%.log"
)
########### END ###########
%fromhost-ip%
は受信IPアドレスのマクロとなりますが、そのほかにも%fromhos%
でホスト名を指定することなどが可能です。
https://www.rsyslog.com/doc/master/configuration/properties.html
3.2 ルール定義
RULESディレクティブの最上段に以下を追記します。
このルールruleset001
は、
--- *.*
(全てのFacilityかつ全てのServerity)において、
--- omfile
(Output Module File(標準モジュール))を利用して
--- LogGroup001
(ログ定義)に出力される
という表現となります。
また、このルールに合致する場合、他のルールを適用しないこととしてstop
を記述しています。
(stop
がないと次のルールも評価対象となります)
########### ADD ###########
ruleset(name="ruleset001"){
*.* action(type="omfile" DynaFile="LogGroup001")
stop
}
########### END ###########
3.3 UDP受信
ログ定義を利用してルール定義を準備したら、ルール定義を利用して受信モジュールを作成します。
MODULESディレクティブの#input(type="imudp" port="514")
の下あたりに以下を追記します。
(Input Module UDPをOnにし、UDP514で受信可能とし、ルールruleset001
を適用する)
########### ADD ###########
module(load="imudp") # needs to be done just once
input(type="imudp" port="514" ruleset="ruleset001")
########### END ###########
3.4 サービス再起動
3.1~3.3の後、サービスを再起動すれば完了です。
systemctl restart rsyslog
4.送信確認
では実際に送信確認してみましょう。
4.1 送信元
logger
コマンドを利用してログ生成します。
# logger -ip local1.warn -t logger-cmd "test message"
# tail -f /var/log/messages
Aug 21 14:20:59 cent83-01 logger-cmd[2201]: test message
ローカルの/var/log/messagesに書き込まれています。
4.2送信先
ファイルおよび内容を確認します。
# ll /var/log/remote/192.168.142.43/
合計 4
-rw------- 1 root root 57 8月 21 14:21 2022-08-21.log
# tail -f /var/log/remote/192.168.142.31/2022-08-21.log
Aug 21 14:20:59 cent83-01 logger-cmd[2201]: test message
問題なくログが転送されたことが確認できました。
出典