ログ集約・収集について【syslog-ng - 集約】

これは、連載のような記事です。

syslog - 集約

syslog-ng - 集約 <= いまココ

syslog-ng - 収集

と見ると一番分かりやすいです。収集・集約の言葉の定義も分かります。

syslog-ngを使ったログの集約

syslog-ngはsyslogの実装の1つです。syslog-ngではsyslogに比べて、出力するログのフィルタリングや、ログのローテート、ログを出力するディレクトリの自動的な作成など、いろいろ便利な機能を備えています。その便利な機能の1つに「マクロ」があります。これはログにメタ情報などを表すもので、イメージ的には変数が近いです。用意されているマクロには現在の日時やログのファシリティ/プライオリティを表すもの、ログを出力したホストを表すもの、出力されたログに設定されているタグ(プログラム名)を表すものなどがあります。syslog-ngではこのマクロを使って、ログを出力するファイルの名前を組み立てることができます。つまり、たとえば出力先のファイル名としてタグを表すマクロを使えば、同じタグが付いているログは同じファイルに出力されることになります。ログのローテートも、ファイル名の一部に日付を表すマクロを使えば表現できます。

サーバ/インフラを支える技術 P298「Webサーバのログの扱い」

便利そうです・・・やってみます。

安全性の高いログ・サーバへの乗り換えのススメ(1)〜 syslogサーバからsyslog-ngへの乗り換え 〜:止められないUNIXサーバのセキュリティ対策(9) - @IT
安全性の高いログ・サーバへの乗り換えのススメ(2)〜 ログ管理のセキュリティ強化を実現する方法を知ろう 〜:止められないUNIXサーバのセキュリティ対策(10) - @IT
上記のページを参考に行います。


絵で表すと以下のようなイメージになります。

なお、転送元サーバとログサーバの環境は以下です。

$ cat /etc/redhat-release
CentOS release 5 (Final)

syslog-ngのインストール

syslog-ng を利用するには、ログサーバ、転送元サーバともにsyslog-ngをインストールする必要があります。では、インストールしましょう。

syslog-ng のインストール
$ wget http://www.balabit.com/downloads/files/syslog-ng/open-source-edition/3.0.2/setups/rhel-5-i386/syslog-ng-3.0.2-1.rhel5.i386.rpm
$ sudo rpm -i syslog-ng-3.0.2-1.rhel5.i386.rpm 
Password:
warning: syslog-ng-3.0.2-1.rhel5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 2aa28252
Shutting down kernel logger: [  OK  ]
Shutting down system logger: [  OK  ]
Starting syslog-ng: OK
$ rpm -qa | grep syslog-ng
syslog-ng-3.0.2-1.rhel5

RPMですんなり入りました。転送元、ログサーバともにインストールします。

syslog-ngの基本

まず、syslog-ngの基本を理解しましょう。

設定ファイルは、 /opt/syslog-ng/etc/syslog-ng.conf です。初期設定では、すべてのログが /var/log/messages にいくように設定されていますので、カスタマイズが必要です。後述します。
syslog-ng.confは以下の5つの項目が基本となっており、(1)(2)(3)に値をセットして(4)でルールの対応付けを行う。(5)は任意の設定である。

(1)source(ログの受信に関する設定)
(2)filter(出力するログのフィルタリング)
(3)destination(ログの送信に関する設定)
(4)log(source、destination、filterの対応付け)
(5)options(オプション)

転送元(192.168.0.50)のsyslog-ngの設定

まず、こちらを参考に転送元サーバのsyslogdの設定を引き継ぎます。

syslogの設定(/etc/syslog.conf)

#kern.*                                                 /dev/console
# local6 は lighttpdのログを流すので /var/log/messagesに行かないようにします
*.info;mail.none;authpriv.none;cron.none;local6.none    /var/log/messages
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
cron.*                                                  /var/log/cron
*.emerg                                                 *
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log
# lighttpdのログは local6.info に固定します
local6.info             @loghost

syslogの設定をsyslog-ngに引き継ぎます。以下のようになります。

syslog-ngの設定(/opt/syslog-ng/etc/syslog-ng.conf)

@version: 3.0

source s_local { file ("/proc/kmsg" program_override("kernel: ")); unix-stream("/dev/log"); internal(); };

#destination d_console  { pipe("/dev/console"); };
destination d_messages { file("/var/log/messages"); };
destination d_authlog  { file("/var/log/secure"); };
destination d_maillog  { file("/var/log/maillog");};
destination d_cron     { file("/var/cron/log"); };
destination d_alluser  { usertty("*"); };
destination d_spooler  { file("/var/log/spooler"); };
destination d_local7   { file("/var/log/boot.log");};
destination d_loghost  { udp("loghost");};
# IP指定もOK  destination d_loghost  { udp("192.168.0.10");};

#filter f_console  { facility(kern) and level(debug..emerg)};
filter f_messages { level(info) and
                    not facility(mail) and
                    not facility(authpriv) and
                    not facility(cron) and
                    not facility(local6) and level(info);
                  };
filter f_authlog  { facility(authpriv) and level(debug..emerg); };
filter f_maillog  { facility(mail) and level(debug..emerg); };
filter f_cron     { facility(cron) and level(debug..emerg); };
filter f_alluser  { level(emerg); };
filter f_spooler  { facility(uucp) and level(crit) or
                    facility(news) and level(crit);
                  };
filter f_local7   { facility(local7) and level(debug..emerg); };
filter f_loghost  { facility(local6) and level(info); };

#log { source(s_local); filter(f_console);  destination(d_console); };
log { source(s_local); filter(f_messages); destination(d_messages); };
log { source(s_local); filter(f_authlog);  destination(d_authlog); };
log { source(s_local); filter(f_maillog);     destination(d_maillog); };
log { source(s_local); filter(f_cron);     destination(d_cron); };
log { source(s_local); filter(f_alluser);  destination(d_alluser); };
log { source(s_local); filter(f_spooler);  destination(d_spooler); };
log { source(s_local); filter(f_local7);  destination(d_local7); };
log { source(s_local); filter(f_loghost);  destination(d_loghost); };

これで、同じ・・・はずです。
※「local6.info @loghost」については、前回(syslog - 集約)の「転送元サーバ(192.168.0.50)の設定」をご覧ください。


次に、ログサーバの設定です。

ログサーバ(192.168.0.10)のsyslog-ngの設定

移行前:syslogdの設定(syslog.conf)

#kern.*                                                 /dev/console
# local6 は lighttpdのログを流すので /var/log/messagesに行かないようにします
*.info;mail.none;authpriv.none;cron.none;local6.none    /var/log/messages
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
cron.*                                                  /var/log/cron
*.emerg                                                 *
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log
# lighttpdのログは local6.info に固定します
local6.info             /var/log/siteA.lighttpd.access.log


/opt/syslog-ng/etc/syslog-ng.conf は以下のようになりました。

@version: 3.0

source s_local { file ("/proc/kmsg" program_override("kernel: ")); unix-stream("/dev/log"); internal(); udp(); };

#destination d_console  { pipe("/dev/console"); };
destination d_messages { file("/var/log/messages"); };
destination d_authlog  { file("/var/log/secure"); };
destination d_maillog  { file("/var/log/maillog");};
destination d_cron     { file("/var/cron/log"); };
destination d_alluser  { usertty("*"); };
destination d_spooler  { file("/var/log/spooler"); };
destination d_local7   { file("/var/log/boot.log");};
destination d_loghost  { file("/var/log/siteA.lighttpd.access.${YEAR}${MONTH}${DAY}.log");};

#filter f_console  { facility(kern) and level(debug..emerg)};
filter f_messages { level(info) and
                    not facility(mail) and
                    not facility(authpriv) and
                    not facility(cron) and
                    not facility(local6) and level(info);
                  };
filter f_authlog  { facility(authpriv) and level(debug..emerg); };
filter f_maillog  { facility(mail) and level(debug..emerg); };
filter f_cron     { facility(cron) and level(debug..emerg); };
filter f_alluser  { level(emerg); };
filter f_spooler  { facility(uucp) and level(crit) or
                    facility(news) and level(crit);
                  };
filter f_local7   { facility(local7) and level(debug..emerg); };
filter f_loghost  { facility(local6) and level(info); };

#log { source(s_local); filter(f_console);  destination(d_console); };
log { source(s_local); filter(f_messages); destination(d_messages); };
log { source(s_local); filter(f_authlog);  destination(d_authlog); };
log { source(s_local); filter(f_maillog);     destination(d_maillog); };
log { source(s_local); filter(f_cron);     destination(d_cron); };
log { source(s_local); filter(f_alluser);  destination(d_alluser); };
log { source(s_local); filter(f_spooler);  destination(d_spooler); };
log { source(s_local); filter(f_local7);  destination(d_local7); };
log { source(s_local); filter(f_loghost);  destination(d_loghost); };

ポイントは、source s_local の udp(); と、d_loghost の file です。
source s_local の udp(); で UDPポート(514)を待機しログメッセージを受信します。
d_loghost の file は受信したlighttpdのログを保存する場所です。
今回のログの保存ファイル名には ${YEAR}${MONTH}${DAY} というのが付いていて、これは日付を表すことができます。
つまり、日付が変わると自動的にログローテするということです。
これは、Macrosという変数のようなもので実現しています。

siteBからのログ受信も同じように設定すれば実現できます。

udpからtcpにしてログの精度を上げる

(サーバ/インフラを支える技術の「集約」とは少し趣旨がずれるのですが)
udpは高速ですが、データの保障がありません。そこで、udpではなくtcpを利用して、ログの精度を上げましょう。
といっても、設定は簡単でsyslog-ng.confのsource部分の udp(); となっているところを tcp(); などにすれば完了です。
デフォルトではtcpポート 514 が割り当てられます。

ログサーバで以下のようになっていれば大丈夫です。

$ netstat -an | grep -i tcp | grep 514
tcp        0      0 0.0.0.0:514                 0.0.0.0:*                   LISTEN      

また、転送元サーバの syslog-ng.conf も修正します。

destination d_loghost  { udp("loghost");};
↓
destination d_loghost  { tcp("loghost");};

これで、syslog-ng を再起動すれば完了です。ログがtcp通信で送られてきます。



次回はログの収集について行います。