使用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绘制柱状图等。

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