1
3

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 1 year has passed since last update.

InfluxDB: Telegrafからデータを取得する

Posted at
実施環境:
Linux
[root@testhost ~]# uname -a
Linux testhost 4.18.0-448.el8.x86_64 #1 SMP Wed Jan 18 15:02:46 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
[root@testhost ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@testhost ~]# yum list installed | grep telegraf
telegraf.x86_64                                    1.28.3-1                                                   @@commandline
[root@testhost ~]# yum list installed | grep influxdb
influxdb2.x86_64                                   2.7.3-1                                                    @@commandline

0. 概要

データベースのソフトウェアは、データの格納方法によって大きく RDB ( Relational Database )と NoSQL に分けられます。
RDB は複数の表を組み合わせる形でデータを格納するデータベースであり、 NoSQL は RDB 以外のデータベース全般を指す言葉です。
NoSQL の中には様々な特徴を持つデータベースがありますが、その中でも時系列データに特化したデータベースが InfluxDB です。

InfluxDB

この InfluxDB にデータを格納する方法はいろいろありますが、そのうちの1つが Telegraf を用いる方法です。
Telegraf は InfluxDB 向けに作成されたオープンソースのメトリクス収集エージェントで、システムのパフォーマンス情報やその他各種ソフトウェアから出力される情報を取得することが可能です。

Telegraf

今回はこの Telegraf を用いて、 CPU 使用率を InfluxDB に格納することを目指します。

1. インストール

InfluxDB のインストールについては、以下の過去記事を参照してください。
なお、インストール時に表示される API トークンは後程使用するので控えておいてください。

Linux: 時系列データに特化したDB、「InfluxDB」を無料インストールしてみた

Telegraf は、以下の手順でインストールします。
なお、今回は InfluxDB と同じサーバにインストールします。

まずは、パッケージを GitHub からダウンロードします。
今回使用する環境は x86_64 環境なので、「 amd64 」を使用します。

GitHub

WS000703.JPG

ダウンロードしたパッケージを Linux 上に配置し、 yum コマンドでインストールします。

Linux
[root@testhost tmp]# ls -l telegraf*
-rw-rw-r--. 1 testuser testuser 55225598 11月  4 20:54 telegraf-1.28.3-1.x86_64.rpm
[root@testhost tmp]# yum -y install telegraf-1.28.3-1.x86_64.rpm
メタデータの期限切れの最終確認: 10:15:52 時間前の 2023年11月04日 10時50分12秒 に実施しました。
(省略)
完了しました!
[root@testhost tmp]# yum list installed | grep telegraf
telegraf.x86_64                                    1.28.3-1                                                   @@commandline
[root@testhost tmp]#

これで Telegraf はインストールできました。

2. 設定

さて、 Telegraf 自体はインストールできましたが、今の段階では InfluxDB とは連携していません。
Telegraf の設定を InfluxDB と連携できるように変更する必要があります。

InfluxDB の設定ファイルは、 /etc/telegraf/telegraf.conf です。

Linux
[root@testhost ~]# ls -l /etc/telegraf/
合計 488
-rw-r--r--. 1 root root 497672 10月 24 00:26 telegraf.conf
drwxr-xr-x. 2 root root     21 11月  4 21:06 telegraf.d
[root@testhost ~]# 

この設定ファイルの中には様々な設定テンプレートがコメントアウトされていますが、 InfluxDB との連携に関係のある箇所は以下の通りです。
なお、今回使用する InfluxDB のバージョンは 2.7.3 ですが、これが 1.x の場合は別の箇所になります。

telegraf.conf
# # Configuration for sending metrics to InfluxDB 2.0
# [[outputs.influxdb_v2]]
#   ## The URLs of the InfluxDB cluster nodes.
#   ##
#   ## Multiple URLs can be specified for a single cluster, only ONE of the
#   ## urls will be written to each interval.
#   ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
#   urls = ["http://127.0.0.1:8086"]
#
#   ## Token for authentication.
#   token = ""
#
#   ## Organization is the name of the organization you wish to write to.
#   organization = ""
#
#   ## Destination bucket to write into.
#   bucket = ""
#
#   ## The value of this tag will be used to determine the bucket.  If this
#   ## tag is not set the 'bucket' option is used as the default.
#   # bucket_tag = ""
#
#   ## If true, the bucket tag will not be added to the metric.
#   # exclude_bucket_tag = false
#
#   ## Timeout for HTTP messages.
#   # timeout = "5s"
#
#   ## Additional HTTP headers
#   # http_headers = {"X-Special-Header" = "Special-Value"}
#
#   ## HTTP Proxy override, if unset values the standard proxy environment
#   ## variables are consulted to determine which proxy, if any, should be used.
#   # http_proxy = "http://corporate.proxy:3128"
#
#   ## HTTP User-Agent
#   # user_agent = "telegraf"
#
#   ## Content-Encoding for write request body, can be set to "gzip" to
#   ## compress body or "identity" to apply no encoding.
#   # content_encoding = "gzip"
#
#   ## Enable or disable uint support for writing uints influxdb 2.0.
#   # influx_uint_support = false
#
#   ## HTTP/2 Timeouts
#   ## The following values control the HTTP/2 client's timeouts. These settings
#   ## are generally not required unless a user is seeing issues with client
#   ## disconnects. If a user does see issues, then it is suggested to set these
#   ## values to "15s" for ping timeout and "30s" for read idle timeout and
#   ## retry.
#   ##
#   ## Note that the timer for read_idle_timeout begins at the end of the last
#   ## successful write and not at the beginning of the next write.
#   # ping_timeout = "0s"
#   # read_idle_timeout = "0s"
#
#   ## Optional TLS Config for use on HTTP connections.
#   # tls_ca = "/etc/telegraf/ca.pem"
#   # tls_cert = "/etc/telegraf/cert.pem"
#   # tls_key = "/etc/telegraf/key.pem"
#   ## Use TLS but skip chain & host verification
#   # insecure_skip_verify = false

非常に長いですが、最初の方の [[outputs.influxdb_v2]]urlstokenorganizationbucket の5行だけがあればとりあえず動作するので、まずはこれらの行のコメントアウトを外します。

urls には InfluxDB の接続 URL を指定しますが、今回 InfluxDB は同一サーバ上にありますのでデフォルトのままでよいです。
token には InfluxDB の構築時に表示された API トークンを指定します。
organization には InfluxDB の構築時に指定した組織名、今回は test_organization を指定します。
bucket には取得したデータを格納するバケツ名、今回は test_bucket を指定します。

最終的には、以下のようになるかと思います。

telegraf.conf
# # Configuration for sending metrics to InfluxDB 2.0
[[outputs.influxdb_v2]]
#   ## The URLs of the InfluxDB cluster nodes.
#   ##
#   ## Multiple URLs can be specified for a single cluster, only ONE of the
#   ## urls will be written to each interval.
#   ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
urls = ["http://127.0.0.1:8086"]
#
#   ## Token for authentication.
token = " InfluxDB の API トークン"
#
#   ## Organization is the name of the organization you wish to write to.
organization = "test_organization"
#
#   ## Destination bucket to write into.
bucket = "test_bucket"
#

データの取得に関連する箇所については、デフォルトで利用可能な状態になっているものがいくつかあるので、それをそのまま使用します。
以下はその1つで、今回目的とする CPU 使用率の取得に関する箇所です。

telegraf.conf
# Read metrics about cpu usage
[[inputs.cpu]]
  ## Whether to report per-cpu stats or not
  percpu = true
  ## Whether to report total system cpu stats or not
  totalcpu = true
  ## If true, collect raw CPU time metrics
  collect_cpu_time = false
  ## If true, compute and report the sum of all non-idle CPU states
  report_active = false
  ## If true and the info is available then add core_id and physical_id tags
  core_tags = false

3. 起動

設定が変更できたら、いよいよ Telegraf を起動します。
systemctl コマンドから起動が可能です。
なお、 Telegraf を起動する前に InfluxDB は起動した状態にしておいてください。

Linux
[root@testhost ~]# systemctl list-unit-files -t service | grep telegraf
telegraf.service                           enabled
[root@testhost ~]# systemctl start telegraf.service
[root@testhost ~]# systemctl is-active telegraf.service
active
[root@testhost ~]#

4. 確認

InfluxDB の Web 画面において、以下の通りデータを検索します。

FROM : test_bucket
Filter : _measurement = cpu
検索期間 : Past 5m

すると、以下の通り Telegraf で取得したデータが確認できました。

WS000704.JPG

1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?