Repository navigation
Expand file tree
/
Copy pathmonitor_instance.cpp
More file actions
160 lines (141 loc) · 5.8 KB
/
Copy pathmonitor_instance.cpp
File metadata and controls
160 lines (141 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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.
*
**/
// 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"
// 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.
*/
bool AwsDoc::EC2::enableMonitoring(const Aws::String &instanceId,
const Aws::Client::ClientConfiguration &clientConfiguration) {
// snippet-start:[ec2.cpp.enable_monitor_instance.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::MonitorInstancesRequest request;
request.AddInstanceIds(instanceId);
request.SetDryRun(true);
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;
} else if (dryRunOutcome.GetError().GetErrorType()
!= Aws::EC2::EC2Errors::DRY_RUN_OPERATION) {
std::cerr << "Failed dry run to enable monitoring on instance " <<
instanceId << ": " << dryRunOutcome.GetError().GetMessage() <<
std::endl;
return false;
}
request.SetDryRun(false);
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;
} else {
std::cout << "Successfully enabled monitoring on instance " <<
instanceId << std::endl;
}
// snippet-end:[ec2.cpp.enable_monitor_instance.code]
return monitorInstancesOutcome.IsSuccess();
}
// snippet-end:[cpp.example_code.ec2.MonitorInstances]
// 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.
*/
bool AwsDoc::EC2::disableMonitoring(const Aws::String &instanceId,
const Aws::Client::ClientConfiguration &clientConfiguration) {
// snippet-start:[ec2.cpp.disable_monitor_instance.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::UnmonitorInstancesRequest unrequest;
unrequest.AddInstanceIds(instanceId);
unrequest.SetDryRun(true);
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;
} else if (dryRunOutcome.GetError().GetErrorType() !=
Aws::EC2::EC2Errors::DRY_RUN_OPERATION) {
std::cout << "Failed dry run to disable monitoring on instance " <<
instanceId << ": " << dryRunOutcome.GetError().GetMessage() <<
std::endl;
return false;
}
unrequest.SetDryRun(false);
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;
} else {
std::cout << "Successfully disable monitoring on instance " <<
instanceId << std::endl;
}
// snippet-end:[ec2.cpp.disable_monitor_instance.code]
return unmonitorInstancesOutcome.IsSuccess();
}
// snippet-end:[cpp.example_code.ec2.UnmonitorInstances]
/*
*
* 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;
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) {
AwsDoc::EC2::enableMonitoring(instance_id, clientConfig);
}
else {
AwsDoc::EC2::disableMonitoring(instance_id, clientConfig);
}
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD