将 Linux 的 systemd 日志转发到 NewRelic
Linux 上的 systemd-journal-upload 服务可用于将 systemd 的日志发送到远程的日志服务器。这样便于将日志进行集中式的归档,搜索等;而将日志保存在远程服务器,在一定程度上也提高了安全性。但是 systemd-journal-upload 的目标日志服务器是 systemd-journal-remote 服务。在云服务器大行其道的今日,搭建和使用 systemd-journal-remote 有一些老派了。时髦的做法,是将日志发送到云服务器,这样在浏览器里可以查看和全文检索。
NewRelic 是一个 SaaS 服务器,它的 Infrastruct Agent 可用于收集系统指标、日志等。对其进行简单配置,即可将 systemd 的日志,发送到 NewRelic。
安装 NewRelic Infrastructure Agent
参考《Automatically forward logs with guided install》 一节,登录 NewRelic 后,根据指引在服务器上安装 NewRelic infrastructure agent。即可收集 Linux 服务器的日志和指标。
此时 infrastructure agent 并没有转发 systemd 的日志。
配置 NewRelic Infrastructure Agent
创建 fluentbit 配置文件
默认情况下, systemd 的日志保存在 /var/log/journal 目录下,日志以二进制格式存储。为此,创建 fluent-bit 配置文件,使其处理该目录下的文件。
配置文件的路径为 /etc/fluent-bit/fluent-bit.conf,内容如下:
[INPUT] Name systemd Path /var/log/journal Tag host.*创建 NewRelic 的日志配置文件
创建配置文件 /etc/newrelic-infra/logging.d/fluentbit.yml,内容为:
logs: - name: external-fluentbit-config-and-parsers-file fluentbit: config_file: /etc/fluent-bit/fluent-bit.conf parsers_file: /etc/fluent-bit/parsers.conf重启 newrelic-infra 服务
准备好配置文件后,使用 systemctl 重启 newrelic-infra 服务:
sudo systemctl restart newrelic-infra确保服务正常运行:
sudo systemctl status newrelic-infra验证
如果一切顺利,那么 systemd 日志就出现在 NewRelic 站点的 Logs 中了。