Logstash傳輸給ES的數據會自動映射為5索引,5備份,字段都為text的的索引。這樣基本上無法進行數據分析。
所以必須將Logstash的數據按照既定的格式存儲在ES中,這時候就要使用到ES模板技術了。在ES中可以定義自定義模板和動態模板,之后es會自動將相關索引映射為模板規定的格式
編譯動態映射模板文件bigdata.template:
在Json日志文件中的KEY的位置不固定、或字段數不明確時使用動態映射模板
{ "template": "bigdata-template", "settings": { "index.number_of_shards": 5, "number_of_replicas": 1 }, "mappings": { "_default_": { "_all": { "enabled": true, "omit_norms": true }, "dynamic_templates": [{ "message_field": { "match": "message", "match_mapping_type": "string", "mapping": { "type": "string", "index": "analyzed", "omit_norms": true, "fielddata": { "format": "disabled" } } } }, { "string_fields": { "match": "*", "match_mapping_type": "string", "mapping": { "type": "string", "index": "not_analyzed", "doc_values": true } } }], "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "string", "index": "not_analyzed" } } } } }
dynamic_templates 就是配置具體的動態模板匹配項
"match_mapping_type": "string" 是匹配固定的類型
"match": "time" 匹配字段名為time的數據
"unmatch": "data" 不匹配字段名為data的數據
mapping 就是將匹配的數據項映射為定義的數據類型
Logstash配置文件 nginx.conf:
input { file { path => "/usr/local/openresty/nginx/logs/user2.log" type => "nginx-bigdata" codec => "json" } } filter { json { source => "u_data" } } output { if [type] == "nginx-bigdata" { elasticsearch { hosts => ["172.17.213.60:9200", "172.17.213.61:9200"] index => "nginx-bigdata" manage_template => false template_overwrite => true template_name => "bigdata-template" template => "/usr/local/logstash-6.2.4/bigdata.template" document_type => "nginx-bigdata" } } }
Nginx的配置文件中關於JSON日志格式轉換的配置:(此處我只保留了需要的一個字段范圍)
escape=json :nginx 1.11.8版本后才提供此參數
log_format userlog escape=json '{"u_data":"$u_data","@timestamp":"$time_iso8601"}';
...
access_log logs/user.log userlog;
產生的日志格式:
{"u_data":"{\"appid\":\"nchaopai\",\"args\":{\"contentId\":0,\"duration\":111811,\"parentId\":0,\"totaltime\":0,\"type\":0},\"bk\":\"-\",\"cp_ver\":\"3.0.5\",\"duid\":\"2cba98f8ddc18464\",\"e\":\"nchaopai.main.stay-duration\",\"os\":\"A\",\"ts\":1572584611,\"ver\":\"8.11.11\"}"}
之后在Kibana里看到就是這樣的:
常用格式如下:
log_format log_json escape=json '{"timestamp": "$time_local",' '"remote_addr": "$remote_addr",' '"referer": "$http_referer",' '"request": "$request",' '"statu": "$status",' '"byte": "$body_bytes_sent",' '"agen": "$http_user_agent",' '"x_forwarded": "$http_x_forwarded_for",' '"up_resp_time": "$upstream_response_time",' '"request_time": "$request_time"}';
參考資料:https://doc.yonyoucloud.com/doc/logstash-best-practice-cn/filter/json.html