1.filebeat安装
基于二进制安装filebeat
(1)下载filebeat软件包
(2)解压软件包
tar xf filebeat-7.17.5-linux-x86_64.tar.gz -C /app/softwares/
(3)验证filebeat安装是否成功
cd /app/softwares/filebeat-7.17.5-linux-x86_64/
ln -svf `pwd`/filebeat /usr/local/sbin/
filebeat -h(查看帮助信息)
2.filebeat相关配置
创建目录,之后将配置文件写入这里,方便管理
mkdir -p /app/softwares/filebeat-7.17.5-linux-x86_64/config
3.输入端为标准输入
vim 01-stdin-to-console.yaml
# 配置filebeat的输入端
filebeat.inputs:
# 指定输入端的类型为标准输入
- type: stdin
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
运行此配置文件:filebeat -e -c 01-stdin-to-console.yaml
当往标准输入端输入时,会从控制台输出,如下二图所示
4.filbeat的input插件之tcp案例
vim 02-tcp-to-console.yaml
filebeat.inputs:
# 指定类型为tcp
- type: tcp
# 定义tcp监听的主机和端口
host: 0.0.0.0:8888
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
运行此配置文件:filebeat -e -c 02-tcp-to-console.yaml
此时发送一个tcp请求(echo "wnhuscasc" |nc 10.0.0.101 8888)
5.filbeat的input插件之log案例
vim 03-log-to-console.yaml
filebeat.inputs:
# 指定输入类型是log
- type: log
# 指定文件路径
paths:
- /tmp/xx/*.log
- /tmp/xx/*/*.json
# 注意,两个*可以递归匹配
- /tmp/xx/**/*.exe
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
6. filbeat的input插件通用
一些通用字段的如下:
filebeat.inputs:
- type: log
paths:
- /tmp/xx/*.log
# 是否启用该类型,默认值为true。
fields:
type: log
enabled: false
- type: tcp
enabled: true
host: "0.0.0.0:8888"
# 给数据打标签,会在顶级字段多出来多个标签
tags: ["health"]
# 给数据添加KEY-VALUE类型的字段,默认是放在"fields"中的
fields:
type: tcp
form: 10.0.0.101
# 若设置为true时,则将fields添加的自定义字段放在顶级字段中,默认值为false。
fields_under_root: true
# 定义处理器,过滤指定的数据
processors:
# 删除消息是以linux开头的事件(event)
- drop_event:
when:
regexp:
message: "^linux"
# 消息包含error内容事件(event)就可以删除自定义字段或者tags。无法删除内置的字段.
- drop_fields:
when:
contains:
message: "error"
fields: ["IShealth"]
ignore_missing: false
# 修改字段的名称
- rename:
fields:
# 源字段
- from: "tags"
# 目标字段
to: "IShealth"
- from: "log"
to: "日志"
- from: "message"
to: "信息"
# 转换数据,将字段的类型转换对应的数据类型
- convert:
fields:
- {from: "ip", to: "ip", type: "ip"}
- {from: "port", to: "port", type: "integer"}
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
7.包含指定数据采集,排除指定数据采集及json格式数据采集案例
filebeat.inputs:
- type: log
paths:
- /tmp/xx/*
# 排除以log结尾的文件
exclude_files: ['\.log$']
# 只采集包含指定信息的数据
# include_lines: ['linux']
# 只要包含特定的数据就不采集该事件(event)
# exclude_lines: ['^linux']
# 将message字段的json数据格式进行解析,并将解析的结果放在顶级字段中
json.keys_under_root: true
# 如果解析json格式失败,则会将错误信息添加为一个"error"字段输出
json.add_error_key: true
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
8.使用filebeat采集nginx日志
(1)搭建nginx环境
1.1 添加yum源
cat > /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
1.2 安装nginx
yum -y install nginx
systemctl start nginx
(2)使用filebeat采集nginx日志
vim 06-log_nginx-to-console.yaml
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
output.console:
pretty: true
filebeat -e -c config/06-log_nginx-to-console.yaml
9.使用filebeat采集nginx日志
使用filebeat采集nginx的json格式日志
(1)修改nginx的配置文件,注释掉原来的日志格式,改为json格式
vim /etc/nginx/nginx.conf
...
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# access_log /var/log/nginx/access.log main;
log_format oldboyedu_nginx_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"SendBytes":$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 oldboyedu_nginx_json;
(2)热加载nginx
systemctl reload nginx
(3)测试访问nginx
curl http://10.0.0.101/
(4)filebeat采集nginx的json格式日志
vim 06-log_nginx-to-console.yaml
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
json.keys_under_root: true
json.add_error_key: true
output.console:
pretty: true
(5)启动filebeat实例
filebeat -e -c 06-log_nginx-to-console.yaml
10.使用filebeat采集tomcat访问日志
1.安装tomcat
tar xf apache-tomcat-9.0.73.tar.gz -C /app/softwares/
2.配置环境变量
cat >>/etc/profile.d/tomcat.sh <<EOF
export TOMCAT_HOME=/app/softwares/apache-tomcat-9.0.73
export PATH=$PATH:$TOMCAT_HOME/bin
EOF
3.加载一下配置文件
source /etc/profile.d/tomcat.sh
4.修改tomcat的配置文件
vim /app/softwares/apache-tomcat-9.0.73/conf/server.xml
将最后<HOST></HOST>部分修改,修改了主机名,及日志名字
<Host name="lxc" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="lxc_access_log" suffix=".txt"
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","request":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","http_user_agent":"%{User-Agent}i"}"/>
</Host>
5. 使用filebeat采集tomcat日志
vim 07-log_tomcat-to-console.yaml
filebeat.inputs:
- type: log
paths:
- /app/softwares/apache-tomcat-9.0.73/logs/lxc_access_log*.txt
json.keys_under_root: true
json.add_error_key: true
output.console:
pretty: true
6.启动tomcat
catalin start
7.访问测试
做好host解析,curl lxc:8080
11.采集tomcat的错误日志多行匹配案例
multiline为合并多行错误日志的一个设置,具体详见es官网
filebeat.inputs:
- type: log
paths:
- /app/softwares/filebeat-7.17.5-linux-x86_64/logs/catalina*
multiline.type: pattern
multiline.pattern: '^\d{2}'
multiline.negate: true
multiline.match: after
# 指定输出端为ES集群
output.elasticsearch:
hosts: ["http://10.0.0.101:9200","http://10.0.0.102:9200","http://10.0.0.103:9200"]
12.docker容器的日志收集
filebeat.inputs:
# 指定输入类型为docker类型
- type: docker
# 指定容器的ID
containers.ids:
- '*'
output.console:
pretty: true
或者
filebeat.inputs:
- type: container
paths:
- '/var/lib/docker/containers/*/*.log'
# output.console:
# pretty: true
output.elasticsearch:
hosts: ["http://10.0.0.101:9200","http://10.0.0.102:9200","http://10.0.0.103:9200"]