-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathamazon-cloudwatch-agent-ctl
More file actions
executable file
·317 lines (270 loc) · 10.9 KB
/
Copy pathamazon-cloudwatch-agent-ctl
File metadata and controls
executable file
·317 lines (270 loc) · 10.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/sh
# Copyright 2017 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
set -e
set -u
readonly AGENTDIR="/opt/aws/amazon-cloudwatch-agent"
readonly CMDDIR="${AGENTDIR}/bin"
readonly CONFDIR="${AGENTDIR}/etc"
readonly LOGDIR="${AGENTDIR}/logs"
readonly RESTART_FILE="${CONFDIR}/restart"
readonly VERSION_FILE="${CMDDIR}/CWAGENT_VERSION"
readonly AGENT_LAUNCHD_NAME="com.amazon.cloudwatch.agent"
readonly AGENT_LAUNCHD_CONFIG="/Library/LaunchDaemons/${AGENT_LAUNCHD_NAME}.plist"
readonly TOML="${CONFDIR}/amazon-cloudwatch-agent.toml"
readonly OTEL_YAML="${CONFDIR}/amazon-cloudwatch-agent.yaml"
readonly JSON="${CONFDIR}/amazon-cloudwatch-agent.json"
readonly JSON_DIR="${CONFDIR}/amazon-cloudwatch-agent.d"
readonly CV_LOG_FILE="${AGENTDIR}/logs/configuration-validation.log"
readonly COMMON_CONIG="${CONFDIR}/common-config.toml"
readonly ALL_CONFIG='all'
UsageString="
usage: amazon-cloudwatch-agent-ctl -a stop|start|status|fetch-config|append-config|remove-config [-m ec2|onPremise|onPrem|auto] [-c default|ssm:<parameter-store-name>|file:<file-path>] [-s] [-d]
e.g.
1. apply a SSM parameter store config on EC2 instance and restart the agent afterwards:
amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-Config.json -s
2. append a local json config file on onPremise host and restart the agent afterwards:
amazon-cloudwatch-agent-ctl -a append-config -m onPremise -c file:/tmp/config.json -s
3. query agent status:
amazon-cloudwatch-agent-ctl -a status
4. apply a SSM parameter store config with dual-stack endpoints:
amazon-cloudwatch-agent-ctl -d -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-Config.json -s
-a: action
stop: stop the agent process.
start: start the agent process.
status: get the status of the agent process.
fetch-config: use this json config as the agent's only configuration.
append-config: append json config with the existing json configs if any.
remove-config: remove json config based on the location (ssm parameter store name, file name)
-m: mode
ec2: indicate this is on ec2 host.
onPremise, onPrem: indicate this is on onPremise host.
auto: use ec2 metadata to determine the environment, may not be accurate if ec2 metadata is not available for some reason on EC2.
-c: configuration
default: default configuration for quick trial.
ssm:<parameter-store-name>: ssm parameter store name
file:<file-path>: file path on the host
all: all existing configs. Only apply to remove-config action.
-s: optionally restart after configuring the agent configuration
this parameter is used for 'fetch-config', 'append-config', 'remove-config' action only.
-d: enable dual-stack endpoints for AWS API calls during config download
this parameter is used for 'fetch-config', 'append-config' actions only.
"
cwa_start() {
mode="${1:-}"
if [ "$(cwa_runstatus)" = 'running' ]; then
return 0
fi
if [ ! -f "${TOML}" ]; then
echo "amazon-cloudwatch-agent is not configured. Applying default configuration before starting it."
cwa_config 'default' 'false' "${mode}" 'default' 'false'
fi
launchctl load $AGENT_LAUNCHD_CONFIG
}
cwa_stop() {
if [ "$(cwa_runstatus)" = 'stopped' ]; then
return 0
fi
launchctl unload $AGENT_LAUNCHD_CONFIG
}
# support for restart during upgrade via SSM packages
cwa_prep_restart() {
if [ "$(cwa_runstatus)" = 'running' ]; then
touch "$RESTART_FILE"
fi
}
# support for restart during upgrade via SSM packages
cwa_cond_restart() {
if [ -f "${RESTART_FILE}" ]; then
cwa_start
rm -f "${RESTART_FILE}"
fi
}
cwa_preun() {
cwa_stop
}
cwa_status() {
cwa_config_status='configured'
if [ ! -f "${TOML}" ]; then
cwa_config_status='not configured'
fi
starttime_fmt=''
local pid=$(cwa_pid)
if [[ $pid =~ ^[\-0-9]+$ ]] && [ "$pid" -gt 0 ]; then
starttime="$(TZ=UTC LC_ALL=C ps -o lstart= "${pid}")"
starttime_fmt="$(TZ=UTC date -jf "%a %b %d %T %Y " "${starttime}" +%FT%T%z)"
else
echo ${pid}
fi
version="$(cat ${VERSION_FILE})"
echo "{"
echo " \"status\": \"$(cwa_runstatus)\","
echo " \"starttime\": \"${starttime_fmt}\","
echo " \"configstatus\": \"${cwa_config_status}\","
echo " \"version\": \"${version}\""
echo "}"
}
cwa_runstatus() {
running=false
set +e
local pid=$(cwa_pid)
if [[ "$pid" =~ ^[0-9]+$ ]] && [ "$pid" -gt 0 ]; then
running='true'
elif [[ "$pid" == "-" ]]; then
running='true'
fi
set -e
if [ "${running}" = 'true' ]; then
echo "running"
else
echo "stopped"
fi
}
cwa_pid() {
echo "$({ sudo launchctl list | grep ${AGENT_LAUNCHD_NAME} | awk '{print $1}'; } 2>/dev/null)"
}
cwa_config() {
config_location="${1:-}"
restart="${2:-}"
mode="${3:-}"
multi_config="${4:-}"
use_dualstack="${5:-false}"
mkdir -p "${CONFDIR}"
if [ "${config_location}" = "${ALL_CONFIG}" ] && [ "${multi_config}" != 'remove' ]; then
echo "ignore cwa configuration \"${ALL_CONFIG}\" as it is only supported by action \"remove-config\""
return
fi
if [ "${config_location}" = "${ALL_CONFIG}" ]; then
rm -rf "${JSON_DIR}"/*
else
downloader_cmd="${CMDDIR}/config-downloader --output-dir ${JSON_DIR} --download-source ${config_location} --mode ${mode} --config ${COMMON_CONIG} --multi-config ${multi_config}"
if [ "${use_dualstack}" = 'true' ]; then
downloader_cmd="${downloader_cmd} --dualstack"
fi
runDownloaderCommand=$(${downloader_cmd})
echo "${runDownloaderCommand}"
fi
if [ ! "$(ls ${JSON_DIR})" ]; then
echo "all amazon-cloudwatch-agent configurations have been removed"
rm -f "${TOML}"
rm -f "${OTEL_YAML}"
else
runTranslatorCommand=$("${CMDDIR}/config-translator" --input "${JSON}" --input-dir "${JSON_DIR}" --output "${TOML}" --mode ${mode} --config "${COMMON_CONIG}" --multi-config ${multi_config})
echo "${runTranslatorCommand}"
runAgentSchemaTestCommand="${CMDDIR}/amazon-cloudwatch-agent -schematest -config ${TOML}"
echo "${runAgentSchemaTestCommand}"
# We will redirect the verbose error message out
if ! ${runAgentSchemaTestCommand} >${CV_LOG_FILE} 2>&1; then
echo "Configuration validation second phase failed"
echo "======== Error Log ========"
cat ${CV_LOG_FILE}
exit 1
fi
echo "Configuration validation second phase succeeded"
echo "Configuration validation succeeded"
chmod ug+rw "${TOML}"
if [ -f "${OTEL_YAML}" ]; then
chmod ug+rw "${OTEL_YAML}"
fi
# for translator:
# default: only process .tmp files
# append: process both existing files and .tmp files
# remove: only process existing files
# At this point, all json configs have been validated
# multi_config:
# default: delete non .tmp file, rename .tmp file
# append: rename .tmp file
# remove: no-op
if [ "${multi_config}" = 'default' ]; then
rm -f "${JSON}"
for file in "${JSON_DIR}"/*; do
base="${JSON_DIR}/$(basename "${file}" .tmp)"
if [ "${file}" = "${base}" ]; then
rm -f "${file}"
else
mv -f "${file}" "${base}"
fi
done
elif [ "${multi_config}" = 'append' ]; then
for file in "${JSON_DIR}"/*.tmp; do
mv -f "${file}" "${JSON_DIR}/$(basename "${file}" .tmp)"
done
fi
fi
if [ "${restart}" = 'true' ]; then
cwa_stop
cwa_start "${mode}"
fi
}
main() {
action=''
config_location='default'
restart='false'
mode='auto'
use_dualstack='false'
OPTIND=1
while getopts ":hsa:r:c:m:d" opt; do
case "${opt}" in
h)
echo "${UsageString}"
exit 0
;;
s) restart='true' ;;
a) action="${OPTARG}" ;;
c) config_location="${OPTARG}" ;;
m) mode="${OPTARG}" ;;
d) use_dualstack='true' ;;
\?)
echo "Invalid option: -${OPTARG} ${UsageString}" >&2
;;
:)
echo "Option -${OPTARG} requires an argument ${UsageString}" >&2
exit 1
;;
esac
done
shift "$((${OPTIND} - 1))"
case "${mode}" in
ec2) ;;
onPremise) ;;
onPrem) ;;
auto) ;;
*)
echo "Invalid mode: ${mode} ${UsageString}" >&2
exit 1
;;
esac
current_user=$(id -u -n)
if [ "${action}" != 'status' -a "${current_user}" != 'root' ]; then
echo "Please use root to run this script"
exit 1
fi
case "${action}" in
stop) cwa_stop ;;
start) cwa_start "${mode}" ;;
fetch-config) cwa_config "${config_location}" "${restart}" "${mode}" 'default' "${use_dualstack}" ;;
append-config) cwa_config "${config_location}" "${restart}" "${mode}" 'append' "${use_dualstack}" ;;
remove-config) cwa_config "${config_location}" "${restart}" "${mode}" 'remove' ;;
status) cwa_status ;;
# helpers for ssm package scripts to workaround fact that it can't determine if invocation is due to
# upgrade or install
prep-restart) cwa_prep_restart ;;
cond-restart) cwa_cond_restart ;;
preun) cwa_preun ;;
*)
echo "Invalid action: ${action} ${UsageString}" >&2
exit 1
;;
esac
}
main "$@"