【新機能】カスタムメトリクスとCloudWatchアラームが秒間隔で利用可能になりました!

2017.07.27

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ご機嫌いかがでしょうか、豊崎です。

CloudWatchの新機能が発表されました。

New – High-Resolution Custom Metrics and Alarms for Amazon CloudWatch

早速内容をご紹介したいと思います。

できるようになったこと

今回の新機能で、カスタムメトリクスを1秒間隔で利用できるようになりました。 また、10秒間隔で評価するCloudWatchアラームを設定することができるようになりました。

利用方法

利用する方法はAPIを利用する方法と、collectd pluginを利用する方法のの2種類があります。

APIでは、「PutMetricData」の「StorageResolution」パラメータを、1にして実行します。 http://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html

collectd pluginでは、オープンソースのcollectedプラグインを利用します https://github.com/awslabs/collectd-cloudwatch

監視間隔とロールアップ

メトリクスは監視間隔によって、一定時間が経過すると閲覧できるメトリクスの解像度が減少します。間隔ごとに指定の粒度で保存ができる期間を定めて容量をセーブしているということですね。

監視間隔 経過時間
1秒 3時間
60秒 15日
5分 63日
1時間 455日

デモをやってみる

EC2は以下を使用しました。 EC2には「CloudWatchFullAccess」を許可したIAMロールを付与しています。

  • amzn-ami-hvm-2017.03.1.20170623-x86_64-gp2 (ami-3bd3c45c)
$ sudo yum install collectd collectd-python

$ wget https://raw.githubusercontent.com/awslabs/collectd-cloudwatch/master/src/setup.py
$ chmod a+x setup.py
$ sudo ./setup.py

Installing dependencies ... OK
Installing python dependencies ... OK
Downloading plugin ... OK
Extracting plugin ... OK
Moving to collectd plugins directory ... OK
Copying CloudWatch plugin include file ... OK

Choose AWS region for published metrics:
  1. Automatic [ap-northeast-1]
  2. Custom
Enter choice [1]: 1

Choose hostname for published metrics:
  1. EC2 instance id [i-XXXXXXXXXXXXXXXX]
  2. Custom
Enter choice [1]: 1

Choose authentication method:
  1. IAM Role [demo-HighResolutionCustomMetrics]
  2. IAM User
Enter choice [1]: 1

Enter proxy server name:
  1. None
  2. Custom
Enter choice [1]: 1

Enter proxy server port:
  1. None
  2. Custom
Enter choice [1]: 1

Include the Auto-Scaling Group name as a metric dimension:
  1. No
  2. Yes
Enter choice [1]: 1

Include the FixedDimension as a metric dimension:
  1. No
  2. Yes
Enter choice [1]: 1

Enable high resolution:
  1. Yes
  2. No
Enter choice [2]: 1

Enter flush internal:
  1. Default 60s
  2. Custom
Enter choice [1]: 2

Enter the customized flush interval ([1, 60] s):
Enter choice [60]: 1

Choose how to install CloudWatch plugin in collectd:
  1. Do not modify existing collectd configuration
  2. Add plugin to the existing configuration
  3. Use CloudWatch recommended configuration (4 metrics)
Enter choice [3]: 3
Plugin configuration written successfully.
Creating backup of the original configuration ... OK
Replacing collectd configuration ... OK
Replacing whitelist configuration ... OK

しばらくして、CloudWatchダッシュボードからメトリクスを確認してみるとメトリクスが3つ増えています。

CloudWatch_Management_Console

001

  • cpu使用率
  • メモリ使用率
  • ルートディスク

collectd-pluginsの設定ファイル的には以下に記述されていました。 (swapの指定もありますが、swapが設定されていなかったため、メトリクスとして表示されなかったものと思われます。)

$ cat /opt/collectd-plugins/cloudwatch/config/whitelist.conf

df-root-percent_bytes-used
memory--percent-used
swap--percent-used
cpu--percent-active

各メトリクスの間隔を1秒(10秒以下)に変更したところ、グラフでは10秒間隔で値が表示されました。1秒間隔のグラフは3時間前までしか取得できません。それ以前を確認したい場合は監視間隔を1分や5分などに戻せばOKです。

001

003

また、collectd pluginの/opt/collectd-plugins/cloudwatch/config/whitelist.confを変更することで 他の値も取得できます。詳しくは以下をご覧ください。

https://github.com/awslabs/collectd-cloudwatch

さいごに

監視間隔が短くなったことで、他のAWSサービスとの連携においても俊敏性を発揮するものと思われます。 もう少し詳しく触ってみようと思います。取り急ぎ簡単な利用方法をお伝えしました。誰かの役に当てば幸いです。