td-agent3(Fluentd v1.0)에서 BigQuery에 로그를 저장합니다. 2018-03-07

내용



htps : // 기주 b. 코 m / 카이젠 p ぁ t ぉ rm / f 을 참고로 td-agent3 ( Fluentd v1.0 )에서 BigQuery 에 로그를 저장하는 설정을 합니다.

준비


  • td-agent3 ( Fluentd v1.0 ) 설치가 완료된 것으로 합니다.
  • 참고: Ubuntu 16.04.3 LTS에 td-agent3(Fluentd v1.0) 설치

  • GCP, BigQuery에서 준비 작업이 필요하지만 할애합니다

  • fluent-plugin-bigquery 설치


  • fluent-plugin-bigquery 라는 플러그인이 필요하므로 설치합니다.
  • 설치에 사용되는 명령은 /opt/td-agent/embedded/bin/fluent-gem에 있습니다.

  • 설치


    $ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-bigquery
    ... 表示が多いので詳細は割愛
    Done installing documentation for uber, declarative, declarative-option, representable, retriable, mime-types-data, mime-types, little-plugger, logging, jwt, memoist, os, signet, googleauth, google-api-client, fluent-plugin-bigquery after 159 seconds
    16 gems installed
    

    조금 시간이 걸립니다.

    확인


    $ /opt/td-agent/embedded/bin/fluent-gem list fluent-plugin-bigquery
    
    *** LOCAL GEMS ***
    
    fluent-plugin-bigquery (1.2.0)
    

    [email protected] 설치(2018-03-07 시점)


  • @type bigquery_insert
  • @type bigquery_load

  • 를 이용하고 싶습니다만, 사용할 수 있는 것은 v2.0.0부터와 같습니다.
    위의 단계로 설치하면 v1.2.0이므로 한 번에 v2.0.0을 설치합니다.

    추가



    htps : / / 루 바이 게 ms. rg / 게 ms / f ぅ 엔 tp ぅ 긴 - 비 g 쿠 ry / ぇ r 시온 s / 2.0.0. 베타
    젬에는 2.0.0.beta가 있습니다. 여기를 설치하면 다음 GitHub에서 설치할 필요가 없습니다.

    GitHub에서 설치할 준비


    $ sudo /opt/td-agent/embedded/bin/fluent-gem install --no-document specific_install
    Fetching: specific_install-0.3.3.gem (100%)
    Successfully installed specific_install-0.3.3
    1 gem installe
    

    GitHub에서 최신 fluent-plugin-bigquery 설치


    $ sudo /opt/td-agent/embedded/bin/fluent-gem specific_install https://github.com/kaizenplatform/fluent-plugin-bigquery.git
    /usr/bin/git
    git installing from https://github.com/kaizenplatform/fluent-plugin-bigquery.git
    Cloning into '/tmp/d20180307-1588-1vvfhc6'...
    remote: Counting objects: 2133, done.
    remote: Compressing objects: 100% (60/60), done.
    remote: Total 2133 (delta 60), reused 94 (delta 39), pack-reused 2008
    Receiving objects: 100% (2133/2133), 461.14 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (799/799), done.
    Checking connectivity... done.
    WARNING:  open-ended dependency on rake (>= 0, development) is not recommended
      if rake is semantically versioned, use:
        add_development_dependency 'rake', '~> 0'
    WARNING:  open-ended dependency on rr (>= 0, development) is not recommended
      if rr is semantically versioned, use:
        add_development_dependency 'rr', '~> 0'
    WARNING:  open-ended dependency on test-unit (>= 0, development) is not recommended
      if test-unit is semantically versioned, use:
        add_development_dependency 'test-unit', '~> 0'
    WARNING:  open-ended dependency on test-unit-rr (>= 0, development) is not recommended
      if test-unit-rr is semantically versioned, use:
        add_development_dependency 'test-unit-rr', '~> 0'
    WARNING:  open-ended dependency on google-api-client (>= 0.11.0) is not recommended
      if google-api-client is semantically versioned, use:
        add_runtime_dependency 'google-api-client', '~> 0.11', '>= 0.11.0'
    WARNING:  open-ended dependency on googleauth (>= 0.5.0) is not recommended
      if googleauth is semantically versioned, use:
        add_runtime_dependency 'googleauth', '~> 0.5', '>= 0.5.0'
    WARNING:  open-ended dependency on multi_json (>= 0) is not recommended
      if multi_json is semantically versioned, use:
        add_runtime_dependency 'multi_json', '~> 0'
    WARNING:  See http://guides.rubygems.org/specification-reference/ for help
      Successfully built RubyGem
      Name: fluent-plugin-bigquery
      Version: 2.0.0.beta
      File: fluent-plugin-bigquery-2.0.0.beta.gem
    Successfully installe
    

    확인


    $ /opt/td-agent/embedded/bin/fluent-gem list fluent-plugin-bigquery
    
    *** LOCAL GEMS ***
    
    fluent-plugin-bigquery (2.0.0.beta, 1.2.0)
    

    설정 예(bigquery_insert)


  • Apache 액세스 로그를 BigQuery로 보내는 예입니다.
  • auth_methodjson_key 를 사용합니다

  • /etc/td-agent/td-agent.conf
    # <source> についての詳細は割愛
    <source>
      @type tail
      path /var/log/apache2/access.log
      tag apache.access
      pos_file /var/log/td-agent/apache-access-log.pos
      format apache2
      keep_time_key true
    </source>
    
    # <match> の内容が本題
    <match apache.access>
      @type bigquery_insert
    
      auth_method json_key
      json_key /tmp/json_key.json
    
      project PROJECT_NAME
      dataset DATASET_NAME
    
      auto_create_table true
      table apache_access_%Y%m%d
    
      <buffer time>
        @type file
        path /var/log/td-agent/bigquery
        timekey 1d
      </buffer>
    
      <inject>
        time_key time
        time_type string
        time_format %Y-%m-%d %H:%M:%S
      </inject>
    
      schema [
        {"name": "time", "type": "TIMESTAMP"},
        {"name": "host", "type": "STRING"},
        {"name": "user", "type": "STRING"},
        {"name": "method", "type": "STRING"},
        {"name": "path", "type": "STRING"},
        {"name": "code", "type": "INTEGER"},
        {"name": "size", "type": "INTEGER"},
        {"name": "referer", "type": "STRING"},
        {"name": "agent", "type": "STRING"}
      ]
    </match>
    

    동작 확인



    위의 설정 예에서 td-agent를 시작하여 로그 (/var/log/td-agent/td-agent.log)를 확인하십시오.
    특히 문제가 없는 것 같으면
    $ while true; do curl localhost > /dev/null && sleep 1; done
    

    등으로 액세스 로그를 출력하여 BigQuery에 로그가 쌓여 있는지 확인합니다.

    BigQuery 상태 확인


  • 날짜가 지정된 테이블이 자동으로 생성되어 거기에 삽입되었습니다.

  • BigQuery에 쌓인 로그
  • #standardSQL
    SELECT
      *
    FROM
      DATASET_NAME.apache_access_20180307
    ORDER BY
      time DESC
    



    설정 예(bigquery_load)


  • Apache 액세스 로그를 BigQuery로 보내는 예입니다.
  • auth_methodjson_key 를 사용합니다
  • flush_XXX 의 defalut 값을 참고로 하면서 설정해, 어느 간격으로 정기적으로 보내도록(듯이) 합니다
  • htps : // 기주 b. 코 m / 카이젠 p ぁ t ぉ rm / f ぅ 엔 t ぅ 긴 - 비 g 쿠 ry # 뵈후 ぇ 루 세 c 치온

  • 상한이 있으므로 조심합시다.
  • h tps : // c ぉ d. 오, ぇ. 코 m/비g 쿠에 ry/쿠오타 s? hl = 그럼


  • /etc/td-agent/td-agent.conf
    # <source> についての詳細は割愛
    <source>
      @type tail
      path /var/log/apache2/access.log
      tag apache.access
      pos_file /var/log/td-agent/apache-access-log.pos
      format apache2
      keep_time_key true
    </source>
    
    # <match> の内容が本題
    <match apache.access>
      @type bigquery_load
    
      auth_method json_key
      json_key /tmp/json_key.json
    
      project PROJECT_NAME
      dataset DATASET_NAME
    
      auto_create_table true
      table apache_access_%Y%m%d
    
      <buffer time>
        @type file
        path /var/log/td-agent/bigquery
        timekey 1d
    
        flush_at_shutdown true
        flush_interval 25
      </buffer>
    
      <inject>
        time_key time
        time_type string
        time_format %Y-%m-%d %H:%M:%S
      </inject>
    
      schema [
        {"name": "time", "type": "TIMESTAMP"},
        {"name": "host", "type": "STRING"},
        {"name": "user", "type": "STRING"},
        {"name": "method", "type": "STRING"},
        {"name": "path", "type": "STRING"},
        {"name": "code", "type": "INTEGER"},
        {"name": "size", "type": "INTEGER"},
        {"name": "referer", "type": "STRING"},
        {"name": "agent", "type": "STRING"}
      ]
    </match>
    

    동작 확인


    bigquery_insert 때처럼

    BigQuery 상태 확인


  • 기본적으로 bigquery_insert 때와 다르지 않으므로 자세한 내용은 할애
  • flush_XXX 간격으로 어느 정도 쌓여서 보내졌습니다

  • 요약


    td-agent3 ( Fluentd v1.0 )에서 BigQuery 에 로그를 저장할 수 있습니다.

    기타



    오류가 발생했습니다.


    [error]: #0 Permission denied @ rb_sysopen - /var/log/apache2/access.log
    

    같은 오류가 발생하면 chmod합니다.
    $ sudo chmod -R 755 /var/log/apache2/
    

    오류가 발생한 2


    fluent-plugin-bigquery 또한 오류가 발생했지만 베타 버전이므로 신경 쓰지 않기로 결정했습니다.
    오류가 나왔지만 똑같이 움직였다.

    좋은 웹페이지 즐겨찾기