-
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
·440 lines (369 loc) · 14.5 KB
/
Copy pathamazon-cloudwatch-agent-ctl
File metadata and controls
executable file
·440 lines (369 loc) · 14.5 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/bin/sh
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT
set -e
set -u
readonly AGENTDIR="/opt/aws/amazon-cloudwatch-agent"
readonly CMDDIR="${AGENTDIR}/bin"
readonly CONFDIR="${AGENTDIR}/etc"
readonly CWA_RESTART_FILE="${CONFDIR}/restart"
readonly VERSION_FILE="${CMDDIR}/CWAGENT_VERSION"
# The systemd and upstart scripts assume exactly this .toml file name
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 ENV_CONFIG="${CONFDIR}/env-config.json"
readonly CWA_NAME='amazon-cloudwatch-agent'
readonly ALL_CONFIG='all'
SYSTEMD='false'
UsageString="
usage: amazon-cloudwatch-agent-ctl -a
stop|start|status|fetch-config|append-config|remove-config|set-log-level
[-m ec2|onPremise|onPrem|auto]
[-c default|all|ssm:<parameter-store-name>|file:<file-path>]
[-s]
[-d]
[-l INFO|DEBUG|WARN|ERROR|OFF]
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: apply config for agent, followed by -c. Target config can be based on location (ssm parameter store name, file name), or 'default'.
append-config: append json config with the existing json configs if any, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'default'.
remove-config: remove config for agent, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'all'.
set-log-level: sets the log level, followed by -l to provide the level in all caps.
-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: amazon-cloudwatch-agent 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.
-l: log level to set the agent to INFO, DEBUG, WARN, ERROR, or OFF
this parameter is used for 'set-log-level' only.
"
start_all() {
mode="${1:-}"
echo ""
echo "****** processing amazon-cloudwatch-agent ******"
agent_start "${CWA_NAME}" "${mode}"
}
agent_start() {
agent_name="${1:-}"
mode="${2:-}"
if [ "$(runstatus ${agent_name})" = 'running' ]; then
echo "${agent_name} has already been started"
return 0
fi
if [ "${agent_name}" = "${CWA_NAME}" ] && [ ! -f "${TOML}" ]; then
echo "${CWA_NAME} is not configured. Applying amazon-cloudwatch-agent default configuration."
cwa_config 'default' 'false' "${mode}" 'default' 'false'
fi
if [ "${SYSTEMD}" = 'true' ]; then
systemctl daemon-reload || return
systemctl enable "${agent_name}.service" || return
systemctl restart "${agent_name}.service" || return
else
start "${agent_name}" || return
sleep 1
fi
}
stop_all() {
echo ""
echo "****** processing amazon-cloudwatch-agent ******"
agent_stop_and_disable "${CWA_NAME}"
}
agent_stop_and_disable() {
agent_name="${1:-}"
agent_stop "${agent_name}"
}
agent_stop() {
agent_name="${1:-}"
if [ "$(runstatus ${agent_name})" = 'stopped' ]; then
echo "${agent_name} has already been stopped"
return 0
fi
if [ "${SYSTEMD}" = 'true' ]; then
systemctl stop "${agent_name}.service" || return
else
stop "${agent_name}" || return
fi
}
# support for restart during upgrade via SSM packages
prep_restart_all() {
agent_prep_restart "${CWA_NAME}" "${CWA_RESTART_FILE}"
}
agent_prep_restart() {
agent_name="${1:-}"
restart_file="${2:-}"
if [ "$(runstatus ${agent_name})" = 'running' ]; then
touch "${restart_file}"
fi
}
# support for restart during upgrade via SSM packages
cond_restart_all() {
agent_cond_restart "${CWA_NAME}" "${CWA_RESTART_FILE}"
}
agent_cond_restart() {
agent_name="${1:-}"
restart_file="${2:-}"
if [ -f "${restart_file}" ]; then
agent_start "${agent_name}" "${mode}"
rm -f "${restart_file}"
fi
}
preun_all() {
agent_preun "${CWA_NAME}"
}
agent_preun() {
agent_name="${1:-}"
agent_stop "${agent_name}"
if [ "${SYSTEMD}" = 'true' ]; then
systemctl disable "${agent_name}.service" || return
systemctl daemon-reload || return
systemctl reset-failed || return
fi
}
status_all() {
cwa_config_status='configured'
if [ ! -f "${TOML}" ]; then
cwa_config_status='not configured'
fi
version="$(cat ${VERSION_FILE})"
echo "{"
echo " \"status\": \"$(runstatus ${CWA_NAME})\","
echo " \"starttime\": \"$(get_starttime_fmt ${CWA_NAME})\","
echo " \"configstatus\": \"${cwa_config_status}\","
echo " \"version\": \"${version}\""
echo "}"
}
get_starttime_fmt() {
agent_name="${1:-}"
agentPid=''
if [ "${SYSTEMD}" = 'true' ]; then
agentPid="$(systemctl show -p MainPID "${agent_name}.service" | sed s/MainPID=//)"
else
agentPid="$(initctl status "${agent_name}" | sed -n s/^.*process\ //p)"
fi
starttime_fmt=''
if [ "${agentPid}" ] && [ "${agentPid}" -ne "0" ]; then
starttime="$(TZ=UTC ps -o lstart= "${agentPid}")"
starttime_fmt="$(TZ=UTC date -Isec -d "${starttime}")"
fi
echo "${starttime_fmt}"
}
runstatus() {
agent_name="${1:-}"
running=false
if [ "${SYSTEMD}" = 'true' ]; then
set +e
if systemctl is-active "${agent_name}.service" 1>/dev/null; then
running='true'
fi
set -e
else
if [ "$(initctl status "${agent_name}" | grep -c running)" = 1 ]; then
running='true'
fi
fi
if [ "${running}" = 'true' ]; then
echo "running"
else
echo "stopped"
fi
}
config_all() {
cwa_config_location="${1:-}"
restart="${2:-}"
mode="${3:-}"
multi_config="${4:-}"
use_dualstack="${5:-}"
if [ -z "${cwa_config_location}" ]; then
cwa_config_location='default'
fi
mkdir -p "${CONFDIR}"
if [ -n "${cwa_config_location}" ]; then
echo "****** processing amazon-cloudwatch-agent ******"
cwa_config "${cwa_config_location}" "${restart}" "${mode}" "${multi_config}" "${use_dualstack}"
fi
}
cwa_config() {
cwa_config_location="${1:-}"
restart="${2:-}"
param_mode="${3:-}"
multi_config="${4:-}"
use_dualstack="${5:-}"
if [ "${cwa_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 [ "${cwa_config_location}" = "${ALL_CONFIG}" ]; then
rm -rf "${JSON_DIR}"/*
else
downloader_cmd="${CMDDIR}/config-downloader --output-dir ${JSON_DIR} --download-source ${cwa_config_location} --mode ${param_mode} --config ${COMMON_CONIG} --multi-config ${multi_config}"
if [ "${use_dualstack}" = 'true' ]; then
downloader_cmd="${downloader_cmd} --dualstack"
fi
runDownloaderCommand=$(${downloader_cmd})
echo "${runDownloaderCommand}" || return
fi
if [ ! "$(ls ${JSON_DIR})" ]; then
echo "all amazon-cloudwatch-agent configurations have been removed"
rm -f "${TOML}"
rm -f "${OTEL_YAML}"
else
echo "Start configuration validation..."
runTranslatorCommand=$("${CMDDIR}/config-translator" --input "${JSON}" --input-dir "${JSON_DIR}" --output "${TOML}" --mode ${param_mode} --config "${COMMON_CONIG}" --multi-config ${multi_config})
echo "${runTranslatorCommand}" || return
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
agent_stop_and_disable "${CWA_NAME}"
agent_start "${CWA_NAME}" "${param_mode}"
fi
}
set_log_level_all() {
log_level="${1:-}"
case "${log_level}" in
INFO) ;;
DEBUG) ;;
ERROR) ;;
WARN) ;;
OFF) ;;
*)
echo "Invalid log level: ${log_level} ${UsageString}" >&2
exit 1
;;
esac
runEnvConfigCommand=$("${CMDDIR}/amazon-cloudwatch-agent" -setenv CWAGENT_LOG_LEVEL=${log_level} -envconfig "${ENV_CONFIG}")
echo "${runEnvConfigCommand}" || return
echo "Set CWAGENT_LOG_LEVEL to ${log_level}"
}
main() {
action=''
cwa_config_location=''
restart='false'
mode='ec2'
use_dualstack='false'
# detect which init system is in use
if [ "$(/sbin/init --version 2>/dev/null | grep -c upstart)" = 1 ]; then
SYSTEMD='false'
elif [ "$(systemctl | grep -c -E '\-\.mount\s')" = 1 ]; then
SYSTEMD='true'
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
echo "sysv-init is not supported" >&2
exit 1
else
echo "unknown init system" >&2
exit 1
fi
OPTIND=1
while getopts ":hsa:c:m:l:d" opt; do
case "${opt}" in
h)
echo "${UsageString}"
exit 0
;;
s) restart='true' ;;
a) action="${OPTARG}" ;;
c) cwa_config_location="${OPTARG}" ;;
m) mode="${OPTARG}" ;;
l) log_level="${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
case "${action}" in
stop) stop_all ;;
start) start_all "${mode}" ;;
fetch-config) config_all "${cwa_config_location}" "${restart}" "${mode}" 'default' "${use_dualstack}" ;;
append-config) config_all "${cwa_config_location}" "${restart}" "${mode}" 'append' "${use_dualstack}" ;;
remove-config) config_all "${cwa_config_location}" "${restart}" "${mode}" 'remove' ;;
status) status_all ;;
# helpers for ssm package scripts to workaround fact that it can't determine if invocation is due to
# upgrade or install
prep-restart) prep_restart_all ;;
cond-restart) cond_restart_all ;;
# helper for rpm+deb uninstallation hooks, not expected to be called manually
preun) preun_all ;;
set-log-level) set_log_level_all "${log_level}" ;;
*)
echo "Invalid action: ${action} ${UsageString}" >&2
exit 1
;;
esac
}
main "$@"