我想知道是否有办法通过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结果但到目前为止没有任何乐趣。有没有人实现过这个目标呢?
由于
答案 0 :(得分: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);
}