1
1

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.

AWS CLIで取得したAmazon CloudWatch Metricsのget-metric-statisticsの結果を、jqでCSVに変換する

Last updated at Posted at 2022-08-04

What's?

Amazon CloudWatch Metricsから統計情報を取得した結果を、CSVに変換したいなというメモです。

環境

今回の環境は、こちらです。

$ aws --version
aws-cli/2.7.21 Python/3.9.11 Linux/5.4.0-122-generic exe/x86_64.ubuntu.20 prompt/off

AWSのクレデンシャルは、環境変数で設定しているものとします。

$ export AWS_ACCESS_KEY_ID=.....
$ export AWS_SECRET_ACCESS_KEY=.....
$ export AWS_DEFAULT_REGION=.....

お題

Amazon RDSのCPUUtilizationを対象にしましょう。

こちらをAWS CLIで取得し、jqでCSV形式に変換します。この時に時刻(Timestamp)でソートもします。

なお、メトリクスをAWS CLIで取得する方法は、このあたりを参考に。

使用するコマンドは、aws cloudwatch get-metric-statisticsですね。

メトリクスを取得するAmazon RDSインスタンスは、以下のように変数で定義しているものとします。

$ RDS_INSTANCE_IDENTIFIER=.....

メトリクスを取得する

まずは、メトリクスを取得しましょう。

使用するAWS CLIのコマンドはaws cloudwatch get-metric-statisticsです。

取得で使う名前空間とメトリクス名はこちら。

  • namespaceAWS/RDS
  • metric-nameCPUUtilization

statisticsAverageとし、取得期間は2022-08-04T08:00:00Z2022-08-04T08:10:00Z(UTC)、period60とします。

こういうコマンドになりますね。

$ aws cloudwatch get-metric-statistics \
    --namespace AWS/RDS \
    --metric-name CPUUtilization  \
    --dimensions Name=DBInstanceIdentifier,Value=$RDS_INSTANCE_IDENTIFIER \
    --statistics Average \
    --start-time 2022-08-04T08:00:00Z \
    --end-time 2022-08-04T08:10:00Z \
    --period 60

手元の環境での結果。

{
    "Label": "CPUUtilization",
    "Datapoints": [
        {
            "Timestamp": "2022-08-04T08:02:00+00:00",
            "Average": 10.433681122704089,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:06:00+00:00",
            "Average": 10.20017000283338,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:08:00+00:00",
            "Average": 9.858004733175562,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:03:00+00:00",
            "Average": 10.04149930834486,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:07:00+00:00",
            "Average": 10.533333333333333,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:09:00+00:00",
            "Average": 9.600320010667023,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:00:00+00:00",
            "Average": 10.683155280745321,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:04:00+00:00",
            "Average": 9.566666666666665,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:01:00+00:00",
            "Average": 10.26649555840736,
            "Unit": "Percent"
        },
        {
            "Timestamp": "2022-08-04T08:05:00+00:00",
            "Average": 10.39149347510875,
            "Unit": "Percent"
        }
    ]
}

CSVに変換する

では、この結果をCSVに変換しましょう。jqの@csvフォーマットを使用します。

The input must be an array, and it is rendered as CSV with double quotes for strings, and quotes escaped by repetition.

jq Manual / Builtin operators and functions / Format strings and escaping

また、ソートはsort_byを使用します。

jq Manual / Builtin operators and functions / sort, sort_by(path_expression)

こんな感じになります。

$ aws cloudwatch get-metric-statistics \
    --namespace AWS/RDS \
    --metric-name CPUUtilization  \
    --dimensions Name=DBInstanceIdentifier,Value=$RDS_INSTANCE_IDENTIFIER \
    --statistics Average \
    --start-time 2022-08-04T08:00:00Z \
    --end-time 2022-08-04T08:10:00Z \
    --period 60 \
    | jq -r '.Datapoints | sort_by(.Timestamp) | .[] | [.Timestamp, .Average, .Unit] | @csv'

結果。

"2022-08-04T08:00:00+00:00",10.683155280745321,"Percent"
"2022-08-04T08:01:00+00:00",10.26649555840736,"Percent"
"2022-08-04T08:02:00+00:00",10.433681122704089,"Percent"
"2022-08-04T08:03:00+00:00",10.04149930834486,"Percent"
"2022-08-04T08:04:00+00:00",9.566666666666665,"Percent"
"2022-08-04T08:05:00+00:00",10.39149347510875,"Percent"
"2022-08-04T08:06:00+00:00",10.20017000283338,"Percent"
"2022-08-04T08:07:00+00:00",10.533333333333333,"Percent"
"2022-08-04T08:08:00+00:00",9.858004733175562,"Percent"
"2022-08-04T08:09:00+00:00",9.600320010667023,"Percent"

ヘッダー行を付けたくなるかもしれません。その場合はこちら。

$ aws cloudwatch get-metric-statistics \
    --namespace AWS/RDS \
    --metric-name CPUUtilization  \
    --dimensions Name=DBInstanceIdentifier,Value=$RDS_INSTANCE_IDENTIFIER \
    --statistics Average \
    --start-time 2022-08-04T08:00:00Z \
    --end-time 2022-08-04T08:10:00Z \
    --period 60 \
    | jq -r '["Timestamp", "Average", "Unit"], (.Datapoints | sort_by(.Timestamp) | .[] | [.Timestamp, .Average, .Unit]) | @csv'

結果。

"Timestamp","Average","Unit"
"2022-08-04T08:00:00+00:00",10.683155280745321,"Percent"
"2022-08-04T08:01:00+00:00",10.26649555840736,"Percent"
"2022-08-04T08:02:00+00:00",10.433681122704089,"Percent"
"2022-08-04T08:03:00+00:00",10.04149930834486,"Percent"
"2022-08-04T08:04:00+00:00",9.566666666666665,"Percent"
"2022-08-04T08:05:00+00:00",10.39149347510875,"Percent"
"2022-08-04T08:06:00+00:00",10.20017000283338,"Percent"
"2022-08-04T08:07:00+00:00",10.533333333333333,"Percent"
"2022-08-04T08:08:00+00:00",9.858004733175562,"Percent"
"2022-08-04T08:09:00+00:00",9.600320010667023,"Percent"

こんな感じでしょうか。

TimestampはUTCなので、日本時間に変更するなどしたかったら、さらにこれを加工する感じですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?