我在使用AmazonCloudWatch获取CPU利用率方面遇到了一些麻烦。当我尝试使用AmazonCloudWatch.GetMetricStatistics()时,我得到了异常消息:
抛出了'Amazon.CloudWatch.AmazonCloudWatchException'类型的异常。
这是内部异常:
{“远程服务器返回错误:(400)错误请求。”}
以下是我用来拨打电话的代码:
public static String getCPUStats(String Endpoint, String InstanceID)
{
try
{
AmazonCloudWatchConfig cloudConfig = new AmazonCloudWatchConfig();
cloudConfig.ServiceURL = Endpoint;
string AWSAccessKey = Sql.ToString(appConfig["AWSAccessKey"]);
string AWSSecretKey = Sql.ToString(appConfig["AWSSecretKey"]);
AmazonCloudWatch client = AWSClientFactory.CreateAmazonCloudWatchClient(AWSAccessKey, AWSSecretKey, cloudConfig);
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();
request.Dimensions.Add(new Dimension { Name = "InstanceId", Value = InstanceID });
request.StartTime = DateTime.UtcNow.AddMinutes(-5);
request.EndTime = DateTime.UtcNow;
request.Namespace = "AWS/EC2";
request.Statistics.Add("Maximum");
request.Statistics.Add("Average");
request.MetricName = "CPUUtilization";
request.Period = 60;
GetMetricStatisticsResponse r = client.GetMetricStatistics(request);
if (r.GetMetricStatisticsResult.Datapoints.Count > 0)
{
Datapoint dataPoint = r.GetMetricStatisticsResult.Datapoints[0];
return "CPU maximum load: " + dataPoint.Maximum;
}
return "No data available.";
}
catch (Exception ex)
{
return ex.Message;
}
}
一些附注 - 访问密钥,秘密访问密钥和端点适用于创建AmazonEC2Client,所以我很确定问题不存在。
我已经完成了相当多的谷歌搜索和仔细阅读文档,但没有成功解决这个问题。有任何想法吗?非常感谢!
答案 0 :(得分:1)
不幸的是,我们无法解决这个问题 - 我们最终决定使用Microsoft Azure而不是Amazon Web Services :(
答案 1 :(得分:0)
我认为您一次只能请求一个统计信息。因此,请尝试删除request.Statistics.Add("Maximum");
或request.Statistics.Add("Average");