:>/dev/null

ラガードエンジニアの不撓不屈の精神/unlearning/go beyond

Amazon Kinesis Client Library (KCL)を使用したログ収集基盤構成

Amazon Kinesis Client Library (KCL)を使用したログファイル収集基盤構成を構築する必要があり、調査を行った時の勉強メモ。

  • 以下構築要件

    • apacheaccess_logを集約サーバへ収集する
    • 集約サーバでAmazon Kinesis Client Library (KCL) ライブラリを使用して送信←今回、構築部分
    • KCL → aws kinesis stream へ送信
    • aws kinesis stream → s3/aes等へ送信
      • バックアップ/可視化など
  • KCLの設定

[ec2-user@ip-172-xx-xx-xx aws-kinesis]$ cat agent.json
{
  "awsAccessKeyId": "******",
  "awsSecretAccessKey": "******",
  "cloudwatch.endpoint": "monitoring.ap-northeast-1.amazonaws.com",
  "cloudwatch.emitMetrics": true,
  "kinesis.endpoint": "",
  "firehose.endpoint": "firehose.ap-northeast-1.amazonaws.com",

  "flows": [
    {
      "filePattern": "/tmp/test.log",
      "deliveryStream": "teststream",
      "dataProcessingOptions": [
         {
             "optionName": "LOGTOJSON",
             "logFormat": "COMMONAPACHELOG",
             "matchPattern": "^([\\d.]+) - - \\[([\\w:/]+).*\\] \"(\\w+) /(\\w+).*(\\d.+)\" (\\d{3}) (\\d+) \"(\\S+)\" \"(.*?)\" ([\\d.]+) \"(.*?)\" (\\d+);(\\d+)",
             "customFieldNames": ["client_ip", "time_stamp", "verb", "uri_path", "http_ver", "http_status", "bytes", "referrer", "agent", "response_time", "graphql", "company_id", "user_id"]
         }
      ]
    }
  ]
}
[ec2-user@ip-172-xx-xx-xx ~]$ cat test.sh
#!/bin/bash

start=$(date)
for i in $(seq 1 ${1})
do
    echo ${i}
    echo -e "$( echo $(cat sample.json))" >> /tmp/test.log
    sleep 0.01s
done
echo ${start}
date
  • サンプルデータのフォーマット確認
[ec2-user@ip-172-xx-xx-xx ~]$ cat sample.json
111.111.111.111 - - [02/Dec/2016:13:58:47 +0000] "POST /graphql HTTP/1.1" 200 1161 "https://www.myurl.com/endpoint/12345" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" 0.172 "query user userMessages hasPermissions F0 F1" 11111;222222

KCL ライブラリを使用したログ収集基盤構成を構築した。
収集時、アグリゲーターでtd-agentを使用した環境と比較し、安易に送信及び構築が可能な為、今後は本環境を使用した基盤構成を行うケースが増えそうです。