

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

# Verwenden von Alarmaktionen in CloudWatch
<a name="examples-cloudwatch-use-alarm-actions"></a>

Mithilfe von CloudWatch Alarmaktionen können Sie Alarme erstellen, die Aktionen wie automatisches Stoppen, Beenden, Neustarten oder Wiederherstellen von Amazon EC2 EC2-Instances ausführen.

[Alarmaktionen können einem Alarm hinzugefügt werden, indem Sie bei der Erstellung eines Alarms [PutMetricAlarmRequest](https://docs.aws.amazon.com/sdk-for-cpp/latest/api/aws-cpp-sdk-monitoring/html/class_aws_1_1_cloud_watch_1_1_model_1_1_put_metric_alarm_request.html)die `SetAlarmActions` Funktion verwenden.](examples-cloudwatch-create-alarms.md)

## Voraussetzungen
<a name="codeExamplePrereq"></a>

Bevor Sie beginnen, empfehlen wir Ihnen, [Erste Schritte mit dem zu](getting-started.md) lesen AWS SDK für C\$1\$1. 

Laden Sie den Beispielcode herunter und erstellen Sie die Lösung wie unter beschrieben[Erste Schritte mit Codebeispielen](getting-started-code-examples.md). 

Um die Beispiele ausführen zu können, muss das Benutzerprofil, das Ihr Code für die Anfragen verwendet, über die entsprechenden Berechtigungen verfügen AWS (für den Dienst und die Aktion). Weitere Informationen finden Sie unter [Bereitstellen von AWS Anmeldeinformationen](credentials.md).

## Aktivieren von Alarmaktionen
<a name="enable-alarm-actions"></a>

Um Alarmaktionen für einen CloudWatch Alarm zu aktivieren, rufen Sie die CloudWatchClient s `EnableAlarmActions` mit einem auf, das einen oder mehrere Namen von Alarmen [EnableAlarmActionsRequest](https://docs.aws.amazon.com/sdk-for-cpp/latest/api/aws-cpp-sdk-monitoring/html/class_aws_1_1_cloud_watch_1_1_model_1_1_enable_alarm_actions_request.html)enthält, deren Aktionen Sie aktivieren möchten.

 **Beinhaltet** 

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

 **Code** 

```
    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;
```

Siehe [vollständiges Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/cloudwatch/enable_alarm_actions.cpp).

## Deaktivieren von Alarmaktionen
<a name="disable-alarm-actions"></a>

Um Alarmaktionen für einen CloudWatch Alarm zu deaktivieren, rufen Sie die CloudWatchClient s `DisableAlarmActions` mit einem auf, der einen oder mehrere Namen von Alarmen [DisableAlarmActionsRequest](https://docs.aws.amazon.com/sdk-for-cpp/latest/api/aws-cpp-sdk-monitoring/html/class_aws_1_1_cloud_watch_1_1_model_1_1_disable_alarm_actions_request.html)enthält, deren Aktionen Sie deaktivieren möchten.

 **Beinhaltet** 

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

 **Code** 

```
        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;
        }
```

Siehe [vollständiges Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/cpp/example_code/cloudwatch/disable_alarm_actions.cpp).

## Weitere Informationen
<a name="more-information"></a>
+  [Alarme zum Stoppen, Beenden, Neustarten oder Wiederherstellen einer Instance im CloudWatch Amazon-Benutzerhandbuch erstellen](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/UsingAlarmActions.html)
+  [PutMetricAlarm](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/PutMetricAlarm.html)in der Amazon CloudWatch API-Referenz
+  [EnableAlarmActions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/EnableAlarmActions.html)in der Amazon CloudWatch API-Referenz
+  [DisableAlarmActions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/DisableAlarmActions.html)in der Amazon CloudWatch API-Referenz