GetMetricStatistics() public method

Gets statistics for the specified metric.

Amazon CloudWatch retains metric data as follows:

  • Data points with a period of 60 seconds (1 minute) are available for 15 days

  • Data points with a period of 300 seconds (5 minute) are available for 63 days

  • Data points with a period of 3600 seconds (1 hour) are available for 455 days (15 months)

Note that CloudWatch started retaining 5-minute and 1-hour metric data as of 9 July 2016.

The maximum number of data points returned from a single call is 1,440. If you request more than 1,440 data points, Amazon CloudWatch returns an error. To reduce the number of data points, you can narrow the specified time range and make multiple requests across adjacent time ranges, or you can increase the specified period. A period can be as short as one minute (60 seconds). Note that data points are not returned in chronological order.

Amazon CloudWatch aggregates data points based on the length of the period that you specify. For example, if you request statistics with a one-hour period, Amazon CloudWatch aggregates all data points with time stamps that fall within each one-hour period. Therefore, the number of values aggregated by CloudWatch is larger than the number of data points returned.

For a list of metrics and dimensions supported by AWS services, see the Amazon CloudWatch Metrics and Dimensions Reference in the Amazon CloudWatch User Guide.

/// Request processing has failed due to some unknown error, exception, or failure. /// /// Parameters that cannot be used together were used together. /// /// The value of an input parameter is bad or out-of-range. /// /// An input parameter that is required is missing. ///
public GetMetricStatistics ( GetMetricStatisticsRequest request ) : GetMetricStatisticsResponse
request Amazon.CloudWatch.Model.GetMetricStatisticsRequest Container for the necessary parameters to execute the GetMetricStatistics service method.
return Amazon.CloudWatch.Model.GetMetricStatisticsResponse
コード例 #1
3
ファイル: CloudWatchSamples.cs プロジェクト: aws/aws-sdk-net
    public static void CWGetMetricStatistics()
    {
      #region CWGetMetricStatistics
      var client = new AmazonCloudWatchClient();

      var request = new GetMetricStatisticsRequest
      {
        Dimensions = new List<Dimension>() { dimension },
        EndTime = DateTime.Today,
        MetricName = "CPUUtilization",
        Namespace = "AWS/EC2",
        // Get statistics by day.
        Period = (int)TimeSpan.FromDays(1).TotalSeconds,
        // Get statistics for the past month.
        StartTime = DateTime.Today.Subtract(TimeSpan.FromDays(30)),
        Statistics = new List<string>() { "Minimum" },
        Unit = StandardUnit.Percent
      };

      var response = client.GetMetricStatistics(request);

      if (response.Datapoints.Count > 0)
      {
        foreach (var point in response.Datapoints)
        {
          Console.WriteLine(point.Timestamp.ToShortDateString() +
            " " + point.Minimum + "%");
        }
      }
      #endregion

      Console.ReadLine();
    }
コード例 #2
0
ファイル: Metrics.cs プロジェクト: srcliq/cliq
        private static GetMetricStatisticsResponse GetCWMetrics(Metric metric, List <string> statisticTypeList, int metricPeriod)
        {
            var clientCW  = new Amazon.CloudWatch.AmazonCloudWatchClient();
            var cwMetrics = clientCW.GetMetricStatistics(new GetMetricStatisticsRequest
            {
                Namespace  = metric.Namespace,
                MetricName = metric.MetricName,
                StartTime  = DateTime.UtcNow.AddDays(-1),
                EndTime    = DateTime.UtcNow,
                Period     = metricPeriod,
                Statistics = statisticTypeList,
                Dimensions = metric.Dimensions
            });

            return(cwMetrics);
        }
コード例 #3
0
        /// <summary>
        /// Given a Profile and Region, return a table with data about bucket sizes,
        /// collected from CloudWatch.
        /// </summary>
        /// <param name="aprofile"></param>
        /// <param name="Region2Scan"></param>
        /// <returns></returns>
        public DataTable S3SizeCloudWatch(string aprofile, string Region2Scan)
        {
            DataTable ToReturn =AWSTables.GetS3SizesTable();
            
            string accountid = GetAccountID(aprofile);
            RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1;
            //Convert the Region2Scan to an AWS Endpoint.
            foreach (var aregion in RegionEndpoint.EnumerableAllRegions)
            {
                if (aregion.DisplayName.Equals(Region2Scan))
                {
                    Endpoint2scan = aregion;
                    continue;
                }
            }

            DataTable metlist = new DataTable();
            try
            {
                var credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile);
                AmazonCloudWatchClient CWClient = new AmazonCloudWatchClient(credential, Endpoint2scan);
                Amazon.CloudWatch.Model.ListMetricsRequest LMReq = new Amazon.CloudWatch.Model.ListMetricsRequest();               

                //Using to explore the metrics
                LMReq.Namespace = "AWS/S3";
                LMReq.MetricName = "BucketSizeBytes";
                var getmetrics = CWClient.ListMetrics(LMReq).Metrics;

                //This is just stuff I used to view data from the List
                metlist.Columns.Add("MetricName");
                metlist.Columns.Add("NameSpace");

                //These are the dimensions for S3.
                metlist.Columns.Add("Bucketname");
                metlist.Columns.Add("StorageType");

                foreach (var ametric in getmetrics)
                {
                    var DR = metlist.NewRow();
                    try
                    {
                        DR["MetricName"] = ametric.MetricName;
                        DR["NameSpace"] = ametric.Namespace;
                        var dim = ametric.Dimensions;
                        //These are the dimensions for S3.
                        DR["BucketName"] = dim[0].Value;
                        DR["StorageType"] =  dim[1].Value;
                        metlist.Rows.Add(DR);
                    }
                    catch(Exception ex)
                    {

                    }
                }

                // Okay, collect the daters for these here buckets




                foreach (var abucket in metlist.AsEnumerable())
                {
                    Amazon.CloudWatch.Model.GetMetricStatisticsRequest GMReq = new Amazon.CloudWatch.Model.GetMetricStatisticsRequest();
                    string bucketname = abucket[2].ToString();
                    string storagetype = abucket[3].ToString();
                    
                    try
                    {
                        Amazon.CloudWatch.Model.Dimension dimbo = new Amazon.CloudWatch.Model.Dimension();
                        dimbo.Name = "BucketName";
                        dimbo.Value = bucketname;
                        GMReq.Dimensions.Add(dimbo);
                        Amazon.CloudWatch.Model.Dimension dimbo2 = new Amazon.CloudWatch.Model.Dimension();
                        dimbo2.Name = "StorageType";
                        dimbo2.Value = storagetype;
                        GMReq.Dimensions.Add(dimbo2);

                        //Build the request:
                        GMReq.Namespace = "AWS/S3";
                        GMReq.EndTime = DateTime.Now;
                        GMReq.StartTime = DateTime.Now - TimeSpan.FromDays(21);
                        GMReq.Period = (60 * 60 * 24 * 7);//Seconds in a week.
                        GMReq.Statistics.Add("Minimum");
                        GMReq.Statistics.Add("Maximum");
                        GMReq.Statistics.Add("Average");
                        GMReq.MetricName = "BucketSizeBytes";

                        //Execute request:
                        var metricresponse = CWClient.GetMetricStatistics(GMReq);
                        
                        //Process Return
                        var dp = metricresponse.Datapoints;
                         if(dp.Count==0)
                        {
                            //none
                        }


                        var arow = ToReturn.NewRow();

                        Dictionary<DateTime, Amazon.CloudWatch.Model.Datapoint> sortem = new Dictionary<DateTime, Amazon.CloudWatch.Model.Datapoint>();
                        foreach(var ap in dp)
                        {
                            sortem.Add(ap.Timestamp, ap);
                        }
                        var sorteddates = sortem.Keys.ToList();
                        sorteddates.Sort();
                        var firstpass = true;
                        foreach(var key in sorteddates)
                        {
                            var ap = sortem[key];
                            var min = ap.Minimum;
                            var max = ap.Maximum;
                            var av = ap.Average;
                            var ts = ap.Timestamp;

                            if (firstpass)
                            {
                                firstpass = false;
                                arow["AccountID"] = accountid;
                                arow["Profile"] = aprofile;
                                arow["Bucket"] = bucketname;
                                arow["Region"] = Region2Scan;
                                arow["StartDate"] = ts.ToShortDateString();
                                arow["EndDate"] = ts.ToShortDateString();
                                arow["StartSizeMin"] =GetFileSize( min);
                                arow["StartSizeMax"] =GetFileSize( max);
                                arow["StartSizeAVG"]= GetFileSize(av);
                            }
                            else
                            {
                                arow["EndDate"] = ts.ToShortDateString();
                                arow["EndSizeMin"] = GetFileSize(min);
                                arow["EndSizeAVG"] = GetFileSize(av);
                                arow["EndSizeMax"] = GetFileSize(max);
                                arow["EndSizeMaxBytes"] = Math.Round(av);

                            }
                        }
                        ToReturn.Rows.Add(arow.ItemArray);
                    }
                    catch(Exception ex)
                    {

                    }
                }


            }
            catch(Exception ex)
            {

            }
            int buckets = metlist.Rows.Count;

            int returning = ToReturn.Rows.Count;
            return ToReturn;
        }