ログ集計に何使うか迷ったんだけど、fluentdにfluent-plugin-groongaってのがあったのでfluentdを使ってgroongaにログを集計していくとこまでをやった。rbenvから色々やろうとしたら面倒だった。

rbenvを使う

バージョン管理が面倒なので、やっぱrbenv使わないといかんっしょと思ってやった。Ansible と Vagrant を使って Rails 開発環境(Ubuntu + rbenv + MySQL5.6 + node.js)を構築する - Qiitaを参考にして変数を適当にしたりsudoのとこを変更したりして出来た

---
- hosts: all
  user: hoge
  tasks:
    - name: Install dependencies for rbenv
      sudo: yes
      apt: name=\{\{ item \}\} state=latest
      with_items:
        - git
    - name: Install rbenv
      git: repo=https://github.com/sstephenson/rbenv.git dest=~/.rbenv
    - name: Add ~.rbenv/bin to PATH
      lineinfile: >
        dest="~/.bashrc"
        line="export PATH=$HOME/.rbenv/bin:$PATH"
    - name: Eval rbenv init in ~/.bashrc
      lineinfile: >
        dest="~/.bashrc"
        line='eval "$(rbenv init -)"'
    - name: Install dependencies for ruby-build (see. https://github.com/sstephenson/ruby-build/wiki)
      apt: name=\{\{ item \}\} state=latest
      with_items:
        - autoconf
        - bison
        - build-essential
        - libssl-dev
        - libyaml-dev
        - libreadline6-dev
        - zlib1g-dev
        - libncurses5-dev
        - libffi-dev
        - libgdbm3
        - libgdbm-dev
    - name: Install ruby-build as rbenv plugin
      git: repo=https://github.com/sstephenson/ruby-build.git dest=~/.rbenv/plugins/ruby-build
    - name: Check if version is installed ruby
      shell: "~/.rbenv/bin/rbenv versions | grep \{\{ rbenv_ruby_version \}\}"
      register: rbenv_check_install
      changed_when: False
      ignore_errors: yes
    - name: Install ruby
      command: "~/.rbenv/bin/rbenv install \{\{ rbenv_ruby_version \}\}"
      when: rbenv_check_install|failed
    - name: Check if version is the default ruby version
      shell: "~/.rbenv/bin/rbenv version | grep \{\{ rbenv_ruby_version \}\}"
      register: rbenv_check_default
      changed_when: False
      ignore_errors: yes
    - name: Set default ruby version
      command: "~/.rbenv/bin/rbenv global \{\{ rbenv_ruby_version \}\}"
      when: rbenv_check_default|failed

rbenv_ruby_versionはインベントリファイルで設定しておく。

shellのところrbenvだけにしたら何回やってもエラーで止まってしまった。なので結局rbenvはフルパス指定にした。bashrcに書いた分その後色々やった時に書いたので.profileでも良いかも知れない。

fluentdのインストール

こっから面倒だった。

    - name: Gem install fluentd
      gem: name=\{\{ item \}\}
       executable=~/.rbenv/versions/\{\{ rbenv_ruby_version \}\}/bin/gem user_install=no
      with_items:
        - fluentd
        - fluent-plugin-groonga

たったこれだけ書くのに2時間くらい掛かったorz。fluentdをインストールする前にtd-agentから何とかならないか試したり、gemがなかなかrbenvでインストールしたバージョンになってくれなかったり、fluentdが変な場所にインストールされたり・・・でも結論としてはこれでおk。フルパス指定とuserinstall=noがポイントかな。

fluentdの設定

設定やったこと無かったので、ここが手探りになるのは仕方ない。includeのパスを間違えてて設定を読み込めないとか色々しょーもないハマりをした。ログが正しく送られてるかどうかはsourceのタグをdebug.accessに変えて、デバッグ表示させてtailがちゃんとされているか確認→実際にlog.accessでやってみるという手順で行った

- name: setup fluentd dir
      command: ~/.rbenv/versions/\{\{ rbenv_ruby_version \}\}/bin/fluentd --setup /path/to/fluent/config/file
    - name: set fluent.conf
      lineinfile: dest=/path/to/fluent/config/file/fluent.conf insertafter=EOF line="@include conf.d/*" state=present
    - name: create conf.d folder
      file: path=/path/to/fluent/config/file/conf.d state=directory
    - name: copy groonga.fluentd.conf
      copy: src=groonga.fluentd.conf dest=~/path/to/fluent/config/file/conf.d
<source>
    type tail
    format ltsv
    tag log.access
    path /path/to/log/file
    pos_file /path/to/pos/file
</source>
<match log.*>
    type groonga
    store_table logs
    host xxx.xxx.xxx.xx
    port xxxxx
</match>

としてIPがxxx.xxx.xxx.xxでポートがxxxxxのところにgroongaを用意すると勝手にフィールドとか色々作ってログを保存してくれるようになる。ちなみにfluentdの開始コマンドは

$ fluentd -c /path/to/fluent/config/file -vv &

とした。

とりあえずここまでで十分ハマった。

td-agentは使わない

使用OSがubuntuなので、td-agentを使ってみたんだが、rbenv上のgemでインストールしたfluent-plugin-groongaを読み込む方法が最後まで分かんなかったので使わないことにした。td-agentだとserveceコマンドでリスタートとかやってくれるので楽なんだけどなー。

参考: