介绍 官网
Filebeat Filebeat是一个轻量级的日志采集器
当面对成百上千、甚至成千上万的服务器、虚拟机和容器生成的日志时,采用ssh十分麻烦。而Filebeat 可以提供一种轻量型方法,用于转发和汇总日志与文件,让简单的事情不再繁杂。
启动Filebeat后, 打开Logs UI, 直接在Kibana中观看对您的文件进行tail操作的过程。通过搜索栏按照服务、应用程序、主机、数据中心或者其他条件筛选,以跟踪您的全部汇总日志中的异常行为。
安装 1 2 3 4 curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.15.1-linux-x86_64.tar.gz tar xzvf filebeat-7.15.1-linux-x86_64.tar.gz mv filebeat-7.15.1-linux-x86_64 /filebeat
运行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd filebeatvim gszbeat.yml filebeat.inputs: - type : stdin enabled: true setup.template.settings: index.number_of_shards: 3 output.console: pretty: true enable : true ./filebeat -e -c gszbeat.yml chmod go-w /home/ec2-user/test /filebeat/gszbeat.yml
然后我们在控制台输入hello,就能看到我们会有一个json的输出,是通过读取到我们控制台的内容后输出的。
读取文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 vim gszbeat-log.yml filebeat.inputs: - type : log enabled: true paths: - /home/ec2-user/test /filebeattest/*.log setup.template.settings: index.number_of_shards: 2 output.console: pretty: true enable : true ./filebeat -e -c gszbeat.yml cd /home/ec2-user/test /filebeattest/ echo "ganshizhong" >> a.log
自定义字段 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 vim gszbeat-log.yml filebeat.inputs: - type : log enabled: true paths: - /home/ec2-user/test /filebeattest/*.log tags:["web" ,"test" ] fields: from: test-web fields_under_root: true setup.template.settings: index.number_of_shards: 2 output.console: pretty: true enable : true ./filebeat -e -c gszbeat.yml cd /home/ec2-user/test /filebeattest/ echo "ganshizhong" >> a.log
输出到Elasticsearch 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 vim gszbeat-log.yml filebeat.inputs: - type : log enabled: true paths: - /home/ec2-user/test /filebeattest/*.log tags:["web" ,"test" ] fields: from: test-web fields_under_root: true setup.template.settings: index.number_of_shards: 2 output.elasticsearch: hosts: ["127.0.0.1:9200" ] ./filebeat -e -c gszbeat.yml cd /home/ec2-user/test /filebeattest/ echo "ganshizhong" >> a.log
Filebeat工作原理 Filebeat主要由下面几个组件组成: harvester、prospector 、input
harvester
负责读取单个文件的内容
harvester逐行读取每个文件(一行一行读取),并把这些内容发送到输出
每个文件启动一个harvester,并且harvester负责打开和关闭这些文件,这就意味着harvester运行时文件描述符保持着打开的状态。
在harvester正在读取文件内容的时候,文件被删除或者重命名了,那么Filebeat就会续读这个文件,这就会造成一个问题,就是只要负责这个文件的harvester没用关闭,那么磁盘空间就不会被释放,默认情况下,Filebeat保存问价你打开直到close_inactive到达
prospector
prospector负责管理harvester并找到所有要读取的文件来源
如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester
Filebeat目前支持两种prospector类型:log和stdin
Filebeat如何保持文件的状态
Filebeat保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中
该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。
如果输出(例如ElasticSearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可以用时继续读取文件。
在Filebeat运行时,每个prospector内存中也会保存的文件状态信息,当重新启动Filebat时,将使用注册文件的数量来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取
文件状态记录在data/registry文件中
input
一个input负责管理harvester,并找到所有要读取的源
如果input类型是log,则input查找驱动器上与已定义的glob路径匹配的所有文件,并为每个文件启动一个harvester
每个input都在自己的Go例程中运行
下面的例子配置Filebeat从所有匹配指定的glob模式的文件中读取行
1 2 3 4 5 filebeat.inputs: - type: log paths: - /var/log/*.log - /var/path2/*.log
启动命令 1 2 3 4 5 6 ./filebeat -e -c mogublog-es.yml ./filebeat -e -c mogublog-es.yml -d "publish" -e: 输出到标准输出,默认输出到syslog和logs下 -c:指定配置文件 -d:输出debug信息
Module 日志数据处理 前面要想实现日志数据的读取以及处理都是自己手动配置的,其实,在Filebeat中,有大量的Module,可以简化我们的配置,直接就可以使用,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 ./filebeat modules list Enabled: nginx Disabled: ... kafka kibana mongodb mysql redis ...
1 2 ./filebeat modules enable nginx ./filebeat modules disable nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 cd modules.d/grep -Ev '*#|^$' nginx.yml - module: nginx access: enabled: true var.paths: ["/home/ec2-user/program/gitlab/logs/nginx/*.log" ] error: enabled: true var.paths: ["/home/ec2-user/program/gitlab/logs/nginx/*.log" ] ingress_controller: enabled: false cd ..vi nginx.yml filebeat.inputs: setup.template.settings: index.number_of_shards: 3 output.elasticsearch: hosts: ["127.0.0.1:9200" ] filebeat.config.modules: path: ${path.config} /modules.d/*.yml reload.enabled: false
s
Metricbeat 用于从系统和服务搜集指标。Matricbeat能够以一种轻量型的方式,输送各种系统和服务统计数据,从CPU到内存,从Redis到Nginx,不一而足。
定期收集操作系统或应用程序的指标数据
存储到Elasticsearch中,进行实施的分析
Metricbeat组成 Metricbeat有2部分组成,一部分是Module,另一个部分为Metricset
Module
收集的对象:如 MySQL、Redis、Nginx、操作系统等
Metricset
收集指标的集合:如 cpu、memory,network等
安装 1 2 3 4 5 6 7 8 9 curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.15.1-linux-arm64.tar.gz tar xzvf filebeat-7.15.1-linux-x86_64.tar.gz wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - sudo apt-get install apt-transport-https echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list sudo systemctl enable metricbeat
配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 grep -Ev "^*#|^$" metricbeat.yml metricbeat.config.modules: path: ${path.config} /modules.d/*.yml reload.enabled: false setup.template.settings: index.number_of_shards: 1 index.codec: best_compression setup.kibana: output.elasticsearch: hosts: ["119.255.249.177:9200" ] processors: - add_host_metadata: ~ - add_cloud_metadata: ~ - add_docker_metadata: ~ - add_kubernetes_metadata: ~ sed -i 's#hosts: \["localhost:9200"\]#hosts: ["119.255.249.177:9200"]#' metricbeat.yml ./metricbeat -e
Nginx Module 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ./configure --prefix=/usr/local /nginx --with-http_stub_status_module make make install ./nginx -V nginx version: nginx/1.11.6 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) configure arguments: --prefix=/usr/local /nginx --with-http_stub_status_module vim nginx.conf location /nginx-status { stub_status on; access_log off; } ./nginx -s reload
结果说明:
Active connections:正在处理的活动连接数
server accepts handled requests
第一个 server 表示Nginx启动到现在共处理了9个连接
第二个 accepts 表示Nginx启动到现在共成功创建 9 次握手
第三个 handled requests 表示总共处理了 21 次请求
请求丢失数 = 握手数 - 连接数 ,可以看出目前为止没有丢失请求
Reading: 0 Writing: 1 Waiting: 1
Reading:Nginx 读取到客户端的 Header 信息数
Writing:Nginx 返回给客户端 Header 信息数
Waiting:Nginx 已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于 Active - (Reading+Writing))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 配置nginx module ./metricbeat modules enable nginx vim modules.d/nginx.yml html - module: nginx period: 10s hosts: ["http://127.0.0.1" ] server_status_path: "nginx-status" 修改完成后,启动nginx ./metricbeat -e
更多Module使用参见官方文档:https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-modules.html
https://www.cnblogs.com/cjsblog/p/9495024.html