preface
logstash--> redis --> logstash --> es這套架構在講究松耦合關系里面是最簡單的,
架構圖如下:
解釋下這個架構圖的流程
- 首先前端logstash讀取apache的日志(input讀取)。然后放到redis的db里面(output存入)。存儲形式為列表形式存放
- 后端logstash從redis讀取日志內容(input讀取),從前端logstash指定的庫里面讀取指定的key。讀取之后filter(filter-grok)過濾。然后傳送到es上(output推送)。
- es接受日志,處理。
我們可以參考官網的建議:https://www.elastic.co/guide/en/logstash/2.3/deploying-and-scaling.html#deploying-message-queueing
廢話不多說,我們開始着手配置吧,。
安裝redis
在linux-node2節點上操作
安裝redis,並且啟動,
[root@linux-node2 conf.d]# yum -y install redis # 確保一定是2.4版本以上的,不然logstash input的時候會報錯的 [root@linux-node2 conf.d]# grep bind /etc/redis.conf bind 0.0.0.0 # 這里記得修改監聽地址
確認無誤后啟動redis
[root@linux-node2 conf.d]# /etc/init.d/redis start
配置logstash
linux-node1操作
logstash對應的模塊是output里面的redis模塊,當然,也可以支持rabbitMQ,選擇redis是因為redis性能強,而且配置簡單。那么為啥要在output上做呢,是因為output是logstash的輸出,直接對接在redis上,所以是ouput。
下面就看看官網對redis模塊的講解:https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-redis.html
下面貼下logstash配置,這里把屏幕輸入傳送到redis上
[root@linux-node1 conf.d]# cat redis.conf input { stdin { } } output { redis { host => "192.168.141.4" port => "6379" db => "6" # 選擇那個庫 data_type => "list" # 存入數據的類型 key => "demo" # 數據的鍵 } }
隨便回車敲入一些內容,使其redis庫里面有東西
查看redis結果
我們到登陸redis后查看內容:
redis 127.0.0.1:6379> select 6 # 切換到數據庫6下面 OK redis 127.0.0.1:6379[6]> keys * 1) "demo" redis 127.0.0.1:6379[6]> type demo list redis 127.0.0.1:6379[6]> llen demo # 查看列表長度 (integer) 3 redis 127.0.0.1:6379[6]> lindex demo -1 # 從redis的左邊取第一位 "{\"message\":\"man\",\"@version\":\"1\",\"@timestamp\":\"2016-12-11T07:18:44.751Z\",\"host\":\"linux-node1\"}"
到此,可以說明logstash的output和redis已經能夠正常的工作了
收集apache的日志
linux-node1操作
我們此時更改下logstash的配置,配置如下:
[root@linux-node1 conf.d]# cat /etc/logstash/conf.d/redis.conf input { file { path => "/var/log/httpd/access_log" start_position => "beginning" } } output { redis { host => "192.168.141.4" port => "6379" db => "6" data_type => "list" key => "apache" } }
配置確認無誤后,啟動logstash
[root@linux-node1 conf.d]# /opt/logstash/bin/logstash -f redis.conf
此時切換到linux-node2的終端上查看
redis 127.0.0.1:6379[6]> keys * 1) "demo" 2) "apache" # 出現這個key了 redis 127.0.0.1:6379[6]> llen apache (integer) 2002 redis 127.0.0.1:6379[6]> lindex apache 0 "{\"message\":\"192.168.141.4 - - [11/Dec/2016:15:54:09 +0800] \\\"GET / HTTP/1.0\\\" 403 4961 \\\"-\\\" \\\"ApacheBench/2.3\\\"\",\"@version\":\"1\",\"@timestamp\":\"2016-12-11T07:54:09.745Z\",\"path\":\"/var/log/httpd/access_log\",\"host\":\"linux-node1\"}" # 有內容了
下面我們在linux-node2上配置logstash,來讀取redis的內容
[root@linux-node2 conf.d]# cat getredis.conf input { redis { host => "192.168.141.4" db => "6" data_type => "list" key => "apache" } } output { stdout{ codec => rubydebug } }
確認沒有問題,啟動logstash
[root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
我擦,報錯了,報錯內容如下,該報錯內容一直刷屏:
Redis connection problem {:exception=>#<Redis::CommandError: ERR unknown command 'script'>, :level=>:warn}
出現上面這個報錯的問題是因為redis版本太低,yum安裝的redis版本是2.4.10的,然后我自己源碼包安裝的是3.2.5,步驟如下:
[root@linux-node2 tmp]# wget http://download.redis.io/releases/redis-3.2.5.tar.gz [root@linux-node2 tmp]# tar xzf redis-3.2.5.tar.gz [root@linux-node2 tmp]# cd redis-3.2.5 [root@linux-node2 redis-3.2.5]# make [root@linux-node2 redis-3.2.5]# src/redis-server /etc/redis.conf #配置文件稍作更改即可,該監聽地址
再次啟動logstash,就可以了,完美啟動
[root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
此時我們可以停止剛才啟動的logstash,重新配置一下。
使用grok模塊處理apache日志
我們再次配置下linuix-node2節點上的logstash。先找到分析apache日志的模塊,然后添加filter-grok。如下所示:
首先過濾出分析apache日志的模塊,方便待會調用
[root@linux-node2 conf.d]# grep APACHE /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/grok-patterns --color # 首先過濾出分析apache日志的模塊,方便待會調用 COMMONAPACHELOG %{IPORHOST:clientip} %{HTTPDUSER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-) COMBINEDAPACHELOG %{COMMONAPACHELOG} %{QS:referrer} %{QS:agent}
配置logstash
添加filter-grok。
[root@linux-node2 conf.d]# cat getredis.conf input { redis { host => "192.168.141.4" db => "6" data_type => "list" key => "apache" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } } output { elasticsearch { hosts => ["192.168.141.3:9200"] index => "apache-log-%{+YYYY.MM}" } }
確認無誤后,再次啟動logstash
[root@linux-node2 conf.d]# /opt/logstash/bin/logstash --verbose -f getredis.conf
訪問下apache,然后我們在head上就可以看到apapche的日志。
轉自
elk系列8之logstash+redis+es的架構來收集apache的日志 - 溫柔易淡 - 博客園
http://www.cnblogs.com/liaojiafa/p/6160215.html