CloudWatch 使用示例Amazon SDK for .NET - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CloudWatch 使用示例Amazon SDK for .NET

以下代码示例显示如何通过Amazon SDK for .NET和 CloudWatch.

操作代码节选 CloudWatch 函数。

方案展示如何通过调用多个来完成特定任务的代码示例,这些示例介绍如何通过调用多个 CloudWatch 函数。

每个示例都包含一个指向 GitHub,其中包含了有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示如何删除 Amazon CloudWatch 警报。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example shows how to delete Amazon CloudWatch alarms. The example /// was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DeleteAlarms { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var alarmNames = CreateAlarmNameList(); await DeleteAlarmsAsyncExample(cwClient, alarmNames); } /// <summary> /// Delete the alarms whose names are listed in the alarmNames parameter. /// </summary> /// <param name="client">The initialized Amazon CloudWatch client.</param> /// <param name="alarmNames">A list of names for the alarms to be /// deleted.</param> public static async Task DeleteAlarmsAsyncExample(IAmazonCloudWatch client, List<string> alarmNames) { var request = new DeleteAlarmsRequest { AlarmNames = alarmNames, }; try { var response = await client.DeleteAlarmsAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Alarms successfully deleted:"); alarmNames .ForEach(name => Console.WriteLine($"{name}")); } } catch (ResourceNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); } } /// <summary> /// Defines and returns the list of alarm names to delete. /// </summary> /// <returns>A list of alarm names.</returns> public static List<string> CreateAlarmNameList() { // The list of alarm names passed to DeleteAlarmsAsync // can contain up to 100 alarm names. var theList = new List<string> { "ALARM_NAME_1", "ALARM_NAME_2", }; return theList; } }
  • 有关详细信息,请参阅。DeleteAlarmsAmazon SDK for .NETAPI 参考.

以下代码示例显示了如何描述 Amazon CloudWatch 告告历史记录。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

获取出来的列表,列出的 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)); } }

以下代码示例显示如何禁用Amazon CloudWatch 警警警报报报报

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example shows how to disable the Amazon CloudWatch actions for /// one or more CloudWatch alarms. The example was created using the /// AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DisableAlarmActions { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var alarmNames = new List<string> { "ALARM_NAME", "ALARM_NAME_2", }; var success = await DisableAlarmsActionsAsync(cwClient, alarmNames); if (success) { Console.WriteLine("Alarm action(s) successfully disabled."); } else { Console.WriteLine("Alarm action(s) were not disabled.") } } /// <summary> /// Disable the actions for the list of CloudWatch alarm names passed /// in the alarmNames parameter. /// </summary> /// <param name="client">An initialized CloudWatch client object.</param> /// <param name="alarmNames">The list of CloudWatch alarms to disable.</param> /// <returns>A Boolean value indicating the success of the call.</returns> public static async Task<bool> DisableAlarmsActionsAsync( IAmazonCloudWatch client, List<string> alarmNames) { var request = new DisableAlarmActionsRequest { AlarmNames = alarmNames, }; var response = await client.DisableAlarmActionsAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } }

以下代码示例显示如何启用 Amazon CloudWatch 警警警报报报报

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example shows how to enable the Amazon CloudWatch actions for /// one or more CloudWatch alarms. The example was created using the /// AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class EnableAlarmActions { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var alarmNames = new List<string> { "ALARM_NAME", "ALARM_NAME_2", }; var success = await EnableAlarmActionsAsync(cwClient, alarmNames); if (success) { Console.WriteLine("Alarm action(s) successfully enabled."); } else { Console.WriteLine("Alarm action(s) were not enabled.") } } /// <summary> /// Enable the actions for the list of CloudWatch alarm names passed /// in the alarmNames parameter. /// </summary> /// <param name="client">An initialized CloudWatch client object.</param> /// <param name="alarmNames">The list of CloudWatch alarms to enable.</param> /// <returns>A Boolean value indicating the success of the call.</returns> public static async Task<bool> EnableAlarmActionsAsync(IAmazonCloudWatch client, List<string> alarmNames) { var request = new EnableAlarmActionsRequest { AlarmNames = alarmNames, }; var response = await client.EnableAlarmActionsAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } }

以下代码示例演示了如何获取 Amazon CloudWatch 控制面板。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example shows how to retrieve the details of an Amazon CloudWatch /// dashboard. The return value from the call to GetDashboard is a json /// object representing the widgets in the dashboard. The example was /// created using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class GetDashboard { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); string dashboardName = "CloudWatch-Default"; var body = await GetDashboardAsync(cwClient, dashboardName); Console.WriteLine(body); } /// <summary> /// Get the json that represents the dashboard. /// </summary> /// <param name="client">An initialized CloudWatch client.</param> /// <param name="dashboardName">The name of the dashboard.</param> /// <returns>The string containing the json value describing the /// contents and layout of the CloudWatch dashboard.</returns> public static async Task<string> GetDashboardAsync(IAmazonCloudWatch client, string dashboardName) { var request = new GetDashboardRequest { DashboardName = dashboardName, }; var response = await client.GetDashboardAsync(request); return response.DashboardBody; } }
  • 有关详细信息,请参阅。GetDashboardAmazon SDK for .NETAPI 参考.

以下代码示例显示如何列出 Amazon CloudWatch 控制面板。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// Shows how to retrieve a list of Amazon CloudWatch dashboards. This /// example was written using AWSSDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class ListDashboards { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var dashboards = await ListDashboardsAsync(cwClient); DisplayDashboardList(dashboards); } /// <summary> /// Get the list of available dashboards. /// </summary> /// <param name="client">The initialized CloudWatch client used to /// retrieve a list of defined dashboards.</param> /// <returns>A list of DashboardEntry objects.</returns> public static async Task<List<DashboardEntry>> ListDashboardsAsync(IAmazonCloudWatch client) { var response = await client.ListDashboardsAsync(new ListDashboardsRequest()); return response.DashboardEntries; } /// <summary> /// Displays the name of each CloudWatch Dashboard in the list passed /// to the method. /// </summary> /// <param name="dashboards">A list of DashboardEntry objects.</param> public static void DisplayDashboardList(List<DashboardEntry> dashboards) { if (dashboards.Count > 0) { Console.WriteLine("The following dashboards are defined:"); foreach (var dashboard in dashboards) { Console.WriteLine($"Name: {dashboard.DashboardName} Last modified: {dashboard.LastModified}"); } } else { Console.WriteLine("No dashboards found."); } } }
  • 有关详细信息,请参阅。ListDashboardsAmazon SDK for .NETAPI 参考.

以下代码示例显示如何列出 Amazon CloudWatch 指标。

Amazon SDK for .NET
提示

要了解如何设置和运行此示例,请参阅GitHub.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example demonstrates how to list metrics for Amazon CloudWatch. /// The example was created using the AWS SDK for .NET version 3.7 and /// .NET Core 5.0. /// </summary> public class ListMetrics { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var filter = new DimensionFilter { Name = "InstanceType", Value = "t1.micro", }; string metricName = "CPUUtilization"; string namespaceName = "AWS/EC2"; await ListMetricsAsync(cwClient, filter, metricName, namespaceName); } /// <summary> /// Retrieve CloudWatch metrics using the supplied filter, metrics name, /// and namespace. /// </summary> /// <param name="client">An initialized CloudWatch client.</param> /// <param name="filter">The filter to apply in retrieving metrics.</param> /// <param name="metricName">The metric name for which to retrieve /// information.</param> /// <param name="nameSpaceName">The name of the namespace from which /// to retrieve metric information.</param> public static async Task ListMetricsAsync( IAmazonCloudWatch client, DimensionFilter filter, string metricName, string nameSpaceName) { var request = new ListMetricsRequest { Dimensions = new List<DimensionFilter>() { filter }, MetricName = metricName, Namespace = nameSpaceName, }; var response = new ListMetricsResponse(); do { response = await client.ListMetricsAsync(request); if (response.Metrics.Count > 0) { foreach (var metric in response.Metrics) { Console.WriteLine(metric.MetricName + " (" + metric.Namespace + ")"); foreach (var dimension in metric.Dimensions) { Console.WriteLine(" " + dimension.Name + ": " + dimension.Value); } } } else { Console.WriteLine("No metrics found."); } request.NextToken = response.NextToken; } while (!string.IsNullOrEmpty(response.NextToken)); } }
  • 有关详细信息,请参阅。ListMetricsAmazon SDK for .NETAPI 参考.