111.logstash收集nginx日誌、filebeat

logstash收集nginx日誌html

使用Beats採集日誌(filebeat)nginx

 

 

 

logstash收集nginx日誌redis

 

 

 

1.vim

132上(在logstash這臺機器上加入這個配置文件)瀏覽器

編輯配置文件 vi /etc/logstash/conf.d/nginx.conf//加入以下內容ruby

input { #輸入curl

file { #此處指定一個文件,把這個文件的內容,做爲logstash的輸入elasticsearch

path => "/tmp/elk_access.log" #指定這個文件的路徑url

start_position => "beginning" #指定這個文件從何時開始收集.net

type => "nginx" #自定義

}

}

filter { #這個是對這個日誌對一個過濾(好比輸出格式)。因此訪問日誌的格式也要定義,下面會編輯nginx日誌的格式(第3步驟)

grok {

match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}

}

geoip {

source => "clientip"

}

}

output { #輸出

stdout { codec => rubydebug }

elasticsearch {

hosts => ["192.168.133.132:9200"]

index => "nginx-test-%{+YYYY.MM.dd}"

}

}

#(注意花括號)

2.

下面是配置代理。咱們要產生日誌,得去配置。配置以前要先檢測一下,上面寫的日誌是否正確:

如下在132上操做

檢測配置文件是否有錯

cd /usr/share/logstash/bin

./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf --config.test_and_exit

yum install -y nginx #沒有nginx要安裝

vi /etc/nginx/conf.d/elk.conf//寫入以下內容 #寫一個虛擬主機配置文件

server {

listen 80;

server_name elk.aming.com;

 

location / {

proxy_pass http://192.168.133.130:5601; #代理的目標

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

access_log /tmp/elk_access.log main2; #自動生成

}

3.

如下在132上操做

vim /etc/nginx/nginx.conf//增長以下內容 #定義nginx的日誌格式

log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$upstream_addr" $request_time';

nginx -t #/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload

systemctl start nginx

綁定hosts 192.168.133.132 elk.aming.com

瀏覽器訪問,檢查是否有日誌產生

systemctl restart logstash

4.

130上curl 'localhost:9200/_cat/indices?v'

檢查是否有nginx-test開頭的索引生成

若是有,才能到kibana裏去配置該索引

左側點擊「Managerment」-> 「Index Patterns」-> 「Create Index Pattern」

Index pattern這裏寫nginx-test-* #支持統配,直接這樣寫就能夠

以後點擊左側的Discover

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

使用Beats採集日誌(filebeat)

 

 

 

beats屬於一個輕量的日誌採集器。而logstash在啓動的時候很慢,會消耗資源。那咱們能夠嘗試着使用beats去採集日誌。

beats能夠自定義咱們想要的beats

 

 

https://www.elastic.co/cn/products/beats

filebeat metricbeat packetbeat winlogbeat auditbeat heartbeat #filebeat的成員組件(咱們用到的是filebeat,他是針對日誌的)

可擴展,支持自定義構建

在133上執行

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.0.0-x86_64.rpm

rpm -ivh filebeat-6.0.0-x86_64.rpm #下載rpm包安裝filebeat

如下是在屏幕上的輸出:

首先編輯配置文件

vim /etc/filebeat/filebeat.yml //增長或者更改 #注意文件的格式,空格

filebeat.prospectors:

- input_type: log #定義input類型是log(此處type前面的input字樣,在新版本不會出現,需注意)

paths: #定義日誌的路徑

- /var/log/messages

output.console: #此處文件裏可能沒有output.console:,須要添加這兩行進去。可是添加完,要將out.elasticsearch:註釋掉。以下圖:

enable: true

/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml //能夠在屏幕上看到對應的日誌信息 #-c指定配置文件,啓動filebeat

 

如下是以服務的角色出現:

再編輯配置文件

vim /etc/filebeat/filebeat.yml //增長或者更改

filebeat.prospectors:

- input_type: log

paths:

- /var/log/messages #針對這個日誌

output.elasticsearch: #注意此處跟上面有區分,就不是output.console:了

hosts: ["192.168.133.130:9200"] #以下圖:

systemctl start filebeat

而後,能夠在kibana上建立新的索引

 

 

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

 

擴展部分

 

 

 

x-pack 收費,免費  http://www.jianshu.com/p/a49d93212eca

https://www.elastic.co/subscriptions

Elastic stack演進 http://70data.net/1505.html

基於kafka和elasticsearch,linkedin構建實時日誌分析系統 http://t.cn/RYffDoE  

使用redis http://blog.lishiming.net/?p=463

ELK+Filebeat+Kafka+ZooKeeper 構建海量日誌分析平臺 https://www.cnblogs.com/delgyd/p/elk.html

http://www.jianshu.com/p/d65aed756587

相關文章
相關標籤/搜索