FileBeat
FileBeat
filebeat部署(二进制方式)
1.解压
tar -zxf filebeat-7.xx.x-linux-x86_64.tar.gz -C /opt/software
2.环境变量
vim /etc/profile.d/elk-filebeat.sh
export FILEBEAT_HOME=/opt/elk/filebeat
export PATH=$PATH:$FILEBEAT_HOME
3.编写配置文件
vim 01-stdin-to-console.yml
filebeat.inputs:
- type: sdin
output.console:
pretty: true
EOF
4.启动filebeat实例
./filebeat -e -c 01-stdin-to-console.yml
5.启动文件
vim /usr/lib/systemd/system/filebeat.service
[Unit]
Description=filebeat
After=network.target
[Service]
ExecStart=/opt/elk/filebeat/filebeat \
-c /opt/elk/filebeat/filebeat/01-tomcat-to-logstash.yml
-path.home /usr/local/filebeat \
-path.config /usr/local/filebeat \
-path.data /var/lib/filebeat \
-path.logs /var/log/filebeat
Restart=no
[Install]
WantedBy=multi-user.target
yum启动filebeat
filebeat -e -c /path/to/filebeat-config-file
-e 将日志流输出到标准错误流(stderr)而不是默认的日志文件
-c 指定配置文件
filebeat多实例
filebeat -c /path/to/filebeat-config-file --path.data /other/path/to/filebeat-config-file
--path.data 指定不同的配置文件启动
# 加载模块
filebeat.config.modules:
path: /usr/share/filebeat/modules.d/*.yml
# 是否开启热加载。修改模块文件后直接生效
reload.enabled: true
filebeat.inputs:
- type: log
# 启用这个input
enabled: false
paths:
- /tmp/test.log
- /tmp/*.txt
tags: ["json", "test"]
# 将json格式日志中的key设置为日志查看字段
json.keys_under_root: true
# 自定义字段,会在输出中展示
fields:
name: "xiaoming"
hobby: "linux,tictoc"
# 顶级字段,默认false。将fields中的字段设置为与tags等相同的缩进
fields_under_root: true
# 包含sometext字段的日志会被收集,区分大小写
include_lines: ['sometext']
# 以DBG开头的会被排除
exclude_lines: ['^DBG']
- type: log
enabled: true
paths:
- /tmp/test/*/*.log
tags: ["host", "good"]
# 多行匹配的类型,pattern或count
multiline.type: pattern
# 具体的匹配模式,两个数字开头
multimine.pattern: "^\d{2}"
# 合并行的方式,true或false
multimine.negate: true
# 合并行的方式,after或before
multimine.match: after
output.elasticsearch:
hosts: ["http://es1IP:9200", "http://es2IP:9200"]
# 设置默认索引名称,需要关闭索引生命周期管理
# index: "hello-world-%{+yyyy.MM.dd}"
# 根据tags字段的不同,来设置不同的索引
indeices:
- index: "hello-world-json%{+yyyy.MM.dd}"
when.contains:
tags: "json"
- index: "hello-world-host%{+yyyy.MM.dd}"
when.contains:
tags: "host"
# 禁用索引生命周期管理
setup.ilm.enabled: false
# 设置索引模板的名字
setup.template.name: "hello-world-linux"
# 设置索引模板匹配的索引模式
setup.template.pattern: "hello-world-liunx*"
# 是否覆盖已有的索引模板
setup.template.overwrite: false
setup.template.settings:
# 分片数
index.number_of_shards: 3
# 副本数
index.number_of_replicas: 0
# 输出到控制台,调试用
output.console:
pretty: true
filebeat收集nginx的json格式日志
1.将nginx日志格式化为json格式
...
log_format json_nginx '{"@timestamp": "$time_iso8601",'
'"host": "$server_addr",'
'"clientip": "$remote_addr",'
'"size": "$body_bytes_sent",'
'"responsetime": "$request_time",'
'"upstreamtime": "$upstream_response_time",'
'"upstreamhost": "$upstream_addr",'
'"http_host": "$host",'
'"uri": "$uri",'
'"domain": "$host",'
'"xff": "$http_x_forwarded_for",'
'"referer": "$http_referer",'
'"tcp_xff": "$proxy_protocol_addr",'
'"http_user_agent": "$http_user_agent",'
'"status": "$status"}';
access_log /var/log/nginx/access.log json_nginx;
2.filebeat设置
json.keys_under_root: true
filebeat内置模块收集nginx日志
1.编辑filebeat.yml,filebeat加载nginx模块
filebeat.config.modules:
path: /usr/share/filebeat/modules.d/*.yml
reload.enabled: false
filebeat modules list
filebeat modules enable/disable nginx
2.编辑nginx模块文件,指定nginx日志路径
vi modules.d/nginx.yml
access:
...
var.paths: ["/var/log/nginx/access.log*"]
error:
...
var.paths: ["/var/log/nginx/error.log*"]
filebeat使用模块收集tomcat日志
1.编辑filebeat.yml,filebeat加载tomcat模块
filebeat.config.modules:
path: /usr/share/filebeat/modules.d/*.yml
reload.enabled: false
filebeat modules list
filebeat modules enable/disable tomcat
2.编辑tomcat模块文件,指定tomcat日志路径
- module: tomcat
log:
enabled: true
# 从文件读取日志
var.input: file
# 使用TCP协议读取日志
# var.input: tcp
# var.syslog_host: 0.0.0.0
# var.syslog_port: 9501
# 使用UDP协议读取日志
# var.input: udp
# var.syslog_host: 0.0.0.0
# var.syslog_port: 9501
var.paths:
- "/path/to/tomcat_log"
filebeat收集tomcat原生日志&日志的多行匹配
filebeat.inputs:
- type: log
enabled: true
paths:
- /path/to/tomca_log
# 多行匹配的类型,pattern或count
multiline.type: pattern
# 具体的匹配模式,两个数字开头
multimine.pattern: "^\d{2}"
# 合并行的方式,true或false
multimine.negate: true
# 合并行的方式,after或before
multimine.match: after
output.elasticsearch:
hosts: ["http://es1IP:9200", "http://es2IP:9200"]
index: "hello-world-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "hello-world-linux"
setup.template.pattern: "hello-world-liunx*"
setup.template.overwrite: false
filebeat收集tomcat的json格式日志
在原生日志配置的基础上增加
json.keys_under_root: true
KQL kibana查询语句
filebeat日志过滤
filebeat.inputs:
...
# 包含sometext字段的日志会被收集,区分大小写
include_lines: ['sometext']
# 以DBG开头的会被排除
exclude_lines: ['^DBG']
filestream类型json解析&多行匹配
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /path/to/tomca_log
# 配置解析器
parsers:
# 日志转换为json格式
- ndjson:
keys_under_root: true
# 日志多行匹配
- multiline:
type: pattern
pattern: "^\d{2}"
negate: true
match: before
output.elasticsearch:
hosts: ["http://es1IP:9200", "http://es2IP:9200"]
index: "hello-world-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "hello-world-linux"
setup.template.pattern: "hello-world-liunx*"
setup.template.overwrite: false
input源配置一旦超过4个,在写入ES时,可能会导致无法写入:
解决方法一:拆分成多个filebeat实例,运行多个filebeat实例需要指定新的数据路径"--path.data"。因为数据路径下有filbbeat.lock
解决方法二:日志聚合,思路就是将多个日志合并为一个
rsyslog日志聚合
yum install rsyslog
vim /etc/rsyslog.conf
...
#$ModLoad imudp
#$UDPServerRun 514
...
# 匹配所有日志到指定目录
*.* /var/log/all-log.log
systemctl restart rsyslog
filebeat通过TCP方式读取日志(实时发送给ES)
适合读取交换机等不能安装filebeat的设备的日志
可以使用telnet filebeatIP 9000 来模拟日志传入
filebeat.inputs:
# 开启9000端口读取日志,其他应用将日志发送到本机的9000端口
- type: tcp
host: "0.0.0.0:9000"
tags: ["TCP"]
output.elasticsearch:
hosts: ["http://es1IP:9200", "http://es2IP:9200"]
index: "hello-world-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "hello-world-linux"
setup.template.pattern: "hello-world-liunx*"
setup.template.overwrite: false
filebeat通过TCP方式读取日志聚合到本地
filebeat.inputs:
- type: tcp
host: "0.0.0.0:9000"
tags: ["TCP"]
# 输出到本地
output.file:
path: "/tmp/filebeat"
filename: all-log
# 目录中的文件个数,2-1024
number_of_files: 10
# 单个文件最大容量,默认102400KB
rotate_every_kb: 102400
# 权限,默认0600
permissions: 0644
setup.ilm.enabled: false
setup.template.name: "hello-world-linux"
setup.template.pattern: "hello-world-liunx*"
setup.template.overwrite: false
filebeat通过TCP方式将日志写入redis
filebeat.inputs:
- type: tcp
host: "0.0.0.0:9000"
tags: ["TCP"]
# 输出到redis
output.redis:
# 指定redis主机和端口
hosts: ["0.0.0.0:6379"]
# 指定redis的认证密码
password: "password"
# 指定key值
key: "my-filebeat"
# 指定数据库编号
db: 0
# 指定redis连接超时时间,单位秒
timeout: 5
setup.ilm.enabled: false
setup.template.name: "hello-world-linux"
setup.template.pattern: "hello-world-liunx*"
setup.template.overwrite: false
nc和telnet数据写入说明
cat /tmp/sometext | nc 10.0.0.1 9000
echo "hello" | nc 10.0.0.1 9000
分析IP来自与哪个省,访问的设备是苹果用户还是安卓用户。或者分析自研非开源软件的日志,这种日至filebet没有对应的模块。此时引入logstash
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)