Filebeat 介绍

Filebeat 安装

# 上传代码包
[root@redis03 ~]# rz filebeat-6.6.0-x86_64.rpm # 安装
[root@redis03 ~]# rpm -ivh filebeat-6.6.0-x86_64.rpm

Filebeat 配置

# Filebeat 配置文件
[root@redis03 ~]# rpm -qc filebeat
/etc/filebeat/filebeat.yml

Filebeat 日志

# Filebeat 日志位置
[root@web01 ~]# tail -f -n 100 /var/log/filebeat/filebeat

Log-file => Filebeat => File

编辑配置文件

# 备份原始配置文件
[root@redis03 ~]# cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak # 配置
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log output.file:
path: "/tmp"
filename: "filebeat.log"

启动 Filebeat

[root@m01 ~]# systemctl start filebeat.service

# 验证
[root@m01 ~]# ps -ef | grep filebeat
root 3415 1 0 11:04 ? 00:00:00 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/sharefilebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat
root 3434 125832 0 11:04 pts/0 00:00:00 grep --color=auto filebeat

访问目录测试

# 访问 nginx 以后,查看 /tmp目录下

[root@web01 ~]# ll /tmp/
total 52
-rw------- 1 root root 3037 May 25 11:08 filebeat.log

Log-file => Filebeat => ElasticSearch

编辑配置文件

[root@web01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log output.elasticsearch:
hosts: ["10.0.0.121:9200"]

重启 Filebeat

[root@web01 ~]# systemctl restart filebeat.service

访问页面测试

Filebeat 收集日志格式设置(JSON)

编辑配置文件

[root@m01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true output.elasticsearch:
hosts: ["10.0.0.121:9200"] # keys_under_root
默认情况下,解码后的 JSON 放在输出文档中的 “json” 键下。 如果启用此设置,则会将键复制到输出文档的顶层。 默认值是 false # overwrite_keys
如果启用了 keys_under_root 和此设置,则来自解码的JSON对象的值会覆盖 Filebeat 通常添加的字段(类型,源,偏移量等)以防冲突

配置 Nginx 日志格式

[root@m01 ~]# vim /etc/nginx/nginx.conf
........
log_format json '{"@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",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}'; access_log /var/log/nginx/access.log json;
........ # 上面的 Nginx 日志格式,某些情况,无法收集到 ElasticSearch 数据库中
# 如果 ElasticSearch 数据库中,只出现了索引,但不能够收集到日志数据,试试改成下面的 Json 格式
[root@m01 ~]# vim /etc/nginx/nginx.conf
........
log_format json '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"upstream_time": "$upstream_response_time",'
'"request_time": "$request_time" }'; access_log /var/log/nginx/access.log json;
........ # 删除原来的索引,重启 nginx
[root@m01 ~]# systemctl reload nginx

访问页面测试

Log-file => Filebeat => ElasticSearch(指定索引)

编辑配置文件

[root@m01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true output.elasticsearch:
hosts: ["10.0.0.121:9200"]
index: "nginx-%{+yyyy.MM.dd}"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.overwrite: false
setup.template.enabled: false #============== 参数说明 ================#
# 模板的名称
setup.template.name: "nginx"
# 模板模式,通配符 * 用于匹配每日索引
setup.template.pattern: "nginx-*"
# 禁用模板加载
setup.template.enabled: false
# 是否覆盖现有模板(不加也可以)
setup.template.overwrite: false

重启 Filebeat

# 重启 filebeat
[root@m01 ~]# systemctl restart filebeat.service

访问页面测试

指定分片和副本数

setup.template.settings:
index.number_of_shards: 2
index.number_of_replicas: 1

Log-file => Filebeat => Redis

编辑配置文件

[root@m01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true output.redis:
hosts: ["172.16.1.121:6379"]
key: "nginx_log"
db: 0
password: 123

重启 Filebeat(略)

访问页面查看 Redis

[root@redis01 ~]# redis-cli
127.0.0.1:6379> keys *
1) "nginx_log" 127.0.0.1:6379> LLEN nginx_log
(integer) 342
127.0.0.1:6379> LRANGE nginx_log 0 -1

Redis => Logstash => ElasticSearch

[root@web01 ~]# vim /etc/logstash/conf.d/beats_redis_logstash_es.conf
input {
redis {
data_type => "list"
host => ["172.16.1.121"]
port => 6379
key => "nginx_log"
db => "0"
codec => "json"
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "redis-%{+YYYY-MM-dd}"
}
} # 运行后观察 ES-head ,若有 redis 索引及数据,成功

Log-file => Filebeat => Logstash => ElasticSearch

编辑配置文件

# 配置 filebeat
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true output.logstash:
hosts: ["10.0.0.7:6666"]
# 配置 logstash
[root@web01 ~]# vim /etc/logstash/conf.d/beats_logstash_es.conf
input {
beats {
port => 6666
codec => "json"
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "filebeat-%{+YYYY-MM-dd}"
}
} # 运行后观察 ES-head ,若有 filebeat 索引及数据,成功

Log-flies => Filebeat => ElasticSearch(多份日志)

方法一(通过 source 字段划分)

[root@web01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enable: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
- type: log
enable: true
paths:
- /var/log/messages output.elasticsearch:
hosts: ["10.0.0.121:9200"]
indices:
- index: "nginx_%{+YYYY-MM-dd}"
when.contains:
source: "/var/log/nginx/access.log"
- index: "message_%{+YYYY-MM-dd}"
when.contains:
source: "/var/log/messages"
setup.template.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"

方法二(通过 tag 字段划分)

[root@web01 ~]# vim /etc/filebeat/filebeat.yml

filebeat.inputs:
- type: log
enable: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["nginx"] - type: log
enable: true
paths:
- /var/log/messages
tags: ["messages"] output.elasticsearch:
hosts: ["10.0.0.121:9200"]
indices:
- index: "nginx_%{+YYYY-MM-dd}"
when.contains:
tags: "nginx"
- index: "message_%{+YYYY-MM-dd}"
when.contains:
tags: "messages"
setup.template.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"

Log-files => Filebeat => Redis => Logstash => ElasticSearch(多日志)

# 配置 filebeat
[root@db05 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["nginx"] - type: log
enabled: true
paths:
- /var/log/messages
tags: ["messages"] output.redis:
hosts: ["172.16.1.121:6379"]
password: "123"
keys:
- key: "nginx_log"
when.contains:
tags: "nginx"
- key: "messages_log"
when.contains:
tags: "messages"
db: "0" # 配置 logstash
[root@db05 ~]# vim /etc/logstash/conf.d/redis.conf
input {
redis {
data_type => "list"
host => ["172.16.1.121"]
port => 6379
key => "nginx_log"
password => "123"
db => "0"
codec => "json"
type => "nginx"
} redis {
data_type => "list"
host => ["172.16.1.121"]
port => 6379
key => "messages_log"
password => "123"
db => "0"
codec => "json"
type => "messages"
} } output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "%{type}-%{+YYYY-MM-dd}"
}
}

Filebeat 收集 Java 报错

# 编辑配置文件,收集 tomcat 错误日志
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enable: true
paths:
- /usr/local/tomcat/logs/catalina.*.log
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after output.elasticsearch:
hosts: ["10.0.0.121:9200"]
index: "tomcat_error_%{+YYYY-MM-dd}" setup.template.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*" # 下载测试日志
[root@web01 ~]# wget https://www.linuxyz.top/download/software/test_log/tomcat_error.log
[root@web01 ~]# cat tomcat_error.log >> /usr/local/tomcat/logs/catalina.2019-06-12.log

Filebeat 日志收集的更多相关文章

  1. 【linux】【ELK】搭建Elasticsearch+Logstash+Kibana+Filebeat日志收集系统

    前言 ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部. Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大功能:是 ...

  2. Filebeat 日志收集器 安装和配置

    Filebeat的配置文件是/etc/filebeat/filebeat.yml,遵循YAML语法.具体可以配置如下几个项目: Filebeat Output Shipper Logging(可选) ...

  3. Filebeat日志收集简单使用

    1.简略介绍 轻量型日志采集器,用于转发和汇总日志与文件. 官网: https://www.elastic.co/cn/beats/filebeat 2.本文实现的功能 3.事先必备: 至少一台Kaf ...

  4. ELK Stack 介绍 & Logstash 日志收集

    ELK Stack 组成 Software Description Function E:Elasticsearch Java 程序 存储,查询日志 L:Logstash Java 程序 收集.过滤日 ...

  5. 快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana)

    快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana) 概要说明 需求场景,系统环境是CentOS,多个应用部署在多台服务器上,平时查看应用日志及排查问题十 ...

  6. CentOS7下 简单安装和配置Elasticsearch Kibana Filebeat 快速搭建集群日志收集平台

    目录 1.添加elasticsearch官网的yum源 2.Elasticsearch 安装elasticsearch 配置elasticsearch 启动elasticsearch并设为开机启动 3 ...

  7. FILEBEAT+ELK日志收集平台搭建流程

    filebeat+elk日志收集平台搭建流程 1.         整体简介: 模式:单机 平台:Linux - centos - 7 ELK:elasticsearch.logstash.kiban ...

  8. 日志分析平台ELK之日志收集器filebeat

    前面我们了解了elk集群中的logstash的用法,使用logstash处理日志挺好的,但是有一个缺陷,就是太慢了:当然logstash慢的原因是它依赖jruby虚拟机,jruby虚拟机就是用java ...

  9. 日志收集之filebeat使用介绍

    此系列文章一共分为三部分,分为filebeat部分,logstash部分,es部分.这里会按照每天几百亿条的数据量来考虑,去设计.部署.优化这个日志系统,来最大限度的利用资源,并达到一个最优的性能.本 ...

随机推荐

  1. 【ORACLE】11g rac+dg

    首先感谢群友分享的文档,在这里先感谢哆啦B梦,非常感谢 该文档主要指导如何利用现有的RAC环境搭建一套RAC与单实例的DG的环境  ============================主机配置信息 ...

  2. 聊聊 React

    都说 React 开发效率高,但效率高在哪呢?来细看看. 用 d3 写一个 List: const renderList = data => { d3.select("ul" ...

  3. 为什么[] == false 为true

    首先要讲一下js的数据类型分为: 1.基本数据类型(原始数据类型):String.Boolean.Number.null.undefined.Symbol 2.引用数据类型:Object.Array. ...

  4. jQuery库 之 jquery slimscroll插件使用

    1.引入jQuery插件 <script type="text/javascript" src="jquery.min.js"></scrip ...

  5. Centos 7 网卡配置

    网卡配置位置 1 /etc/sysconfig/network-scripts/ifcfg-ethx  -----其中x为网卡名称,centos 默认网卡名称为ens33 如何配置  1 2 3 4 ...

  6. uni-app开发经验分享二十: 微信小程序 授权登录 获取详细信息 获取手机号

    授权页面 因为微信小程序提供的 权限弹窗 只能通用户确认授权 所以可以 写一个授权页面,让用户点击 来获取用户相关信息 然后再配合后台就可以完成登录 <button class="bt ...

  7. JS编写的科学计算器

    最近半个月编写了一个JS+CSS+HTML的网页计算器,从最初的具有简陋界面的简单计算器改版到最终具有科学/标准计算器转换功能并且界面非常友好的计算器,收获良多!总的来说,代码简单,通俗易读,下面贴上 ...

  8. uni-app开发经验分享三: Vuex实现登录和用户信息留存

    在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...

  9. 简单的DbContext工厂类(EFCore)

    前言 根据appsettings.json的中配置的数据库类型,使用工厂模式创建DbContext 代码实现 appsettings.json中的配置项 //使用的数据库类型 "Server ...

  10. Python攻城——查看,生成幫助文檔

    1. python在控制台中查看文檔 1 python -m pydoc 模塊名 2. pydoc生成HTML文檔 1 python -m pydoc -w 模塊名 1 python -m pydoc ...