关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

使用 Filebeat 收集 Ubuntu 系统日志

发布时间:2022/10/20 20:38:20
香港云服务器

Filebeat是用于转发和集中日志数据的轻量级传送程序。作为服务器上的代理安装,Filebeat监视您指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引。

启用 System Module Filebeat 支持的模块默认都是未启用的,我们可以通过下面的方式启用模块。找到 filebeat 程序,执行 moudles enable 命令:

$ sudo ./filebeat modules enable system

上面的命令启用了 system 模块,用下面的命令可以查看当前已经启用的模块有哪些:

$ sudo ./filebeat modules list


把数据发送给 logstash 配置 Filebeat 将日志行发送到 Logstash。要做到这一点,在配置文件 filebeat.yml 中禁用 Elasticsearch 输出,并启用 Logstash 输出:

#output.elasticsearch: #hosts: ["xxx.xxx.xxx.xxx:9200"] output.logstash: hosts: ["xxx.xxx.xxx.xxx:5044"]

重启 filebeat 服务

$ sudo systemctl restart filebeat.service

配置 Logstash 处理数据

要让 logstash 接受 Filebeat System Module 发送来的数据还是有些难度的,至少我们需要一个看上去有点复杂的配置:

处理时区问题 看到这段配置我多么希望它能够直接工作啊!不幸的是它并不能很好的工作,至少在我的 ubuntu 18.04 上不行。问题的核心是无论 auth.log 还是 syslog,记录的都是本地时区的区时:


而上面的配置中把这些时间都当成 UTC 时间来处理了。搞清楚了原因,纠正起来就很容易了,在 date 插件中添加本地的时区信息就可以了。比如笔者所在时区为东八区,那么就分别在两个 date 的配置中添加下面的信息:

timezone => "Asia/Chongqing"

让独立的 pipeline 处理该数据流 下面创建一个新的目录 /etc/logstash/myconf.d,并在 /etc/logstash/myconf.d 目录下创建 Logstash 配置文件 krtest.conf。然后在 /etc/logstash/pipelines.yml 文件中添加新的 pipeline 配置: – pipeline.id: main path.config: “/etc/logstash/conf.d/*.conf” – pipeline.id: krtest path.config: “/etc/logstash/myconf.d/krtest.conf” 其中 pipeline.id 为 main 的管道是默认的配置,我们新添加了 id 为 krtest 的管道并指定了对应的配置文件路径。把上面的配置写入到 /etc/logstash/myconf.d/krtest.conf 文件中。然后重启 logstash 服务:

$ sudo systemctl restart logstash.service

在 Kibana 中查看日志

最后在 kibana 中添加 filebeat 开头的 index pattern,就可以通过图形界面查看 ubuntu 的系统日志了: