EF(filebeak)K收集nginx與tomcat日志(全)


1.Filebeat基本介紹

官網:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html

Filebeat是用於“轉發”和“集中日志數據’的“輕量型數據采集器”。Filebeat監視您指定的日志文件路徑;

收集日志事件並將數據轉發到Elasticsearch或Logstash、Redis、 Kafka等。

正如官方描述:“當您要面對成百上千、甚至成千上萬的服務器、虛擬機和容器生成的日志時,請告別 SSH 吧。Filebeat 將為您提供一種輕量型方法,用於轉發和匯總日志與文件,讓簡單的事情不再繁雜。”

2.Filebeat工作流程

當filebeat啟動后, filebeat通過Input讀取指定的日志路徑,然后為該日志啟動-個收割進程(harvester )

每一 個收割進程讀取一個日志文件的新內容,並發送這些新的日志數據到處理程序(spooler ) ,

處理程序會集合這些事件,最后filebeat會發送集合的數據到你指定的地點。

Filebeat 是安裝在服務器上的數據中轉代理。它采集數據,並上報到Logstash或Elasticsearch等模塊。【日志采集+輸送展示】

Filebeat 是 Elastic Stack 的一部分,因此能夠與 Logstash、Elasticsearch 和 Kibana 無縫協作。無論您要使用 Logstash 轉換或充實日志和文件,還是在 Elasticsearch 中隨意處理一些數據分析,亦或在 Kibana 中構建和分享儀表板,Filebeat 都能輕松地將您的數據發送至最關鍵的地方。

3、FileBeat 的原理

1. FIlebeat 的4大組件

  • 關於Filebeat的組成, 有4個非常重要的概念需要我們知道

Prospector 探測--(收取保護費的黑社會大哥)

Harvest 收取--(黑社會馬仔小弟)

libeat 匯集對外輸送--(黑社會社長)

registry 記錄收取進度--(社團財務會計)

在一開始要提前在配置文件中寫好日志所在的位置,Prospector就如黑社會大哥一樣,如果要去收取保護費,它會負責探索哪里能收取到,在日志所在的位置探索。而Harvest就好比黑社會小弟一樣,Prospector決定去哪里收取保護費后,就派小弟Harvest去收取。

每個Prospector 都有一個對應的Harvest,相當於每個大哥手下都有小弟,然后他們有一個共同的老大叫做Libeat,他是黑社會的社長,會匯總所有收集到的東西,然后把所有的東西(日志)傳送給指定的地方去消費(酒吧、KTV等),這其中還有個非常重要的角色”registry“,它相當於一個會計,它會記錄Harvest小弟 都收割了些啥,收割到哪里了,這樣一但有問題了之后,harvest就會跑到會計哪里問:上次大哥指定的那幾家的保護費,我收到哪里了? Registry 就會告訴Harvest 你收到哪里了,接下來繼續收取就行了。這樣就避免了保護費數據重復收集的問題!

2.Filebeat配置

首先,需要知道的是:filebeat.yml 是 filebeat 的配置文件。配置文件的路徑會因為你安裝方式的不同變化。

Beat 所有系列產品的配置文件都基於 YAML 格式,FileBeat 當然也不例外。

filebeat.yml 部分配置示例:


filebeat:
  prospectors:
    - type: log
      paths:
        - /var/log/*.log
      multiline:
        pattern: '^['
        match: after

4.系統日志收集概述

1.系統日志-般指的是哪些?

系統日志其實很寬泛通常我們說的是messages、 secure、 cron、 dmesg、 ssh、 boot等日志。

2.系統日志如何統-規范收集?

系統中有很多日志,挨個配置收集就變得非常麻煩了。所以我們需要對這些日志進行統一、集中的管理。

可以通過rsyslog將本地所有類型的日志都寫Varl/log/oldxu.log文件中,然后使用filebeat對該文件進行收集。

3.收集系統日志架構圖

rsyslog+filebeat --> elasticsearch集群<--kibana

 # 配置rsyslog與filebeat
[root@web01 ~]# yum install rsyslog -y
[root@web01 ~]# yum install filebeat -y
[root@web01 ~]# rpm -ivh filebeat-7.4.0-x86_64.rpm   #需要首先將其上傳至家目錄
[root@web01 ~]# cd /etc/filebeat/
[root@web01 filebeat]# cp filebeat.yml filebeat.yml.bak
[root@web01 filebeat]# vim filebeat.yml
[root@web01 filebeat]# cat filebeat.yml
#從文件中讀取數據
filebeat.inputs:
  - type: log                    #收集日志類型
    enabled: true								 #啟用日志收集
    paths:											 #日志路徑
      - /var/log/oldxu.log

#輸出讀取內容到Elasticsearch
output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]

[root@web01 filebeat]# systemctl start filebeat.service 
[root@web01 filebeat]# systemctl enable filebeat.service 
[root@web01 filebeat]# vim /etc/rsyslog.conf 
# ### end of the forwarding rule ###
*.* /var/log/oldxu.log        #oldxu所在日志路徑
[root@web01 ~]# systemctl restart rsyslog.service 

創建索引

  • 索引模式配置

  • 日志信息

5、需求:進行日志分析的時候kibana有很多無用消息,只收集警告、err、ssd相關的。
# 添加規則記錄,修改filebeat.yml文件
[root@web01 filebeat]# vim filebeat.yml
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/oldxu.log
  include_lines: ['^WARN','^ERR','sshd']   #錯誤、告警、規則記錄

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
[root@web01 filebeat]# systemctl restart filebeat
[root@web01 filebeat]# systemctl restart rsyslog.service 
注意:必須要刪除之前創建的filebeat,否則無法添加。

[root@web01 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log                                  #收集日志的類型
  enabled: true                              #啟動
  paths:																		 #收集日志的路徑
    - /var/log/oldxu.log
  include_lines: ['^ERR', '^WARN', 'sshd']   #只看指定的日志,錯誤,警告,sshd

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  index: "system-%{[agent.version]}-%{+yyyy.MM.dd}"   #索引名書寫格式

setup.ilm.enabled: false
setup.template.name: system   #索引關聯的模板名稱
setup.template.pattern: system-*

#方式一:
###設定system模板的分片數和副本數
#setup.template.settings:            #定義索引分片數和副本
#  index.number_of_shards: 3
#  index.number_of_replicas: 1
#此處取消注釋表示在服務器添加
方式二:

   "number_of_routing_shards": "30",
   "number_of_shards": "10",
   "number_of_replicas": "1",
點擊update


	1.修改system模板   --->  添加 shards 分片數數量,replicas的數量
	2.刪除模板關聯的索引
	3.刪除filebeat自行指定的分片數和副本數
	4.重啟filebeat
	5.產生新的日志

  • 分片效果展示

1.Nginx日志收集概述

1.為什么要收集Nginx日志?
我們需要獲取用戶的信息,比如:來源的IP是哪個地域,網站的PV、UV、 狀態碼、訪問時間等等

nginx+filebeat---->elasticsearch <--kibana

步驟一:安裝、配置nginx

#1、安裝nginx
[root@web01 conf.d]# yum install nginx -y
[root@web01 conf.d]# cd /etc/nginx/conf.d/
[root@web01 filebeat]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat /etc/nginx/conf.d/elk.oldxu.com.conf 
server {
       listen 80;
       server_name elk.oldxu.com;
    
       root /code;
       location / {
		   index index.html;
       }
}
[root@web01 conf.d]# mkdir /code
[root@web01 conf.d]# echo "--elk--web01--" >/code/index.html
[root@web01 conf.d]# systemctl start nginx
[root@web01 conf.d]# tail -f /var/log/nginx/access.log

步驟二:配置filebeat

#2、配置filebeat
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true    #默認Flase,還會將json解析的日志存儲至messages字段
  json.overwrite_keys: true     #覆蓋默認的key,使用自定義json格式的key

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  index: "nginx-%{[agent.version]}-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: nginx   #索引關聯的模板名稱
setup.template.pattern: nginx-*

[root@web01 conf.d]# curl -H Host:elk.oldxu.com http://10.0.0.7

瀏覽器通過訪問http://10.0.0.161:5601我們會發現日志雖然已經收集成功,但是沒有達到我們的期望值。

1.問題現象?

如上實現了日志收集,但是所有的數據都在message字段中,無法滿足分析的需求。比如我們想
統計狀態碼,但是會發現沒有辦法過濾數據。
2.解決方案?
需要將日志中的每一個選項都拆分出來 ,拆分成key-value的形式,那么就需要借助json的格
式。
基本寫法: oldxu 男20
json寫法:姓名:oldxu性別:男年齡: 20.
當我們需要篩選姓名時, JSON的寫法會將所有姓名對應的值都會被列出來,便於后續的統計與
分析。
3.如何將nginx日志格式轉換成json格式?

###配置log_format即可
[root@web01 conf.d]# cat /etc/nginx/nginx.conf 
user www;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
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;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

[root@web01 conf.d]# > /var/log/nginx/access.log
[root@web01 conf.d]# systemctl restart nginx filebeat
#刪除之前創建的所有索引
[root@web01 conf.d]#  curl -H Host:elk.oldxu.com http://10.0.0.7  #寫入幾條數據

但是我們希望的狀態如下:

nginx訪問日志--存儲-> nginx-access-索引
nginx錯誤日志--存儲--> nginx-error-索引

配置filebeat收集多個日志,通過tags標記進行區分

[root@web01 ~]# cd /etc/filebeat/
[root@web01 filebeat]# vim filebeat.yml
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true    #默認Flase,還會將json解析的日志存儲至messages字段
  json.overwrite_keys: true     #覆蓋默認的key,使用自定義json格式的key
  tags: ["access"]


- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]



output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.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-*
[root@web01 filebeat]#  systemctl restart nginx filebeat
[root@web01 filebeat]#  > /var/log/nginx/access.log
[root@web01 filebeat]# curl -H Host:elk.oldxu.com http://10.0.0.7

#在web上刪除之前的日志
  • 效果如下

  • 創建兩個索引模式,效果圖展示

場景還原-->收集多虛擬主機日志

多站點日志分析?公司有三個正在運行的網站域名分別是:

a:elk.oldxu.com bk.oldxu.com bs.oldxu.com 三個都跑在nginx下,要進行日志收集。應該怎么做?

#1、實現
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim bk.oldxu.com.conf 
[root@web01 conf.d]# vim bs.oldxu.com.conf 
[root@web01 conf.d]# vim elk.oldxu.com.conf 
[root@web01 conf.d]# cat bk.oldxu.com.conf 
server {
       listen 80;
       server_name bk.oldxu.com;
       root /code/bk;
       access_log /var/log/nginx/bk.oldxu.com.log json;

       location / {
		index index.html;
}
}

[root@web01 conf.d]# cat bs.oldxu.com.conf 
server {
       listen 80;
       server_name bs.oldxu.com;
       root /code/bs;
       access_log /var/log/nginx/bs.oldxu.com.log json;

       location / {
		index index.html;
}
}

[root@web01 conf.d]# cat elk.oldxu.com.conf 
server {
       listen 80;
       server_name elk.oldxu.com;
       root /code/elk;
       access_log /var/log/nginx/elk.oldxu.com.log json;

       location / {
		index index.html;
}
}

#2、測試
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# curl -H Host:elk.oldxu.com http://10.0.0.7
elk.oldxu.com
[root@web01 conf.d]# curl -H Host:bs.oldxu.com http://10.0.0.7
bs.oldxu.com
[root@web01 conf.d]# curl -H Host:bk.oldxu.com http://10.0.0.7
bk.oldxu.com

3、收集多虛擬主機日志之filebeat配置
[root@web01 filebeat]# vim filebeat.yml
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/elk.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-elk-host"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/bs.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-bs-host"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/bk.oldxu.com.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["nginx-bk-host"]


- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["nginx-error"]


output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  indices:
    - index: "nginx-elk-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-elk-host"

    - index: "nginx-bs-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-bs-host"

    - index: "nginx-bk-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-bk-host"

    - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx-error"

setup.ilm.enabled: false
setup.template.name: nginx   #索引關聯的模板名稱
setup.template.pattern: nginx-*


[root@web01 filebeat]# systemctl restart filebeat
[root@web01 filebeat]# curl -H Host:elk.oldxu.com http://10.0.0.7
[root@web01 filebeat]# curl -H Host:elk.oldxu.com http://10.0.0.7
[root@web01 filebeat]# curl -H Host:elk.oldxu.com http://10.0.0.7

tomcat日志收集

2.收集Java日志思路?

例1: Tomcat正常日志是以"日期"開頭的。而報錯日志中間的錯誤都不是以"日期"開頭的。

所以我們可以匹配以“日期’開頭的一直到下一一個日期出現則為一個事件日志。

例2: Elasticsearch.正常日志是以]開頭的。而報錯日志中間的錯誤信息不是以]開頭,

所以匹配以[開頭的行, - -直到下一一個[開頭的出現則為一個事件日志。官方多行匹配方式

[root@web03 ~]# yum install java -y
[root@web03 ~]# mkdir /soft
[root@web03 ~]# cd /soft/
[root@web03 soft]# rz apache-tomcat-9.0.27.tar.gz
[root@web03 soft]# tar xf apache-tomcat-9.0.27.tar.gz
[root@web03 soft]# ln -s apache-tomcat-9.0.27 /soft/tomcat
[root@web03 soft]# cd /soft/tomcat/conf/
[root@web03 conf]# vim server.xml    #  pattern="后面為將tomcat日志轉換為json日志格式
......
</Realm>

      <Host name="tomcat.oldxu.com"  appBase="/code/tomcat"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="tomcat.oldxu.com.log" suffix=".txt"
               pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}" />

      </Host>
    </Engine>
[root@web03 soft]# mkdir /code/tomcat/ROOT
[root@web03 soft]# echo "tomcat.oldxu.com" > /code/tomcat/ROOT/index.html
[root@web03 soft]# pkill java
[root@web03 soft]# /soft/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /soft/tomcat
Using CATALINA_HOME:   /soft/tomcat
Using CATALINA_TMPDIR: /soft/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /soft/tomcat/bin/bootstrap.jar:/soft/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@web03 soft]# curl -H Host:tomcat.oldxu.com http://10.0.0.9:8080
tomcat.oldxu.com

安裝配置filebeat

#安裝filebeat
[root@web03 ~]# rz filebeat-7.4.0-x86_64.rpm
[root@web03 ~]# rpm -ivh filebeat-7.4.0-x86_64.rpm 
warning: filebeat-7.4.0-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing...                          ################################# [100%]
	package filebeat-7.4.0-1.x86_64 is already installed

[root@web03 ~]# cd /etc/filebeat/
[root@web03 filebeat]# vim filebeat-tomcat.yml 
[root@web03 filebeat]# cat filebeat-tomcat.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/tomcat.oldxu.com.log.*.txt
  json.keys_under_root: true    #默認Flase,還會將json解析的日志存儲至messages字段
  json.overwrite_keys: true     #覆蓋默認的key,使用自定義json格式的key


output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200"]
  index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"

setup.ilm.enabled: false
setup.template.name: tomcat   #索引關聯的模板名稱
setup.template.pattern: tomcat-*


[root@web03 filebeat]# systemctl start filebeat
[root@web03 filebeat]# systemctl restart filebeat
[root@web03 filebeat]# curl -H Host:tomcat.oldxu.com http://10.0.0.9:8080
tomcat.oldxu.com
[root@web03 filebeat]# curl -H Host:tomcat.oldxu.com http://10.0.0.9:8080
tomcat.oldxu.com
[root@web03 filebeat]# curl -H Host:tomcat.oldxu.com http://10.0.0.9:8080
tomcat.oldxu.com
[root@web03 filebeat]# curl -H Host:tomcat.oldxu.com http://10.0.0.9:8080
tomcat.oldxu.com

tomcat錯誤日志收集

[root@web01 filebeat]# vim filebeat.yml
[root@web01 filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/tomcat.oldxu.com.log.*.txt
  json.keys_under_root: true    #默認Flase,還會將json解析的日志存儲至messages字段
  json.overwrite_keys: true     #覆蓋默認的key,使用自定義json格式的key
  tags: ["tomcat-access"]

- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/catalina.out
  multiline.pattern: '^\d{2}'   #匹配以2個數字開頭的
  multiline.negate: true
  multiline.match: after
  multiline.max_lines: 10000    #默認最大合並行為500,可根據實際情況調整。
  tags: ["tomcat-error"]


output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200"]
  indices:
    - index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat-access"

    - index: "tomcat-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat-error"


setup.ilm.enabled: false
setup.template.name: tomcat   #索引關聯的模板名稱
setup.template.pattern: tomcat-*

[root@web03 filebeat]# systemctl restart filebeat

  • 改錯配置文件測試(此處不再演示--->一般進行多行匹配,多行匹配在上述文件中已經配置)


免責聲明!

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



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