業(yè)務(wù)場景
最近在做CA卡相關(guān)業(yè)務(wù)處理,用戶量700w,對于相關(guān)操作日志,按照傳統(tǒng)的方式顯然不合理的,出現(xiàn)問題排查也是個(gè)事,最后我考慮通過Logstash直接全寫到es內(nèi),下面我就簡單寫一下我怎么弄的,當(dāng)然還有很多別的可以實(shí)現(xiàn):例如 Filebeat等。
安裝部署
- 安裝Logstash,之前通過docker安裝過,這里我使用安裝包的方式。
- 訪問鏈接,下載文件
https://www.elastic.co/cn/downloads/past-releases
-
選擇對應(yīng)的產(chǎn)品及版本
image.png - 解壓
image.png
安裝完成 ~~~
定制化配置
1.創(chuàng)建文件
/work/elasticsearch/logstash-7.1.0/config/logstash-omc.conf
2.日志打印例子
2020-01-14 17:13:18,419 DEBUG http-bio-8082-exec-1 - {"createAt":"2020-01-14 17:13:03","status":"0","developer":null,"servid":"760000","action":"UPDATE","devno":"keyno01","devtype":1,"modifyAt":"2020-01-14 17:13:03","accountid":"131022","servstatus":null,"uapuserid":"152022","serialno":"76a58c28cff741aba621a6098cebaf6c"}
3.logstash-omc.conf 編寫配置文件例子
# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.
input {
file{
#配置相關(guān)業(yè)務(wù)日志全部存入此文件
path=>["/work/docker/bandDev.log"]
type=>"apache-log"
start_position => "beginning"
ignore_older => 0
}
}
filter {
grok {
match => {
#正則表達(dá)式 根據(jù)日志打印格式編寫
"message" => ["([\s\S]*) (?<Msg>({.*?}))"]
}
#覆蓋之前的message
overwrite => ["message"]
#去除無用的字段
remove_field => ["host","beat","offset"]
}
json{
source => "Msg"
target => "xa"
}
}
output {
elasticsearch {
#es的連接地址
hosts => ["localhost:9200"]
#es中的索引名稱
index => "xa-logstash"
#user => "elastic"
#password => "changeme"
}
啟動Logstash
#進(jìn)入bin目錄下
e24-6:bin dailong$ pwd
/work/elasticsearch/logstash-7.1.0/bin
e24-6:bin dailong$ ./logstash -f /work/elasticsearch/logstash-7.1.0/config/logstash-omc.conf
Sending Logstash logs to /work/elasticsearch/logstash-7.1.0/logs which is now configured via log4j2.properties
[2020-01-16T16:30:18,257][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
[2020-01-16T16:30:18,285][INFO ][logstash.runner ] Starting Logstash {"logstash.version"=>"7.1.0"}
[2020-01-16T16:30:27,887][INFO ][logstash.outputs.elasticsearch] Elasticsearch pool URLs updated {:changes=>{:removed=>[], :added=>[http://localhost:9200/]}}
[2020-01-16T16:30:28,175][WARN ][logstash.outputs.elasticsearch] Restored connection to ES instance {:url=>"http://localhost:9200/"}
[2020-01-16T16:30:28,250][INFO ][logstash.outputs.elasticsearch] ES Output version determined {:es_version=>7}
[2020-01-16T16:30:28,254][WARN ][logstash.outputs.elasticsearch] Detected a 6.x and above cluster: the `type` event field won't be used to determine the document _type {:es_version=>7}
[2020-01-16T16:30:28,292][INFO ][logstash.outputs.elasticsearch] New Elasticsearch output {:class=>"LogStash::Outputs::ElasticSearch", :hosts=>["http://localhost:9200"]}
[2020-01-16T16:30:28,299][INFO ][logstash.outputs.elasticsearch] Using default mapping template
[2020-01-16T16:30:28,464][INFO ][logstash.outputs.elasticsearch] Attempting to install template {:manage_template=>{"index_patterns"=>"logstash-*", "version"=>60001, "settings"=>{"index.refresh_interval"=>"5s", "number_of_shards"=>1}, "mappings"=>{"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"}}}}}}}
[2020-01-16T16:30:28,634][INFO ][logstash.javapipeline ] Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50, "pipeline.max_inflight"=>500, :thread=>"#<Thread:0x1234b969 run>"}
[2020-01-16T16:30:29,188][INFO ][logstash.inputs.file ] No sincedb_path set, generating one based on the "path" setting {:sincedb_path=>"/work/elasticsearch/logstash-7.1.0/data/plugins/inputs/file/.sincedb_20b96030a2ca04be6e9a04f4363503a2", :path=>["/work/docker/bandDev.log"]}
[2020-01-16T16:30:29,246][INFO ][logstash.javapipeline ] Pipeline started {"pipeline.id"=>"main"}
[2020-01-16T16:30:29,343][INFO ][filewatch.observingtail ] START, creating Discoverer, Watch with file and sincedb collections
[2020-01-16T16:30:29,352][INFO ][logstash.agent ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[2020-01-16T16:30:29,992][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
logstash就是類似于咱的Listener,一個(gè)監(jiān)聽器,監(jiān)聽這個(gè)文件的變化,當(dāng)變化了會根據(jù)咱之前寫的那個(gè)配置文件的格式往es寫數(shù)據(jù)。
結(jié)果

image.png
開發(fā)工具

image.png
參考鏈接
- https://www.elastic.co/cn/downloads/past-releases 安裝包
- https://blog.csdn.net/sinat_35930259/article/details/81044846 運(yùn)行機(jī)制及相關(guān)配置
- https://www.cnblogs.com/Su-per-man/p/10770019.html 掃描配置文件相關(guān)

