跳到主要內容

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的結果喔!



留言

這個網誌中的熱門文章

透過Google Apps Script結合Google Form做即時郵件通知

體驗過Google Apps Script的功能後,也發現他結合GmailApps的模組 GmailApps的應用可以用在表單填寫完成後,做發信的通知 例如您開立了一個訂購的表單,為了要在第一時間通知商家有訂單進入 就可以直接呼叫Gmail做發信的通知,讓手持Smart Phone的我們可以很快的知道生意上門了! 下面規劃三個function,其中: onCommit():為form commit時候觸發的function,需要掛載於form commit trigger上 jsonArrToTable():目的將json array解析成為一個Table getLastRowTable():目的將整個table的回傳過濾為剩下第一筆(表頭,含有Form的欄位說明)與最後一筆(原則上就是剛剛送出的那一筆表單) 完整程式碼如下: function onCommit(){   var sheet = SpreadsheetApp.getActiveSheet();   var rows = sheet.getDataRange();   var numRows = rows.getNumRows();   var values = rows.getValues();   var content = getLastRowTable(values);   var htmlBody = "Hi Admin: <br/><br/>有訂單拉,檢查一下吧! <br/><br/>" + content + '<br/><br/>Send by Google Apps';   GmailApp.sendEmail(     " your-email-address@gmail.com ",      "Order Confirm Notice",      htmlBody,      {from: ' from-email-...

Share a chrome plugin for manage google cloud platform

好玩意兒報報.... 同事的新作,把Google Project List在Chrome Plugin中! 對一次管理多個專案的人來說,真得超方便的拉! 下載: https://chrome.google.com/webstore/detail/gdclauncher/bicgkglnnilldakpenngnblekooejnpg 使用說明: 1. Use browser url bar to quick search: Type "gdcl" in browser Press "TAB" to start search Type the project id key word then select the search result... 2. Using quick launch bar... You can search by keyword or click project name to go to the project or gae link to go to gae or go to billing page....

Cloud Monitor嚐鮮

GCP上,我們非常想要的一個功能,終於問世.... Cloud Monitor來了!