一、简介
Filebeat是一个轻量级的日志采集工具,可以用于转发日志数据到Elasticsearch。在使用Filebeat采集Nginx和Tomcat日志时,特别是在处理多行日志模式时,需要进行适当的配置以确保日志能够被正确解析和收集。
二、配置
0.nginx/tomcat/docker服务器安装filebeat
#创建目录
mkdir -p /es/softwares/
#软件包下载
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.5-linux-x86_64.tar.gz
#解压软件包
tar xf filebeat-7.17.5-linux-x86_64.tar.gz -C /es/softwares/
#创建软连接 全局可用
cd /es/softwares/filebeat-7.17.5-linux-x86_64/
ln -s /es/softwares/filebeat-7.17.5-linux-x86_64/filebeat /usr/local/sbin/
#版本查看
filebeat version
1.创建Filebeat配置文件 采集nginx/tomcat日志
cat >01-nginx-tomcat-to-es.yaml<<'EOF'
filebeat.inputs:
#nginx日志文件采集
- type: log
enabled: true
paths:
- /var/log/nginx/access.log*
- /var/log/nginx/error.log*
#多行选项配置
multiline.type: pattern
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
#tomcat日志文件采集
- type: log
enabled: true
paths:
- /app/tools/tomcat/logs/localhost_access_log.*.txt
- /app/tools/tomcat/logs/catalina*
#多行选项配置
multiline.type: pattern
multiline.pattern: '^\s*(\d{4}|\d{2})\-(\d{2}|[a-zA-Z]{3})\-(\d{2}|\d{4})'
multiline.negate: true
multiline.match: after
#指定输出端为ES集群
output.elasticsearch:
hosts: ["http://192.168.77.176:9200","http://192.168.77.177:9200","http://192.168.77.178:9200"]
#配置kibana地址
setup.kibana:
host: "192.168.77.176:5601"
EOF
请根据你的实际日志路径、端口和Elasticsearch地址和kibana地址进行相应的调整。
启动Filebeat时,确保指定正确的配置文件:
filebeat -e -c 01-nginx-tomcat-to-es.yaml
kibana查看
#KQL语法
log.file.path : "/app/tools/tomcat/logs/catalina.out" and message:"Caused by"
2.创建Filebeat配置文件 采集docker日志
cat >01-docker-to-es.yaml<<'EOF'
filebeat.inputs:
#docker日志文件采集
- type: container
paths:
- '/var/lib/docker/containers/*/*.log'
json.keys_under_root: true
json.add_error_key: true
json.message_key: log
#指定输出端为ES集群
output.elasticsearch:
hosts: ["http://192.168.77.176:9200","http://192.168.77.177:9200","http://192.168.77.178:9200"]
#配置kibana地址
setup.kibana:
host: "192.168.77.176:5601"
EOF
请根据你的实际日志路径、端口和Elasticsearch地址和kibana地址进行相应的调整。
启动Filebeat时,确保指定正确的配置文件:
filebeat -e -c 01-docker-to-es.yaml
kibana查看