目錄
- 前言
- 多配置文件的實現方式
- 爲logstash 增長模板
- 將 logstash 做爲服務啓動
git
在使用 logstash 編寫多個配置文件,寫入到 elasticsearch 時,會出現數據寫入混亂的問題,舉例來講:json
多個配置文件中規則以下:vim
A -> es-logstash-A B -> es-logstash-B A 寫入到 es-logstash-A 索引中 B 寫入到 es-logstash-B 索引中
然而當 logstash 服務運行起來的時候並非這樣的,可能出現以下想象:bash
A-> es-logstash-A B-> es-logstash-A
究其緣由,是由於 logstash 運行起來的時候,會將全部的配置文件合併執行。所以,每一個 input 的數據都必須有一個惟一的標識,在 filter 和 output 時,經過這個惟一標識來實現過濾或者存儲到不一樣的索引。
app
如上所說,寫入須要惟一標識,在logstash 中惟一標識推薦使用 type 或 tags 字段,而後經過 if 條件判斷來實現。elasticsearch
首先來看下面一個示例:ide
在 /etc/logstash/conf.d 目錄下有這樣兩個配置文件 [1.conf a.conf ] spa
[root@192.168.118.14 /etc/logstash/conf.d]#ls 1.conf a.conf 1.conf input { file { path => "/data/log/1.log" start_position => "beginning" sincedb_path => "/tmp/1_progress" } } output { elasticsearch { hosts => ["192.168.118.14"] index => "1-log-%{+YYYY.MM.dd}" } } a.conf input { file { path => "/data/log/a.log" start_position => "beginning" sincedb_path => "/tmp/a_progress" } } output { elasticsearch { hosts => ["192.168.118.14"] index => "a-log-%{+YYYY.MM.dd}" } } /data/log/a.log [root@192.168.118.14 ~]#cat /data/log/a.log a /data/log/1.log [root@192.168.118.14 ~]#cat /data/log/1.log 1
這兩個配置很簡單,規則:日誌
兩個日誌文件,都只有 1 行日誌記錄。code
正確的結果是 生成兩個索引,每一個索引裏只有一條記錄。
接下來啓動服務查看,多配置文件 命令啓動方式以下:
正確的啓動方式:
[root@192.168.118.14 ~]#logstash -f /etc/logstash/conf.d/
錯誤的啓動方式:
[root@192.168.118.14 ~]#logstash -f /etc/logstash/conf.d/*
啓動成功後,經過 elasticsearch-head 查看 索引及數據
發現每一個 索引裏卻有 2 條記錄,這不符合正常的邏輯,查看數據發現,每一個索引裏都是 1.log 和 a.log 的數據總和。
這也證實了 logstash 在寫入數據的時候,是將全部的配置文件合併在一塊兒的,運行起來數據寫入就會混亂。要解決這種混亂就須要經過惟一標識和if 判斷,logstash配置文件調整以下:
1.conf input { file { path => "/data/log/1.log" start_position => "beginning" sincedb_path => "/tmp/1_progress" type => "1-log" } } output { if [type] == "1-log" { elasticsearch { hosts => ["192.168.118.14"] index => "1-log-%{+YYYY.MM.dd}" } } } a.conf input { file { path => "/data/log/a.log" start_position => "beginning" sincedb_path => "/tmp/a_progress" type => "a-log" } } output { if [type] == "a-log" { elasticsearch { hosts => ["192.168.118.14"] index => "a-log-%{+YYYY.MM.dd}" } } }
上面修改的部分, input 裏 增長了 type 字段,定義了惟一標識而在 output 中 經過if判斷惟一標識來作響應的寫入操做。
啓動服務:
[root@192.168.118.14 ~]#logstash -f /etc/logstash/conf.d/
經過 elasticsearch-head 查看:
此次就徹底符合預期的標準了。
經過 elasticsearch-head 查看到 elasticsearch 默認是經過分片入庫的,並且默認是 5 個主分片,5 個備份分片。 看成爲日誌存儲時,數據可能沒那麼重要,不須要作 elasticsearch 的集羣,可是也不想看到這些告警信息,這時候就須要 模板 了。
這裏直接提供一個模板樣本,能夠直接使用。
{ "template" : "*", "version" : 60001, "settings" : { "index.refresh_interval" : "5s", "number_of_shards": 3, "number_of_replicas": 0 }, "mappings" : { "_default_" : { "dynamic_templates" : [{ "message_field" : { "path_match" : "message", "match_mapping_type" : "string", "mapping" : { "type" : "text", "norms" : false } } }, { "string_fields" : { "match" : "*", "match_mapping_type" : "string", "mapping" : { "type" : "text", "norms" : false, "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }], "properties" : { "@timestamp" : { "type" : "date" }, "@version" : { "type" : "keyword" }, "geoip" : { "dynamic" : true, "properties" : { "ip" : { "type" : "ip" }, "location" : { "type" : "geo_point" }, "latitude" : { "type" : "half_float" }, "longitude" : { "type" : "half_float" } } } } } } }
放置在這裏目錄裏:
[root@192.168.118.14 ~]#ls /etc/logstash/template/template.json
"number_of_shards": 3,
"number_of_replicas": 0
這一部分就是定義 主分片 和 複製分片的,能夠適當的調整。
"properties" : { "@timestamp" : { "type" : "date" }, "@version" : { "type" : "keyword" }, "geoip" : { "dynamic" : true, "properties" : { "ip" : { "type" : "ip" }, "location" : { "type" : "geo_point" }, "latitude" : { "type" : "half_float" }, "longitude" : { "type" : "half_float" } } } }
當要使用地圖定位客戶端位置的時候,這一段就必須加上, location 的type 的必須是 geo_point
爲 logstash 配置文件添加模板配置,以下:
a.conf input { file { path => "/data/log/a.log" start_position => "beginning" sincedb_path => "/tmp/a_progress" type => "a-log" } } output { if [type] == "a-log" { elasticsearch { hosts => ["192.168.118.14"] index => "a-log-%{+YYYY.MM.dd}" template => "/etc/logstash/template/template.json" template_overwrite => "true" } } } 1.conf input { file { path => "/data/log/1.log" start_position => "beginning" sincedb_path => "/tmp/1_progress" type => "1-log" } } output { if [type] == "1-log" { elasticsearch { hosts => ["192.168.118.14"] index => "1-log-%{+YYYY.MM.dd}" template => "/etc/logstash/template/template.json" template_overwrite => "true" } } }
啓動服務
[root@192.168.118.14 ~]#logstash -f /etc/logstash/conf.d/
經過 elasticsearch-head 查看集羣狀態及索引分片:
ok,新增的模板已經生效。嘗試爲不一樣的索引添加不一樣的模板結果出現各類問題,所以建立一個通用的模板。
在上面的啓動中,都是直接經過命令 logstash 來啓動的,其實能夠經過修改 logstash.service 啓動腳原本啓動服務。
修改以下:
[root@192.168.118.14 ~]#vim /etc/systemd/system/logstash.service … ExecStart=/usr/share/logstash/bin/logstash "--path.settings" "/etc/logstash" "-f" "/etc/logstash/conf.d" …
啓動服務:
[root@192.168.118.14 ~]#systemctl daemon-reload [root@192.168.118.14 ~]#systemctl start logstash