private static void UnmarshallResult(XmlUnmarshallerContext context, GetMetricStatisticsResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("Datapoints/member", targetDepth))
                    {
                        var unmarshaller = DatapointUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.Datapoints.Add(item);
                        continue;
                    }
                    if (context.TestExpression("Label", targetDepth))
                    {
                        var unmarshaller = StringUnmarshaller.Instance;
                        response.Label = unmarshaller.Unmarshall(context);
                        continue;
                    }
                }
            }

            return;
        }
Пример #2
0
        /// <summary>
        /// <para> Gets statistics for the specified metric. </para>
        /// <para><b>NOTE:</b> The maximum number of data points returned from a
        /// single GetMetricStatistics request is 1,440. If a request is made that
        /// generates more than 1,440 data points, Amazon CloudWatch returns an
        /// error. In such a case, alter the request by narrowing the specified
        /// time range or increasing the specified period. Alternatively, make
        /// multiple requests across adjacent time ranges. </para> <para> Amazon
        /// CloudWatch aggregates data points based on the length of the
        /// <c>period</c> that you specify. For example, if you request statistics
        /// with a one-minute granularity, Amazon CloudWatch aggregates data
        /// points with time stamps that fall within the same one-minute period.
        /// In such a case, the data points queried can greatly outnumber the data
        /// points returned. </para> <para><b>NOTE:</b> The maximum number of data
        /// points that can be queried is 50,850; whereas the maximum number of
        /// data points returned is 1,440. </para> <para> The following examples
        /// show various statistics allowed by the data point query maximum of
        /// 50,850 when you call <c>GetMetricStatistics</c> on Amazon EC2
        /// instances with detailed (one-minute) monitoring enabled: </para>
        /// <ul>
        /// <li>Statistics for up to 400 instances for a span of one hour</li>
        /// <li>Statistics for up to 35 instances over a span of 24 hours</li>
        /// <li>Statistics for up to 2 instances over a span of 2 weeks</li>
        ///
        /// </ul>
        /// </summary>
        ///
        /// <param name="getMetricStatisticsRequest">Container for the necessary
        ///           parameters to execute the GetMetricStatistics service method on
        ///           AmazonCloudWatch.</param>
        ///
        /// <returns>The response from the GetMetricStatistics service method, as
        ///         returned by AmazonCloudWatch.</returns>
        ///
        /// <exception cref="InvalidParameterValueException"/>
        /// <exception cref="InternalServiceException"/>
        /// <exception cref="InvalidParameterCombinationException"/>
        /// <exception cref="MissingRequiredParameterException"/>
        public GetMetricStatisticsResponse GetMetricStatistics(GetMetricStatisticsRequest getMetricStatisticsRequest)
        {
            IRequest <GetMetricStatisticsRequest> request  = new GetMetricStatisticsRequestMarshaller().Marshall(getMetricStatisticsRequest);
            GetMetricStatisticsResponse           response = Invoke <GetMetricStatisticsRequest, GetMetricStatisticsResponse> (request, this.signer, GetMetricStatisticsResponseUnmarshaller.GetInstance());

            return(response);
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            GetMetricStatisticsResponse response = new GetMetricStatisticsResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("GetMetricStatisticsResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
Пример #4
0
        public static void SaveCWMetrics(Metric metric, GetMetricStatisticsResponse cwMetrics, Instance instance, List <string> statisticTypeList)
        {
            var    instanceId   = (instance != null) ? instance.InstanceId : null;
            var    instanceType = (instance != null) ? instance.InstanceType : null;
            string path         = string.Format("CWMetrics{0}.csv", DateTime.Now.ToString("MMddyyyy"));

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    var header = "instanceid,namekey,value,metricstimestamp,createddate,instancetype,unit,statistic,namespace";
                    sw.WriteLine(header);
                    sw.Flush();
                }
            }
            using (var sw = File.AppendText(path))
            {
                foreach (var dataPoint in cwMetrics.Datapoints)
                {
                    foreach (var statisticType in statisticTypeList)
                    {
                        var line = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}", instanceId, metric.MetricName, Convert.ToInt32(Common.GetPropertyValue(dataPoint, statisticType)), dataPoint.Timestamp, DateTime.UtcNow, instanceType, dataPoint.Unit.Value, statisticType, metric.Namespace);
                        sw.WriteLine(line);
                        sw.Flush();
                    }
                }
            }
        }
Пример #5
0
        public void TestPutMetricData()
        {
            string metricName = "DotNetSDKTestMetric";
            string nameSpace  = "AWS-EC2";

            List <MetricDatum> data = new List <MetricDatum>();

            DateTime ts = DateTime.Now;

            data.Add(new MetricDatum()
            {
                MetricName = metricName,
                Timestamp  = ts.AddHours(-1),
                Unit       = "None",
                Value      = 23.0
            });

            data.Add(new MetricDatum()
            {
                MetricName = metricName,
                Timestamp  = ts,
                Unit       = "None",
                Value      = 21.0
            });

            data.Add(new MetricDatum()
            {
                MetricName = metricName,
                Timestamp  = DateTime.Now.AddHours(1),
                Unit       = "None",
                Value      = 22.0
            });

            Client.PutMetricData(new PutMetricDataRequest()
            {
                Namespace  = nameSpace,
                MetricData = data
            });

            Thread.Sleep(1 * 1000);
            GetMetricStatisticsResponse gmsRes = Client.GetMetricStatistics(new GetMetricStatisticsRequest()
            {
                Namespace  = nameSpace,
                MetricName = metricName,
                StartTime  = ts.AddDays(-1),
                Period     = 60 * 60,
                Statistics = new List <string> {
                    "Minimum", "Maximum"
                },
                EndTime = ts.AddDays(1)
            });

            Assert.IsTrue(gmsRes.Datapoints.Count > 0);
        }
Пример #6
0
        public void UseDoubleForData()
        {
            var currentCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE", false);
            try
            {
                var data = new List <MetricDatum>
                {
                    new MetricDatum()
                    {
                        MetricName = "SDK-TEST-CloudWatchAppender",
                        Unit       = "Seconds",
                        Timestamp  = DateTime.Now,
                        Value      = 1.1
                    }
                };

                Client.PutMetricData(new PutMetricDataRequest()
                {
                    Namespace  = "SDK-TEST-CloudWatchAppender",
                    MetricData = data
                });

                Thread.Sleep(1000);
                GetMetricStatisticsResponse gmsRes = Client.GetMetricStatistics(new GetMetricStatisticsRequest()
                {
                    Namespace  = "SDK-TEST-CloudWatchAppender",
                    MetricName = "SDK-TEST-CloudWatchAppender",
                    StartTime  = DateTime.Today.AddDays(-2),
                    Period     = 60 * 60,
                    Statistics = new List <string> {
                        "Maximum"
                    },
                    EndTime = DateTime.Today.AddDays(2)
                });

                bool found = false;
                foreach (var dp in gmsRes.Datapoints)
                {
                    if (dp.Maximum == 1.1)
                    {
                        found = true;
                        break;
                    }
                }
                Assert.IsTrue(found, "Did not found the 1.1 value");
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
            }
        }
Пример #7
0
        public static void SaveCWMetrics(Metric metric, GetMetricStatisticsResponse cwMetrics, Instance instance, List <string> statisticTypeList)
        {
            DataSet   ds = new DataSet();
            DataTable dt = new DataTable();

            var server             = "cloudiq-dw-dev.cne8vdl5tict.us-west-2.redshift.amazonaws.com";
            var port               = "5439";
            var masterUsername     = "******";
            var masterUserPassword = "******";
            var DBName             = "cloudiqdwdev";
            var instanceId         = (instance != null) ? instance.InstanceId : null;
            var instanceType       = (instance != null) ? instance.InstanceType : null;
            var query              = new StringBuilder("insert into factusagemetrics(instanceid, namekey, value, metricstimestamp, createddate, instancetype, unit, statistic, namespace) values ");

            foreach (var dataPoint in cwMetrics.Datapoints)
            {
                foreach (var statisticType in statisticTypeList)
                {
                    query.Append(string.Format("('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}'),", instanceId, metric.MetricName, Convert.ToInt32(Common.GetPropertyValue(dataPoint, statisticType)), dataPoint.Timestamp, DateTime.UtcNow, instanceType, dataPoint.Unit.Value, statisticType, metric.Namespace));
                }
            }
            try
            {
                if (cwMetrics.Datapoints.Count > 0)
                {
                    // Create the ODBC connection string.
                    //Redshift ODBC Driver - 64 bits
                    string connString = "Driver={Amazon Redshift (x64)};" +
                                        String.Format("Server={0};Database={1};" +
                                                      "UID={2};PWD={3};Port={4};SSL=true;Sslmode=Require",
                                                      server, DBName, masterUsername,
                                                      masterUserPassword, port);

                    // Make a connection using the psqlODBC provider.
                    OdbcConnection conn = new OdbcConnection(connString);
                    conn.Open();

                    // Try a simple query.
                    string          sql = query.ToString().TrimEnd(',');
                    OdbcDataAdapter da  = new OdbcDataAdapter();
                    da.InsertCommand = new OdbcCommand(sql, conn);
                    var recordCount = da.InsertCommand.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
Пример #8
0
        protected long GetConsumedCapacityUnits(string table_name, string index_name, string metric_name, int look_back_minutes)
        {
            List <Dimension> dimensions = new List <Dimension> {
                new Dimension {
                    Name = "TableName", Value = table_name
                }
            };

            if (!string.IsNullOrWhiteSpace(index_name))
            {
                dimensions.Add(new Dimension {
                    Name = "GlobalSecondaryIndexName", Value = index_name
                });
            }

            DateTime end_time   = DateTime.UtcNow.AddMinutes(-5);
            DateTime start_time = end_time.AddMinutes(-look_back_minutes);

            AmazonCloudWatchClient amazon_cloud_watch_client = AWS.GetAmazonCloudWatchClient();

            GetMetricStatisticsRequest get_metric_statistics_request = new GetMetricStatisticsRequest
            {
                Namespace  = "AWS/DynamoDB",
                Dimensions = dimensions,
                MetricName = metric_name,
                StartTime  = start_time,
                EndTime    = end_time,
                Period     = 300,
                Statistics = new List <string> {
                    "Sum"
                }
            };

            GetMetricStatisticsResponse get_metric_statistics_response = amazon_cloud_watch_client.GetMetricStatistics(get_metric_statistics_request);

            long consumed_capacity_units = 0;

            if (get_metric_statistics_response != null && get_metric_statistics_response.Datapoints.Any())
            {
                double total_seconds = (end_time - start_time).TotalSeconds;
                double total_sum     = get_metric_statistics_response.Datapoints.Sum(datapoint => datapoint.Sum);
                consumed_capacity_units = (long)Math.Ceiling(total_sum / total_seconds);
            }

            return(consumed_capacity_units);
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            GetMetricStatisticsResponse response = new GetMetricStatisticsResponse();

            while (context.Read())
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("GetMetricStatisticsResult", 2))
                    {
                        response.GetMetricStatisticsResult = GetMetricStatisticsResultUnmarshaller.GetInstance().Unmarshall(context);
                        continue;
                    }
                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.GetInstance().Unmarshall(context);
                    }
                }
            }


            return(response);
        }
Пример #10
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetMetricStatisticsResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("Label", targetDepth))
                    {
                        response.Label = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("Datapoints/member", targetDepth))
                    {
                        response.Datapoints.Add(DatapointUnmarshaller.GetInstance().Unmarshall(context));

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }



            return;
        }