CloudWatch でのアラームアクションの使用 - AWS SDK for C++

CloudWatch でのアラームアクションの使用

CloudWatch アラームアクションを使用して、Amazon EC2 インスタンスを自動的に停止、終了、再起動、または復旧するといったアクションを実行するアラームを作成できます。

アラームの作成時に PutMetricAlarmRequestSetAlarmActions 関数を使用することで、アラームアクションをアラームに追加できます。

前提条件

作業を始める前に「AWS SDK for C++ の開始方法」を読むことをお勧めします。

コード例をダウンロードし、「コード例の開始方法」の説明に従ってソリューションをビルドします。

例を実行するには、リクエストに使用するユーザープロファイルに、AWS のサービスとアクションに対する適切なアクセス許可が付与されている必要があります。詳細については、「AWS 認証情報の提供」を参照してください。

アラームアクションの有効化

CloudWatch アラームのアクションを有効にするには、アクションを有効にする 1 つ以上のアラームの名前を含む EnableAlarmActionsRequest を指定して、CloudWatchClient の EnableAlarmActions を呼び出します。

含まれるもの:

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/EnableAlarmActionsRequest.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

コード

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetComparisonOperator( Aws::CloudWatch::Model::ComparisonOperator::GreaterThanThreshold); request.SetEvaluationPeriods(1); request.SetMetricName("CPUUtilization"); request.SetNamespace("AWS/EC2"); request.SetPeriod(60); request.SetStatistic(Aws::CloudWatch::Model::Statistic::Average); request.SetThreshold(70.0); request.SetActionsEnabled(false); request.SetAlarmDescription("Alarm when server CPU exceeds 70%"); request.SetUnit(Aws::CloudWatch::Model::StandardUnit::Seconds); request.AddAlarmActions(actionArn); Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("InstanceId"); dimension.SetValue(instanceId); request.AddDimensions(dimension); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; return; } Aws::CloudWatch::Model::EnableAlarmActionsRequest enable_request; enable_request.AddAlarmNames(alarm_name); auto enable_outcome = cw.EnableAlarmActions(enable_request); if (!enable_outcome.IsSuccess()) { std::cout << "Failed to enable alarm actions:" << enable_outcome.GetError().GetMessage() << std::endl; return; } std::cout << "Successfully created alarm " << alarm_name << " and enabled actions on it." << std::endl;

完全な例をご覧ください。

アラームアクションの無効化

CloudWatch アラームのアクションを無効にするには、アクションを無効にする 1 つ以上のアラームの名前を含む DisableAlarmActionsRequest を指定して、CloudWatchClient の DisableAlarmActions を呼び出します。

含まれるもの:

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

コード

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DisableAlarmActionsRequest disableAlarmActionsRequest; disableAlarmActionsRequest.AddAlarmNames(alarm_name); auto disableAlarmActionsOutcome = cw.DisableAlarmActions(disableAlarmActionsRequest); if (!disableAlarmActionsOutcome.IsSuccess()) { std::cout << "Failed to disable actions for alarm " << alarm_name << ": " << disableAlarmActionsOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully disabled actions for alarm " << alarm_name << std::endl; }

完全な例をご覧ください。

詳細情報