logstash日志收集之json格式 & 日志參數分離


ELK日志收集分離篇

一、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"
  }
}

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"]
    index => "messages_%{+YYYY-MM-dd}.log"
  }
}

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 => "beginn	ing"
  }       
}        
output {  
  if [type] == "messages_log" { 
    file {
      path => "/tmp/messages_%{+YYYY-MM-dd}"
    }        
  }
  if [type] == "secure_log" {
    file {
      path => "/tmp/secure_%{+YYYY-MM-dd}"
    }
  } 
}

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"
    start_position => "beginning"
  }
}
output {
  if [type] == "messages_log" {
    elasticsearch {
      hosts => ["10.0.0.51:9200"]
      index => "messages_%{+YYYY-MM-dd}"
    }
  }
  if [type] == "secure_log" {
    elasticsearch {
      hosts => ["10.0.0.51:9200"]
      index => "secure_%{+YYYY-MM-dd}"
    }
  }
}

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

#啟動后查看頁面

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"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200"]
    index => "%{type}_%{+YYYY-MM-dd}"
  }
}

# 這里兩個同時運行,所以需要指定數據目錄
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es_2.conf --path.data=/data/logstash/more_es_2 &

3)啟動多實例

#創建不同的數據目錄
[root@web01 ~]# mkdir /data/logstash/more_es_2
[root@web01 ~]# mkdir /data/logstash/more_es

#啟動時使用--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 &

#如果資源充足,可以使用多實例收集多日志,如果服務器資源不足,啟動不了多實例,配置一個文件收集多日志啟動

二、Tomcat日志收集

1.普通收集

0> 說明

# 在一般情況中,收集的日志不應該是local_access_log日志,因為這是一個訪問日志,在一般架構中,
tomcat前端應該有nginx或者其他代理服務,那么對於訪問日志,直接在代理服務器中收集即可,在tomcat中收集的日志應該為catalina.XXXX-XX-XX.log日志,此日志文件為服務啟動文件。

1>.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"
  }
}

2>.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"
  }
}

2.收集日志並修改為json格式

0>.說明

#收集tomcat日志,當遇到報錯時,一條報錯會被分割成很多條數據,不方便查看,所以需要將原本格式改為json格式,
然后再使用input插件下的mutiline模塊,或者直接使用mutiline模塊,也就是方法二的方式。

解決方法:
1.修改tomcat日志格式為json
	1)開發修改輸出日志為json
	2)修改tomcat配置,日志格式為json
2.使用logstash的input插件下的mutiline模塊
3.使用filebeat模塊,修改日志格式

# 這里展示1.2和2中的方式,通過修改tomcat配置文件和直接使用模塊兩種方式

1>.方法一:修改tomcat的日志格式

① 修改tomcat配置文件日志格式
# 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;}"/>
② 重啟tomcat
[root@web01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh
③ 啟動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}.log"
  }
}

2> 方法二:使用mutiline模塊實現錯誤日志收集

①說明
# 此方式可在不修改tomcat主配置文件中的日志格式下操作,直接使用模塊會將輸出結果以json格式輸出
② 收集日志以json格式輸出到屏幕
[root@web01 ~]# vim /etc/logstash/conf.d/test_mutiline.conf
input {
  stdin {
  	  # 調用匹配合並模塊
    codec => multiline {
	  # 遇到以 [ 開頭的開始合並
      pattern => "^\["
      #匹配到上面指定的字符進行合並,如果是false則匹配到進行合並
      negate => true
      #向上合並,向下合並是next
      what => "previous"
    }
  }
}
output {
  stdout {
    codec => json
  }
}

#測試,輸入內容不會直接輸出,當遇到以 [ 開頭才會收集以上的日志
③ 收集日志以json格式輸出到ES
# 因為在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"
  }
}
④ 模擬將錯誤日志寫入
[root@web01 ~]# cat 1.txt >> /usr/local/tomcat/logs/tomcat_access_json.2020-08-14.log

# 最后網頁查看結果,錯誤日志沒有像以往一樣分隔成多條數據,而是成塊顯示

三、nginx日志收集

1.普通收集

# 將日志文件輸出到es中
[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"
  }
}

2.日志修改json格式

1.# 將原本的日志格式刪除,使用這種方式會將輸出日志以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;
    .............
    
 2.#實驗將日志文件再次輸出到es中,查看結果(圖一)
  • 這里發現出現兩種數據格式,最上面為json鍵值對格式,下面為舊的默認格式

四、日志參數分離

1.說明

# 需要配合json一起使用,當數據轉換成json后,message里面存放為鍵值對形式,然后將鍵值對中的每個鍵值進行拆分。

方法一:

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"		# 將message中的鍵值對進行拆分
  }
}

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

2.去掉多余數據

#message數據已經拆分,但是原本message中數據還在,去掉老的message數據,只留新拆分數據
filter {
  json {
    source => "message"
    remove_field => ["message"]
  }
}

方法二:

#nginx不需要配置修改獲取日志,只需要收集同時修改格式即可(前提是這個日志文件已經是json格式了)
[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"
  }
}

# 網頁格式查看(圖二)
  • 圖二(都進行了拆分)


免責聲明!

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



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