首頁 > 軟體

ELK收集Tomcat紀錄檔的實現

2022-07-19 14:06:28

01 Tomcat 安裝與測試

1.1 安裝 Tomcat

安裝Tomcat的本體和相關官方測試demo,參考連結

apt-get install tomcat8 -y # 安裝Tomcat本體
apt-get install tomcat8-docs tomcat8-examples tomcat8-admin -y # 安裝測試demo

1.2 Tomcat 啟動檢查

systemctl start tomcat8 # 啟動Tomcat
systemctl status tomcat8
netstat -lntup|grep 8080 # 埠測試
lsof -i:8080
# 埠檢查得到輸出
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    4502 tomcat8   63u  IPv6 125026      0t0  TCP *:http-alt (LISTEN)

1.3 檢視 Tomcat 紀錄檔

啟動tomcat之後,使用本地瀏覽器存取http://localhost:8080/存取tomcat頁面,在頁面中點選按鈕產生HTTP請求,讓tomcat產生紀錄檔

tail -f /var/log/tomcat8/localhost_access_log.2021-08-01.txt 

02 修改 Tomcat 紀錄檔為 Json 格式

開啟Tomcat的server.xml組態檔進行修改,在紀錄檔檔案中的文末修改如下對應設定

# 編輯組態檔
vim /etc/tomcat8/server.xml
#將以下內容替換組態檔中135行對應內容
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".log"
               pattern="{&quot;client&quot;:&quot;%h&quot;,  &quot;client user&quot;:&quot;%l&quot;,   &quot;authenticated&quot;:&quot;%u&quot;,   &quot;access time&quot;:&quot;%t&quot;,     &quot;method&quot;:&quot;%r&quot;,   &quot;status&quot;:&quot;%s&quot;,  &quot;send bytes&quot;:&quot;%b&quot;,  &quot;Query?string&quot;:&quot;%q&quot;,  &quot;partner&quot;:&quot;%{Referer}i&quot;,  &quot;Agent version&quot;:&quot;%{User-Agent}i&quot;}"/>
# 檢視修改內容
cat -n /etc/tomcat8/server.xml

重新啟動tomcat並檢視紀錄檔,檢驗是否設定成功,產生新的紀錄檔還是需要通過使用瀏覽器存取8080埠,在Tomcat的demo樣例中對tomcat傳送請求產生紀錄檔。

# 先清空紀錄檔
> /var/log/tomcat8/localhost_access_log.2021-08-02.txt 
# 重新啟動Tomcat
systemctl restart tomcat8
# 檢視紀錄檔
root@master:/var/log/tomcat8# tail -f /var/log/tomcat8/localhost_access_log.2021-08-02.log  # 檢視紀錄檔命令
{"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:23:55 +0000]", "method":"GET /examples/servlets/images/return.gif HTTP/1.1", "status":"200",  "send bytes":"1231",  "Query?string":"", "partner":"http://172.16.255.131:8080/examples/servlets/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}
{"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:23:57 +0000]", "method":"GET /examples/servlets/servlet/RequestParamExample HTTP/1.1", "status":"200",  "send bytes":"673",  "Query?string":"", "partner":"http://172.16.255.131:8080/examples/servlets/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}
{"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:24:01 +0000]", "method":"GET /host-manager/html HTTP/1.1", "status":"401",  "send bytes":"2044",  "Query?string":"", "partner":"http://172.16.255.131:8080/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}

03 設定 Filebeat 採集 Tomcat 紀錄檔

新增Filebeat輸入設定,將tomcat紀錄檔參照Nginx的Json格式紀錄檔採集方式設定如下

vim小技巧:將連續多行內容複製對應位置使用t命令,在Normal模式中輸入:2,7t11表示將第二到第七行的內容複製到第十一行開頭;將連續多行內容移動對應位置使用m命令,在Normal模式中輸入:2,7m11表示將第二到第七行的內容移動到第十一行開頭

vim小技巧:在輸入內容時要使用到某個檔案路徑可以是用!命令然後使用shell命令檢視內容,例如檢視某個檔案的路徑可以在Normal模式中輸入:!ls /var/log/tomcat8/...提示

# ================== Filebeat inputs ===============
# ------------------------------Tomcat----------------------------------
- type: log
  enabled: true
  paths:
    # - /var/log/tomcat8/localhost_access_log.2021-08-02.log
    # 為了能夠採集所有日期的紀錄檔,將檔名中的指定日期改成萬用字元`*`
    - /var/log/tomcat8/localhost_access_log.*.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["tomcat"]

# ================================== Outputs ===================================
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  hosts: ["172.16.255.131:9200"]
  indices:
      - index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
        when.contains:
            tags: "access"
      - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM}"
        when.contains:
            tags: "error"
# 在輸出設定中新增如下索引設定識別tomcat紀錄檔,值得注意的時這裡不需要再重新編輯template設定,應該pattern設定只在第一次使用時進行匹配識別
      - index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM}"
        when.contains:
            tags: "tomcat"

04 使用Kibana檢視Tomcat紀錄檔

設定完成之後,重新啟動Filebeat採集Json格式紀錄檔

systemctl restart filebeat

檢視ES中儲存的Tomcat紀錄檔是否是Json格式

到此這篇關於ELK收集Tomcat紀錄檔的實現的文章就介紹到這了,更多相關ELK Tomcat紀錄檔內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com