プログラマ 福重 伸太朗 〜基本へ帰ろう〜 このページをアンテナに追加 RSSフィード

2009-05-20

syslog-ngでrailsのログを集約する方法について

Sysloggerのインストール

Sysloggerをインストールする。

sudo gem install SyslogLogger

rails側の設定

config/environments/production.rb

以下を追記する。

require 'syslog_logger'
RAILS_DEFAULT_LOGGER = SyslogLogger.new "appname"
config/environment.rb

以下を追記する。

RAILS_DEFAULT_LOGGER ||= Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")

syslog-ngの設定

/opt/syslog-ng/etc/syslog-ng.conf

以下を追記する。

destination d_rails_local { file("/var/log/appname.rails.production.${YEAR}${MONTH}${DAY}.log"); };
filter f_rails_log  { program("appname.*"); };
log { source(s_local); filter(f_rails_log);  destination(d_rails_local); };

参考

404 Not Found

Railsアプリケーションのログをsyslogに記録する方法 - Hello, world! - s21g

環境

$ ruby -v
ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-linux]
$ rails -v
Rails 1.0.0
$ cat /etc/redhat-release
CentOS release 5 (Final)

SyslogLogger (1.4.0)
    SyslogLogger is a Logger replacement that logs to syslog.  It is
    almost drop-in with a few caveats.

syslog-ngのプロセスがずっとCPU使用率99.9%になった・・・

syslog-ngのプロセスCPU使用率99.9%になったsyslog-ng.conf は以下の通りです。

source s_local { pipe ("/proc/kmsg" log_prefix("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); };

syslog-ngを起動すると、2つある syslog-ng の1つが 99.9% CPUをずっと占領しています・・・。これは明らかにおかしいです・・・。

f:id:japanrock_pg:20090520214959p:image


原因

syslog-ng.conf の以下の部分の設定が良くありませんでした。

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

正しくは、以下です。

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

ただしくは、pipe が file になります。これで、CPUの使用率は 0% となり正常になりました。


f:id:japanrock_pg:20090520215000p:image

起動時のエラーメッセージを読む

syslog-ng 起動時に以下のようなメッセージがでる

f:id:japanrock_pg:20090520215001p:image


Starting syslog-ng:  Starting syslog-ng: Your configuration file uses an obsolet
ed keyword, please update your configuration; keyword='log_prefix', change='prog
ram_override'
Error opening pipe, underlying file is not a FIFO, it should be used by file();
filename='/proc/kmsg'
OK
                                                           [  OK  ]

ここに原因が書いてありました・・・。

4〜5行目の

Error opening pipe, underlying file is not a FIFO, it should be used by file();
filename='/proc/kmsg'

に、「Error opening pipe, filename='/proc/kmsg' は file(); を使用すべきです! 」と書いてあります。

また、1〜3行目については、log_prefix が program_override という記述に変わったようです。



上記を修正して、起動すると、

$ sudo /etc/init.d/syslog-ng restart
Restarting syslog-ng: Stopping syslog-ng: OK
Starting syslog-ng: OK

きれいに起動しました。CPUの使用率も問題ありません。エラーメッセージはちゃんと読まないとですね・・・。



pipeについて

Log Management & Network Security Solutions | BalaBit IT Security

Log Management & Network Security Solutions | BalaBit IT Security

gem install rails -v 1.1.6 で ERROR

バージョンが古い Railsの環境構築しようとしたら・・・

$ sudo gem install -y rails -v 1.1.6
Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR:  While executing gem ... (Gem::GemNotFoundException)
    Could not find rails (= 1.1.6) in any repository

なぜかできない・・。


gem install rails - Ruby Forum

↑ここをみる。


$ sudo gem install -y rails -v=1.1.6

これでいけた。