AWS SDK 또는 CLI와 함께 DescribeAlarmsForMetric 사용 - Amazon CloudWatch

AWS SDK 또는 CLI와 함께 DescribeAlarmsForMetric 사용

다음 코드 예제는 DescribeAlarmsForMetric의 사용 방법을 보여 줍니다.

작업 예시는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

.NET
AWS SDK for .NET
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Describe the current alarms for a specific metric. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricName">The name of the metric.</param> /// <returns>The list of alarm data.</returns> public async Task<List<MetricAlarm>> DescribeAlarmsForMetric(string metricNamespace, string metricName) { var alarmsResult = await _amazonCloudWatch.DescribeAlarmsForMetricAsync( new DescribeAlarmsForMetricRequest() { Namespace = metricNamespace, MetricName = metricName }); return alarmsResult.MetricAlarms; }
C++
SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DescribeAlarmsRequest.h> #include <aws/monitoring/model/DescribeAlarmsResult.h> #include <iomanip> #include <iostream>

경보를 설명합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DescribeAlarmsRequest request; request.SetMaxRecords(1); bool done = false; bool header = false; while (!done) { auto outcome = cw.DescribeAlarms(request); if (!outcome.IsSuccess()) { std::cout << "Failed to describe CloudWatch alarms:" << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "Arn" << std::setw(64) << "Description" << std::setw(20) << "LastUpdated" << std::endl; header = true; } const auto &alarms = outcome.GetResult().GetMetricAlarms(); for (const auto &alarm : alarms) { std::cout << std::left << std::setw(32) << alarm.GetAlarmName() << std::setw(64) << alarm.GetAlarmArn() << std::setw(64) << alarm.GetAlarmDescription() << std::setw(20) << alarm.GetAlarmConfigurationUpdatedTimestamp().ToGmtString( SIMPLE_DATE_FORMAT_STR) << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }
CLI
AWS CLI

지표와 관련된 경보에 대한 정보를 표시하는 방법

다음 예제에서는 describe-alarms-for-metric 명령을 사용하여 Amazon EC2 CPUUtilization 지표 및 ID i-0c986c72의 인스턴스와 관련된 모든 경보에 대한 정보를 표시합니다.

aws cloudwatch describe-alarms-for-metric --metric-name CPUUtilization --namespace AWS/EC2 --dimensions Name=InstanceId,Value=i-0c986c72

출력:

{ "MetricAlarms": [ { "EvaluationPeriods": 10, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm2", "StateUpdatedTimestamp": "2013-10-30T03:03:51.479Z", "AlarmConfigurationUpdatedTimestamp": "2013-10-30T03:03:50.865Z", "ComparisonOperator": "GreaterThanOrEqualToThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:NotifyMe" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2013-10-30T03:03:51.479+0000\",\"startDate\":\"2013-10-30T02:08:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[40.698,39.612,42.432,39.796,38.816,42.28,42.854,40.088,40.760000000000005,41.316],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm2", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 10 datapoints were not greater than or equal to the threshold (70.0). The most recent datapoints: [40.760000000000005, 41.316].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" }, { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2014-04-09T22:26:05.958Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:HighCPUAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": false, "MetricName": "CPUUtilization" } ] }
Java
SDK for Java 2.x
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

/** * Checks for a metric alarm in AWS CloudWatch. * * @param fileName the name of the file containing the JSON configuration for the custom metric * @return a {@link CompletableFuture} that completes when the check for the metric alarm is complete */ public CompletableFuture<Void> checkForMetricAlarmAsync(String fileName) { CompletableFuture<String> readFileFuture = CompletableFuture.supplyAsync(() -> { try { JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); return rootNode.toString(); // Return JSON as a string for further processing } catch (IOException e) { throw new RuntimeException("Failed to read file", e); } }); return readFileFuture.thenCompose(jsonContent -> { try { com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(jsonContent); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); DescribeAlarmsForMetricRequest metricRequest = DescribeAlarmsForMetricRequest.builder() .metricName(customMetricName) .namespace(customMetricNamespace) .build(); return checkForAlarmAsync(metricRequest, customMetricName, 10); } catch (IOException e) { throw new RuntimeException("Failed to parse JSON content", e); } }).whenComplete((result, exception) -> { if (exception != null) { throw new RuntimeException("Error checking metric alarm", exception); } }); } // Recursive method to check for the alarm. /** * Checks for the existence of an alarm asynchronously for the specified metric. * * @param metricRequest the request to describe the alarms for the specified metric * @param customMetricName the name of the custom metric to check for an alarm * @param retries the number of retries to perform if no alarm is found * @return a {@link CompletableFuture} that completes when an alarm is found or the maximum number of retries has been reached */ private static CompletableFuture<Void> checkForAlarmAsync(DescribeAlarmsForMetricRequest metricRequest, String customMetricName, int retries) { if (retries == 0) { return CompletableFuture.completedFuture(null).thenRun(() -> logger.info("No Alarm state found for {} after 10 retries.", customMetricName) ); } return (getAsyncClient().describeAlarmsForMetric(metricRequest).thenCompose(response -> { if (response.hasMetricAlarms()) { logger.info("Alarm state found for {}", customMetricName); return CompletableFuture.completedFuture(null); // Alarm found, complete the future } else { return CompletableFuture.runAsync(() -> { try { Thread.sleep(20000); logger.info("."); } catch (InterruptedException e) { throw new RuntimeException("Interrupted while waiting to retry", e); } }).thenCompose(v -> checkForAlarmAsync(metricRequest, customMetricName, retries - 1)); // Recursive call } })); }
JavaScript
SDK for JavaScript (v3)
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

SDK 및 클라이언트 모듈을 가져오고 API를 호출합니다.

import { DescribeAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { const command = new DescribeAlarmsCommand({ AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

별도의 모듈에서 클라이언트를 생성하고 내보냅니다.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});
SDK for JavaScript (v2)
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

// 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.describeAlarms({ StateValue: "INSUFFICIENT_DATA" }, function (err, data) { if (err) { console.log("Error", err); } else { // List the names of all current alarms in the console data.MetricAlarms.forEach(function (item, index, array) { console.log(item.AlarmName); }); } });
Kotlin
SDK for Kotlin
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

suspend fun checkForMetricAlarm(fileName: String?) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() var hasAlarm = false var retries = 10 val metricRequest = DescribeAlarmsForMetricRequest { metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> while (!hasAlarm && retries > 0) { val response = cwClient.describeAlarmsForMetric(metricRequest) if (response.metricAlarms?.count()!! > 0) { hasAlarm = true } retries-- delay(20000) println(".") } if (!hasAlarm) { println("No Alarm state found for $customMetricName after 10 retries.") } else { println("Alarm state found for $customMetricName.") } } }
Python
SDK for Python (Boto3)
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

class CloudWatchWrapper: """Encapsulates Amazon CloudWatch functions.""" def __init__(self, cloudwatch_resource): """ :param cloudwatch_resource: A Boto3 CloudWatch resource. """ self.cloudwatch_resource = cloudwatch_resource def get_metric_alarms(self, metric_namespace, metric_name): """ Gets the alarms that are currently watching the specified metric. :param metric_namespace: The namespace of the metric. :param metric_name: The name of the metric. :returns: An iterator that yields the alarms. """ metric = self.cloudwatch_resource.Metric(metric_namespace, metric_name) alarm_iter = metric.alarms.all() logger.info("Got alarms for metric %s.%s.", metric_namespace, metric_name) return alarm_iter
Ruby
SDK for Ruby
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예제 리포지토리에서 더 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

# # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @example # describe_metric_alarms(Aws::CloudWatch::Client.new(region: 'us-east-1')) def describe_metric_alarms(cloudwatch_client) response = cloudwatch_client.describe_alarms if response.metric_alarms.count.positive? response.metric_alarms.each do |alarm| puts '-' * 16 puts "Name: #{alarm.alarm_name}" puts "State value: #{alarm.state_value}" puts "State reason: #{alarm.state_reason}" puts "Metric: #{alarm.metric_name}" puts "Namespace: #{alarm.namespace}" puts "Statistic: #{alarm.statistic}" puts "Period: #{alarm.period}" puts "Unit: #{alarm.unit}" puts "Eval. periods: #{alarm.evaluation_periods}" puts "Threshold: #{alarm.threshold}" puts "Comp. operator: #{alarm.comparison_operator}" if alarm.key?(:ok_actions) && alarm.ok_actions.count.positive? puts 'OK actions:' alarm.ok_actions.each do |a| puts " #{a}" end end if alarm.key?(:alarm_actions) && alarm.alarm_actions.count.positive? puts 'Alarm actions:' alarm.alarm_actions.each do |a| puts " #{a}" end end if alarm.key?(:insufficient_data_actions) && alarm.insufficient_data_actions.count.positive? puts 'Insufficient data actions:' alarm.insufficient_data_actions.each do |a| puts " #{a}" end end puts 'Dimensions:' if alarm.key?(:dimensions) && alarm.dimensions.count.positive? alarm.dimensions.each do |d| puts " Name: #{d.name}, Value: #{d.value}" end else puts ' None for this alarm.' end end else puts 'No alarms found.' end rescue StandardError => e puts "Error getting information about alarms: #{e.message}" end # Example usage: def run_me region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby cw-ruby-example-show-alarms.rb REGION' puts 'Example: ruby cw-ruby-example-show-alarms.rb us-east-1' exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? region = 'us-east-1' # Otherwise, use the values as specified at the command prompt. else region = ARGV[0] end cloudwatch_client = Aws::CloudWatch::Client.new(region: region) puts 'Available alarms:' describe_metric_alarms(cloudwatch_client) end run_me if $PROGRAM_NAME == __FILE__

AWS SDK 개발자 가이드 및 코드 예시의 전체 목록은 AWS SDK에서 CloudWatch 사용 단원을 참조하세요. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.