版本與Elasticsearch一致
**1、下載并安裝適用于 Linux 的存檔編輯
**Elasticsearch v8.4.1 的 Linux 存檔可以按如下方式下載和安裝:
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.6-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.6-linux-x86_64.tar.gz.sha512
shasum -a 512 -c logstash-7.17.6-linux-x86_64.tar.gz.sha512
tar -xzf logstash-7.17.6-linux-x86_64.tar.gz
cd logstash-7.17.6/
2绪钥、logstash.yml配置文件
- 基礎(chǔ)配置:
#該值為true時餐曼,開啟轉(zhuǎn)義(避免寫法格式錯誤導(dǎo)致浪費太多時間)
config.support_escapes: true
<meta name="wolai" content="bAHmkmE9rVVWdBhyjXgUDd1666174563784">
配合ES開啟認(rèn)證:
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: logstash_system > xpack.monitoring.elasticsearch.password: *
xpack.monitoring.elasticsearch.hosts: ["http://ip:9200"]
如開啟認(rèn)證纬黎,pipeline output如使用es毛好,需配置user/password(elastic/****)
-
配置說明如下:
------------ Node identity ------------
#節(jié)點名稱孽水,默認(rèn)主機名
node.name: test------------ Data path ------------------
#數(shù)據(jù)存儲路徑陕贮,默認(rèn)LOGSTASH_HOME/data
path.data:------------ Pipeline Settings --------------
#pipeline ID典予,默認(rèn)main
pipeline.id: main#輸出通道的工作workers數(shù)據(jù)量甜滨,默認(rèn)cpu核心數(shù)
pipeline.workers:How many events to retrieve from inputs before sending to filters+workers
#單個工作線程在嘗試執(zhí)行其過濾器和輸出之前將從輸入收集的最大事件數(shù)量,默認(rèn)125
pipeline.batch.size: 125#將較小的批處理分派給管道之前瘤袖,等待的毫秒數(shù)衣摩,默認(rèn)50ms
pipeline.batch.delay: 50#此值為true時,即使內(nèi)存中仍然有運行中事件捂敌,也會強制Logstash在關(guān)機期間退出
#默認(rèn)flase
pipeline.unsafe_shutdown: false#管道事件排序
#選項有艾扮;auto,true,false,默認(rèn)auto
pipeline.ordered: auto------------ Pipeline Configuration Settings --------------
#配置文件路徑
path.config:#主管道的管道配置字符串
config.string:#該值為true時,檢查配置是否有效占婉,然后退出泡嘴,默認(rèn)false
config.test_and_exit: false#該值為true時,會定期檢查配置是否已更改逆济,并在更改后重新加載配置酌予,默認(rèn)false
config.reload.automatic: false#檢查配置文件更改的時間間隔,默認(rèn)3s
config.reload.interval: 3s#該值為true時奖慌,將完整編譯的配置顯示為調(diào)試日志消息霎终,默認(rèn)false
config.debug: false#該值為true時,開啟轉(zhuǎn)義
config.support_escapes: false------------ HTTP API Settings -------------
#是否開啟http訪問升薯,默認(rèn)true
http.enabled: true#綁定主機地址,可以是ip,主機名击困,默認(rèn)127.0.0.1
http.host: 127.0.0.1#服務(wù)監(jiān)聽端口涎劈,可以是單個端口广凸,也可以是范圍端口,默認(rèn)9600-9700
http.port: 9600-9700------------ Module Settings ---------------
#模塊定義蛛枚,必須為數(shù)組
#模塊變量名格式必須為var.PLUGIN_TYPE.PLUGIN_NAME.KEY
modules:
- name: MODULE_NAME
var.PLUGINTYPE1.PLUGINNAME1.KEY1: VALUE
var.PLUGINTYPE1.PLUGINNAME1.KEY2: VALUE
var.PLUGINTYPE2.PLUGINNAME1.KEY1: VALUE
var.PLUGINTYPE3.PLUGINNAME3.KEY1: VALUE------------ Queuing Settings --------------
#事件緩沖的內(nèi)部排隊模型谅海,可選項:memory,persisted,默認(rèn)memory
queue.type: memory#啟用持久隊列(queue.type: persisted)后將在其中存儲數(shù)據(jù)文件的目錄路徑
#默認(rèn)path.data/queue
path.queue:#啟用持久隊列(queue.type: persisted)時使用的頁面數(shù)據(jù)文件的大小
#默認(rèn)64mb
queue.page_capacity: 64mb#啟用持久隊列(queue.type: persisted)后蹦浦,隊列中未讀事件的最大數(shù)量
#默認(rèn)0
queue.max_events: 0#啟用持久隊列(queue.type: persisted)后扭吁,隊列的總?cè)萘浚瑔挝蛔止?jié)盲镶,默認(rèn)1024mb
queue.max_bytes: 1024mb#啟用持久隊列(queue.type: persisted)后侥袜,在強制檢查點之前的最大ACKed事件數(shù),默認(rèn)1024
queue.checkpoint.acks: 1024#啟用持久隊列(queue.type: persisted)后溉贿,在強制檢查點之前的最大書面事件數(shù)枫吧,默認(rèn)1024
queue.checkpoint.writes: 1024If using queue.type: persisted, the interval in milliseconds when a checkpoint is forced on the head page
Default is 1000, 0 for no periodic checkpoint.#啟用持久隊列(queue.type: persisted)后,執(zhí)行檢查點的時間間隔宇色,單位ms九杂,默認(rèn)1000ms
queue.checkpoint.interval: 1000------------ Dead-Letter Queue Settings --------------
#是否啟用插件支持的DLQ功能的標(biāo)志,默認(rèn)false
dead_letter_queue.enable: false#dead_letter_queue.enable為true時宣蠕,每個死信隊列的最大大小
#若死信隊列的大小超出該值例隆,則被刪除,默認(rèn)1024mb
dead_letter_queue.max_bytes: 1024mb#死信隊列存儲路徑抢蚀,默認(rèn)path.data/dead_letter_queue
path.dead_letter_queue:------------ Debugging Settings --------------
#日志輸出級別镀层,選項:fatal,error,warn,info,debug,trace,默認(rèn)info
log.level: info#日志格式,選項:json,plain,默認(rèn)plain
log.format:#日志路徑思币,默認(rèn)LOGSTASH_HOME/logs
path.logs:------------ Other Settings --------------
#插件存儲路徑
path.plugins: []#是否啟用每個管道在不同日志文件中的日志分隔
#默認(rèn)false
pipeline.separate_logs: false
3鹿响、pipeline配置
-
多個管道
若是須要在同一進程中運行多個管道,Logstash提供了一種經(jīng)過名為
pipelines.yml
的配置文件完成此操做的方法谷饿,這個文件必須放在path.settings
文件夾惶我,并遵循此結(jié)構(gòu):pipeline.id: my-pipeline_1
path.config: "/etc/path/to/p1.config"
pipeline.workers: 3
pipeline.id: my-other-pipeline
path.config: "/etc/different/path/p2.cfg"
queue.type: persisted該文件在YAML中格式化并包含一個字典列表,其中每一個字典描述一個管道博投,每一個鍵/值對指定該管道的設(shè)置绸贡。這個示例展現(xiàn)了兩個不一樣的管道,它們由ID和配置路徑描述毅哗,對于第一個管道听怕,
pipeline.workers
的值被設(shè)置為3,而在另外一個則啟用持久隊列特性虑绵,在pipelines.yml
文件中未顯式設(shè)置的設(shè)置值將退回到pipelines.yml
設(shè)置文件中指定的默認(rèn)值尿瞭。性能在沒有參數(shù)的狀況下啟動Logstash時,它將讀取
pipelines.yml
文件并實例化文件中指定的全部管道翅睛,另外一方面声搁,當(dāng)你使用-e
或-f
時黑竞,Logstash會忽略pipelines.yml
文件,并記錄對此的警告疏旨。
4很魂、啟動命令
sudo -u elasticsearch ./bin/logstash
# 指定文件運行
sudo -u elasticsearch ./bin/logstash -f aaa.config
# -t 指定文件運行,驗證文件是否存在問題
sudo -u elasticsearch ./bin/logstash -f aaa.config -t
nohup sudo -u elasticsearch ./logstash/bin/logstash > /www/logs/logstash.log &1 &
nohup sudo -u elasticsearch ./bin/logstash > /www/logs/logstash.log &1 &
5、使用logstash過濾出特定格式的日志
日志內(nèi)容:[2018-11-24 08:33:43,253][ERROR][http-nio-8080-exec-4][com.hh.test.logs.LogsApplication][code:200,msg:測試錄入錯誤日志,param:{}]
filter {
if "nova" in [tags]{
grok {
# 篩選過濾
match => {
"message" => "(?<date>\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})\]\[(?<level>[A-Z]{4,5})\]\[(?<thread>[A-Za-z0-9/\-]{4,40})\]\[(?<class>[A-Za-z0-9/.]{4,40})\]\[(?<msg>.*)"
}
mutate {
remove_field => [
"message",
]
}
# 不匹配正則則刪除檐涝,匹配正則用=~
if [level] !~ "(ERROR|WARN|INFO)" {
# 刪除日志
drop {}
}
}
}
正則表達式在線的調(diào)試庫遏匆,以供參考: http://grokdebug.herokuapp.com/
需注意:”-“ 破折號,需要轉(zhuǎn)移不然會出現(xiàn)解析異常
6谁榜、異常問題
-
配置文件格式問題
"LogStash::ConfigurationError", :message=>"Expected one of #, input, filter, output at line 1
- 方式1:配置文件設(shè)置
config.support_escapes: true
-
方式2:問題需區(qū)分部署環(huán)境系統(tǒng)(windows/linux)
-
windows
文件編碼格式問題幅聘,改為UTF-8無BOM模式(可參考相關(guān)鏈接:https://blog.csdn.net/qq_32131499/article/details/91338972 );
如不行惰爬,可考慮參考:http://www.04007.cn/article/835.html
-
linux
如第一步還不生效喊暖,可以直接用vi命令編寫簡單的配置驗證。大有可能是從windows創(chuàng)建提交導(dǎo)致該問題撕瞧。
如果未能定位陵叽,則考慮通過命令行 logstash -f **.config -t 指定文件執(zhí)行,看看是否成功丛版。
-
-
如果開啟pipeline巩掺,也出現(xiàn)提示文件格式化問題
-
先確認(rèn)日志中是否出現(xiàn)“Ignoring the 'pipelines.yml' file because modules or command line options are specified”,如出現(xiàn)上述警告內(nèi)容页畦,代表未成功開啟pipeline配置導(dǎo)致胖替。
比如:
logstash.yml開啟pipeline 單通道配置與 pipeline.yml配置同時開啟
如上述未能解決,參照第一個問題豫缨。
-
-
如果出現(xiàn)某個通道數(shù)據(jù)抓取不到独令?
- 考慮抓取的目錄是否有權(quán)限訪問『冒牛可通過切換logstash運行用戶去訪問該文件燃箭,來確認(rèn)有權(quán)限。
- 如果不能訪問舍败,可通過root用戶運行招狸,確認(rèn)數(shù)據(jù)是否同步成功。如同步成功邻薯,則授權(quán)目錄權(quán)限為755即可達到目的裙戏。
??參考文獻:
http://www.noobyard.com/article/p-ucnfmacz-gk.html
https://www.elastic.co/guide/en/logstash/7.17/introduction.html
https://blog.51cto.com/u_15047490/4228036
樣例:
input {
file {
path => "/www/log/java-web/log_info.log"
type => "java-web-info"
start_position => "beginning"
# 多行輸入
codec => multiline {
pattern => "^%{TIMESTAMP_ISO8601} "
negate => true
what => previous
}
}
file {
path => "/www/log/java-web/log_error.log"
type => "java-web-error"
start_position => "beginning"
# 多行輸入
codec => multiline {
pattern => "^%{TIMESTAMP_ISO8601} "
negate => true
what => previous
}
}
}
filter {
if [type] == "java-web-info" {
grok {
match => {
"message" => "(?<date>\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3})\s(?<level>[A-Z]{4,5})\s{1,2}(?<pid>\d{1,20})\s\-{1,5}\s\[(?<thread>[A-Za-z0-9./-]{4,40})\]\s(?<method>[A-Z
a-z0-9.\\(\\:/\-\\)]{0,200}):\s(?<msg>.*)"
}
}
# 過濾debug
if [level] !~ "(ERROR|WARN|INFO)" {
drop {}
}
# 過濾指定內(nèi)容
if [thread] == "com.alibaba.nacos.naming.push.receiver" {
drop {}
}
}
}
output {
if [type] == "java-web-info" {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "java-web-info-%{+YYYY.MM.dd}"
#開啟認(rèn)證后,需配置
#user => "elastic"
#password => "******"
}
}
if [type] == "java-web-error" {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "java-web-error-%{+YYYY.MM.dd}"
#開啟認(rèn)證后厕诡,需配置
#user => "elastic"
#password => "******"
}
}
}