使用elk處理apache訪問日誌

需求描述

web端每天會有大量得訪問日誌,一旦出錯看日誌得時間可能會超過處理問題得時間,日誌量過大對於運維人員來說也是一個痛苦得過程

環境描述

server-01http/nginx提供訪問日誌給filebeat
server-01
filebeat採集訪問日誌提供給server-02上的elasticsearch
server-02elasticsearch
收集server-01日誌將索引提供給kibana
server-02kibana展示elasticsearch提供可視化處理

操作步驟

1、web、filebeat安裝部署

環境準備yum安裝環境,以便安裝apache和filebeat,源碼安裝也可以

filebeat的yum環境可以參考《使用logstash收集並json化MySQL慢日誌

1.1安裝apache、filebeat

[root@mode-01-0004 ~]# yum -y install httpd filebeat

編寫測試頁面放在html目錄下

[root@mode-01-0004 html]# echo "hello world" > /var/www/html/index.html

訪問測試

[root@mode-01-0004 ~]# curl 127.0.0.1
hello world
[root@mode-01-0004 lib]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Date: Sun, 23 Dec 2018 04:17:18 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sun, 23 Dec 2018 04:11:32 GMT
ETag: "c-57da8ace92d75"
Accept-Ranges: bytes
Content-Length: 12
Content-Type: text/html; charset=UTF-8

查看apache最初的access.log日誌格式,如下:

[root@mode-01-0004 ~]# cat /var/log/httpd/access_log
127.0.0.1 - - [23/Dec/2018:12:12:35 +0800] "GET / HTTP/1.1" 200 12 "-" "curl/7.29.0"
127.0.0.1 - - [23/Dec/2018:12:17:18 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.29.0"

1.2配置filebeat

[root@mode-01-0004 ~]# egrep -v "^$|#" /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/httpd/access_log
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  hosts: ["10.16.0.15:9200"]

啓動filebeat

[root@mode-01-0004 ~]# systemctl restart filebeat

在server-02接收端查看elasticsearch是否生成索引,server-02端elasticsearch和kibana配置參考《使用logstash收集並json化MySQL慢日誌》這裏不再贅述。

[root@mode-01-0005 ~]# curl http://10.16.0.15:9200/_cat/indices
yellow open filebeat-6.5.4-2018.12.23 lw-Zhu1TQu-y7EZ_ZpmvYQ 3 1  5 0  36.7kb  36.7kb   ##注意時間,新生成的索引
yellow open filebeat-6.5.4-2018.12.22 G_yLOnP0S2GItVZjOqZLUQ 5 1 13 0 265.5kb 265.5kb
green  open .kibana_1                 gP6vi3f8Q6WgspSrA7f7KQ 1 0  7 0  36.8kb  36.8kb

2、json化http日誌

編輯httpd配置文件

[root@mode-01-0004 ~]# vim /etc/httpd/conf/httpd.conf
<IfModule log_config_module>
LogFormat "{ \
            \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \         ##時間戳
            \"@version\": \"1\", \                         ##版本
            \"tags\":[\"apache\"], \                        ##標籤
            \"message\": \"%h %l %u %t \\\"%r\\\" %>s %b\", \        ##消息            
            \"clientip\": \"%a\", \                         ##訪問IP            
            \"duration\": %D, \                           ##持續時間            
            \"status\": %>s, \                            ##狀態            
            \"request\": \"%U%q\", \                        ##請求            
            \"urlpath\": \"%U\", \                         ##訪問路徑            
            \"urlquery\": \"%q\", \                         ##url查詢            
            \"bytes\": %B, \                             ##字節數            
            \"method\": \"%m\", \                          ##請求方式            
            \"site\": \"%{Host}i\", \                       ##站點IP
            \"referer\": \"%{Referer}i\", \                   ##訪問來源
            \"useragent\": \"%{User-agent}i\" \                 ##用戶瀏覽器
           }" apache_json
    CustomLog "logs/access_log" apache_json                                 ##指定log日誌調用的格式
</IfModule>

重啓apache查看日誌格式

[root@mode-01-0004 ~]# systemctl restart httpd
[root@mode-01-0004 ~]# tailf -100 /var/log/httpd/access_log
{             "@timestamp": "2018-12-23T13:33:18+0800",             "@version": "1",             "tags":["apache"],             "message": "10.16.0.15 - - [23/Dec/2018:13:33:18 +0800] \"GET / HTTP/1.1\" 200 12",             "clientip": "10.16.0.15",             "duration": 312,             "status": 200,             "request": "/index.html",             "urlpath": "/index.html",             "urlquery": "",             "bytes": 12,             "method": "GET",             "site": "10.16.0.14",             "referer": "-",             "useragent": "curl/7.29.0"            }

3、修改filebeat配置

[root@mode-01-0004 ~]# egrep -v "^$|#" /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/httpd/access_log
  json.keys_under_root: true
   json.overwrite_keys:  true
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  hosts: ["10.16.0.15:9200"]

重啓filebeat,並做訪問測試,登陸kibana查看索引是否生效,定義圖形收集的字段,根據狀態碼繪製餅圖、根據訪問IP繪製柱狀圖等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章