Filebeat日志收集器
一、Filebeat 日志收集器
1.1 Filebeat簡介
Filebeat是用于“轉發"和“集中日志數據”的“輕量型數據采集器",用go語言開發,相比Logstash來說輕便。
Filebeat會監視指定的日志文件路輕,收集日志事件并將數據轉發到Elasticsearch、Logstash、Redis、Kafka等存儲服務器器
1.2 Filebeat主要組件
Filebeat包含兩個主要組件,輸入和收割機,兩個組件協同工作將文件尾部最新數據發送出去
- 輸入Input:輸入負責管理收割機從哪個路徑查找所有可讀取的資源。
- 收割機Harvester:負責逐行讀取單個文件的內容,然后將內容發送到輸出。
1.3 Filebeat工作流程
當filebeat啟動后,filebeat通過Input讀取指定的日志路徑,然后為該日志啟動一個收割進程harvester,每一個收割進程讀取一個日志文件的新內容,并發送這些新的日志數據到處理程序spooler ,處理程序會集合這些事件,最后filebeat會發送集合的數據到你指定的位置。
1.4 Filebeat 配置說明
二、Filebeat基本使用
2.1 安裝
需要安裝在業務系統上
[root@web01 ~]# rpm -ivh filebeat-7.8.1-x86_64.rpm
啟動報錯: Exiting: Could not start registrar: Error loading state: Error decoding states: EOF
rm -r /var/lib/filebeat/registry
systemctl reset-failed filebeat
systemctl start filebeat
2.2 測試從終端中讀入,輸出到中端
[root@web01 ~]# cat /etc/filebeat/test.yml
filebeat.inputs:
- type: stdinenabled: true
output.console:pretty: trueenable: true
[root@web01 filebeat]# filebeat -e -c /etc/filebeat/test.yml
hello world
{"@timestamp": "2021-10-27T13:29:07.422Z","@metadata": {"beat": "filebeat","type": "_doc","version": "7.8.1"},"log": {"offset": 0,"file": {"path": ""}},"message": "hello world","input": {"type": "stdin"},"ecs": {"version": "1.5.0"},"host": {"name": "web01"},"agent": {"version": "7.8.1","hostname": "web01","ephemeral_id": "3d0de9b0-b486-494a-823d-305491f44950","id": "457b924d-450b-49eb-8126-047091c09920","name": "web01","type": "filebeat"}
}
2.3 從文件中讀取數據,輸出到中端
1.修改yml文件
[root@web01 ~]# cat /etc/filebeat/test.yml
filebeat.inputs:
- type: logenabled: truepaths: - /var/log/test.log
output.console:pretty: trueenable: true
2.創建/var/log/test.log目錄
3.啟動Filebeat
[root@web01 filebeat]# filebeat -e -c /etc/filebeat/test.yml
4.另一個終端往日志中追加數據
[root@web01 ~]# echo "test log" > /var/log/test.log
5.查看中端面板,是否能獲取到數據
{"@timestamp": "2021-10-27T13:35:20.083Z","@metadata": {"beat": "filebeat","type": "_doc","version": "7.8.1"},"log": {"offset": 0,"file": {"path": "/var/log/test.log"}},"message": "test log","input": {"type": "log"},"host": {"name": "web01"},"agent": {"hostname": "web01","ephemeral_id": "cce5fd00-ba6f-44bb-b40a-1f9e39e27986","id": "457b924d-450b-49eb-8126-047091c09920","name": "web01","type": "filebeat","version": "7.8.1"},"ecs": {"version": "1.5.0"}
}
2.5 從文件中讀取數據,輸入到es集群
[root@web01 filebeat]# cat /etc/filebeat/test.yml
filebeat.inputs:
- type: log # 日志類型enabled: true # 啟動收集paths: - /var/log/test.log # 日志路徑
output.elasticsearch:hosts: ["172.16.1.161:9200"] # es集群ip+port
# 如果不自定義索引,則默認索引為filebeat
模擬日志追加數據
[root@web01 ~]# echo "filebeat test data" > /var/log/test.log
[root@web01 ~]# echo "filebeat test data123" > /var/log/test.log
去
cerebro中查看,可以看到索引,
去kibana中查看具體的數據
2.6 輸出至ES集群實戰
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/messagesoutput.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]
2.7 自定義索引
默認在kibana中查看字段不方便,其實可以在Discove中查看,但是首先需要自定義索引。
kibana中點擊 StackManagement ----> 創建索引模式
點擊Discover
自己追加日志
[root@web01 ~]# echo "test bertwu" >> /var/log/messages
2.8 Filebeat 自定義索引名稱
默認情況下,所有的索引名稱文filebeat開頭,難以區分,我們可以自定義索引名稱
- 修改filebeat配置文件;
- 刪除ES 的索引;刪除Kibana的索引;
- 重啟filebeat 服務重新產生新的索引;
filebeat.inputs:
- type: logenabled: truepaths: /var/log/messagesoutput.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "message-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱setup.ilm.enabled: false # 索引生命周期,需要關閉自定義的索引名稱才能生效
setup.template.name: "message" #定義模板名稱
setup.template.pattern: "message-*" #定義模板的匹配索引名稱# 如果是 filebeat--elasticsearch--kibana 架構,可以這樣設置分片,否則無效。
#setup.template.settings:
# index.number_of_shards: 3
# index.number_of_replicas: 0
創建在kibana中message索引即可檢索到。
默認情況下 Filebeat 寫入到 ES 的索引分片為1,如果需要修訂分片,可以通過如下兩種方式:
方式一:修改filebeat配置文件,增加如下內容;然后刪除索引的模板,以及索引,重新產生數據;
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 1
方式二:使用 cerebro web頁面修改;
1.修改模板 settings 配置,調整分片以及副本;
2.刪除模板關聯的索引;
3.重啟filebeat產生新的索引;
二、Filebeat收集系統日志
2.1.系統日志有哪些
系統日志其實很寬泛、通常我們說的是 messages、secure、cron、dmesg、ssh、boot 等日志。
2.2 系統日志收集思路
系統中有很多日志,挨個配置收集就變得非常麻煩了。所以我們需要對這些日志進行統一、集中的管理。可以通過 rsyslog 將本地所有類型的日志都寫入/var/log/system.log 文件中,然后使用 filebeat 對該文件進行收集即可。
rsyslog+filebeat --> elasticsearch集群 <–kibana
2.3 環境準備
| 主機名稱 | 服務 | IP地址 |
|---|---|---|
| web01 | rsyslog+filebeat | 172.16.1.7 |
| es-node1 | es | 172.16.1.161 |
| es-node2 | es | 172.16.1.162 |
| es-node3 | es | 172.16.1.163 |
2.4 rsyslog安裝及配置
[root@web01 ~]# yum install rsyslog -y[root@web01 ~]# vim /etc/rsyslog.conf
#配置收集日志的方式
#*.* @IP:514 #將本地所有日志通過網絡發送給遠程服務器
*.* /var/log/oldxu.log #將本地所有日志保存至本地/var/log/system.log# 啟動
systemctl start rsyslog
2.5 配置filebeat
[root@web01 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/sys.logoutput.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "system-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱setup.ilm.enabled: false
setup.template.name: "system" #定義模板名稱
setup.template.pattern: "system-*" #定義模板的匹配索引名稱
2.6 kibana創建system索引并查看
2.7 優化
kibana 展示的結果上有很多 Debug 消息,其實該類消息無需收集,所以我們可以對收集的日志內容進行優化,只收集警告 WARN、ERR、sshd 相關的日志;
[root@web01 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/sys.loginclude_lines: ["WARN","ERR","sshd"] #包含這些
# exclude_lines: ["DEBUG","INFO"] # 或者排除這些output.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "system-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱setup.ilm.enabled: false
setup.template.name: "system" #定義模板名稱
setup.template.pattern: "system-*" #定義模板的匹配索引名稱
三、Filebeat收集Nginx日志
我們需要獲取用戶的信息,比如:來源的IP是哪個地域,網站的PV、UV、狀態碼、訪問時間等等;所以需要收集 Nginx 日志;
3.1 Nginx日志收集架構圖
nginx+filebeat --> elasticsearch <–kibana
3.2 安裝nginx并配置默認訪問站點
[root@web01 filebeat]# cat /etc/nginx/conf.d/elk.conf
server {listen 5555;server_name elk.bertwu.net;location / {root /code;index index.html; } }
3.3 配置filebeat
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/nginx/access.logoutput.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱setup.ilm.enabled: false
setup.template.name: "nginx" #定義模板名稱
setup.template.pattern: "nginx-*" #定義模板的匹配索引名稱
3.4 kibana創建索引并展示
3.5 Nginx json日志收集
3.5.1 原始收集問題
我們實現了 Nginx 日志的收集,但是所有的數據都在 message 字段中,無法滿足分析的需求,比如:
需要統計狀態碼的情況;
統計所有請求總產生的流量大小;
統計來源使用的客戶端;等等
這些是沒有辦法實現的
3.5.2 解決方案
需要將日志中的每一個選項都拆分出來,拆分成 key-value 的形式,那么就需要借助 json 的格式。
3.5.3 將nginx日志格式轉換為json
1.重置nginx日主格式為json格式
log_format json '{ "time_local": "$time_local",''"remote_addr": "$remote_addr",''"referer": "$http_referer",''"request": "$request",''"status": $status,''"bytes": $body_bytes_sent,''"test_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"''}';
2.重新配置nginx.conf
[root@web01 filebeat]# cat /etc/nginx/conf.d/elk.conf
server {listen 5555;server_name elk.bertwu.net;access_log /var/log/nginx/access.log json; # 定義日志格式為jsonlocation / {root /code;index index.html; } }
3.重新配置filebeat文件
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/nginx/access.logjson.keys_under_root: true # Flase會將json解析的格式存儲至messages,改為true則不存儲至json.overwrite_keys: true #覆蓋默認message字段,使用自定義json格式的keyoutput.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱setup.ilm.enabled: false
setup.template.name: "nginx" #定義模板名稱
setup.template.pattern: "nginx-*" #定義模板的匹配索引名稱
4.重啟filebeat、Nginx,然后清空日志,在重新產生json格式的日志
[root@web01 nginx]# > /var/log/nginx/access.log
[root@web01 nginx]#
[root@web01 nginx]#
[root@web01 nginx]# systemctl restart nginx
[root@web01 nginx]# systemctl restart filebeat
5.查看
3.6 nginx多種日志類型收集
nginx 存在訪問日志和錯誤日志,那么如何使用filebeat 同時收集 nginx 的訪問日志、錯誤日志;
我們希望的狀態如下:
nginx訪問日志 --存儲–> nginx-access-xxx 索引
nginx錯誤日志 --存儲–> nginx-error-xxx 索引
1.配置 filebeat 收集多個日志,需要通過 tags 標簽進行區分;
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/nginx/access.logjson.keys_under_root: truejson.overwrite_keys: truetags: ["access"]- type: logenabled: truepaths: /var/log/nginx/error.logtags: ["error"]output.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]indices:- index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱when.contains: tags: "access"- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "error"setup.ilm.enabled: false
setup.template.name: "nginx" #定義模板名稱
setup.template.pattern: "nginx-*" #定義模板的匹配索引名稱
2.kibana中創建nginx-err索引并查看
3.7 Nginx多虛擬主機收集
Nginx 如果有多個站點;filebeat 該如何收集多個域名的訪問日志
基于原有的基礎上改進
1.配置nginx多站點
[root@web01 ~]# cat /etc/nginx/conf.d/elk.conf
server {listen 5555;server_name elk.bertwu.net;access_log /var/log/nginx/access.log json;location / {root /code;index index.html; } }server {listen 5555;server_name blog.bertwu.net;access_log /var/log/nginx/blog.log json;location / {root /code;index index.html; } } server {listen 5555;server_name www.bertwu.net;access_log /var/log/nginx/www.log json;location / {root /code;index index.html; } }
2.配置filebeat
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /var/log/nginx/access.logjson.keys_under_root: truejson.overwrite_keys: truetags: ["access"]- type: logenabled: truepaths: /var/log/nginx/error.logtags: ["error"]- type: logenabled: truepaths: /var/log/nginx/www.logjson.keys_under_root: truejson.overwrite_keys: truetags: ["nginx-www"]- type: logenabled: truepaths: /var/log/nginx/blog.logjson.keys_under_root: truejson.overwrite_keys: truetags: ["nginx-blog"]output.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]indices:- index: "nginx-access-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱when.contains: tags: "access"- index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "error"- index: "nginx-www-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-www"- index: "nginx-blog-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "nginx-blog"setup.ilm.enabled: false
setup.template.name: "nginx" #定義模板名稱
setup.template.pattern: "nginx-*" #定義模板的匹配索引名稱
3.kibana查看
四、Filebeat收集Tomcat日志
我們只需要安裝好tomcat,然后將 tomcat 修改為 json 格式日志,在使用 filebeat 進行收集即可;
4.1 Tomcat日志收集架構圖
tomcat+filebeat --> elasticsearch <–kibana
4.2 Tomcat訪問日志收集
1.安裝tomcat并設置訪問站點
[root@web01 ~]# mkdir -p /soft/ && cd /soft
[root@web01 soft]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.26/bin/apache-tomcat9.0.26.tar.gz
[root@web01 soft]# tar xf apache-tomcat-9.0.26.tar.gz
[root@web01 soft]# ln -s /soft/apache-tomcat-9.0.26 /soft/tomcat
2.修改tomcat server.xml文件,修改日志格式
<Host name="elk.bertwu.net" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="json_elk_log" suffix=".txt" pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}" />
</Host>
3.重啟tomcat
[root@web01 tomcat]# /soft/tomcat/bin/startup.sh
4.檢查訪問日志是否為json格式
[root@web01 tomcat]# cat /soft/tomcat/logs/json_elk_log.2021-10-30.txt {"clientip":"10.0.0.1"," ClientUser":"-"," authenticated":"-"," AccessTime":"[30/Oct/2021:11:08:45 +0800]"," method":"GET / HTTP/1.1"," status":"200"," SendBytes":"200"," Query?string":""," partner":"-"," AgentVersion":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"}
5.修改filebeat配置
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /soft/tomcat/logs/json_elk_log*.txtjson.keys_under_root: truejson.overwrite_keys: truetags: ["access"]output.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"setup.ilm.enabled: false
setup.template.name: "tomcat" #定義模板名稱
setup.template.pattern: "tomcat-*" #定義模板的匹配索引名稱
6.kibana中創建tomcat-access索并展示
4.3 Tomcat 錯誤日志收集
4.3.1 java錯誤日志特點
1.報錯信息比較多。
2.報錯信息分很多行。
4.3.2 收集思路
例1: Tomcat正常日志是以 “日期” 開頭的。而報錯日志中間的錯誤都不是以 “日期” 開頭的。所以我們可以匹配以 “日期” 開頭的一直到下一個日期出現則為一個事件日志。
例2: Elasticsearch正常日志是以 [] 開頭的。而報錯日志中間的錯誤信息不是以 [] 開頭,所以可以匹配以 [開頭的行,一直到下一個 [開頭的出現則為一個事件日志。官方多行匹配方式
4.3.3 filebeat配置
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: logenabled: truepaths: /soft/tomcat/logs/json_elk_log*.txtjson.keys_under_root: true # 默認為False; 就是將所有的日志記錄到Message字段;true不存儲至Message字段json.overwrite_keys: true # 會覆蓋掉Message字段的內容,然后使用自行定義的Json格式的Key作為字段,來存儲對應的值tags: ["access"]- type: logenabled: truepaths: /soft/tomcat/logs/catalina.outtags: ["error"]multiline.pattern: '^\d{2}'multiline.negate: truemultiline.match: aftermultiline.max_lines: 1000 # 最大的合并行數 默認合并的數量是500output.elasticsearch:hosts: ["172.16.1.161:9200","172.16.1.162:9200","172.16.1.163:9200"]indices:- index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}" #自定義索引名稱when.contains:tags: "access"- index: "tomcat-error-%{[agent.version]}-%{+yyyy.MM.dd}"when.contains:tags: "error"setup.ilm.enabled: false
setup.template.name: "tomcat" #定義模板名稱
setup.template.pattern: "tomcat-*" #定義模板的匹配索引名稱
總結
以上是生活随笔為你收集整理的Filebeat日志收集器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P4381 [IOI2008]Islan
- 下一篇: P4178 Tree