在 CentOS 上让 Filebeat 与 Logstash 协同工作
架构与端口
安装与准备
sudo yum install filebeatsudo yum install logstashsudo firewall-cmd --permanent --zone=public --add-port=5044/tcpsudo firewall-cmd --reload配置步骤
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
- /var/log/nginx/error.log
fields:
log_type: nginx-access # 用于 Logstash 条件判断
fields_under_root: false
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
fields:
log_type: nginx-error
fields_under_root: false
output.logstash:
hosts: ["logstash_host:5044"]
input {
beats {
port => 5044
}
}
filter {
if [fields][log_type] == "nginx-access" {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
}
}
if [fields][log_type] == "nginx-error" {
grok {
match => { "message" => "%{ERRORLOG}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
}
}
}
output {
elasticsearch {
hosts => ["elasticsearch_host:9200"]
index => "nginx-%{+YYYY.MM.dd}"
# 如启用安全认证,补充:
# user => "elastic"
# password => "your_password"
}
}
启动与验证
sudo systemctl start filebeat && sudo systemctl enable filebeatsudo systemctl start logstash && sudo systemctl enable logstashsudo systemctl status filebeatsudo systemctl status logstashsudo journalctl -u filebeat -fsudo journalctl -u logstash -fcurl -X GET "localhost:9200/_cat/indices?v"curl -X GET "localhost:9200/filebeat-*/_search?pretty"(如启用了安全认证,请在命令中加入 -u elastic:password)常见问题与优化