es--基础--2.10.3--部署--Filebeat--案例

es–基础–2.10.3–部署–Filebeat–案例


1、input

1.1、input的log类型

cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型: /tmp/test.log
filebeat.inputs:
- type: log
  paths:
    - /tmp/test.log

#指定输出的类型
output.console:
  #打印漂亮的输出格式
  pretty: true
EOF 

# 启动  
/app/filebeat/filebeat -e -c /app/filebeat/config/test.yml

运行成功后观察终端输出,在/tmp/test.log里输入任意内容,将会以json格式来返回输入的内容到终端,即为测试成功;
在这里插入图片描述

1.2、input的通配符

cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型
filebeat.inputs:
- type: log
  paths:
    - /tmp/test.log
    - /tmp/*.txt

- type: log
  paths:
    - /tmp/test/*/*.log

#指定输出的类型
output.console:
  #打印漂亮的输出格式
  pretty: true
EOF

# 启动  
/app/filebeat/filebeat -e -c /app/filebeat/config/test.yml

采集/tmp/test.log/tmp/test/*/*.log/tmp/*.txt的文件
在这里插入图片描述

1.3、input的通用字段

cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型
filebeat.inputs:
- type: log
  #是否启用当前的输入类型,默认值为true
  enabled: true
  #指定数据路径
  paths:
    - /tmp/test.log
    - /tmp/*.txt
  #给当前的输入类型打上标签
  tags: ["test","中国","华润","招商局"]
  #自定义字段
  fields:
    school: "北京大学"
    class: "数学"

- type: log
  enabled: true
  paths:
    - /tmp/test/*/*.log
  tags: ["麦迪","云原生开发"]
  fields:
    name: "华为"
    hobby: "农业银行,抖音"
  #将自定义字段的key-value放到顶级字段,
  #默认值为false,会将数据放在一个叫fields的字段下面;
  fields_under_root: true

output.console:
  pretty: true
EOF

# 启动  
/app/filebeat/filebeat -e -c /app/filebeat/config/test.yml


在这里插入图片描述

2、输出到ES

2.1、一般配置



cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型
filebeat.inputs:
- type: log
  #是否启用当前的输入类型,默认值为true
  enabled: true
  #指定数据路径
  paths:
    - /tmp/test.log
    - /tmp/*.txt
  #给当前的输入类型打上标签
  tags: ["test","中国","华润","招商局"]
  #自定义字段
  fields:
    school: "北京大学"
    class: "数学"

- type: log
  enabled: true
  paths:
    - /tmp/test/*/*.log
  tags: ["麦迪","云原生开发"]
  fields:
    name: "华为"
    hobby: "农业银行,抖音"
  #将自定义字段的key-value放到顶级字段,
  #默认值为false,会将数据放在一个叫fields的字段下面;
  fields_under_root: true

# 改动点-----输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test666-%{+yyyy.MM.dd}"

#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test666"
#设置索引模板的匹配模式
setup.template.pattern: "test666-*"
EOF

在这里插入图片描述

2.2、根据不同的标签/字段,写入不同的索引


cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型
filebeat.inputs:
- type: log
  #是否启用当前的输入类型,默认值为true
  enabled: true
  #指定数据路径
  paths:
    - /tmp/test.log
    - /tmp/*.txt
  #给当前的输入类型打上标签
  tags: ["test","中国","华润","招商局"]
  #自定义字段
  fields:
    school: "北京大学"
    class: "数学"

- type: log
  enabled: true
  paths:
    - /tmp/test/*/*.log
  tags: ["麦迪","云原生开发"]
  fields:
    name: "华为"
    hobby: "农业银行,抖音"
  #将自定义字段的key-value放到顶级字段,
  #默认值为false,会将数据放在一个叫fields的字段下面;
  fields_under_root: true

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  #index: "test666-%{+yyyy.MM.dd}"
  # 改动点如下------------
  indices:
    - index: "test666-hello1-%{+yyyy.MM.dd}"
      #匹配指定字段包含的内容
      when.contains:
        tags: "中国"
    - index: "test666-hello2-%{+yyyy.MM.dd}"
      when.contains:
        tags: "云原生开发"

#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test666"
#设置索引模板的匹配模式
setup.template.pattern: "test666-*"
EOF

测试数据

mkdir -p /tmp/test/test/ 
echo hello1  >> /tmp/test.log
echo hello2  >> /tmp/test/test/1.log

在这里插入图片描述

在这里插入图片描述

2.3、自定义分片和副本数量案例




cat > /app/filebeat/config/test.yml << EOF
# 指定输入的类型
filebeat.inputs:
- type: log
  #是否启用当前的输入类型,默认值为true
  enabled: true
  #指定数据路径
  paths:
    - /tmp/test.log
    - /tmp/*.txt
  #给当前的输入类型打上标签
  tags: ["test","中国","华润","招商局"]
  #自定义字段
  fields:
    school: "北京大学"
    class: "数学"

- type: log
  enabled: true
  paths:
    - /tmp/test/*/*.log
  tags: ["麦迪","云原生开发"]
  fields:
    name: "华为"
    hobby: "农业银行,抖音"
  #将自定义字段的key-value放到顶级字段,
  #默认值为false,会将数据放在一个叫fields的字段下面;
  fields_under_root: true

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  #index: "test777-%{+yyyy.MM.dd}" 
  indices:
    - index: "test777-hello1-%{+yyyy.MM.dd}"
      #匹配指定字段包含的内容
      when.contains:
        tags: "中国"
    - index: "test777-hello2-%{+yyyy.MM.dd}"
      when.contains:
        tags: "云原生开发"

#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test777"
#设置索引模板的匹配模式
setup.template.pattern: "test777-*"

# 改动点如下------------
#覆盖已有的索引模版,如果为true则会直接覆盖现有的模版,为false则为不覆盖;
setup.template.overwrite: false
#配置索引模版
setup.template.settings:
  #设置分片数量
  index.number_of_shards: 3
  #设置副本数量,要求小于集群节点的数量,否则会出现副本分片无法分配的情况(集群状态呈黄色)
  index.number_of_replicas: 1
EOF


在这里插入图片描述

3、nginx日志

3.1、基于log类型收集nginx原生日志





cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log*
  tags: ["nginx-access"]


# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test888-%{+yyyy.MM.dd}" 
 

#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test888"
#设置索引模板的匹配模式
setup.template.pattern: "test888-*"

#覆盖已有的索引模版,如果为true则会直接覆盖现有的模版,为false则为不覆盖;
setup.template.overwrite: false
#配置索引模版
setup.template.settings:
  #设置分片数量
  index.number_of_shards: 3
  #设置副本数量,要求小于集群节点的数量,否则会出现副本分片无法分配的情况(集群状态呈黄色)
  index.number_of_replicas: 2
EOF
 

在这里插入图片描述
在这里插入图片描述

3.2、基于log类型收集nginx的json日志

3.2.1、修改nginx的日志格式

log_format main '{"@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"}';

nginx的日志

{"@timestamp":"2024-10-27T19:45:38+08:00","host":"192.168.187.88","clientip":"192.168.187.1","SendBytes":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.187.88","uri":"/index.html","domain":"192.168.187.88","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/130.0.0.0 Safari/537.36","status":"304"}

{"@timestamp":"2024-10-27T19:45:38+08:00","host":"192.168.187.88","clientip":"192.168.187.1","SendBytes":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.187.88","uri":"/index.html","domain":"192.168.187.88","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/130.0.0.0 Safari/537.36","status":"304"}
 

3.2.2、配置





cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log*
  tags: ["nginx-access"]
  #-------改动点如下-------
  #以JSON格式解析message内容,拆分字段;
  json.keys_under_root: true

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test999-%{+yyyy.MM.dd}" 
 

#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test999"
#设置索引模板的匹配模式
setup.template.pattern: "test999-*"

#覆盖已有的索引模版,如果为true则会直接覆盖现有的模版,为false则为不覆盖;
setup.template.overwrite: false
#配置索引模版
setup.template.settings:
  #设置分片数量
  index.number_of_shards: 3
  #设置副本数量,要求小于集群节点的数量,否则会出现副本分片无法分配的情况(集群状态呈黄色)
  index.number_of_replicas: 2
EOF


  

在这里插入图片描述

4、基于modules类型采集文件

4.1、采集nginx日志文件

4.1.1、编辑nginx模块的配置文件



# 查看并启用nginx模块
# 查看现有的模块
# /app/filebeat/filebeat modules list

#开启nginx模块,禁用使用disable
/app/filebeat/filebeat modules enable nginx

 

# 编辑nginx模块的配置文件
cat >  /app/filebeat/modules.d/nginx.yml << EOF
- module: nginx
  access:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/access.log"]
  error:
    enabled: false
    var.paths: ["/usr/local/nginx/logs/error.log"]
  ingress_controller:
    enabled: false
EOF
	
	

4.1.2、配置

cat > /app/filebeat/config/test.yml << EOF
filebeat.config.modules:
  #指定模块的配置文件路径; 
  path: /app/filebeat/modules.d/*.yml
  #开启热加载功能
  reload.enabled: false

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1111" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1111"
#设置索引模板的匹配模式
setup.template.pattern: "test1111"
EOF

	

在这里插入图片描述

4.2、采集tomcat日志文件

4.2.1、编辑tomcat模块的配置文件


# 查看并启用tomcat模块
# 查看现有的模块
cd /app/filebeat/
# ./filebeat modules list

#开启tomcat模块,禁用使用disable
./filebeat modules enable tomcat




# 编辑tomcat模块的配置文件
cat >  /app/filebeat/modules.d/tomcat.yml << EOF
- module: tomcat
  log:
    enabled: true
    # 指定输入的类型是文件(file),默认是监听udp端口(不会向es传输日志)
    var.input: file
    #tomcat访问日志的位置
    var.paths:
      - /tmp/demo/logs/access_log*.log
EOF
	
	

4.2.2、tomcat配置

# tomcat配置
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=/tmp/demo/logs/
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
server.tomcat.accesslog.pattern='[%{yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ}t][%I][access|%m|%q|%U|%a|%D|%s|%b][%{tRequestId}i|%{X-Request-ID}i|%{Referer}i|%{User-Agent}i][%{X-B3-TraceId}i|%{X-B3-SpanId}i|%{X-B3-ParentSpanId}i|%{X-Span-Export}i][%{keywords}i]'
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.rename-on-rotate=false
server.tomcat.accesslog.rotate=true

	

4.2.3、配置


cat > /app/filebeat/config/test.yml << EOF
filebeat.config.modules:
  #指定模块的配置文件路径; 
  path: /app/filebeat/modules.d/*.yml
  #开启热加载功能
  reload.enabled: false

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1112" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1112"
#设置索引模板的匹配模式
setup.template.pattern: "test1112"
EOF



	

在这里插入图片描述

5、tomcat日志

5.1、基于log类型收集tomcat的原生日志


cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  enabled: true
  #tomcat的访问日志位置
  paths:
    - /tmp/demo/logs/access_log*.log
  tags: ["tomcat-access-json"]

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1113" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1113"
#设置索引模板的匹配模式
setup.template.pattern: "test1113"
EOF


	

在这里插入图片描述

5.2、基于log类型多行匹配案例-收集tomcat的错误日志


cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  enabled: true
  #tomcat的访问日志位置
  # -------改动点如下-------
  paths:
    - /tmp/demo/*.out 
  tags: ["tomcat-error-json"]  
  #指定多行匹配的类型,可选值为pattern(常用,只要下面匹配的参数出现就换行),count(次数,用于下面匹配的参数出现多少次就换行)
  multiline.type: pattern
  #指定匹配的模式,这里的'^\d{2}代表的是以两个数字开头的,例如11或者12' 
  multiline.pattern: '^\d{2}'
  #下面两个参数参考官方架构图即可;https://www.elastic.co/guide/en/beats/filebeat/7.17/multiline-examples.html
  multiline.negate: true
  multiline.match: after

  

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1114" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1114"
#设置索引模板的匹配模式
setup.template.pattern: "test1114"
EOF


	

6、日志过滤

6.1、排除日志中指定的内容

类似【黑名单】的作用


 
cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  paths:
    - /tmp/test.log
  #不采集的日志中包含指定的内容(例如下方指定的以"INFO"开头的数据、日志中包含"发送消息成功"的数据、日志中以"SUCCESS"结尾的数据)区分大小写
  exclude_lines: ['^INFO','发送消息成功','SUCCESS$']

output.console:
  pretty: true
EOF

6.2、只收集日志中指定的内容

类似【白名单】的作用


 
cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  paths:
    - /tmp/test.log
  #只采集的日志中包含的指定内容(例如下方指定的以"ERROR"开头的数据、日志中包含"返回消息失败"的数据、日志中以"Failed"结尾的数据)区分大小写!
  include_lines: ['^ERROR', '发送消息失败','Failed$']

output.console:
  pretty: true
EOF

	

7、filestream类型

7.1、解析json日志案例

官方警告,未来log类型的input可能会被弃用,建议使用filestream类型


cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: filestream
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log*
  tags: ["nginx-access-json"]
  #对于filestream类型而言,不能直接配置json解析,而是需要配置解析器实现
  #json.keys_under_root: true 
  #综上所述,需要以下的写法实现
  parsers:
    - ndjson:
      #对json字段内容进行JSON格式解析,并将key放到顶级字段
      keys_under_root: true

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1115" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1115"
#设置索引模板的匹配模式
setup.template.pattern: "test1115"
EOF


在这里插入图片描述

7.2、多行匹配案例–收集tomcat的错误日志



cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
# 改动点 type: filestream
- type: filestream
  enabled: true
  #tomcat的日志位置
  paths:
    - /tmp/demo/*.out 
  tags: ["tomcat-error-json"]  
  #指定多行匹配的类型,可选值为pattern(常用,只要下面匹配的参数出现就换行),count(次数,用于下面匹配的参数出现多少次就换行)
  multiline.type: pattern
  #指定匹配的模式,这里的'^\d{2}代表的是以两个数字开头的,例如11或者12' 
  multiline.pattern: '^\d{2}'
  #下面两个参数参考官方架构图即可;https://www.elastic.co/guide/en/beats/filebeat/7.17/multiline-examples.html
  multiline.negate: true
  multiline.match: after

  

# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1114" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1114"
#设置索引模板的匹配模式
setup.template.pattern: "test1114"
EOF


8、日志聚合

8.1、聚合到本地

日志聚合就是将多个源端的日志,聚合到同一台服务器上;
例如
客户端A 发送日志111
客户端B 发送日志222
服务器C 收集到的日志为 111和222

#基于input的tcp和udp类型将多个客户端的日志聚合到filebeat本地的文件中
cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
#tcp类型
- type: tcp 
#监听的地址和端口
  host: "192.168.1.4:9000"
- type: tcp
  host: "192.168.1.4:9001"
#udp类型  
- type: udp
  host: "192.168.1.4:8000"

output.file:
  #文件存放的位置
  path: "/tmp/filebeat"
  #指定文件名
  filename: test1104.log
  #指定文件的滚动大小,默认值是10MB
  rotate_every_kb: 10000
  #指定保存文件的个数,默认是7个,有效值是2-1024个
  number_of_files: 7
  #指定文件的权限,默认是0600,属主拥有可读可写的权限
  permissions: 0600
EOF


8.2、聚合到ES

基于input的tcp和udp类型将多个客户端的日志聚合到filebeat然后输出给ES

cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: tcp
  host: "192.168.1.4:9000"
  tags: ["tcp-message"]

- type: tcp
  host: "192.168.1.4:9001"
  tags: ["tcp-message"]

- type: udp
  host: "192.168.1.4:8000"
  tags: ["udp-message"]



# 输出类型选择elasticsearch
output.elasticsearch:
  enabled: true
  #ES地址
  hosts: ["http://192.168.187.88:9200","http://192.168.187.88:9200"]
  username: "elastic"
  password: "123456"
  # 索引名称
  index: "test1114" 
 
 
#禁用索引生命周期管理,如果开启的话则会忽略我们自定义的索引;
setup.ilm.enabled: false
#设置索引模板的名称
setup.template.name: "test1114"
#设置索引模板的匹配模式
setup.template.pattern: "test1114"
EOF

9、将filebeat收集的数据写入redis


cat > /app/filebeat/config/test.yml << EOF
filebeat.inputs:
- type: log
  paths:
    - /tmp/test.log

output.redis:
  #写入redis的主机地址
  hosts: ["192.168.1.2:6379"]
  #指定redis的认证口令(登录密码)
  password: "123456"
  #指定的key的值,可自定义
  key: "filebeat-test-redis"
  #指定redis桶(数据库)编号
  db: 7
  #规定超时时间
  timeout: 5
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值