LoginSignup
2
0

More than 3 years have passed since last update.

Zabbixとfluentdでログからアクセス数と平均レスポンスタイムを取得

Last updated at Posted at 2020-03-15

背景

Zabbix単体でログからレスポンスタイムを計測をしたかったのですが、以下で試したところ問題があったためfluentdと連携して実装することにしました。
https://qiita.com/_zaiko/items/c941594483364a8dc6a8

概要

Zabbixとfluentdを使用してログから以下を取得する。

  • 平均応答時間
  • 最大応答時間
  • アクセス回数

fluentdからZabbixに直接データ登録する方法とzabbix_senderコマンドで登録する元ファイルを作る2パターンの方法で実装する。集計されたデータはログに記載されている時間ではなく、fluentdがデータ取得したタイミングをもとに集計する。
chart2 (1).png

構築環境

centos 7
Zabbix 4.2
fluentd 1.9.2

構築方法

0. Apacheでレスポンスタイムを表示

以下と同じ実装。
https://qiita.com/_zaiko/items/c941594483364a8dc6a8#0-apache%E3%81%A7%E3%83%AC%E3%82%B9%E3%83%9D%E3%83%B3%E3%82%B9%E3%82%BF%E3%82%A4%E3%83%A0%E3%82%92%E8%A1%A8%E7%A4%BA

1. Zabbixの設定

以下のアイテムを作成する。

アクセス数

・名前: ApacheResponseTime_COUNT (任意)
・タイプ: Zabbixトラッパー
・キー: apache.response.num
・データ型: 数値(整数)
・単位: 回

最大値

- 名前:  ApacheResponseTime_MAX (任意)
- タイプ: Zabbixトラッパー
- キー: apache.response.max
- データ型: 数値(浮動小数点)
- 保存前処理: 乗数、0.000001

平均値

- 名前: ApacheResponseTime_AVG (任意)
- タイプ: Zabbixトラッパー
- キー: apache.response.avg
- データ型: 数値(浮動小数点)
- 保存前処理: 乗数、0.000001

2.fluentdの構築

インストール

fluentdのバージョンは 1.9.2 を使った。古いもので試したところ内蔵されているrubyのバージョンが古くてプラグインが動作しなかった。

## インストールとsystemd設定
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
systemctl enable td-agent
systemctl start td-agent

## 稼働確認
echo '{"setup test":"hello"}' | /opt/td-agent/embedded/bin/fluent-cat debug.test

## td-agent.log に以下出力があればOK
tail /var/log/td-agent/td-agent.log
2020-03-15 14:42:24.983374117 +0900 debug.test: {"setup test":"hello"}

## プラグインの追加
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-numeric-monitor fluent-plugin-zabbix
 systemctl restart td-agent

## 一時ファイル出力用ディレクトリ作成
mkdir -m 755 /var/log/fluent

設定

/etc/td-agent/td-agent.conf に以下を追記する。

## ログからデータを抽出する設定
<source>
  @type tail
  path /var/log/httpd/access_log,/var/log/httpd/access_log-%Y%m%d
  read_from_head true
  tag apache.access
  pos_file /var/log/fluent/access_log.pos
# 時間のフォーマット。日付と時間を_でつないでいたり一般的でないかもしれないので必要に応じて修正してください。
  time_format %Y/%m/%d_%H:%M:%S %z
# データとして、timeとresponse_timeを抽出する。
  format /^time:(?<time>[^\t]*).*response_time:(?<response_time>[0-9]*)$/
</source>

## 抽出したデータを集計する
<match apache.access>
  @type numeric_monitor
  @label @monitor_result
  tag monitor.duration
# 1分単位で集計
  unit minute
  aggregate all
  monitor_key response_time
</match>

## データをZabbixに登録する
<label @monitor_result>
  <match monitor.duration>
  @type zabbix
# Zabbix サーバのIP
  zabbix_server 127.0.0.1
# ホスト名。Zabbix上でアイテムを作成したホストと同じである必要がある。
  host ${hostname}
# add_key_prefix + name_keys がZabbix上で作成したアイテムのキーとなるようにする。apache.response.avgなど。
  add_key_prefix apache.response
  name_keys avg,num,max
  </match>
</label>

2
0
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
2
0