SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 55 Java
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.monitor_instance.inc]
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/MonitorInstancesRequest.h>
#include <aws/ec2/model/UnmonitorInstancesRequest.h>
#include <iostream>
// snippet-end:[ec2.cpp.monitor_instance.inc]
#include "ec2_samples.h"
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.MonitorInstances]
//! Enable detailed monitoring for an Amazon Elastic Compute Cloud (Amazon EC2) instance.
/*!
\param instanceId: An EC2 instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::enableMonitoring(const Aws::String &instanceId,
const Aws::Client::ClientConfiguration &clientConfiguration) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.enable_monitor_instance.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::MonitorInstancesRequest request;
request.AddInstanceIds(instanceId);
2021-11-15 13:15:43 +00:00
request.SetDryRun(true);
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::MonitorInstancesOutcome dryRunOutcome = ec2Client.MonitorInstances(request);
if (dryRunOutcome.IsSuccess()) {
std::cerr
<< "Failed dry run to enable monitoring on instance. A dry run should trigger an error."
<<
std::endl;
return false;
2024-08-02 10:24:50 -04:00
} else if (dryRunOutcome.GetError().GetErrorType()
!= Aws::EC2::EC2Errors::DRY_RUN_OPERATION) {
std::cerr << "Failed dry run to enable monitoring on instance " <<
2024-08-02 10:24:50 -04:00
instanceId << ": " << dryRunOutcome.GetError().GetMessage() <<
std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
request.SetDryRun(false);
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::MonitorInstancesOutcome monitorInstancesOutcome = ec2Client.MonitorInstances(request);
if (!monitorInstancesOutcome.IsSuccess()) {
std::cerr << "Failed to enable monitoring on instance " <<
instanceId << ": " <<
monitorInstancesOutcome.GetError().GetMessage() << std::endl;
2024-08-02 10:24:50 -04:00
} else {
2021-11-15 13:15:43 +00:00
std::cout << "Successfully enabled monitoring on instance " <<
instanceId << std::endl;
2021-11-15 13:15:43 +00:00
}
// snippet-end:[ec2.cpp.enable_monitor_instance.code]
return monitorInstancesOutcome.IsSuccess();
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.MonitorInstances]
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.UnmonitorInstances]
//! Disable monitoring for an EC2 instance.
/*!
\param instanceId: An EC2 instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::disableMonitoring(const Aws::String &instanceId,
const Aws::Client::ClientConfiguration &clientConfiguration) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.disable_monitor_instance.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::UnmonitorInstancesRequest unrequest;
unrequest.AddInstanceIds(instanceId);
2021-11-15 13:15:43 +00:00
unrequest.SetDryRun(true);
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::UnmonitorInstancesOutcome dryRunOutcome = ec2Client.UnmonitorInstances(unrequest);
if (dryRunOutcome.IsSuccess()) {
std::cerr
<< "Failed dry run to disable monitoring on instance. A dry run should trigger an error."
<<
std::endl;
return false;
2024-08-02 10:24:50 -04:00
} else if (dryRunOutcome.GetError().GetErrorType() !=
Aws::EC2::EC2Errors::DRY_RUN_OPERATION) {
2021-11-15 13:15:43 +00:00
std::cout << "Failed dry run to disable monitoring on instance " <<
2024-08-02 10:24:50 -04:00
instanceId << ": " << dryRunOutcome.GetError().GetMessage() <<
std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
unrequest.SetDryRun(false);
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::UnmonitorInstancesOutcome unmonitorInstancesOutcome = ec2Client.UnmonitorInstances(unrequest);
if (!unmonitorInstancesOutcome.IsSuccess()) {
std::cout << "Failed to disable monitoring on instance " << instanceId
<< ": " << unmonitorInstancesOutcome.GetError().GetMessage() <<
std::endl;
2024-08-02 10:24:50 -04:00
} else {
2021-11-15 13:15:43 +00:00
std::cout << "Successfully disable monitoring on instance " <<
instanceId << std::endl;
2021-11-15 13:15:43 +00:00
}
// snippet-end:[ec2.cpp.disable_monitor_instance.code]
return unmonitorInstancesOutcome.IsSuccess();
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.UnmonitorInstances]
2021-11-15 13:15:43 +00:00
/*
*
* main function
*
* Usage: 'run_monitor_instance <instance_id> <true|false>'
*
* Prerequisites: An EC2 instance to monitor.
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 3) {
std::cout << "Usage: run_monitor_instance <instance_id> <true|false>" <<
std::endl;
2021-11-15 13:15:43 +00:00
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String instance_id = argv[1];
bool enableMonitoring = false;
Aws::StringStream ss(argv[2]);
ss >> std::boolalpha >> enableMonitoring;
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
if (enableMonitoring) {
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::enableMonitoring(instance_id, clientConfig);
2021-11-15 13:15:43 +00:00
}
else {
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::disableMonitoring(instance_id, clientConfig);
2021-11-15 13:15:43 +00:00
}
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD