Filebeat +Redis+ELK處理Nginx日誌系統

(一)簡述:

 filebeat:具有日誌收集功能,是下一代的Logstash收集器,但是filebeat更輕量,佔用資源更少,適合客戶端使用。

  redis:Redis 服務器通常都是用作 NoSQL 數據庫,不過 logstash 只是用來做消息隊列。

  logstash:主要是用來日誌的蒐集、分析、過濾日誌的工具,支持大量的數據獲取方式。一般工作方式爲c/s架構,client端安裝在需要收集日誌的主機上,server端負責將收到的各節點日誌進行過濾、修改等操作在一併發往elasticsearch上去。

  elasticsearch:Elasticsearch是個開源分佈式搜索引擎,提供蒐集、分析、存儲數據三大功能。它的特點有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。

  kibana:Kibana可以爲 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,可以幫助彙總、分析和搜索重要數據日誌。

下圖展示的基本的架構圖。

Filebeat +Redis+ELK處理Nginx日誌系統

(二)具體步驟

1、filebeat的簡介和具體配置
Filebeat是一個日誌文件託運工具,在你的服務器上安裝客戶端後,filebeat會監控日誌目錄或者指定的日誌文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀),並且轉發這些信息到elasticsearch或者logstarsh、redis中存放

1.1、工作原理
filebeat由2個主要組件構成:prospector和harvesters。這兩類組件一起協同完成Filebeat的工作,從指定文件中把數據讀取出來,然後發送事件數據到配置的output中。

harvesters:主要負責進行單個文件的內容收集;在運行過程中,每一個Harvester會對一個文件逐行進行內容讀取,並且把讀寫到的內容發送到配置的output中。

Prospector負責管理Harvsters,並且找到所有需要進行讀取的數據源。如果input type配置的是log類型,Prospector將會去配置度路徑下查找所有能匹配上的文件,然後爲每一個文件創建一個Harvster。每個Prospector都運行在自己的Go routine裏

1.2工作流程
當你開啓filebeat程序的時候,它會啓動一個或多個探測器(prospectors)去檢測你指定的日誌目錄或文件,對於探測器找出的每一個日誌文件,filebeat啓動收割進程(harvester),每一個收割進程讀取一個日誌文件的新內容,併發送這些新的日誌數據到處理程序(spooler),處理程序會集合這些事件,最後filebeat會發送集合的數據到你指定的地點
Filebeat +Redis+ELK處理Nginx日誌系統

1.3、具體的相關配置

[root@localhost ~]# vim /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs =============================

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /opt/access.log
#================================ Outputs =====================================
# Configure what output to use when sending the data collected by the beat.
output.redis:
  hosts: ["172.20.67.50:6379"]
  #port: 6379
  #password: "123456"  
  db: 2
  timeout: 10
  key: "nginx-log"

    #####備註:目前使用filebeat向redis寫日誌的時候不能向redis集羣裏寫,會提示報錯,所有redis只能寫到單臺裏,
root@localhost ~]# systemctl start filebeat
[root@localhost ~]# systemctl status filebeat
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-09-20 19:48:36 CST; 3s ago
     Docs: https://www.elastic.co/products/beats/filebeat
 Main PID: 11947 (filebeat)
   CGroup: /system.slice/filebeat.service
           └─11947 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat

Sep 20 19:48:36 localhost.localdomain systemd[1]: Started Filebeat sends log files to Logstash or directly to Elasticsearch..
Sep 20 19:48:36 localhost.localdomain systemd[1]: Starting Filebeat sends log files to Logstash or directly to Elasticsearch....
[root@localhost ~]# 

2、Redis具體的操作
在redis上查看具體導入的數據
Filebeat +Redis+ELK處理Nginx日誌系統

3、logstash 具體的配置

[root@localhost ~]# vim /usr/local/logstash/data/redis.conf 
input {
        redis {
                host => "172.20.67.50"
                port => "6379"
                data_type => "list"
                db => 2
                batch_count => 1     ###這個值是指從隊列中讀取數據時,一次性取出多少條。不寫會報錯,解決辦法就是,不使用這個功能,將batch_size設置爲1
                #type => "log"
                key => "nginx-log"
        }
}

filter {
          grok {
      match => { "message" => "%{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] \"%{WORD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}\" %{INT:status} %{INT:body_bytes_sent} %{QS:http_referer} %{QS:http_user_agent}"
                                }
                     }
 }

output {  
        elasticsearch {  
                hosts => "172.20.67.50:9200"
                index => "nginx-access-%{+YYYY.MM.dd}"
                }   
} 

4、查看ES集羣是否有數據
可t通過head進行查看是否有數據。

5、在kibana中創建索引,即可在主頁查看相關的數據了。

錯誤集錦:
錯誤一:

“2018-09-20T17:58:05.029+0800   ERROR   redis/client.go:231 Failed to RPUSH to redis list with: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands 
that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for det
ails about the RDB error.”
解決方案:去redis上查看日誌,出現了“
18180:M 20 Sep 18:10:35.061 * 10 changes in 300 seconds. Saving...
18180:M 20 Sep 18:10:35.061 # Can't save in background: fork: Cannot al”
徹底解決的辦法是在redis服務器上依次執行:
1、vim /etc/sysctl.conf
2、添加 vm.overcommit_memory=1
3、sysctl -p

錯誤二、

[WARN ][logstash.inputs.redis    ] Redis connection problem {:exception=>#<Redis::CommandError: ERR unknown command 'script'>}
Redis connection problem {:exception=>#<Redis::CommandError: ERR Error runni”

解決方法:
Filebeat +Redis+ELK處理Nginx日誌系統

解決的辦法:不使用這個功能,將batch_size設置爲1batch_count => 1

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