FileBeats入門簡介


一、ELK日志業務流程

說明:

  • 通過Beats采集Nginx的指標數據和日志數據
  • Beats采集到數據后發送到Elasticsearch中
  • Kibana讀取數據進行分析
  • 用戶通過Kibana進行查看分析報表

二、安裝nginx

省略

三、安裝filebeat

3.1、什么是filebeat?

### 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模式的文件中讀取行

​```yml
filebeat.inputs:
- type: log
  paths:
    - /var/log/*.log
    - /var/path2/*.log
​```

### 啟動命令

​```bash
./filebeat -e -c mogublog-es.yml
./filebeat -e -c mogublog-es.yml -d "publish"
​```

### 參數說明

- **-e:**輸出到標准輸出,默認輸出到syslog和logs下
- **-c:**指定配置文件
- **-d:**輸出debug信息

3.2、下載

官網地址:https://www.elastic.co/cn/downloads/beats/filebeat
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-linux-x86_64.tar.gz

3.3、安裝

[root@node1 app]# tar -zxvf filebeat-6.5.4-linux-x86_64.tar.gz  && mv filebeat-6.5.4-linux-x86_64 filebeat && cd filebeat

3.4、啟動

# 創建配置文件、添加如下內容
[root@node1 filebeat]# vim test1.yml
filebeat.inputs: # filebeat input輸入
- type: stdin    # 標准輸入
  enabled: true  # 啟用標准輸入
setup.template.settings: 
  index.number_of_shards: 3 # 指定下載數
output.console:  # 控制台輸出
  pretty: true   # 啟用美化功能
  enable: true
  
# 啟動
[root@node1 filebeat]# ./filebeat -e -c test1.yml

3.5、讀取文件啟動

再次創建一個文件名字為test2.yml,寫入以下文件
[root@node1 filebeat]# cat test2.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /app/test/logs/*.log
setup.template.settings:
  index.number_of_shards: 3
output.console:
  pretty: true
  enable: true
添加完成后,我們在到下面目錄創建一個日志文件
[root@node1 filebeat]# mkdir -p /app/test/logs
[root@node1 filebeat]# cd /app/test/logs/
# 追加內容
[root@node1 logs]# echo "hello" >> a.log
啟動filebeat
[root@node1 filebeat]# ./filebeat -e -c test2.yml

3.6、自定義字段啟動

當元數據沒辦法支撐我們的業務時,我們還可以自定義添加一些字段

再次創建一個文件名字為test3.yml,寫入以下文件

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /app/test/logs/*.log
  tags: ["web", "test"]  #添加自定義tag,便於后續的處理
  fields:  #添加自定義字段
    from: test-web
  fields_under_root: true #true為添加到根節點,false為添加到子節點中
setup.template.settings:
  index.number_of_shards: 3
output.console:
  pretty: true
  enable: true

啟動

[root@node1 filebeat]# ./filebeat -e -c test3.yml

再次寫入數據查看結果

# 寫入數據
[root@node1 logs]# echo "aaax" > axxx.log 

2020-12-05T21:48:40.398+0800	INFO	log/harvester.go:254	Harvester started for file: /app/test/logs/axxx.log
{
  "@timestamp": "2020-12-05T13:48:40.398Z",
  "@metadata": {
    "beat": "filebeat",
    "type": "doc",
    "version": "6.5.4"
  },
  "host": {
    "name": "node1"
  },
  "beat": {
    "name": "node1",
    "hostname": "node1",
    "version": "6.5.4"
  },
  "source": "/app/test/logs/axxx.log",
  "offset": 0,
  "message": "aaax",   # 可以看到剛剛自定義的標簽
  "tags": [
    "web",
    "test"
  ],
  "from": "test-web", #	可以看到剛剛自定義的字段(且在子節點)
  "prospector": {
    "type": "log"
  },
  "input": {
    "type": "log"
  }
}

四、輸出到ElasticSearch

我們可以通過配置,將修改成如下所示

[root@node1 filebeat]# cat test3.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /app/test/logs/*.log
  tags: ["web", "test"]  #添加自定義tag,便於后續的處理
  fields:  #添加自定義字段
    from: test-web
  fields_under_root: true #true為添加到根節點,false為添加到子節點中
setup.template.settings:
  index.number_of_shards: 3
#output.console:
#  pretty: true
#  enable: true
# 上面的注釋,改成以下2行
output.elasticsearch:
  hosts: ["192.168.1.111","192.168.1.112","192.168.1.113"]

再次啟動

[root@node1 filebeat]# ./filebeat -e -c test3.yml

寫入數據

[root@node1 logs]# echo "1213" > aa.log

查看結果,說明已經成功連接到了elasticsearch集群中

2020-12-05T22:05:43.376+0800	INFO	log/harvester.go:254	Harvester started for file: /app/test/logs/aa.log
2020-12-05T22:05:44.380+0800	INFO	pipeline/output.go:95	Connecting to backoff(elasticsearch(http://192.168.1.113:9200))
2020-12-05T22:05:44.380+0800	INFO	pipeline/output.go:95	Connecting to backoff(elasticsearch(http://192.168.1.111:9200))
2020-12-05T22:05:44.381+0800	INFO	pipeline/output.go:95	Connecting to backoff(elasticsearch(http://192.168.1.112:9200))

頁面查看, 0 1 2 三個分片

五、讀取nginx配置文件

修改配置文件如下

[root@node1 filebeat]# cp test3.yml red_nginx.yml
[root@node1 filebeat]# cat red_nginx.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /app/nginx/logs/*.log
  tags: ["nginx"]  #添加自定義tag,便於后續的處理
setup.template.settings:
  index.number_of_shards: 3
output.elasticsearch:
  hosts: ["192.168.1.111","192.168.1.112","192.168.1.113"]

啟動

[root@node1 filebeat]# ./filebeat -e -c red_nginx.yml 

六、結合Module收集日志

6.1、Module介紹

​ 前面要想實現日志數據的讀取以及處理都是自己手動配置的,其實,在Filebeat中,有大量的Module,可以簡化我們的配置,直接就可以使用,如下:

[root@node1 filebeat]# ./filebeat modules list
Enabled:

Disabled:
apache2
auditd
elasticsearch
haproxy
icinga
iis
kafka
kibana
logstash
mongodb
mysql
nginx
osquery
postgresql
redis
suricata
system
traefik

以看到,內置了很多的module,但是都沒有啟用,如果需要啟用需要進行enable操作:

#啟動
./filebeat modules enable nginx 
#禁用
./filebeat modules disable nginx 

可以發現,nginx的module已經被啟用。

[root@node1 filebeat]# ./filebeat modules list
Enabled:
nginx

6.2、nginx module 配置

我們到下面的目錄,就能看到module的配置了

# 進入到module目錄
[root@node1 filebeat]# cd modules.d/
#查看文件
[root@node1 modules.d]# vim nginx.yml 

修改后得到的文件內容如下所示

# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-module-nginx.html

- module: nginx
  # Access logs
  access:
    enabled: true
    # 添加日志文件
    var.paths: ["/app/nginx/logs/access.log*"]

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

  # Error logs
  error:
    enabled: true
    var.paths: ["/app/nginx/logs/error.log*"]

修改filebeat配置文件

[root@node1 filebeat]# cat red_nginx.yml 
setup.template.settings:
  index.number_of_shards: 3
output.elasticsearch:
  hosts: ["192.168.1.111","192.168.1.112","192.168.1.113"]
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

又一次啟動!

[root@node1 filebeat]# ./filebeat -e -c red_nginx.yml 

報錯了,意思是要在ES節點上安裝這2個包(好像7版本不會報錯!!!)

sudo bin/elasticsearch-plugin install ingest-user-agent
sudo bin/elasticsearch-plugin install ingest-geoip

安裝,這樣安裝有點慢,可以自行到網上下載安裝包解壓

# 切換到elsearch用戶
# 進入/app/elasticsearch
# 執行,網速不是很慢的朋友都下挺快的
[elsearch@master elasticsearch]$ bin/elasticsearch-plugin install ingest-user-agent
[elsearch@master elasticsearch]$ bin/elasticsearch-plugin install ingest-geoip

# 然后重啟集群
[elsearch@slave1 elasticsearch]$ jps |grep Elasticsearch |awk '{print $1}'|xargs kill -9
[elsearch@slave1 elasticsearch]$ /app/elasticsearch/bin/elasticsearch -d

然后再次啟動,就不報錯了

[root@node1 filebeat]# ./filebeat -e -c red_nginx.yml 

頁面查看日志也正常

{
"_index": "filebeat-6.5.4-2020.12.06",
"_type": "doc",
"_id": "6lYMNHYBOXECivMsy6S_",
"_version": 1,
"_score": 1,
"_source": {
"offset": 7542,
"nginx": {
"access": {
"referrer": "-",
"response_code": "200",
"remote_ip": "192.168.1.129",
"method": "GET",
"user_name": "-",
"http_version": "1.1",
"body_sent": {
"bytes": "10"
},
"remote_ip_list": [
"192.168.1.129"
],
"url": "/",
"user_agent": {
"patch": "0",
"original": "curl/7.29.0",
"major": "7",
"minor": "29",
"os": "Other",
"name": "curl",
"os_name": "Other",
"device": "Other"
}
}
},
"prospector": {
"type": "log"
},
"read_timestamp": "2020-12-05T17:56:59.585Z",
"source": "/app/nginx/logs/access.log",
"fileset": {
"module": "nginx",
"name": "access"
},
"input": {
"type": "log"
},
"@timestamp": "2020-12-05T17:56:53.000Z",
"beat": {
"hostname": "node1",
"name": "node1",
"version": "6.5.4"
},
"host": {
"name": "node1"
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM