如何从AWS CloudWatch到CSV获取指标数据 [英] How to get metrics data from aws cloudwatch to csv

查看:138
本文介绍了如何从AWS CloudWatch到CSV获取指标数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究项目,我想从cloudwatch导出指标数据,例如CPU利用率和Network Out数据,有什么方法可以获取这些数据?并将其转换为csv?

i have been working on project, and i want to export metrics data from cloudwatch like CPU Utilization and Network Out data, is there any way to get that data? and convert it to csv?

推荐答案

导出CloudWatch指标

您不能直接执行此操作,但以下是分步说明:

Export CloudWatch Metrics

You can't do it directly but following is the step by step instructions:

  1. AWS CLI(命令行界面),来自此处
  2. jq 是来自此处的轻巧灵活的命令行JSON处理器.

如何导出

使用以下CLI:

How to export

Use the following CLI:

aws cloudwatch get-metric-statistics 
--namespace AWS/EC2 --metric-name CPUUtilization 
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx 
--statistics Average 
--start-time 2020-03-01T00:00:00 
--end-time 2020-03-31T23:59:00 
--period 3600
--region us-east-1

-metric-name 的有效选项取决于-name-space 参数.对于AWS/EC2,可以通过运行以下CLI命令来查看完整列表:

Valid options for --metric-name depend on the --name-space parameter. For AWS/EC2, the full list can be seen by running the following CLI command:

aws cloudwatch list-metrics --namespace "AWS/EC2"

-统计信息的有效选项是:

SampleCount
Average
Sum
Minimum
Maximum

-开始时间-结束时间指定范围.

-周期返回的数据点的粒度(以秒为单位).

--period The granularity, in seconds, of the returned data points.

-区域监视CloudWatch指标的区域(us-east-1,us-west-2等)

--region The region where the CloudWatch metric is being meatured (us-east-1, us-west-2 etc.)

数据输出将类似于以下内容:

Data output will look something like the following:

{
    "Label": "CPUUtilization",
    "Datapoints": []
}

要将其转换为CSV,我们将使用 jq .为此,您有两个选择:

To convert it to CSV we will use jq. For this you have two options:

将所有aws cli输出输出到jq:

Pipe all the aws cli output to jq:

aws cloudwatch get-metric-statistics 
--namespace AWS/EC2 --metric-name CPUUtilization 
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx 
--statistics Average 
--start-time 2020-03-01T00:00:00 
--end-time 2020-03-31T23:59:00 
--period 3600
--region us-east-1
| jq -r '.Datapoints[] | [.Timestamp, .Minimum, .Unit] | @csv'

选项2

将数据导出到JSON:

Option 2

Export the data to JSON:

aws cloudwatch get-metric-statistics 
--namespace AWS/EC2 --metric-name CPUUtilization 
--dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx 
--statistics Average 
--start-time 2020-03-01T00:00:00 
--end-time 2020-03-31T23:59:00 
--period 3600
--region us-east-1 >> data.json

使用jq将json读取到csv:

Use jq to read the json to csv:

jq -r '.Datapoints[] | [.Timestamp, .Minimum, .Unit] | @csv' data.json

输出

这是输出的样子:

Output

Here is how the output will look like:

"2020-03-24T11:00:00Z",0.327868852454245,"Percent"
"2020-03-11T21:00:00Z",0.327868852454245,"Percent"
"2020-03-15T04:00:00Z",0.322580645156596,"Percent"
"2020-03-27T18:00:00Z",0.327868852478101,"Percent"

这篇关于如何从AWS CloudWatch到CSV获取指标数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆