在 CentOS 上使用 Filebeat 监控日志,一般流程如下(以 Filebeat 监控文件日志并发送到 Elasticsearch / Logstash 为例):
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo cat > /etc/yum.repos.d/filebeat.repo <<EOF
[elastic-8.x]
name=Elastic repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
sudo yum install filebeat -y
如果是 7.x,把
8.x改成7.x
vi /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/nginx/*.log
- /var/log/messages
老版本写法(≤7.x):
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["http://localhost:9200"]
username: "elastic"
password: "your_password"
output.logstash:
hosts: ["localhost:5044"]
systemctl enable filebeat
systemctl start filebeat
查看状态:
systemctl status filebeat
journalctl -u filebeat -f
echo "test log" >> /var/log/messages
curl -X GET "localhost:9200/_cat/indices?v"
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
fields:
env: production
app: nginx
exclude_files: ['.gz$']
例如监控 Nginx:
filebeat modules enable nginx
filebeat setup
配置文件在:
/etc/filebeat/modules.d/nginx.yml
✅ 没数据?
enabled: true✅ 权限问题?
chmod 644 /var/log/messages
如果你能告诉我:
我可以给你 更精确的配置示例。