我在使用AmazonCloudWatch獲取CPU利用率時遇到了一些麻煩。當我嘗試使用AmazonCloudWatch.GetMetricStatistics(),我得到這樣一個異常消息:AWS:從AmazonCloudWatch.GetMetricStatistics()獲取400錯誤的請求錯誤
Exception of type 'Amazon.CloudWatch.AmazonCloudWatchException' was thrown.
而本作的內部異常:
{"The remote server returned an error: (400) Bad Request."}
這裏是我的代碼m使用撥打電話:
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;
}
}
一些備註 - 訪問密鑰,祕密訪問密鑰和endp oint可以很好地創建AmazonEC2Client,所以我很確定問題不在那裏。
我已經完成了很多關於文檔的搜索和編輯工作,但並沒有成功地解決這個問題。有任何想法嗎?非常感謝!
部分的調試挑戰微軟Azure是, 「真正的」亞馬遜錯誤信息在AWSSDK中迷失了,並被轉化爲一個通用的.NET 400(又名:「BadRequest」)...我正在用一些DynamoDB代碼來解決同樣的問題......我認爲唯一的答案是發射提琴手,並看看答案... – Scrappydog