Fluentd exec output測試

由於Google Cloud Logging是透過fluentd實作,加上fluentd介接BigQuery也非常簡單...
有這個機會跟fluentd多親近一下... :D

在玩了fluentd一陣子後,雖然他提供了不少的input, output, filter plugin,但是總是有一些無法滿足的地方
加上fluentd是ruby friendly的系統...@@,沒學過ruby總是難以在上面發揮...@@

看了一陣子,終於下手測試output exec plugin...  期待可以藉由他來用第三方程式語言去介接其他output plugin所未提供的服務...
下面是一段將syslog source輸入,然後output到node.js程式的範例config

<source>
  type syslog
  protocol_type udp
  port 514
  bind 0.0.0.0
  tag system
</source>
<source>
  type syslog
  protocol_type tcp
  port 514
  bind 0.0.0.0
  tag system
</source>

<match **>
  type copy
  <store>
    type stdout
    output_type json
  </store>
  <store>
    type exec
    command /opt/node/bin/node /home/simonsu/data/test.js
    format json
    time_format %Y-%m-%d %H:%M:%S
    flush_interval 5s
    buffer_path    /tmp
  </store>
</match>


然後其中"/home/simonsu/data/test.js"則是我們預計要執行的node.js程式
在exec中,以node.js會在指令列接收到類似:/tmp.20150611.q5183cc42694434d7.log的字串
該字串是buffer中未flush的資料檔,以上面設定而言,資料會以json的方式收錄
內容長得像這樣:

{"host":"simon-centos6","ident":"run-parts","message":"(/etc/cron.hourly)[2050 starting 0anacron"}
{"host":"simon-centos6","ident":"anacron","pid":"2061","message":"Anacron started on 2015-06-11"}
{"host":"simon-centos6","ident":"run-parts","message":"(/etc/cron.hourly)[2063 finished 0anacron"}
{"host":"simon-centos6","ident":"anacron","pid":"2061","message":"Will run job `cron.daily' in 17 min."}
{"host":"simon-centos6","ident":"anacron","pid":"2061","message":"Will run job `cron.weekly' in 37 min."}
{"host":"simon-centos6","ident":"anacron","pid":"2061","message":"Will run job `cron.monthly' in 57 min."}
{"host":"simon-centos6","ident":"anacron","pid":"2061","message":"Jobs will be executed sequentially"}

先不討論關於接收到檔案後要做的事情,我們可以直接在test.js中透過fs把該檔案內容讀出來:

var fs = require('fs');
var txt = fs.readFileSync(process.argv[2],'utf8');
//ex: console.log('[OUT]', txt);

後續可以針對txt來做後續的處理動作...
附註:如果處理的當下有exception,fluentd會將內容暫存在buffer_path內(也就是前面所接到的參數)

接下來啟動測試,透過fluentd或td-agent執行(ex: td-agent -c xxx.conf)後,如果node.js程式當中有做console.log,則在console output內可以看到console.log的結果喔!