CloudWatchからメトリクスを取得する
AWS Storage Gatewayを読んで、
aws-cli経由でStorageGatewayのメトリクスの取り方を調べてみた。
作業内容
まずは、StorageGatewayのメトリクス一覧を取得する。
$ aws cloudwatch list-metrics --namespace AWS/StorageGateway 多いので省略
試しにCacheHitPercentを取るために、--metric-nameを指定して絞り込む。
$ aws cloudwatch list-metrics --namespace AWS/StorageGateway --metric-name CacheHitPercent { "Metrics": [ { "Namespace": "AWS/StorageGateway", "Dimensions": [ { "Name": "GatewayId", "Value": "sgw-xxxxxxxx" }, { "Name": "GatewayName", "Value": "xxxxx" } ], "MetricName": "CacheHitPercent" }, { "Namespace": "AWS/StorageGateway", "Dimensions": [ { "Name": "VolumeId", "Value": "ap-northeast-1-vol-xxxxxxxx" } ], "MetricName": "CacheHitPercent" } ] }
すると、StorageGateway全体とボリュームごとのメトリクスがでてきた。
今回取りたいのは、StorageGateway全体のCacheHitPercentなので、以下のようにしてメトリクス値を取る。
$ aws cloudwatch get-metric-statistics \ --namespace AWS/StorageGateway \ --metric-name CacheHitPercent \ --dimensions Name=GatewayId,Value=sgw-xxxxxxxx Name=GatewayName,Value=xxxxx \ --statistics Average \ --period 300 \ --start-time `date -u -d '9 minutes ago' +%Y-%m-%dT%TZ` \ --end-time `date -u +%Y-%m-%dT%TZ` { "Datapoints": [ { "Timestamp": "2013-11-21T16:00:00Z", "Average": 15.932732316227463, "Unit": "Percent" } ], "Label": "CacheHitPercent" }
ポイントは、--dimensionsで、GatewayIdとGatewayNameを指定すること。
片方だけの指定だとメトリクス値が空になってしまう。
また、--start-timeも5分前の指定だと空になってしまう場合があったので、9分前にした。
以下のようにするとメトリクス値のみ取得できる。
$ aws cloudwatch get-metric-statistics \ --namespace AWS/StorageGateway \ --metric-name CacheHitPercent \ --dimensions Name=GatewayId,Value=sgw-xxxxxxxx Name=GatewayName,Value=xxxxx \ --statistics Average \ --period 300 \ --start-time `date -u -d '9 minutes ago' +%Y-%m-%dT%TZ` \ --end-time `date -u +%Y-%m-%dT%TZ` \ --query "Datapoints.[0].Average" 15.932732316227463
--queryは、JMESPath(ジェームズパス)という記法で書くらしい。
--queryを使わない場合は、jqでも同じことができる。
最終的にはこの値を定期的にzabbixに入れてグラフ化するところまでやってみたい。
参考
https://github.com/aws/aws-cli
http://docs.aws.amazon.com/cli/latest/reference/
http://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html
http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cli-mon-get-stats.html
http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/awssg-metricscollected.html
http://blog.suz-lab.com/2013/03/pythonawscloudwatch.html
http://www.slideshare.net/AmazonWebServices/becoming-a-command-line-expert-with-the-aws-cli-tls304-aws-reinvent-2013-28437764