1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

telegrafで複数入出力をルーティングする方法

Posted at

本記事の目的

  • telegrafを利用してinfluxdb等にデータを取り込む場合に、データソースによって格納先のDBを分けたいケースに対処する方法を解説する
    • 事例1: /var/log/hogeはinfluxdbのhogeデータベース、/var/log/fugaはinfluxdbのfugaデータベースに流したい
    • 事例2: systemデータソースはinfluxdbに流し、apacheデータソースはelasticsearchに流したい
  • 事例1のケースを中心に解説するが、事例2でも考え方は同じとなる。
  • 2通りの手段を解説する

環境

  • telegraf 1.20.4
    • input-filter : tail
    • output-filter : influxdb
  • influxdb 1.8.10
  • AlmaLinux 8.6 ※CentOSでも同様となる

やること

  • /var/log/metrics/stream1/と、/var/log/metrics/stream2/にそれぞれログが吐き出されており、stream1/*.log はinfluxdbDBの「stream1」に、stream2/*.log は「stream2」に格納したい

方式1

config

[[inputs.tail]]
  files = ["/var/log/metrics/stream1/*.log"]

[[inputs.tail]]
  files = ["/var/log/metrics/stream2/*.log"]
  [inputs.tail.tags]
    influxdb_database = "stream2"

[[outputs.influxdb]]
  urls = ["http://127.0.0.1:8086"]
  database = "stream1"
  [outputs.influxdb.tagdrop]
    influxdb_database = ["*"]

[[outputs.influxdb]]
  urls = ["http://127.0.0.1:8086"]
  database = "stream2"
  tagexclude = ["influxdb_database"]
  [outputs.influxdb.tagpass]
    influxdb_database = ["stream2"]

解説

  • stream1/*.logはデフォルトデータソースと位置付け、何もしていない
  • stream2/*.logを制御するため、influxdb_databaseというタグをつけ、タグ値にstream2をつけている
  • stream1向けのouput設定: 「influxdb_database」というタグの付いたinputはすべてdropする
  • stream2向けのoutput設定: 「influxdb_database」タグでタグ値「stream2」が付いたinputを取り込む。また取り込むと同時に、input時に付与したタグ「influxdb_database」を除去している

参考: 別のやり方

[[inputs.tail]]
  files = ["/var/log/metrics/stream1/*.log"]
  [inputs.tail.tags]
    influxdb_database = "stream1"

[[inputs.tail]]
  files = ["/var/log/metrics/stream2/*.log"]
  [inputs.tail.tags]
    influxdb_database = "stream2"

[[outputs.influxdb]]
  urls = ["http://127.0.0.1:8086"]
  database = "stream1"
  tagexclude = ["influxdb_database"]
  [outputs.influxdb.tagpass]
    influxdb_database = ["stream1"]

[[outputs.influxdb]]
  urls = ["http://127.0.0.1:8086"]
  database = "stream2"
  tagexclude = ["influxdb_database"]
  [outputs.influxdb.tagpass]
    influxdb_database = ["stream2"]
  • こちらの方が、より明示的な制御となる。
  • 入力時にinfluxdb_databaseタグと、出力先をタグ値にて指定。出力時にタグ値を元にtagpassにより選別している

方式2

  • 方式2は、telegrafのプロセスを複数立て、それぞれで独立させて制御する方法を解説する

手順

  • telegrafをインストールすると、プロセス起動ファイルが/etc/systemd/system/multi-user.target.wants/telegraf.serviceとして作成されるため、これを複製する。
  • telegraf2.serviceとして複製する
$ cd /usr/lib/systemd/system
$ sudo cp telegraf.service telegraf2.service
$ cd /etc/systemd/system/multi-user.target.wants
$ sudo ln -s /usr/lib/systemd/system/telegraf2.service
  • telegraf2.serviceを編集し、コンフィグファイル先を変更する
  • /etc/telegraf2/を2つ目のtelegrafのconfig置き場とする
$ diff telegraf.service telegraf2.service
7c7
< EnvironmentFile=-/etc/default/telegraf
---
> EnvironmentFile=-/etc/default/telegraf2
9c9
< ExecStart=/usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d $TELEGRAF_OPTS
---
> ExecStart=/usr/bin/telegraf -config /etc/telegraf2/telegraf.conf -config-directory /etc/telegraf2/telegraf.d $TELEGRAF_OPTS
  • ここまで複製したら、あとは個別のサービスとして制御するのみ。
  • $ sudo systemctl start telegraf2としてサービス開始できる。
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?