elk系列3之經過json格式採集Nginx日誌

preface

公司採用的LNMP平臺,跑着挺多nginx,因此能夠利用elk好好分析nginx的日誌。下面就聊聊它吧。html

下面的全部操做都在linux-node2上操做node

安裝Nginx

nginx是開始,因此你得安裝一個Nginx,安裝方法採用yum安裝,yum源:http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm
下面的全部操做都在linux-node2上操做linux

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

安裝完之後,咱們進行經過ab訪問,以此來產生日誌:nginx

[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.htmlredis

獲取Nginx的日誌方式

  1. Nginx日誌改爲json輸出。
  2. 直接獲取nginx的訪問日誌,放入redis裏面。
  3. Python腳本讀取redis,寫成json,寫入ES。

咱們此次主要是講第一種方式:Nginx日誌改爲json輸出。json

配置nginx

咱們須要更改nginx的配置文件,在http模塊下面,添加一個log_format字段便可,配置文件以下:ruby

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;   # 使用剛纔定義的日誌格式

確認無誤後,重啓服務:elasticsearch

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

肯定nginx正常工做 ,那麼咱們就開始配置logstash,對於logstash,咱們應該在output上面首先配置一個屏幕輸出,在確認屏幕輸出沒有問題後,咱們在把output輸入到elasticsearch上。ide

[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日誌收集到此結束。
相關文章
相關標籤/搜索