NGINX日志未显示在CloudWatch中

yizd12fk  于 2023-04-20  发布在  Nginx
关注(0)|答案(1)|浏览(158)

提前感谢您的帮助。我是AWS的新手。
我尝试将NGINX日志从我的EC2示例记录到CloudWatch,但我无法使其工作,我不知道我错过了什么。
总结一下,我遵循的步骤是:
1.当我在docker中运行NGINX时,我将卷Map到我的本地文件夹(假设为/home/ubuntu/nginx/logs/),并检查它是否登录到文件access.logerror.logjson_access.log
1.我安装了CloudWatch代理如下:https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html
1.我使用CloudWatchAgentServerPolicy策略创建了一个IAM角色。
1.我将该IAM角色添加到运行NGINX的EC2示例中
1.我使用sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard配置了CloudWatch代理,并给出了以下答案:
1.您计划在哪个操作系统上使用代理?答:Linux
1.您使用的是EC2主机还是本地主机?EC2
1.您计划由哪个用户运行代理?答:根
1.是否要打开StatsD守护程序?答:否
1.是否要监视CollectD中的指标?答:否
1.是否要监视任何主机指标?答:否
1.您是否有任何现有的CloudWatch日志代理?答:否
1.是否要监视任何日志文件?答:是的
1.日志文件路径:/home/Ubuntu/nginx/logs/
1.日志组名称:nginx
1.日志流名称:[{instance_id}](默认)
1.是否要指定要监视的任何其他日志文件?答:否
1.是否要将配置存储在SSM参数存储区中?答:否
1.我启动了代理:sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a start
通过这些步骤,我希望代理会自动推送日志组并流到cloudwatch中,并且我会在CloudWatch〉Log groups〉nginx〉myinstanceid中获得所有原始日志。
但这并没有发生。
因此,我手动创建了日志组和日志流,命名方式与配置中的相同,但仍然没有任何结果。我还尝试使用显式日志流名称重做所有内容,以防我拼写错误的示例ID,但没有任何结果。
这是向导生成的config.json:

{
    "agent": {
        "run_as_user": "root"
    },
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/home/ubuntu/nginx/logs/",
                        "log_group_name": "nginx",
                        "log_stream_name": "{instance_id}",
                        "retention_in_days": 30
                    }
                ]
            }
        }
    }

我错过了什么?或者我做错了什么?
再次感谢你!
EDIT:代理日志/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log的输出:
(Note:我不得不手动将config.json从向导复制到/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json中。

2023/04/19 06:46:52 I! D! [EC2] Found active network interface
I! Detected the instance is EC2
2023/04/19 06:46:52 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json ...
2023/04/19 06:46:52 I! Valid Json input schema.
I! Detecting run_as_user...
I! Trying to detect region from ec2
No csm configuration found.
No metric configuration found.
Configuration validation first phase succeeded
 
2023/04/19 06:46:52 I! Config has been translated into TOML /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml 
2023/04/19 06:46:52 D! toml config [agent]
  collection_jitter = "0s"
  debug = false
  flush_interval = "1s"
  flush_jitter = "0s"
  hostname = ""
  interval = "60s"
  logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
  logtarget = "lumberjack"
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  omit_hostname = false
  precision = ""
  quiet = false
  round_interval = false

[inputs]

  [[inputs.logfile]]
    destination = "cloudwatchlogs"
    file_state_folder = "/opt/aws/amazon-cloudwatch-agent/logs/state"

    [[inputs.logfile.file_config]]
      file_path = "/home/ubuntu/nginx/logs/"
      from_beginning = true
      log_group_name = "nginx"
      log_stream_name = "MY INSTANCE ID"
      pipe = false
      retention_in_days = 30
    [inputs.logfile.tags]
      metricPath = "logs"

[outputs]

  [[outputs.cloudwatchlogs]]
    force_flush_interval = "5s"
    log_stream_name = "MY INSTANCE ID"
    region = "eu-west-1"
    tagexclude = ["metricPath"]
    [outputs.cloudwatchlogs.tagpass]
      metricPath = ["logs"]
2023/04/19 06:46:52 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json ...
2023/04/19 06:46:52 I! Valid Json input schema.
2023/04/19 06:46:52 I! Detected runAsUser: root
2023/04/19 06:46:52 I! Changing ownership of [/opt/aws/amazon-cloudwatch-agent/logs /opt/aws/amazon-cloudwatch-agent/etc /opt/aws/amazon-cloudwatch-agent/var] to 0:0
2023-04-19T06:46:53Z I! Starting AmazonCloudWatchAgent 1.247358.0
2023-04-19T06:46:53Z I! AWS SDK log level not set
2023-04-19T06:46:53Z I! Loaded inputs: logfile
2023-04-19T06:46:53Z I! Loaded aggregators: 
2023-04-19T06:46:53Z I! Loaded processors: 
2023-04-19T06:46:53Z I! Loaded outputs: cloudwatchlogs
2023-04-19T06:46:53Z I! Tags enabled: host=ip-172-XX-XX-XXX
2023-04-19T06:46:53Z I! [agent] Config: Interval:1m0s, Quiet:false, Hostname:"ip-172-XX-XX-XXX", Flush Interval:1s
2023-04-19T06:46:53Z I! [logagent] starting
2023-04-19T06:46:53Z I! [logagent] found plugin cloudwatchlogs is a log backend
2023-04-19T06:46:53Z I! [logagent] found plugin logfile is a log collection

还有:

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a status
{
  "status": "running",
  "starttime": "2023-04-19T06:46:52+00:00",
  "configstatus": "configured",
  "version": "1.247358.0b252413"
}
ymdaylpp

ymdaylpp1#

你可以参考我的代码片段:

sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -O /tmp/amazon-cloudwatch-agent.deb 
sudo dpkg -i /tmp/amazon-cloudwatch-agent.deb 
 
sudo chmod +x /home/ubuntu/nginx/logs 
sudo tee -a /opt/aws/amazon-cloudwatch-agent/bin/config.json << EOF 
{ 
   "agent": { 
      "run_as_user": "root" 
   }, 
   "metrics":{ 
      "namespace":"CwAgent", 
      "append_dimensions": { 
          "InstanceId": "\${aws:InstanceId}" 
      }, 
      "aggregation_dimensions" : [["InstanceId"]], 
      "metrics_collected":{ 
         "mem":{ 
            "measurement":[ 
               "mem_used_percent" 
            ] 
         }, 
         "disk":{ 
            "measurement":[ 
               "used_percent" 
            ], 
            "resources":[ 
               "/" 
            ] 
         } 
      } 
   }, 
   "logs":{ 
      "logs_collected":{ 
         "files":{ 
            "collect_list":[ 
               { 
                 "file_path": "/home/ubuntu/nginx/logs/access*", 
                 "log_group_name": "/api", 
                 "log_stream_name": "{instance_id}" 
               },
               { 
                 "file_path": "/home/ubuntu/nginx/logs/error*", 
                 "log_group_name": "/api", 
                 "log_stream_name": "{instance_id}" 
               }
            ] 
         } 
      } 
   } 
} 
EOF 
 
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 

sudo amazon-cloudwatch-agent-ctl -a status

相关问题