Fetch multiple couldwatch metrics in one cli call

0

Hi, how do I retrieve multiple metrics of ec2 instance in a single aws cli call? I want to retrieve cpu, writeops, readops etc for an ec2 instance, but instead of of issuing multiple aws cloudwatch get-metric-statistics calls for each metric, is there a way to get data for all metrics in one call?

aws cloudwatch get-metric-statistics --metric-name EBSWriteOps --start-time "2022-04-18" --end-time "2022-04-19" --period 60 --namespace AWS/EC2 --statistics Average --dimensions Name=InstanceId,Value=$id --output text --query 'avg(Datapoints[*].Average)'

aws cloudwatch get-metric-statistics --metric-name CPUUtilization  --start-time "2022-04-18" --end-time "2022-04-19" --period 60 --namespace AWS/EC2 --statistics Average --dimensions Name=InstanceId,Value=$id --output text --query 'avg(Datapoints[*].Average)'

Can above 2 calls be combined as one call?

질문됨 2년 전2155회 조회
1개 답변
0
수락된 답변

You can use* aws cloudwatch get-metric-data* which will allow you to pass an array of metric data queries. (Reference - https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudwatch/get-metric-data.html)

A sample command will look like this -

COMMAND aws cloudwatch get-metric-data --cli-input-json file://input.json

INPUT FILE - input.json

{
	"MetricDataQueries": [{
			"Id": "cpuUtilization",
			"MetricStat": {
				"Metric": {
					"Namespace": "AWS/EC2",
					"MetricName": "CPUUtilization",
					"Dimensions": [{
						"Name": "InstanceId",
						"Value": "i-000000"
					}]
				},
				"Period": 60,
				"Stat": "Average"
			},
			"ReturnData": true
		},
		{
			"Id": "networkPacketsOut",
			"MetricStat": {
				"Metric": {
					"Namespace": "AWS/EC2",
					"MetricName": "NetworkPacketsOut",
					"Dimensions": [{
						"Name": "InstanceId",
						"Value": "i-000000"
					}]
				},
				"Period": 60,
				"Stat": "Average"
			},
			"ReturnData": true
		}
	],
	"StartTime": "2022-04-01T00:00:00",
	"EndTime": "2022-04-30T00:00:00"
}

SAMPLE OUTPUT

{
	"MetricDataResults": [{
			"Id": "cpuUtilization",
			"Label": "CPUUtilization",
			"Timestamps": [
				"2022-04-20T01:07:00+00:00",
				"2022-04-20T01:02:00+00:00",
				"2022-04-20T00:57:00+00:00"
			],
			"Values": [
				0.09945355191256719,
				0.13280540890988418,
				0.13450128228466757
			],
			"StatusCode": "Complete"
		},
		{
			"Id": "networkPacketsOut",
			"Label": "NetworkPacketsOut",
			"Timestamps": [
				"2022-04-20T01:07:00+00:00",
				"2022-04-20T01:02:00+00:00",
				"2022-04-20T00:57:00+00:00"
			],
			"Values": [
				82.4,
				86.6,
				82.2
			],
			"StatusCode": "Complete"
		}
	],
	"Messages": []
}
AWS
ganesh
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠