Pages

2011/07/28

syslog-ngでsyslogをmongodbに直行させる

少々前に似たような記事、logger代わりにmongoimportを上げた。

が、syslogデーモンにsyslog-ngを使った場合、出力先を指定プログラムのSTDINにできるので直接mongodbに突っ込めるのだった。
 



syslogメッセージをタブ区切りに加工、 まずはシンプルにfacility、priorityと本文くらいで。
template t_col { template("$$FACILITY\t$PRIORITY\t$MSGONLY\n"); template_escape(no); };

 

ログ出力先の定義に mongoimport の場所とオプションを指定。
templateに指定した内容はSTDINに入ってくるので、パースされてmongodbに突っ込まれます。
destination d_mongo { program("/usr/bin/mongoimport -d syslogdb -c log -type tsv -f FACILITY,PRIORITY,MSG" template(t_col)); };

 

そして出力へ
log { source(s_src); destination(d_mongo); };

 

確認してみるとちゃんと入っている。
{ "_id" : ObjectId("4e2fc08acd9edcbb16482134"), "FACILITY" : "syslog", "PRIORITY" : "notice", "MSG" : "Configuration reload request received, reloading configuration;" }
{ "_id" : ObjectId("4e2fc100cd9edcbb16482135"), "FACILITY" : "user", "PRIORITY" : "notice", "MSG" : "testlog" }

 

もちろんmongo側は capped.collection にしておこうな。
 
 

今回Varnishのログに対する考え方が参考になったので張っておく。
https://www.varnish-cache.org/docs/3.0/tutorial/logging.html

One of the really nice features in Varnish is how logging works. Instead of logging to normal log file Varnish logs to a shared memory segment. When the end of the segment is reached we start over, overwriting old data. This is much, much faster then logging to a file and it doesn’t require disk space

 

適材適所でこういうログへのアプローチもしていこう。
 

0 件のコメント:

コメントを投稿