当前位置: 首页 > 工具软件 > aws-inventory > 使用案例 >

Amazon EC2 - how to get available ram and cpu usage via AWS API?

龙欣德
2023-12-01
var client = AWSClientFactory.CreateAmazonCloudWatchClient(
    <AWSAccessKey>,
    <AWSSecretKey>);

var dimension = new Dimension
{
    Name = "InstanceId",
    Value = <InstanceId>,
};

var request = new GetMetricStatisticsRequest();
request.Dimensions.Add(dimension);

var currentTime = DateTime.UtcNow;
var startTime = currentTime.AddMinutes(-5);
request.StartTime = startTime.ToString(
    AWSSDKUtils.ISO8601DateFormat,
    CultureInfo.InvariantCulture.DateTimeFormat);
request.EndTime = currentTime.ToString(
    AWSSDKUtils.ISO8601DateFormat,
    CultureInfo.InvariantCulture.DateTimeFormat);

request.Namespace = "AWS/EC2";
request.Statistics.Add("Maximum");
request.Statistics.Add("Average");
request.MeasureName = "CPUUtilization";
request.Period = 300;

var response = client.GetMetricStatistics(request);

if (response.IsSetGetMetricStatisticsResult() &&
    response.GetMetricStatisticsResult.Datapoints.Count > 0)
{
    var dataPoint = response.GetMetricStatisticsResult.Datapoints[0];
    Console.WriteLine(
        "Instance: {0} CPU Average load: {1} CPU Max load: {2}",
        instanceID,
        dataPoint.Average,
        dataPoint.Maximum);
}



You will need AmazonCloudWatch API for gathering this statistics. Unfortunately, it does not provide ram usage information. One of solutions could be creating your own "RAMUsage" metric and implementing ram usage monitoring service, which will run on all EC2 instances and add this information to CloudWatch.

Here is a sample of receiving "CPUUtilization" metric for last 5 minutes using AWS API.

http://stackoverflow.com/questions/6533904/amazon-ec2-how-to-get-available-ram-and-cpu-usage-via-aws-api
 类似资料:

相关阅读

相关文章

相关问答