Question

I'm having a little trouble using AmazonCloudWatch to fetch CPU Utilization. When I try to use AmazonCloudWatch.GetMetricStatistics(), I get this for an exception message:

Exception of type 'Amazon.CloudWatch.AmazonCloudWatchException' was thrown.

And this for an inner exception:

{"The remote server returned an error: (400) Bad Request."}

Here is the code I'm using to make the call:

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;
        }
    }

Some side notes - the access key, secret access key, and endpoint work fine for creating an AmazonEC2Client, so I'm pretty sure the problem isn't there.

I've done quite a bit of googling and poring over the documentation, but haven't been successful in solving this. Any ideas? Thanks so much!

Was it helpful?

Solution

Unfortunately, we weren't able to figure this one out - we ended up deciding to use Microsoft Azure instead of Amazon Web Services :(

OTHER TIPS

I think you can only request one Statistics at a time. So try removing either request.Statistics.Add("Maximum"); or request.Statistics.Add("Average");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top