1

我想知道是否有办法通过 AmazonEC2 API 查看 ec2 实例的警报状态?

警报状态

我们已经实施了一个有效的解决方案来使用 AmazonCloudWatch.GetMetricStatistics 方法获取特定的指标值:

var reg = new GetMetricStatisticsRequest()
            {
                MetricName = metric,
                Period = 60 * 30,
                Statistics = new List<string> { AVERAGE },
                Dimensions = dimensions,
                Namespace = "AWS/EC2",
                EndTime = DateTime.Now.ToUniversalTime(),
                StartTime = DateTime.Parse(startTime)
            };

data.Columns.Add(AVERAGE);
data.Columns.Add(TIMESTAMP);

var points = Service.GetMetricStatistics(reg).GetMetricStatisticsResult.Datapoints.OrderBy(p => p.Timestamp);

但是直接检查警报状态会更有用。

我们已经迭代了 AmazonEC2.DescribeInstancesResponse 结果,但到目前为止还没有任何乐趣。有没有人做到这一点?

谢谢

4

1 回答 1

2

为了获取警报的状态,您可以使用 AmazonCloudWatch.DescribeAlarms 方法。这是一个关于如何获取警报状态的代码示例(在 US-West-2 区域):

        var cloudWatchClient = AWSClientFactory.CreateAmazonCloudWatchClient(RegionEndpoint.USWest2);
        var describeAlarmsResponse = cloudWatchClient.DescribeAlarms(new DescribeAlarmsRequest
        {
            AlarmNames = { "the_name_of_your_alarm" }
        });
        var describeAlarmsResult = describeAlarmsResponse.DescribeAlarmsResult;
        foreach (var alarm in describeAlarmsResult.MetricAlarms)
        {
            Console.WriteLine("Alarm State = " + alarm.StateValue);
            Console.WriteLine("Alarm State Reason = " + alarm.StateReason);
            Console.WriteLine("Alarm JSON Data = " + alarm.StateReasonData);
        }
于 2012-11-07T17:42:14.307 回答