View a markdown version of this page

Use GetAlarmMuteRule with an Amazon SDK - Amazon CloudWatch
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use GetAlarmMuteRule with an Amazon SDK

The following code examples show how to use GetAlarmMuteRule.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Get the full configuration of an alarm mute rule, including its schedule, the /// alarms it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. /// </summary> /// <param name="name">The name of the mute rule.</param> /// <returns>The mute rule.</returns> public async Task<GetAlarmMuteRuleResponse> GetAlarmMuteRule(string name) { var response = await _amazonCloudWatch.GetAlarmMuteRuleAsync( new GetAlarmMuteRuleRequest { AlarmMuteRuleName = name }); _logger.LogInformation($"Mute rule {response.Name} is {response.Status}."); return response; }
C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Include the required files.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmMuteRuleStatus.h> #include <aws/monitoring/model/GetAlarmMuteRuleRequest.h> #include <iostream>

Get the alarm mute rule.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::GetAlarmMuteRuleRequest request; request.SetAlarmMuteRuleName(mute_rule_name); auto outcome = cw.GetAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to get alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { const auto &result = outcome.GetResult(); std::cout << "Mute rule " << result.GetName() << " is " << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(result.GetStatus()) << "." << std::endl; std::cout << " ARN: " << result.GetAlarmMuteRuleArn() << std::endl; std::cout << " schedule: " << result.GetRule().GetSchedule().GetExpression() << " for " << result.GetRule().GetSchedule().GetDuration() << std::endl; const auto &alarm_names = result.GetMuteTargets().GetAlarmNames(); if (!alarm_names.empty()) { std::cout << " muted alarms:"; for (const auto &alarm_name : alarm_names) { std::cout << " " << alarm_name; } std::cout << std::endl; } }
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/** * Gets the full configuration of an alarm mute rule, including its schedule, the * alarms it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. * * @param cw the CloudWatch client * @param name the name of the mute rule * @return the mute rule */ public static GetAlarmMuteRuleResponse getAlarmMuteRule(CloudWatchClient cw, String name) { try { GetAlarmMuteRuleResponse response = cw.getAlarmMuteRule(GetAlarmMuteRuleRequest.builder() .alarmMuteRuleName(name) .build()); System.out.printf("Mute rule %s is %s.%n", response.name(), response.statusAsString()); return response; } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); return null; } }
  • For API details, see GetAlarmMuteRule in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import { GetAlarmMuteRuleCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Get the full configuration of an alarm mute rule, including its schedule, the alarms // it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. const run = async () => { const command = new GetAlarmMuteRuleCommand({ AlarmMuteRuleName: process.env.CLOUDWATCH_MUTE_RULE_NAME, // Set CLOUDWATCH_MUTE_RULE_NAME to the name of an existing mute rule. }); try { const response = await client.send(command); console.log(`Mute rule ${response.Name} is ${response.Status}.`); return response; } catch (err) { console.error(err); } }; export default run();
  • For API details, see GetAlarmMuteRule in Amazon SDK for JavaScript API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun getAlarmMuteRule(muteRuleName: String): GetAlarmMuteRuleResponse { CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> val response = cwClient.getAlarmMuteRule( GetAlarmMuteRuleRequest { alarmMuteRuleName = muteRuleName }, ) println("Mute rule ${response.name} is ${response.status?.value}") println(" ARN: ${response.alarmMuteRuleArn}") println(" schedule: ${response.rule?.schedule?.expression} for ${response.rule?.schedule?.duration}") response.muteTargets?.alarmNames?.let { println(" muted alarms: ${it.joinToString(", ")}") } return response } }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

class CloudWatchOTelWrapper: """Encapsulates the OpenTelemetry-oriented Amazon CloudWatch operations.""" def __init__(self, cloudwatch_client): """ :param cloudwatch_client: A Boto3 CloudWatch client. The OpenTelemetry operations are only available on the client interface, not on the higher-level ``boto3.resource("cloudwatch")`` interface. """ self.cloudwatch_client = cloudwatch_client @classmethod def from_client(cls): """ Creates a wrapper backed by a default CloudWatch client. :return: A CloudWatchOTelWrapper. """ return cls(boto3.client("cloudwatch")) def get_alarm_mute_rule(self, name): """ Gets the full configuration of an alarm mute rule, including its schedule, the alarms it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. :param name: The name of the mute rule. :return: The mute rule. """ try: response = self.cloudwatch_client.get_alarm_mute_rule( AlarmMuteRuleName=name ) except ClientError: logger.exception("Couldn't get alarm mute rule %s.", name) raise else: logger.info("Got alarm mute rule %s.", name) return response
  • For API details, see GetAlarmMuteRule in Amazon SDK for Python (Boto3) API Reference.

Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Gets the full configuration of an alarm mute rule, including its schedule, the alarms # it targets, and whether it is currently SCHEDULED, ACTIVE, or EXPIRED. # # @param cloudwatch_client [Aws::CloudWatch::Client] An initialized CloudWatch client. # @param name [String] The name of the mute rule. # @return [Aws::CloudWatch::Types::GetAlarmMuteRuleOutput, nil] The mute rule, or nil on # error. def alarm_mute_rule(cloudwatch_client, name) cloudwatch_client.get_alarm_mute_rule(alarm_mute_rule_name: name) rescue StandardError => e puts "Error getting alarm mute rule: #{e.message}" nil end

For a complete list of Amazon SDK developer guides and code examples, see Using CloudWatch with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.