将 Google Compute 启动脚本输出发送到 Stackdriver

将 Google Compute 启动脚本输出发送到 Stackdriver

为了便于管理,我希望将我的 GCE 启动脚本的输出发送到 stackdriver,这样管理员无需通过 SSH 进入实例即可查看输出。

我发现本指南用于在 GCE 上设置 Stackdriver但它需要使用手动发送任何日志logger,并且我不想将每个命令包装在logger

我的下一个最佳想法是让文件的第一行运行尾部/var/log/daemon.log并将输出传送到记录器。

有没有更好、更万无一失的解决方案?我找不到任何东西,但这似乎是人们普遍想要的东西,我很惊讶我没有看到解决方案。

答案1

您可以通过添加输入配置来自定义 Logging 代理以向 Logging 发送其他日志。详细说明可以参见这里

  1. 从 Linux 命令提示符创建一个日志文件:

    touch /tmp/test-unstructured-log.log
    
  2. test-unstructured-log.conf在附加配置目录中创建一个新的配置文件/etc/google-fluentd/config.d

sudo tee /etc/google-fluentd/config.d/test-unstructured-log.conf <<EOF
<source>
    @type tail
    <parse>
        # 'none' indicates the log is unstructured (text).
        @type none
    </parse>
    # The path of the log file.
    path /tmp/test-unstructured-log.log
    # The path of the position file that records where in the log file
    # we have processed already. This is useful when the agent
    # restarts.
    pos_file /var/lib/google-fluentd/pos/test-unstructured-log.pos
    read_from_head true
    # The log tag for this log input.
    tag unstructured-log
</source>
EOF
  1. 重新启动代理以应用配置更改:

    sudo service google-fluentd restart
    
  2. 生成日志记录到日志文件中:

    echo 'This is a log from the log file at test-unstructured-log.log' >> /tmp/test-unstructured-log.log
    
  3. 检查日志资源管理器以查看提取的日志条目:

{
   insertId:  "eps2n7g1hq99qp"
   labels: {
    compute.googleapis.com/resource_name:  "add-unstructured-log-resource"
   }
   logName:  "projects/my-sample-project-12345/logs/unstructured-log"
   receiveTimestamp:  "2018-03-21T01:47:11.475065313Z"
   resource: {
    labels: {
     instance_id:  "3914079432219560274"
     project_id:  "my-sample-project-12345"
     zone:  "us-central1-c"
    }
    type:  "gce_instance"
   }
   textPayload:  "This is a log from the log file at test-unstructured-log.log"
   timestamp:  "2018-03-21T01:47:05.051902169Z"
  }

相关内容