简体   繁体   English

Cloudwatch 代理未将日志发送到 cloudwatch

[英]Cloudwatch agent not sending logs to cloudwatch

I am trying to send two lots of logs up to CloudWatch.我正在尝试将两批日志发送到 CloudWatch。

Here are the two logs:这是两个日志:

  1. /var/log/apache2/access.log /var/log/apache2/access.log
  2. /var/log/apache2/error.log /var/log/apache2/error.log

I used the amazon-cloudwatch-agent-config-wizard to create the config file, and here is a snippet of the file showing the correct file path:我使用 amazon-cloudwatch-agent-config-wizard 创建配置文件,这是显示正确文件路径的文件片段:

"collect_list": [
    {
         "file_path": "/var/log/apache2/access.log",
         "log_group_name": "*group_name*",
         "log_stream_name": "apache-access"
    },
    {
         "file_path": "/var/log/apache2/error.log",
         "log_group_name": "group-name*",
         "log_stream_name": "apache-error"
    }
]

I loaded in the config with:我在配置中加载了:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

And ran into no errors and no errors and showing in the amazon-cloudwatch-agent.log.并且在 amazon-cloudwatch-agent.log 中没有遇到任何错误和错误并显示。

Checking the status of the Amazon CloudWatch Agent shows it is running and has not errors.检查 Amazon CloudWatch 代理的状态表明它正在运行并且没有错误。 Also states the schema is valid.还声明架构是有效的。

The unique part of all of this is that I have removed the old CloudWatch agent and installed the new one.所有这一切的独特之处在于我删除了旧的 CloudWatch 代理并安装了新的代理。 I have done this on two EC2 instances, one of which everything is working perfectly on, and the other one is not sending the logs to CloudWatch.我在两个 EC2 实例上完成了此操作,其中一个一切正常,另一个没有将日志发送到 CloudWatch。

In a nutshell, why aren't the logs going up to CloudWatch?简而言之,为什么日志不上传到 CloudWatch? What can I do to troubleshoot this?我能做些什么来解决这个问题?

Any help will be appreciated.任何帮助将不胜感激。

So the problem turned out to be permission-based.所以问题原来是基于权限的。 The CloudWatch config wizard defaults to using cwagent as the user that runs CloudWatch, this is also reiterated in official guides. CloudWatch 配置向导默认使用 cwagent 作为运行 CloudWatch 的用户,官方指南中也重申了这一点。

Changing the running using to root resolved the issue even though the files in question all had 777 permissions at the time of trying to get it running.即使有问题的文件在尝试使其运行时都具有 777 权限,但将运行使用更改为 root 解决了该问题。

The config file you edit is:您编辑的配置文件是:

sudo nano /opt/aws/amazon-cloudwatch-agent/bin/config.json

At the top of the file you will see:在文件顶部,您将看到:

"agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "cwagent"
},

You need to change run_as_user to root, like:您需要将 run_as_user 更改为 root,例如:

"agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "root"
},

Once you have changed that, you simply reload the config file:更改后,只需重新加载配置文件:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

And then restart the service:然后重启服务:

sudo systemctl restart amazon-cloudwatch-agent.service

You should then see the logs coming into CloudWatch.然后,您应该会看到日志进入 CloudWatch。 Expect some backfilling.期待一些回填。

Check查看

  • the CloudWatch Agent log- /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log . CloudWatch 代理/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log Here you should see some lines similar to - Reading from /var/log/apache2/access.log for both the files.在这里,您应该会看到一些类似于 - Reading from /var/log/apache2/access.log这两个文件的行。

  • the agent toml file to make sure both the files path are configured and check the region as well- /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml .代理 toml 文件以确保两个文件路径都已配置并检查区域以及/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM