在CentOS系统下,定制Filebeat的日志收集规则主要涉及编辑Filebeat的配置文件。以下是详细步骤:
如果你还没有安装Filebeat,可以使用以下命令进行安装:
sudo yum install filebeat
Filebeat的配置文件通常位于 /etc/filebeat/filebeat.yml。你可以使用文本编辑器(如 vi 或 nano)打开并编辑该文件。
sudo vi /etc/filebeat/filebeat.yml
在 filebeat.inputs 部分,你可以指定要收集的日志文件路径。例如:
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/*.log
    - /var/log/myapp/*.log
如果你不想收集某些日志文件,可以使用 ignore_older 或 ignore_files 参数。例如:
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/*.log
  ignore_older: 72h
  ignore_files: ['*.log.1', '*.log.2.gz']
你可以在 fields 部分添加自定义字段到每个事件中。例如:
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/myapp/*.log
  fields:
    application: "myapp"
    environment: "production"
Filebeat支持多种处理器,可以在 processors 部分添加处理器来修改事件。例如,添加一个处理器来添加标签:
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/myapp/*.log
  processors:
  - add_fields:
      fields:
        tag: "myapp-logs"
在 output 部分,你可以指定Filebeat将日志发送到的目标。例如,发送到Elasticsearch:
output.elasticsearch:
  hosts: ["localhost:9200"]
  index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
保存并关闭配置文件后,启动Filebeat服务并设置为开机自启:
sudo systemctl start filebeat
sudo systemctl enable filebeat
你可以使用以下命令查看Filebeat的状态和日志,以确保配置正确:
sudo systemctl status filebeat
sudo journalctl -u filebeat -f
通过以上步骤,你可以根据需要定制Filebeat的日志收集规则。如果有更多高级需求,可以参考Filebeat的官方文档进行更详细的配置。