Amazon Linux 2:Fluentdを用いてCloudWatchの情報をZabbixに保存させる

【中古車のガリバー】中古車購入

AWS CloudWatchの情報保存期間は「最長2週間(追記: メトリクスの保持期間は従来の「14日」から「15ヶ月」に延長されている)」のため、長期的に情報保持したい場合、他の仕組みが必要になります。Fluentdを用いてCloudWatchの情報を、Zabbixに保存できるようにします。(2018/12/16作成)

PIRO4D / Pixabay

fluentdとは

Fluentd(フルエントディー)とは、オープンソースのデータコレクターやデータログ収集ツールと呼ばれるソフトウェアです。Fluentdを用いれば、今までのログ収集方式より格段に手軽にログを収集し、活用することができます。(出典:IDC Frontier)

CloudWatchとは

Amazon CloudWatch は、アマゾン ウェブ サービス (AWS) リソースと、AWS で実行されているアプリケーションをリアルタイムでモニタリングします。 リソースとアプリケーションで測定する変数であるメトリクスを収集し、追跡するには、CloudWatch を使用できます。(出典:アマゾンウェブサービス)

0.環境

$ cat /etc/system-release
Amazon Linux release 2 (Karoo)

$ zabbix_get -s localhost -k agent.version
4.0.2

1.AWS IAM作成

先ず、EC2からCloudWatchの情報を取得するためのユーザーを作成します。
IAMのユーザーを作成し、Access Key IDとSecret Access Keyを控えます。
ユーザーには CloudWatchReadOnlyAccess のロールを付与してCloudWatchへのアクセス権限を付与します。

2.fluentdインストール

公式ドキュメント(fluentd):
https://docs.fluentd.org/v1.0/articles/install-by-rpm#amazon-linux

Amazon Linux
Mainly for Amazon Linux 2. Recent two 64bit versions are supported for amazon linux 1. This means if latest version is 2018.03, rpm is provided for 2018.03 and 2017.09.

# Amazon Linux 2
$ curl -L https://toolbelt.treasuredata.com/sh/install-amazon2-td-agent3.sh | sh

3.fluentdの2つのプラグインインストール

$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-zabbix
$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-cloudwatch

4.fluentd設定

参考)RDSのメトリックス一覧

  • CPUUtilization
  • FreeableMemory
  • DatabaseConnections
  • NetworkThroughput
  • NetworkTransmitThroughput
$ sudo vi /etc/td-agent/td-agent.conf
<source>
  type cloudwatch
  tag  cloudwatch
  aws_key_id {YOUR_AWS_KEY_ID}
  aws_sec_key {YOUR_AWS_SECRET_KEY}
  cw_endpoint monitoring.{YOUR REGION CODE}.amazonaws.com

  namespace AWS/RDS
  metric_name CPUUtilization,FreeableMemory,DatabaseConnections,NetworkThroughput,NetworkTransmitThroughput
  dimensions_name {DBClusterIdentifier または DBInstanceIdentifier}
  dimensions_value {DBクラスターID または DBインスタンスID}
</source>
# 《メモ※1》
#<match cloudwatch>
#  type stdout
#</match>
<match cloudwatch>
  type copy
  <store>
    type zabbix
    zabbix_server {ZABBIX SERVER IP}
    port 10051
    host {ZABBIXに設定するホスト名(例:rds-aurora-mysql)}
    name_keys CPUUtilization,FreeableMemory,DatabaseConnections,NetworkThroughput,NetworkTransmitThroughput
    add_key_prefix cloudwatch
  </store>
</match>

※確認のため、取得した内容は標準出力に表示させます。
※その際 fluentdは「トレースモード」で起動させます。

$ sudo td-agent -vv | grep -i cloudwatch
…
# 2018-12-11 09:09:00.000000000 +0000 cloudwatch: {"CPUUtilization":15.750000000028301}
# 2018-12-11 09:09:00.000000000 +0000 cloudwatch: {"FreeableMemory":196077568.0}
# 2018-12-11 09:09:00.000000000 +0000 cloudwatch: {"DatabaseConnections":0.6}
# 2018-12-11 09:09:00.000000000 +0000 cloudwatch: {"NetworkThroughput":334418.3751281075}
# 2018-12-11 09:09:00.000000000 +0000 cloudwatch: {"NetworkTransmitThroughput":317997.0381845628}

5.fluentd起動設定

$ sudo systemctl status td-agent.service
$ sudo systemctl start td-agent.service
$ sudo systemctl enable td-agent.service
$ sudo systemctl is-enabled td-agent.service

6.ZABBIX側の設定

◆ホスト名
ZABBIXに設定するホスト名(例:rds-aurora-mysql)

◆アイテム (ZabbixRDSCPUUtilizationの場合)
名前:ZabbixRDSCPUUtilization
タイプ:Zabbixトラッパー
キー:cloudwatch.CPUUtilization
データ型:整数(浮動小数)
単位:%

以上

About yoshimasa

埼玉県さいたま市在住、2男3女のパパです。Linux系の技術情報を中心にまとめています。1978年2月生まれ。