The Dimension data type further expands on the identity of a metric using a Name, Value pair.

For examples that use one or more dimensions, see PutMetricData.

        public static List<CloudWatchResultRecord> GetCpuUtilization(string instanceId, int minutesBack)
        {
            var cloudWatchClient = CloudWatchConnectionProvider.GetCloudWatchConnection();

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

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

            var currentTime = DateTime.UtcNow;
            var startTime = currentTime.AddDays(-1);
            request.StartTime = startTime;
            request.EndTime = currentTime;

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

            var response = cloudWatchClient.GetMetricStatistics(request);

            if (response.GetMetricStatisticsResult.Datapoints.Count > 0)
            {
                var result = new List<CloudWatchResultRecord>();
                int counter = 0;

                foreach(var dataPoint in response.GetMetricStatisticsResult.Datapoints)
                {
                    result.Add(new CloudWatchResultRecord()
                    {
                        RecordId = counter,
                        CpuAverage = dataPoint.Average
                    });
                }

                return result;
            }
            else
            {
                return null;
            }
        }
    public static void CWGetMetricStatistics()
    {
      #region CWGetMetricStatistics
      var client = new AmazonCloudWatchClient();

      var dimension = new Dimension
      {
        Name = "InstanceType",
        Value = "t1.micro"
      };

      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();
    }
        public void TrackEvent(string eventName, TimeSpan elapsed, IDictionary <string, string> properties = null)
        {
            var dimension = new Amazon.CloudWatch.Model.Dimension
            {
                Name  = eventName,
                Value = eventName
            };

            var metric1 = new Amazon.CloudWatch.Model.MetricDatum
            {
                Dimensions = new List <Amazon.CloudWatch.Model.Dimension> {
                    dimension
                },
                MetricName      = eventName,
                StatisticValues = new Amazon.CloudWatch.Model.StatisticSet(),
                Timestamp       = DateTime.Today,
                Unit            = StandardUnit.Count
            };

            if (properties?.ContainsKey("value") == true)
            {
                metric1.Value = long.Parse(properties["value"]);
            }
            else
            {
                metric1.Value = 1;
            }

            var request = new Amazon.CloudWatch.Model.PutMetricDataRequest
            {
                MetricData = new List <Amazon.CloudWatch.Model.MetricDatum>()
                {
                    metric1
                },
                Namespace = eventName
            };

            _telemetryClient.PutMetricDataAsync(request).Wait();
        }
        protected override void LocalParse(ref List<Match>.Enumerator tokens, string sNum)
        {
            if (!tokens.MoveNext())
                return;

            string name, value;
            if (!string.IsNullOrEmpty(tokens.Current.Groups["lparen"].Value))
            {
                tokens.MoveNext();

                while (tokens.Current != null &&
                       string.IsNullOrEmpty(tokens.Current.Groups["rparen"].Value))
                {
                    if (
                        string.IsNullOrEmpty(
                            name = tokens.Current.Groups["name"].Value.Split(new[] { ':' })[0]))
                    {
                        tokens.MoveNext();
                        continue;
                    }

                    if (!tokens.MoveNext())
                        continue;

                    if (string.IsNullOrEmpty(value = tokens.Current.Groups["word"].Value) &&
                        string.IsNullOrEmpty(sNum = tokens.Current.Groups["float"].Value))
                    {
                        tokens.MoveNext();
                        continue;
                    }

                    _dimensions[name] = new Dimension { Name = name, Value = string.IsNullOrEmpty(sNum) ? value : sNum };
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(tokens.Current.Groups["lparen"].Value))
                    tokens.MoveNext();

                if (
                    string.IsNullOrEmpty(
                        name = tokens.Current.Groups["name"].Value.Split(new[] { ':' })[0]))
                {
                    tokens.MoveNext();
                    return;
                }

                if (!tokens.MoveNext())
                    return;

                if (string.IsNullOrEmpty(value = tokens.Current.Groups["word"].Value))
                {
                    tokens.MoveNext();
                    return;
                }

                _dimensions[name] = new Dimension { Name = name, Value = string.IsNullOrEmpty(sNum) ? value : sNum };
            }
        }
Beispiel #5
0
    public static void CWDescribeAlarmsForMetric()
    {
      #region CWDescribeAlarmsForMetric
      var client = new AmazonCloudWatchClient();

      var dimension = new Dimension
      {
        Name = "AutoScalingGroupName",
        Value = "awseb-e-kkbEXAMPLE-stack-AutoScalingGroup-F4TAUEXAMPLE"
      };

      var request = new DescribeAlarmsForMetricRequest
      {
        Dimensions = new List<Dimension>() { dimension },
        MetricName = "NetworkOut",
        Namespace = "AWS/EC2"
      };

      var response = client.DescribeAlarmsForMetric(request);

      if (response.MetricAlarms.Count > 0)
      {
        foreach (var alarm in response.MetricAlarms)
        {
          Console.WriteLine();
          Console.WriteLine(alarm.AlarmName);
          Console.WriteLine(alarm.AlarmDescription);
          Console.WriteLine(alarm.MetricName + " " +
            alarm.ComparisonOperator + " " + alarm.Threshold);
        }
      }
      else
      {
        Console.WriteLine("No alarms.");
      }
      #endregion

      Console.ReadLine();
    }
Beispiel #6
0
    public static void CWPutMetricAlarm()
    {
      #region CWPutMetricAlarm
      var client = new AmazonCloudWatchClient();

      var dimension = new Dimension
      {
        Name = "Desktop Machine Metrics",
        Value = "Virtual Desktop Machine Usage"
      };

      var request = new PutMetricAlarmRequest
      {
        ActionsEnabled = true,
        AlarmActions = new List<string>() 
          { "arn:aws:sns:us-east-1:80398EXAMPLE:CodingTestResults" },
        AlarmDescription = "Too many instances offline",
        AlarmName = "Offline Instances",
        ComparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold,
        Dimensions = new List<Dimension>() { dimension },
        EvaluationPeriods = 1,
        MetricName = "Desktop Machines Offline",
        Namespace = "Example.com Custom Metrics",
        Period = (int)TimeSpan.FromMinutes(5).TotalSeconds,
        Statistic = new Statistic("Maximum"),
        Threshold = 5,
        Unit = StandardUnit.Count
      };

      client.PutMetricAlarm(request);
      #endregion
    }
 private void AddDimension(Dimension value)
 {
     _dimensions[value.Name] = value;
 }
Beispiel #8
-1
    public static void CWPutMetricData()
    {
      #region CWPutMetricData
      var client = new AmazonCloudWatchClient();

      var dimension = new Dimension
      {
        Name = "Desktop Machine Metrics",
        Value = "Virtual Desktop Machine Usage"
      };

      var metric1 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Online",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 14
      };

      var metric2 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Offline",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 7
      };

      var metric3 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Online",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 12
      };

      var metric4 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Offline",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 9
      };

      var request = new PutMetricDataRequest
      {
        MetricData = new List<MetricDatum>() { metric1, metric2, 
          metric3, metric4 },
        Namespace = "Example.com Custom Metrics"
      };

      client.PutMetricData(request);
      #endregion
    }