我是AWS (和nodejs)的新手,我正在尝试让cloudwatch的getMetricData工作。我使用一个简单的Cloudwatch查询来获取实例的CPU利用率。但不管我做什么,我都会得到这个
{
ResponseMetadata: { RequestId: '5253fbfc-5aa2-4dfb-871b-1453f8610d80' },
MetricDataResults: [
{
Id: 'q1',
Label: 'q1',
Timestamps: [],
Values: [],
StatusCode: 'Complete',
Messages: []
}
],
Messages: [
{
Code: 'MaxQueryTimeRangeExceeded',
Value: 'Max time window exceeded for query'
}
]
}这是我的密码
const AWS = require("aws-sdk");
AWS.config.loadFromPath("./config.json");
var cloudwatch = new AWS.CloudWatch({apiVersion: "2010-08-01"});
var params = {
StartTime: new Date('june 06, 2022 17:30'),
EndTime: new Date('june 06, 2022 18:00'),
MetricDataQueries: [
{
Id: 'q1',
Expression: "SELECT AVG(CPUUtilization) FROM SCHEMA(\"AWS/EC2\", InstanceId) WHERE InstanceId = 'i-**********'",
Period: '600'
},
],
};
cloudwatch.getMetricData(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});发布于 2022-06-30 21:00:40
您只能使用SELECT ...语法请求最新的3小时数据。
来自文档,
度量查询只能查询最近三个小时的度量数据。
看起来,您只获取单个实例的CPUUtilization。您可以通过MetricStat对象获得这个结果。就像这样:
var params = {
StartTime: new Date('june 06, 2022 17:30'),
EndTime: new Date('june 06, 2022 18:00'),
MetricDataQueries: [
{
Id: 'm1',
Label: 'CPUUtilization',
MetricStat: {
Metric: {
Namespace: 'AWS/EC2',
MetricName: 'CPUUtilization',
Dimensions: [
{
Name: 'InstanceId',
Value: 'i-xxxxxxxxxx'
}
]
},
Period: '600',
Stat: 'Average'
}
},
],
};https://stackoverflow.com/questions/72820864
复制相似问题