View a markdown version of this page

Use ListAlarmMuteRules with an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use ListAlarmMuteRules with an AWS SDK

The following code examples show how to use ListAlarmMuteRules.

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
SDK for .NET (v4)
Note

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

/// <summary> /// List the alarm mute rules in the account, optionally filtered to the rules that /// target one alarm. /// </summary> /// <param name="alarmName">When specified, only rules that target this alarm are /// returned.</param> /// <returns>The list of mute rule summaries.</returns> public async Task<List<AlarmMuteRuleSummary>> ListAlarmMuteRules(string? alarmName = null) { var results = new List<AlarmMuteRuleSummary>(); string? nextToken = null; do { var response = await _amazonCloudWatch.ListAlarmMuteRulesAsync( new ListAlarmMuteRulesRequest { AlarmName = alarmName, NextToken = nextToken }); if (response.AlarmMuteRuleSummaries != null) { results.AddRange(response.AlarmMuteRuleSummaries); } nextToken = response.NextToken; } while (!string.IsNullOrEmpty(nextToken)); _logger.LogInformation($"Got {results.Count} alarm mute rules."); return results; }
C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS 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/ListAlarmMuteRulesRequest.h> #include <iostream>

List the alarm mute rules.

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::ListAlarmMuteRulesRequest request; if (argc == 2) { request.SetAlarmName(argv[1]); } bool done = false; while (!done) { auto outcome = cw.ListAlarmMuteRules(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to list alarm mute rules: " << outcome.GetError().GetMessage() << std::endl; break; } for (const auto &summary : outcome.GetResult().GetAlarmMuteRuleSummaries()) { std::cout << summary.GetAlarmMuteRuleArn() << " (" << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(summary.GetStatus()) << ")" << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }
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 AWS Code Examples Repository.

/** * Lists the alarm mute rules in the account, optionally filtered to the rules that * target one alarm. * * @param cw the CloudWatch client * @param alarmName when non-null, only rules that target this alarm are returned * @return the list of mute rule summaries */ public static List<AlarmMuteRuleSummary> listAlarmMuteRules(CloudWatchClient cw, String alarmName) { List<AlarmMuteRuleSummary> summaries = new ArrayList<>(); try { String nextToken = null; do { ListAlarmMuteRulesRequest request = ListAlarmMuteRulesRequest.builder() .alarmName(alarmName) .nextToken(nextToken) .build(); ListAlarmMuteRulesResponse response = cw.listAlarmMuteRules(request); summaries.addAll(response.alarmMuteRuleSummaries()); nextToken = response.nextToken(); } while (nextToken != null && !nextToken.isEmpty()); for (AlarmMuteRuleSummary summary : summaries) { System.out.printf("%s (%s)%n", summary.alarmMuteRuleArn(), summary.statusAsString()); } return summaries; } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); return summaries; } }
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 AWS Code Examples Repository.

import { ListAlarmMuteRulesCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // List the alarm mute rules in the account. Filter by the alarm they target, by // status, or both. const run = async () => { const summaries = []; let nextToken; try { do { const command = new ListAlarmMuteRulesCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set CLOUDWATCH_ALARM_NAME to filter to rules targeting one alarm. Statuses: ["SCHEDULED", "ACTIVE"], // Valid values: SCHEDULED, ACTIVE, EXPIRED. NextToken: nextToken, }); const response = await client.send(command); summaries.push(...(response.AlarmMuteRuleSummaries ?? [])); nextToken = response.NextToken; } while (nextToken); for (const summary of summaries) { console.log(`${summary.AlarmMuteRuleArn} (${summary.Status})`); } return summaries; } catch (err) { console.error(err); } }; export default run();
Kotlin
SDK for Kotlin
Note

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

suspend fun listAlarmMuteRules(alarmNameVal: String? = null): List<AlarmMuteRuleSummary> { val summaries = mutableListOf<AlarmMuteRuleSummary>() CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> var token: String? = null do { val response = cwClient.listAlarmMuteRules( ListAlarmMuteRulesRequest { alarmName = alarmNameVal nextToken = token }, ) response.alarmMuteRuleSummaries?.let { summaries.addAll(it) } token = response.nextToken } while (token != null) summaries.forEach { summary -> println("${summary.alarmMuteRuleArn} (${summary.status?.value})") } } return summaries }
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 AWS 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 list_alarm_mute_rules(self, alarm_name=None, statuses=None): """ Lists alarm mute rules in the account. :param alarm_name: When specified, only rules that target this alarm are returned. :param statuses: When specified, only rules in these statuses are returned. Valid values are 'SCHEDULED', 'ACTIVE', and 'EXPIRED'. :return: The list of mute rule summaries. """ summaries = [] try: next_token = None while True: kwargs = {} if alarm_name is not None: kwargs["AlarmName"] = alarm_name if statuses is not None: kwargs["Statuses"] = statuses if next_token is not None: kwargs["NextToken"] = next_token response = self.cloudwatch_client.list_alarm_mute_rules(**kwargs) summaries.extend(response.get("AlarmMuteRuleSummaries", [])) next_token = response.get("NextToken") if not next_token: break except ClientError: logger.exception("Couldn't list alarm mute rules.") raise else: logger.info("Got %s alarm mute rules.", len(summaries)) return summaries
Ruby
SDK for Ruby
Note

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

# Lists the alarm mute rules in the account, optionally filtered to the rules that # target one alarm. # # @param cloudwatch_client [Aws::CloudWatch::Client] An initialized CloudWatch client. # @param alarm_name [String, nil] When given, only rules that target this alarm are # returned. # @return [Array] The mute rule summaries, as # Aws::CloudWatch::Types::AlarmMuteRuleSummary. def alarm_mute_rules(cloudwatch_client, alarm_name = nil) summaries = [] next_token = nil loop do response = cloudwatch_client.list_alarm_mute_rules( alarm_name: alarm_name, next_token: next_token ) summaries.concat(response.alarm_mute_rule_summaries) next_token = response.next_token break if next_token.nil? || next_token.empty? end summaries rescue StandardError => e puts "Error listing alarm mute rules: #{e.message}" [] end