CloudWatch 代理实例流
CloudWatch agent per-instance streams
我在 Windows 实例上使用 CloudWatch 代理(不是 CloudWatch Logs 代理)。配置文件 amazon-cloudwatch-agent.toml 包含一个硬编码的实例 ID。如果我为自动缩放组创建 AMI,则启动的每个实例都使用相同的陈旧实例 ID 作为日志流。他们都写入同一个流。
我希望每个实例都使用自己的 instance_id 写入流。这似乎是您几乎总是想要的。这怎么可能?
log_stream_name – Optional. Specifies what to use as the log stream name in CloudWatch Logs. As part of the name, you can use {instance_id}, {hostname}, {local_hostname}, and {ip_address} as variables within the name. {hostname} retrieves the hostname from the EC2 metadata, while {local_hostname} uses the hostname from the network configuration file.
If you omit this field, the default of {instance_id} is used. A log stream is created automatically if it does not already exist.
所以最简单的做法就是根本不定义 log_stream_name。
奇怪的是,代理使用 .toml 文件作为其配置,而不是 json 文件,并且 toml 生成仅在安装时完成。也许您可以使用以下命令强制重新生成 toml:
$/opt/aws/amazon-cloudwatch-agent/bin/config-translator \
--input /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
--output /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml \
--mode ec2 \
--config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
您甚至可以将其粘贴到用户数据脚本中。代理可能也需要重新启动。
我相信 "amazon-cloudwatch-agent-ctl start" 从您的用户数据中进行操作可能是他们希望您做的。他们从来没有在任何地方告诉你,不是我发现的,但应该创建 TOML(如果你确定它不存在)然后 start/restart 服务。但是 "start" 有一个下载部分,鉴于我的 JSON 文件是本地的,我不确定那里有什么。
所以我做了一些不同但相同的想法:
- 将AmazonCloudWatchAgent服务设置为手动启动
- 运行 使用任务计划程序启动时的以下批处理文件
下面的批处理文件将创建 TOML 然后启动服务:
SET CLOUD_WATCH="C:\Program Files\Amazon\AmazonCloudWatchAgent"
SET CLOUD_WATCH_DATA=C:\ProgramData\Amazon\AmazonCloudWatchAgent
SET JSON=%CLOUD_WATCH_DATA%\amazon-cloudwatch-agent.json
SET TOML=%CLOUD_WATCH_DATA%\amazon-cloudwatch-agent.toml
SET CONFIG=%CLOUD_WATCH_DATA%\common-config.toml
SET TRANSLATOR=%CLOUD_WATCH%\config-translator.exe
rem Translate JSON into TOML
%TRANSLATOR% --input %JSON% --output %TOML% --mode ec2 --config %CONFIG%
rem Start the service
sc start AmazonCloudWatchAgent
这似乎对我有用。我还确保我的 AMI 有空的日志文件,包括 CloudWatch 代理的日志和我自己的日志。所以每个实例都重新开始。
但如果可以的话,我可能会在某个时候切换到用户数据 "amazon-cloudwatch-agent-ctl start"。
我在 Windows 实例上使用 CloudWatch 代理(不是 CloudWatch Logs 代理)。配置文件 amazon-cloudwatch-agent.toml 包含一个硬编码的实例 ID。如果我为自动缩放组创建 AMI,则启动的每个实例都使用相同的陈旧实例 ID 作为日志流。他们都写入同一个流。
我希望每个实例都使用自己的 instance_id 写入流。这似乎是您几乎总是想要的。这怎么可能?
log_stream_name – Optional. Specifies what to use as the log stream name in CloudWatch Logs. As part of the name, you can use {instance_id}, {hostname}, {local_hostname}, and {ip_address} as variables within the name. {hostname} retrieves the hostname from the EC2 metadata, while {local_hostname} uses the hostname from the network configuration file.
If you omit this field, the default of {instance_id} is used. A log stream is created automatically if it does not already exist.
所以最简单的做法就是根本不定义 log_stream_name。
奇怪的是,代理使用 .toml 文件作为其配置,而不是 json 文件,并且 toml 生成仅在安装时完成。也许您可以使用以下命令强制重新生成 toml:
$/opt/aws/amazon-cloudwatch-agent/bin/config-translator \
--input /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
--output /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml \
--mode ec2 \
--config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
您甚至可以将其粘贴到用户数据脚本中。代理可能也需要重新启动。
我相信 "amazon-cloudwatch-agent-ctl start" 从您的用户数据中进行操作可能是他们希望您做的。他们从来没有在任何地方告诉你,不是我发现的,但应该创建 TOML(如果你确定它不存在)然后 start/restart 服务。但是 "start" 有一个下载部分,鉴于我的 JSON 文件是本地的,我不确定那里有什么。
所以我做了一些不同但相同的想法:
- 将AmazonCloudWatchAgent服务设置为手动启动
- 运行 使用任务计划程序启动时的以下批处理文件
下面的批处理文件将创建 TOML 然后启动服务:
SET CLOUD_WATCH="C:\Program Files\Amazon\AmazonCloudWatchAgent"
SET CLOUD_WATCH_DATA=C:\ProgramData\Amazon\AmazonCloudWatchAgent
SET JSON=%CLOUD_WATCH_DATA%\amazon-cloudwatch-agent.json
SET TOML=%CLOUD_WATCH_DATA%\amazon-cloudwatch-agent.toml
SET CONFIG=%CLOUD_WATCH_DATA%\common-config.toml
SET TRANSLATOR=%CLOUD_WATCH%\config-translator.exe
rem Translate JSON into TOML
%TRANSLATOR% --input %JSON% --output %TOML% --mode ec2 --config %CONFIG%
rem Start the service
sc start AmazonCloudWatchAgent
这似乎对我有用。我还确保我的 AMI 有空的日志文件,包括 CloudWatch 代理的日志和我自己的日志。所以每个实例都重新开始。
但如果可以的话,我可能会在某个时候切换到用户数据 "amazon-cloudwatch-agent-ctl start"。