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

使用 Amazon 开发工具包禁用 CloudWatch 告警操作

以下代码示例展示如何禁用 Amazon CloudWatch 告警操作。

.NET
Amazon SDK for .NET

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; } }
  • GitHub 中查找说明和更多代码。

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

Java
SDK for Java 2.x

public static void disableActions(CloudWatchClient cw, String alarmName) { try { DisableAlarmActionsRequest request = DisableAlarmActionsRequest.builder() .alarmNames(alarmName) .build(); cw.disableAlarmActions(request); System.out.printf( "Successfully disabled actions on alarm %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考中的 DisableAlarmActions

JavaScript
SDK for JavaScript V3

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DisableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmNames: "ALARM_NAME" }; // e.g., "Web_Server_CPU_Utilization" export const run = async () => { try { const data = await cwClient.send(new DisableAlarmActionsCommand(params)); console.log("Success, alarm disabled:", data); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2

导入开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.disableAlarmActions({AlarmNames: ['Web_Server_CPU_Utilization']}, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

suspend fun disableActions(alarmName: String) { val request = DisableAlarmActionsRequest { alarmNames = listOf(alarmName) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.disableAlarmActions(request) println("Successfully disabled actions on alarm ${alarmName}") } }
Python
适用于 Python (Boto3) 的 SDK

class CloudWatchWrapper: """Encapsulates Amazon CloudWatch functions.""" def __init__(self, cloudwatch_resource): """ :param cloudwatch_resource: A Boto3 CloudWatch resource. """ self.cloudwatch_resource = cloudwatch_resource def enable_alarm_actions(self, alarm_name, enable): """ Enables or disables actions on the specified alarm. Alarm actions can be used to send notifications or automate responses when an alarm enters a particular state. :param alarm_name: The name of the alarm. :param enable: When True, actions are enabled for the alarm. Otherwise, they disabled. """ try: alarm = self.cloudwatch_resource.Alarm(alarm_name) if enable: alarm.enable_actions() else: alarm.disable_actions() logger.info( "%s actions for alarm %s.", "Enabled" if enable else "Disabled", alarm_name) except ClientError: logger.exception( "Couldn't %s actions alarm %s.", "enable" if enable else "disable", alarm_name) raise

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