1. 程式人生 > 實用技巧 >logstash收集日誌05

logstash收集日誌05

一、Logstash收集日誌

1.Logstash的配置檔案

[root@web01 ~]# vim /etc/logstash/logstash.yml
path.config: /etc/logstash/conf.d

#預設是這個目錄

2.logstash收集日誌檔案到檔案

[root@web01 ~]# vim /etc/logstash/conf.d/file_file.conf
input {
  file {
    path => "/var/log/messages"
    start_position => "beginning"
  }
}
output {
  file {
    path => "/tmp/messages_%{+YYYY-MM-dd}.log"
  }
}

#配置檔案的名字,通常以'檔名_es.conf'命名,
#%{}表示變數,%{+YYYY-MM-DD}表示今年的第多少天
#配置檔案使用的是yml語法
#虛擬機器關機之後,開機的時候一定要同步時間

3.logstash收集日誌檔案到ES

[root@web01 ~]# vim /etc/logstash/conf.d/file_es.conf
input {
  file {
    path => "/var/log/messages"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["172.16.1.51:9200","10.0.0.52:9200"]
    index => "messages_%{+YYYY-MM-dd}"
  }
}

4.Logstash收集多日誌到檔案

[root@web01 ~]# vim /etc/logstash/conf.d/file_file.conf
input {
  file {
    type => "messages_log"
    path => "/var/log/messages"
    start_position => "beginning"
  }
  file {
    type => "secure_log"
    path => "/var/log/secure"
    start_position => "beginning"
  }       
}        
output {  
  if [type] == "messages_log" { 
    file {
      path => "/tmp/messages_%{+YYYY-MM-dd}.log"
    }        
  }
  if [type] == "secure_log" {
    file {
      path => "/tmp/secure_%{+YYYY-MM-dd}.log"
    }
  } 
}

5.Logstash收集多日誌到ES

1)方法一:

[root@web01 ~]# vim /etc/logstash/conf.d/more_es.conf 
input {
  file {
    type => "messages_log"
    path => "/var/log/messages"
    start_position => "beginning"
  }
  file {
    type => "secure_log"
    path => "/var/log/secure.log"
    start_position => "beginning"
  }
}

output {
  if [type] == "messages_log" {
    elasticsearch {
      hosts => ["10.0.0.51:9200","10.0.0.52:9200"]
      index => "messages_%{+YYYY-MM-dd}"
    }
  }
  if [type] == "secure_log" {
    elasticsearch {
      hosts => ["10.0.0.51:9200","10.0.0.52:9200"]
      index => "secure_%{+YYYY-MM-dd}"
    }
  }
}

[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es.conf &

#啟動後檢視頁面,使用ntp驗證管道

2)方法二:

[root@web01 ~]# vim /etc/logstash/conf.d/more_es_2.conf 
input {
  file {
    type => "messages_log"
    path => "/var/log/messages"
    start_position => "beginning"
  }
  file {
    type => "secure_log"
    path => "/var/log/secure.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200","10.0.0.52:9200"]
    index => "%{type}_%{+YYYY-MM-dd}"
  }
}

#啟動多例項
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es_2.conf

3)啟動多例項

#建立不同的資料目錄
[root@web01 ~]# mkdir /data/logstash/more_es_2 -p
[root@web01 ~]# mkdir /data/logstash/more_es -p

#啟動時使用--path.data指定資料目錄
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es.conf --path.data=/data/logstash/more_es &
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es_2.conf --path.data=/data/logstash/more_es_2 &

#如果資源充足,可以使用多例項收集多日誌,如果伺服器資源不足,啟動不了多例項,配置一個檔案收集多日誌啟動

二、Logstash收集Tomcat日誌

1.安裝Tomcat

1.安裝java環境
[root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
2.上傳包
[root@web01 ~]# rz apache-tomcat-10.0.0-M7.tar.gz
3.解壓
[root@web01 ~]# tar xf apache-tomcat-10.0.0-M7.tar.gz -C /usr/local/
4.做軟連線
[root@web01 ~]# ln -s /usr/local/apache-tomcat-10.0.0-M7 /usr/local/tomcat
5.啟動Tomcat
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh && tailf /usr/local/tomcat/logs/catalina.out
6.訪問頁面 10.0.0.7:8080

#tomcat預設日誌格式
10.0.0.1 - - [13/Aug/2020:16:51:51 +0800] "GET /random.jsp HTTP/1.1" 200 178

#catalina.out是Tomcat的啟動日誌,可以在這檢視Tomcat是否啟動成功
[root@web01 ~]# ls /var/log/tomcat/
catalina.2020-08-13.log     			#報錯日誌 
localhost_access_log.2020-08-13.txt 	#Tomcat的訪問日誌
catalina.out				#啟動日誌
manager.2020-08-13.log
host-manager.2020-08-13.log
localhost.2020-08-13.log

2.配置Logstash收集Tomcat日誌到檔案

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_file.conf
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.txt"
    start_position => "beginning"
  }
}
output {
  file {
    path => "/tmp/tomcat_%{+YYYY-MM-dd}.log"
  }
}

#input外掛中,不識別%{}變數,但是可以識別*
#logstash只會收集發改變的檔案,收集檔案一次之後,如果檔案內容不變的話,logstash是不會再次收集的(類似於增量複製)

3.配置Logstash收集Tomcat日誌到ES

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.txt"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_%{+YYYY-MM-dd}.log"
  }
}


#注意,Tomcat的日誌的路徑
#該配置只適用於Tomcat正確日誌,因為Tomcat正確日誌是一條一條的,可以直接使用file外掛收集,Tomcat一條錯誤日誌有多行,所以不能再使用該配置

三、收集Tomcat日誌修改格式

#問題
收集tomcat日誌,當遇到報錯時,一條'報錯會被分割成很多條'資料,不方便檢視

#解決方法:
1.修改tomcat日誌格式為json(也就是把Tomcat一個日誌變成一條,{"a":"b","c":"d"}),#Tomcat直接把報錯日誌變成為一條
	1)開發修改輸出日誌為json
	2)修改tomcat配置,日誌格式為json
2.使用logstash的input外掛下的mutiline模組,#從logstash管道把一條Tomcat報錯日誌變成一條

1.方法一:修改tomcat日誌格式

1)配置tomcat日誌為json格式

[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
#把原來的日誌格式註釋,新增我們的格式,註釋標籤為<!-- -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="tomcat_access_json." suffix=".log"
               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;}"/>

2)重啟tomcat

[root@web01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh

3)配置收集新的tomcat日誌

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}.log"
  }
}

#通過kibana可以看到,Tomcat一條報錯日誌被歸為一條,所以kibana中,一條報錯日誌以一條的形式顯示
#啟動logstash之前,需要刪除es中tomcat_json_%{+YYYY-MM-dd}.log索引

2.方法二:使用mutiline模組收集日誌

1)配置收集日誌測試

[root@web01 ~]# vim /etc/logstash/conf.d/test_mutiline.conf
input {
  stdin {
    codec => multiline {
	  #以[開頭
      pattern => "^\["
      #匹配到[
      negate => true
      #碰到[向上合併是previous,向下合併是next
      what => "previous"
    }
  }
}
output {
  stdout {
    codec => json
  }
}

#測試,輸入內容不會直接輸出,當遇到以 [ 開頭才會收集以上的日誌

2)配置收集tomcat日誌

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_mutiline.conf 
input {
  file {
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "beginning"
    codec => multiline {
      pattern => "^\["
      negate => true
      what => "previous"
    }
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}"
    codec => "json"
  }
}

3)將tomcat報錯日誌寫入

[root@web01 ~]# cat 1.txt >> /usr/local/tomcat/logs/tomcat_access_json.2020-08-14.log

4)頁面檢視資料,然後把報錯給開發

四、收集Nginx日誌

1.安裝Nginx

[root@web01 ~]# yum install -y nginx

2.配置Nginx日誌格式

[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
    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;
... ...

3.配置收集Nginx日誌

[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "nginx_json_%{+YYYY-MM-dd}.log"
  }
}

#kibana顯示內容
host:web01 message:{"@timestamp":"2020-08-14T16:44:49+08:00","host":"10.0.0.7","clientip":"10.0.0.1","size":555,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.0.0.7","url":"/favicon.ico","referer":"-","agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"404"} path:/var/log/nginx/access.log @version:1

五、獲取的日誌引數分離

1.方法一:

1)修改tomcat日誌收集配置

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "beginning"
  }
}

#把收集到的資料進行處理
filter {
  json {
    source => "message"
    remove_field => ["message"]
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}.log"
  }
}

#kibana中,Jason資料才能做圖 
#注意,配置filter,一定要重新整理kibana中的management中的索引,這樣kibana才能識別新生成的欄位,進而可以做圖
#message資料已經拆分,資料還在,去掉message資料
#使用filter外掛刪除多餘的欄位filter

2.方法二:

1)修改收集Nginx日誌的配置

#nginx不需要配置修改獲取日誌,只需要收集同時修改格式即可,當然也可以使用filter的方式 
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf 
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    codec => "json"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "nginx_json_%{+YYYY-MM-dd}.log"
  }
}

六、Logstash收集日誌寫入redis

1.安裝redis

2.配置將資料寫入redis

[root@web01 ~]# vim /etc/logstash/conf.d/nginx_to_redis.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    codec => "json"
  }
}
output {
  redis {
    host => "172.16.1.51"
    port => "6379"
    data_type => "list"
    db => "0"
    key => "nginx_log"
  }
}

#如果redis有密碼,password => 123
#雖然設定了beginning,但是redis只會儲存管道做好後新訪問的json value

LLEN nginx_log
lrande nginx_log 0 -1
redis-cli --raw

作業:

1.恢復快照,重新搭建ELK叢集
2.收集Nginx日誌,普通日誌和json格式日誌到不同索引
3.收集tomcat日誌,普通日誌和json格式日誌到不同索引
4.收集tomcat錯誤日誌

收集nginx一般日誌(訪問日誌或把報錯日誌都適用)

#nginx預設日誌
[root@web01 conf.d]# vim /var/log/nginx/access.log
10.0.0.1 - - [15/Aug/2020:14:53:11 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
10.0.0.1 - - [15/Aug/2020:14:53:12 +0800] "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
10.0.0.1 - - [15/Aug/2020:14:53:13 +0800] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"

#logstash收集nginx日誌,只能收集管道搭建好之後的日誌
[root@web01 conf.d]# vim /etc/logstash/conf.d/nginx_es.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200","10.0.0.52:9200"]
    index => "nginx_es_%{+YYYY-MM-dd}.log"
  }
}

#kibana檢視資料格式
	10.0.0.1 - - [15/Aug/2020:15:57:36 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://10.0.0.7/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36" "-"

收集nginx json格式資料

#nginx json格式資料
[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
    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檢視日誌格式
{"@timestamp":"2020-08-16T13:21:22+08:00","host":"10.0.0.7","clientip":"10.0.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.0.0.7","url":"/index.html","referer":"-","agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","status":"304"}

#logstash收集nginx日誌
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "nginx_es_json_%{+YYYY-MM-dd}"
  }
}

#kibana檢視資料格式
{"@timestamp":"2020-08-16T13:21:22+08:00","host":"10.0.0.7","clientip":"10.0.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.0.0.7","url":"/index.html","referer":"-","agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","status":"304"}

收集Tomcat訪問日誌

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.txt"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_%{+YYYY-MM-dd}.log"
  }
}

#kibana檢視資料格式

收集Tomcat報錯日誌

#方法一
1.修改Tomcat配置檔案
[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
#把原來的日誌格式註釋,新增我們的格式,註釋標籤為<!-- -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="tomcat_access_json." suffix=".log"
               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;}"/>

2.編輯logstash配置檔案
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
  file {
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}"
  }
}           

#方法二
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_mutiline.conf 
input {
  file {
    path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
    start_position => "beginning"
    codec => multiline {
      pattern => "^\["
      negate => true
      what => "previous"
    }
  }
}

output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_json_%{+YYYY-MM-dd}"
    codec => "json"
  }
}

收集Tomcat訪問日誌 json格式

[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
#把原來的日誌格式註釋,新增我們的格式,註釋標籤為<!-- -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="tomcat_access_json." suffix=".log"
               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;}"/>

#檢視Tomcat日誌
{"client":"10.0.0.1",  "client user":"-",   "authenticated":"-",   "access time":"[16/Aug/2020:13:50:16 +0800]",     "method":"GET / HTTP/1.1",   "status":"304",  "send bytes":"-",  "Query?string":"",  "partner":"-",  "Agent version":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}

#使用mutiline外掛,收集Tomcat日誌
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_mutiline.conf 
input {
  file {
    path => "/var/log/tomcat/localhost_access_log.*.txt"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "tomcat_es_json_%{+YYYY-MM-dd}"
  }
}

#使用kibana檢視日誌
{"client":"10.0.0.1",  "client user":"-",   "authenticated":"-",   "access time":"[16/Aug/2020:13:50:03 +0800]",     "method":"GET / HTTP/1.1",   "status":"304",  "send bytes":"-",  "Query?string":"",  "partner":"-",  "Agent version":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}