CentOS 7でrsyslogサーバを構築し、クライアントからのログを受け付ける設定をする。
1.基本的な設定
サーバ側の設定
まずはrsyslogサーバとなるホスト上で、以下のように設定ファイル(/etc/rsyslog.conf)の記述を変更する。
/etc/rsyslog.conf
properties
# Provides UDP syslog reception
$ModLoad imudp # コメントアウトを解除(UDP)
$UDPServerRun 514 # コメントアウトを解除(UDP)
# Provides TCP syslog reception
$ModLoad imtcp # コメントアウトを解除(TCP)
$InputTCPServerRun 514 # コメントアウトを解除(TCP)
送信元を制限する場合は、以下の設定も追記する。
※TCPとUDPを1行で設定はできないので注意(分けて記述する必要がある)。
properties
$AllowedSender UDP, 127.0.0.1, *.example.jp, 192.168.0.0/24 # UDPの場合
$AllowedSender TCP, 127.0.0.1, *.example.jp, 192.168.0.0/24 # TCPの場合
設定ファイル編集後、Firewalldのポートを開放しサービス再起動をする。
bashfirewall-cmd --permanent --add-port=514/tcp firewall-cmd --permanent --add-port=514/udp firewall-cmd --reload systemctl restart rsyslog
クライアント側の設定
次に、クライアント側でログの転送設定をする。
※今回はクライアント側もCentOS 7を用いている。
●
/etc/rsyslog.conf
properties
# 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
*.* @Rsyslogサーバのホスト名orIPアドレス:514 # UDPの場合
*.* @@Rsyslogサーバのホスト名orIPアドレス:514 #TCPの場合
# ### end of the forwarding rule ###
設定ファイル編集後、サービスを再起動する。
bashsystemctl restart rsyslog
これで、クライアントのログがrsyslogサーバに転送されるようになる。
2.ログ分け
先ほどまでの設定でクライアントからサーバへログが転送されるようになったが、このままだとサーバ側の「/var/log/messages」に書き込まれてしまう。
rsyslogでは以下のような変数を利用できるので、テンプレート機能と組み合わせて設定してやるといいだろう。
- %fromhost% … ログのクライアントホスト名
- %fromhost_ip% … ログのクライアントIPアドレス
- %$year% … 西暦年
- %$month% … 西暦月
- %$day% … 西暦日
以下、設定例(追記)。
※「ClinetMessage」はテンプレート名なので、環境に合わせて任意の名称を付与すること。
クライアントのホスト名ごとに分ける
properties
$template ClinetMessage,"/var/log/rsyslog/%fromhost%/messages.log"
*.* -?ClinetMessage
さらに日付ごとに分ける
properties
$template ClinetMessage,"/var/log/rsyslog/%fromhost%/%$year%%$month%%$day%_messages.log"
*.* -?ClinetMessage