ELK之日誌收集filebeat,並對nginx,tomcat access日誌JSON格式化

一:ELK日誌收集器組件filebeat下載

二:filebeat 配置說明 filebeat.yml

2.1 input plugin 配置說明

`- type: log
# 是否生效該配置
enabled: true 
# 讀取文件的路徑,能夠多個
paths: 
-C:\Users\Administrator\.IntelliJIdea2017.1\system\tomcat\Unnmed_jjj\logs\localhost_access_log.*.txt
- D:\programFiles\nginx-1.13.6\logs\host.access.log
# 字符行是json格式,以下配置
# json 全部的key 是否在頂級key(json)下
  json.keys_under_root: true
# 若是外部存在key,是否覆蓋
  json.overwrite_keys: true
# 是否添加錯誤key,如解析出錯,會添加解析錯誤信息
  json.add_error_key: true
# 添加message 的key
  json.message_key: log

2.2 output plugin 配置說明

2.2.1 開啓ElasticSearch輸出

#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
   #Array of hosts to connect to.
  #hosts: ["localhost:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"

2.2.2 開啓Logstash輸出

#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["127.0.0.1:4560"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

三:統一發送JSON結構定義:

字段名稱 字段類型 字段說明
localIp string 本地服務ip
status string 請求狀態
method string 請求方法
url string 請求地址
reqIp string 請求客戶端ip
port string 端口
duration string 請求時長
reqTime string 請求時間
serviceType string 服務容器類型,nginx,tomcat...

四:nginx access 日誌json格式化配置

字段 說明
request_time 整個請求的總時間,以秒爲單位
status 記錄請求返回的http狀態碼,好比成功是200。
upstream_status upstream狀態,好比成功是200.
body_bytes_sent 發送給客戶端的文件主體內容的大小,好比899,能夠將日誌每條記錄中的這個值累加起來以粗略估計服務器吞吐量
remote_addr 遠程客戶端的IP地址。
request 請求的URI和HTTP協議,這是整個PV日誌記錄中最有用的信息,記錄服務器收到一個什麼樣的請求
http_referer 記錄從哪一個頁面連接訪問過來的(請求頭Referer的內容 )
http_user_agent 客戶端瀏覽器信息(請求頭User-Agent的內容 )
http_x_forwarded_for 客戶端的真實ip,一般web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,經過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,能夠增長
upstream_response_time 請求過程當中,upstream的響應時間,以秒爲單位
upstream_addr upstream的地址,即真正提供服務的主機地址
remote_user 遠程客戶端用戶名稱,用於記錄瀏覽者進行身份驗證時提供的名字,如登陸百度的用戶名scq2099yt,若是沒有登陸就是空白。

在nginx.conf 配置文件,配置以下:html

http {
	·····
	log_format  main '{"localIp":"$http_host","status":"$status","method":"$request_method","url":"$request","reqIp":"$remote_addr","port":"","duration":"$request_time","reqTime":"[$time_local]","serviceType":"nginx"}';
	access_log  logs/access.log  main;
	server {
		·····
		access_log  logs/host.access.log  main;
		·····
	}
}

五:tomcat access 日誌json格式化配置

字段 說明
%a 遠端IP
%A 本地IP
%b 發送的字節數,不包含HTTP頭,若是爲0,使用」-」
%B 發送的字節數,不包含HTTP頭
%h 遠端主機名(若是resolveHosts=false),遠端的IP
%H 請求協議
%l 從identd返回的遠端邏輯用戶名,老是返回’-’
%m 請求的方法
%p 收到請求的本地端口號
%q 查詢字符串
%r 請求的第一行
%s 響應的狀態碼
%S 用戶的sessionID
%t 日誌和時間,使用一般的log格式
%u 認證之後的遠端用戶(若是存在的話,不然爲’-’)
%U 請求的URI路徑
%v 本地服務器的名稱
%D 處理請求的時間,以毫秒爲單位
%T 處理請求的時間,以秒爲單位

在server.xml 配置文件,配置以下:nginx

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
			   prefix="localhost_access_log." suffix=".txt"
			   pattern="{&quot;reqTime&quot;:&quot;%t&quot;,&quot;reqIp&quot;:&quot;%a&quot;,&quot;duration&quot;:&quot;%T&quot;,&quot;url&quot;:&quot;%U&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;method&quot;:&quot;%m&quot;,&quot;localIp&quot;:&quot;%A&quot;,&quot;port&quot;:&quot;%p&quot;,&quot;serviceType&quot;:&quot;tomcat&quot;}" />
相關文章
相關標籤/搜索