LoginSignup
34
35

More than 5 years have passed since last update.

fluentdでアクセスログを集約してページ毎のレスポンスタイムをグラフ化するまで

Last updated at Posted at 2013-07-18

想定サーバー

  1. apl(収集サーバー)
  2. fluentdを起動した集約サーバー
  3. ログの保存やグラフを表示するサーバー(Growthforecast使用)

流れ

aplサーバーからアクセスログをfluent-agent-liteで収集、fluentdを起動しているサーバーに受け渡してログのパース、情報の整形、グラフ表示サーバーに送信

収集サーバー

cd
git clone git@github.com:tagomoris/fluent-agent-lite.git
sudo mv ~/fluent-agent-lite /usr/local/src/
cd /usr/local/src/fluent-agent-lite
sudo ./bin/install.sh

sudo vi /etc/fluent-agent-lite.conf
TAG_PREFIX="apache"
LOGS=$(cat <<"EOF"
access /var/log/httpd/access_log
EOF
)
PRIMARY_SERVER="<集約サーバーip>:24224"
sudo service fluent-agent-lite restart

# 正常に起動していれば出力が無い
tail /tmp/fluent-agent.log

集約サーバー

sudo vi /etc/yum.repos.d/td.repo
[treasuredata]
name=TreasureData
baseurl=http://packages.treasure-data.com/redhat/$basearch
gpgcheck=0
sudo yum install td-agent

sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-file-alternative fluent-plugin-parser fluent-plugin-forest fluent-plugin-amplifier-filter fluent-plugin-numeric-monitor fluent-plugin-growthforecast fluent-plugin-rewrite

sudo chgrp td-agent /var/log/httpd/
sudo chmod g+rx /var/log/httpd/

sudo mv /etc/td-agent/td-agent.conf /etc/td-agent/td-agent.conf.orig
sudo vi /etc/td-agent/td-agent.conf
<source>
  type forward
</source>

# accessログをパース
<match apache.access.** >
    type parser
    remove_prefix apache.access
    add_prefix parsed
    format /^(?<host>[^ ]*) [^ ]+ [^ ]+ \[(?<log_time>.+)\] "(?<method>[^ ]+) (?<path>[^ ]+) (?<protocol>[^"]+)+" (?<status>[^ ]+) (?<body_bytes_sent>[^ ]+) "(?<referer>[^"]+)" "(?<user_agent>[^"]+)" (?<response_time>[^ ]+) "(?<x_forwarded_for>[^"]+)"/
    time_format %d/%b/%Y:%H:%M:%S %z
    key_name message
</match>

# 不要ログの削除
<match parsed.** >
  type rewrite

  remove_prefix parsed
  add_prefix filtered

  <rule>
    key     path
    pattern ^\/(?:images|js|img|favicon)
    ignore  true
  </rule>
  <rule>
    key     response_time
    pattern ^$
    ignore  true
  </rule>
  <rule>
    key     status
    pattern ^(?!200)\d+$
    ignore  true
  </rule>
  <rule>
    key     method
    pattern ^(?!GET).+$
    ignore  true
  </rule>
  # アクセスのパスからグループ分ける
  <rule>
    key     path
    pattern ^/([a-zA-Z]+).+$
    append_to_tag true
    fallback other
  </rule>
</match>

# レスポンスタイムをミリ秒に
<match filtered.**>
  type amplifier_filter
  remove_prefix filtered
  add_prefix amplified
  ratio      1000
  key_names  response_time
</match>

# レスポンスタイムを集計、平均値等の算出
<match amplified.** >
  type numeric_monitor
  #unit minute
  monitor_key response_time

  input_tag_remove_prefix amplified
  tag apache.response
  aggregate all
  percentiles 90,95
</match>

# grh化サーバーに送信
<match apache.response.**>
  type copy
  <store>
    type      growthforecast
    remove_prefix response_time
    gfapi_url http://<growthforecast>:5125/api/
    service   cat
    tag_for   section
    name_key_pattern .*_(num|avg|max|min|percentile_\d+)
  </store>

  <store>
    type file
    path /var/log/td-agent/growthforecast.log
  </store>
</match>
sudo chkconfig td-agent on
sudo service td-agent restart

# 確認
tail -f /var/log/td-agent/parserd-httpd-access*

グラフ化サーバー

growthforecastユーザーを作成してwebアクセス出来るように
apacheでホストしていない

sudo yum install gcc pkgconfig glib2-devel gettext libxml2-devel pango-devel cairo-devel
sudo yum install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts

sudo useradd growthforecast

sudo su - growthforecast

curl -kL http://install.perlbrew.pl | bash
echo '[[ -s "$HOME/perl5/perlbrew/etc/bashrc" ]] && source "$HOME/perl5/perlbrew/etc/bashrc"' >> .bash_profile
source $HOME/perl5/perlbrew/etc/bashrc

perlbrew install perl-5.16.2
# 長い
# install中のログ出力 sudo tail -f /home/growthforecast/perl5/perlbrew/build.perl-5.16.2.log
perlbrew switch perl-5.16.2
perl -v

perlbrew install-cpanm
cpanm -n GrowthForecast
mkdir data

exit

wget -nd -O - https://github.com/hiro-su/gf-sample/archive/master.tar.gz|tar zxvf -

cd gf-sample-master
vi growthforecast
perl $HOME/GrowthForecast/growthforecast.pl --port=5125 --host 0.0.0.0 --front-proxy 0.0.0.0 \

⬇

growthforecast.pl --data-dir=/home/growthforecast/data --port=5125 --host 0.0.0.0 --front-proxy 0.0.0.0 \
sudo cp -rp growthforecast /etc/init.d
chmod +x /etc/init.d/growthforecast
sudo mkdir /var/run/growthforecast
sudo mkdir /var/log/growthforecast

sudo service growthforecast start

# 起動しているか確認
curl http://localhost:5125

ブラウザアクセス
http://<hostname>:5125

# データの登録が出来るか確認
curl -F number=10 http://localhost:5125/api/socialgame/member/register

グループ化しているのをアクセスしてくるパスで分けているんだけど、グループ分けがイミワカランパスのものも混ざってしまっているので、もう少し工夫必要。phpからapacheログに表示させる関数使ったりとか。

後からchefのクックブックに書き換えたんだけど、chefのserviceでstartするとfluent-agent-liteが一度は起動するんだけど謎の異常終了を起こすので結局手動で起動する事に。何か原因知ってる人いないですかね。

以上。

34
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
35