使用 Amazon 开发工具包描述 CloudWatch 告警的历史记录 - Amazon CloudWatch
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包描述 CloudWatch 告警的历史记录

以下代码示例展示如何描述 Amazon CloudWatch 告警的历史记录。

.NET
Amazon SDK for .NET

获取 CloudWatch 告警列表,然后检索每个告警的历史记录。

using System; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example retrieves a list of Amazon CloudWatch alarms and, for /// each one, displays its history. The example was created using the /// AWS SDK for .NET 3.7 and .NET Core 5.0. /// </summary> public class DescribeAlarmHistories { /// <summary> /// Retrieves a list of alarms and then passes each name to the /// DescribeAlarmHistoriesAsync method to retrieve its history. /// </summary> public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var response = await cwClient.DescribeAlarmsAsync(); foreach (var alarm in response.MetricAlarms) { await DescribeAlarmHistoriesAsync(cwClient, alarm.AlarmName); } } /// <summary> /// Retrieves the CloudWatch alarm history for the alarm name passed /// to the method. /// </summary> /// <param name="client">An initialized CloudWatch client object.</param> /// <param name="alarmName">The CloudWatch alarm for which to retrieve /// history information.</param> public static async Task DescribeAlarmHistoriesAsync(IAmazonCloudWatch client, string alarmName) { var request = new DescribeAlarmHistoryRequest { AlarmName = alarmName, EndDateUtc = DateTime.Today, HistoryItemType = HistoryItemType.Action, MaxRecords = 1, StartDateUtc = DateTime.Today.Subtract(TimeSpan.FromDays(30)), }; var response = new DescribeAlarmHistoryResponse(); do { response = await client.DescribeAlarmHistoryAsync(request); foreach (var item in response.AlarmHistoryItems) { Console.WriteLine(item.AlarmName); Console.WriteLine(item.HistorySummary); Console.WriteLine(); } request.NextToken = response.NextToken; } while (!string.IsNullOrEmpty(response.NextToken)); } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 DescribeAlarmHistory

有关 Amazon 开发工具包开发人员指南和代码示例的完整列表,包括帮助入门和有关早期版本的信息,请参阅 将 CloudWatch 与 Amazon 开发工具包结合使用