使用 Cloudwatch API 的实例的 CPU 使用率
如何获取 C# 中实例的 CPU 使用率? 我已阅读Amazon EC2 - 如何通过 AWS API 获取可用的 ram 和 cpu 使用情况? 已经有了,但我无法让它工作。
NameValueCollection appConfig = ConfigurationManager.AppSettings;
var client = AWSClientFactory.CreateAmazonCloudWatchClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
var dimension = new Dimension
{
Name = "InstanceId",
Value = "<i-ad20b4db>",
};
var request = new GetMetricStatisticsRequest();
request.MetricName = "CPUUtilization";
request.Period = 60;
request.Statistics.Add("Maximum");
request.Dimensions.Add(dimension);
request.Namespace = "AWS/EC2";
request.Unit = "Percent";
var currentTime = DateTime.UtcNow;
var startTime = currentTime.AddSeconds(-5);
string currentTimeString= currentTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
string startTimeString= startTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
request.StartTime = Convert.ToDateTime( startTimeString);
request.EndTime = Convert.ToDateTime(currentTimeString);
var response = client.GetMetricStatistics(request);
if ( response.GetMetricStatisticsResult.Datapoints.Count > 0)
{
var dataPoint = response.GetMetricStatisticsResult.Datapoints[0];
Console.WriteLine( "Instance: {0} CPU Max load: {1}", dimension.Value, dataPoint.Maximum);
}
How do i get the CPU usage of an instance in c#?
I've read Amazon EC2 - how to get available ram and cpu usage via AWS API? already, but i can't get it working.
NameValueCollection appConfig = ConfigurationManager.AppSettings;
var client = AWSClientFactory.CreateAmazonCloudWatchClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
var dimension = new Dimension
{
Name = "InstanceId",
Value = "<i-ad20b4db>",
};
var request = new GetMetricStatisticsRequest();
request.MetricName = "CPUUtilization";
request.Period = 60;
request.Statistics.Add("Maximum");
request.Dimensions.Add(dimension);
request.Namespace = "AWS/EC2";
request.Unit = "Percent";
var currentTime = DateTime.UtcNow;
var startTime = currentTime.AddSeconds(-5);
string currentTimeString= currentTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
string startTimeString= startTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
request.StartTime = Convert.ToDateTime( startTimeString);
request.EndTime = Convert.ToDateTime(currentTimeString);
var response = client.GetMetricStatistics(request);
if ( response.GetMetricStatisticsResult.Datapoints.Count > 0)
{
var dataPoint = response.GetMetricStatisticsResult.Datapoints[0];
Console.WriteLine( "Instance: {0} CPU Max load: {1}", dimension.Value, dataPoint.Maximum);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个简单的概念证明,用于显示过去两天的 CPU 利用率。我认为每半小时就会有一个数据点,但您可以通过更改 GetMetricStatisticsRequest 对象中的 period 属性的值来轻松更改。
Here's a simple proof of concept for showing the CPU Utilization during the last two days. I think there will be a datapoint like every half an hour, but you can easily change by changing the value of the Period property in the GetMetricStatisticsRequest object.
这是我的最终代码,对我有用。
我在 IAM aws 管理控制台中添加了新用户,然后将 CloudWatchfullacess 策略附加到新用户,然后我使用了该用户 Accesskey 和 Accesskey。本代码中的密钥。
This is my final code which works for me.
I have added new user in the IAM aws management console then i have Attached CloudWatchfullacess Policy to new user then i have used this user Accesskey & secret key in this Code.
另请注意,当您想要获取 CPU 使用率或任何其他类型的指标时,您应该警惕请求中包含的时间戳。这是因为数据可能需要超过 48 小时才能在 CloudWatch 中可用(根据 Amazon 的说法)。
这就是为什么我个人尝试检索大约 3-4 天前的数据,因为有时即使请求和代码都正确,CloudWatch 也不会返回任何数据,因为它没有前一两天的任何数据可发布。
有关更多信息,请检查此 URL 并向下滚动到 StartTime:http://docs .aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html
Pleas also note that when you want to get CPU usage or any other type of metric, you should be wary of the time-stamp you include in the request. This is because it can take more than 48 hours for data to become available in CloudWatch (according to Amazon).
That is why I personally try to retrieve data from about 3-4 days ago as sometimes CloudWatch will not return any data even if the request and the code are all correct as it does not have any data to publish from the previous day or two.
For more information check this URL and scroll down to StartTime: http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html