LoginSignup
0
0

More than 1 year has passed since last update.

rsyslogによるログ送信および受信設定(Cent7.7)

Posted at

image.png
rsyslogによるログ転送について動作確認してみました。

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

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

1.構成

image.png

2.送信元の設定

送信元の設定としては、「全てのログを送信先サーバへUDP514で転送する」としたいと思います。

送信元の/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

########### ADD ###########
*.* @192.168.142.32:514
########### END ###########

# ### end of the forwarding rule ###

2.1 送信元設定

begin forwarding ruleの末尾に以下内容を追記しています。

送信元設定
########### ADD ###########
*.* @192.168.142.32:514
########### END ###########

送信ログ対象を絞りたい場合は、*.*cron.warnのようにし、
TCPで送信したい場合は@192.168...@@192.168...とします。

2.2 サービス再起動

サービスを再起動すれば完了です。
(直後からログ転送が開始されますが、UDPのため破棄されます)

rsyslog再起動
systemctl restart rsyslog

3.送信元の設定

送信先の設定としては、「192.168.0.0/16から飛んできたログをLogGroup1と定義し、LogGroup1の対象となるものは指定ディレクトリに出力する」というものです。

3.1 UDP受信、3.2 ログ定義、3.3 受信指定、の3ブロックに分けて記述します。

送信先の/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
########### ADD ###########
$ModLoad imudp
$UDPServerRun 514
########### END ###########



# 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

########### ADD ###########
$template LogGroup001, "/var/log/remote/%fromhost-ip%/%$year%-%$month%-%$day%.log"
########### END ###########



#### RULES ####

########### ADD ###########
:fromhost-ip, startswith, "192.168."   -?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


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

3.1 UDP受信

MODULESディレクティブの#$UDPServerRun 514の下あたりに以下を追記します。
(Input Module UDPをOnにし、UDP514で受信可能とする)

UDP受信
########### ADD ###########
$ModLoad imudp
$UDPServerRun 514
########### END ###########

3.2 ログ定義

GLOBAL DIRECTIVESの$IMJournalStateFile imjournal.stateの下あたり(最下部)に以下を追記します。
ロググループ定義とその保存先ファイルを指定します。

ログ定義
########### ADD ###########
$template LogGroup001, "/var/log/remote/%fromhost-ip%/%$year%-%$month%-%$day%.log"
########### END ###########

%fromhost-ip%は受信IPアドレスのマクロとなりますが、そのほかにも%fromhos%でホスト名を指定することなどが可能です。
https://www.rsyslog.com/doc/master/configuration/properties.html

3.3 受信指定

RULESディレクティブの最上段に以下を追記します。
特定のIPアドレスから始まるものについてはログ定義に従うものとし、
その場合はこれ以後のRULESを適用しないこと(&stop)としています。
&stopがないと送信先側の/var/log/messagesなどにも出力されます)

########### ADD ###########
:fromhost-ip, startswith, "192.168."   -?LogGroup001
&stop
########### END ###########

-?LogGroup001の接頭辞について、-は非同期書き込み、?は動的テンプレート指定(②ログ定義)の場合に必要となるものです。

3.4 サービス再起動

3.1~3.3の後、サービスを再起動すれば完了です。

rsyslog再起動
systemctl restart rsyslog

4.送信確認

では実際に送信確認してみましょう。

4.1 送信元

loggerコマンドを利用してログ生成します。

送信元
# logger -ip local1.warn -t logger-cmd "test message"

# tail -f /var/log/messages
Aug 21 10:32:40 cent77-01 logger-cmd[7520]: test message

ローカルの/var/log/messagesに書き込まれています。

4.2送信先

ファイルおよび内容を確認します。

送信元
# ll /var/log/remote/192.168.142.31/
合計 8
-rw------- 1 root root 7508  8月 21 11:16 2022-08-21.log

# tail -f /var/log/remote/192.168.142.31/2022-08-21.log
Aug 21 10:32:40 cent77-01 logger-cmd[7520]: test message

問題なくログが転送されたことが確認できました。

出典

0
0
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
0
0