elk系列3之通过json格式采集Nginx日志【转】

转自

elk系列3之通过json格式采集Nginx日志 - 温柔易淡 - 博客园
http://www.cnblogs.com/liaojiafa/p/6158245.html

preface

公司采用的LNMP平台,跑着挺多nginx,所以可以利用elk好好分析nginx的日志。下面就聊聊它吧。

下面的所有操作都在linux-node2上操作

安装Nginx

nginx是开始,所以你得安装一个Nginx,安装方法采用yum安装,yum源:http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm
下面的所有操作都在linux-node2上操作

[root@linux-node2 ~]# rpm -vhi http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm
[root@linux-node2 ~]# yum -y install nginx

安装完以后,我们进行通过ab访问,以此来产生日志:

[root@linux-node2 nginx]# ab -n 1000 -c 20 http://192.168.141.4/
[root@linux-node2 nginx]# cd /var/log/nginx/
[root@linux-node2 nginx]# cat access.log

学习json模块

我们在官网上学习json模块: https://www.elastic.co/guide/en/logstash/2.3/plugins-codecs-json.html

获取Nginx的日志方式

  1. Nginx日志改成json输出。
  2. 直接获取nginx的访问日志,放入redis里面。
  3. Python脚本读取redis,写成json,写入ES。

我们这次主要是讲第一种方式:Nginx日志改成json输出。

配置nginx

我们需要更改nginx的配置文件,在http模块下面,添加一个log_format字段即可,配置文件如下:

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    log_format access_log_json '{"user_ip":"$http_x_real_ip","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_req":"$request","http_code":"$status","body_bytes_sents":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}';    # 这行是新添加的,指定为json格式,键值对的格式

    access_log  /var/log/nginx/access.log access_log_json;   # 使用刚才定义的日志格式

确认无误后,重启服务:

[root@linux-node2 nginx]# service  nginx reload
配置logstash

确定nginx正常工作 ,那么我们就开始配置logstash,对于logstash,我们应该在output上面首先配置一个屏幕输出,在确认屏幕输出没有问题后,我们在把output输入到elasticsearch上。

[root@linux-node2 ~]# cat /etc/logstash/conf.d/nginx.conf
input{
    file {
        path => "/var/log/nginx/access.log"
        codec => "json"
    }
}
filter{
}
output{
    stdout{
        codec => rubydebug
    }
}

确认无误后,启动logstash

[root@linux-node2 ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/nginx.conf

此时我们通过再打开一个终端,通过ab来发起5个请求,来看看logstash的输出是否有误:

[root@linux-node2 nginx]# ab -n 4 -c 1 http://192.168.141.4/

我们切回到启动logstash的终端,然后看看输出。

{
             "user_ip" => "-",
              "lan_ip" => "192.168.141.4",
            "log_time" => "2016-12-10T16:04:46+08:00",
            "user_req" => "GET / HTTP/1.0",
           "http_code" => "200",
    "body_bytes_sents" => "3698",
            "req_time" => "0.000",
             "user_ua" => "ApacheBench/2.3",
            "@version" => "1",
          "@timestamp" => "2016-12-10T08:04:47.705Z",
                "path" => "/var/log/nginx/access.log",
                "host" => "linux-node2"
}

显然,目前logstash是工作没有问题的,那么就可以把ouput配置到elasticsearch上了,下面更改下配置logstash的文件

[root@linux-node2 nginx]# cat /etc/logstash/conf.d/nginx.conf
input{
    file {
        path => "/var/log/nginx/access.log"
        codec => "json"
        type => "nginx-access-log"
    }
}
filter{
}
output{
    elasticsearch {
        hosts => ["192.168.141.3:9200"]
        index => "nginx-access-log-%{+YYYY.MM.dd}"
    }
}

确定没有问题后,重新启动logstash

[root@linux-node2 logstash]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/nginx.conf

Notice
如果logstash启动后,我们访问192.168.141.3上的head模块,找不到nginx-access-log的话,那么就删除sincedb,删除以后,重新启动logstash就能访问到了。是因为我们刚才使用logstash的时候,通过rubydebug进行了stdout,导致sincedb文件记录了相关的信息,删除sincedb文件后让elasticsearch重新记录即可。

[root@linux-node2 logstash]# rm -f /var/lib/logstash/.sincedb_d883144359d3b4f516b37dba51fab2a2  
/root/.sincedb_ssdafdsafsfasdf   

kibana上配置。

我们可以访问http://192.168.141.3:9200/_plugin/head/,可以看到nginx-access-log,如下图所示
image
image

到了这里,我想你就明白为啥我们要把nginx的日志配制成json数据格式,没错,就是为了方便我们在head和kibana里面处理。
下面在kibana里添加的时候,我们在Time-field 字段选择的log_time,这样以nginx的log_time字段作为时间戳。

image

好了到此,简单的nginx日志收集到此结束。
原文地址:https://www.cnblogs.com/paul8339/p/7356923.html