Trouble parsing json {:source=>"message", :raw=>"{\"@timestamp\":\"2016-05-30T14:51:27+08:00\",\"host\":\"10.139.48.166\",\"clientip\":\"180.109.110.203\",\"request_method\":\"GET\",\"size\":4286,\"responsetime\":0.000,\"upstreamtime\":\"-\",\"upstreamhost\":\"-\",\"http_host\":\"www.xxxx.com\",\"url\":\"/favicon.ico\",\"complete_url\":\"http://www.xxxx.com/favicon.ico\",\"referer\":\"-\",\"agent\":\"\\xE7\\x99\\xBE\\xE5\\xBA\\xA6HD 4.4.1 rv:4.4.1.2 (iPad; iPhone OS 8.3; zh_CN)\",\"status\":\"200\"}", :exception=>#<LogStash::Json::ParserError: Unrecognized character escape 'x' (code 120)報錯信息說明:Unrecognized character escape 'x'html
意思:沒法識別的字符轉義 'x'nginx
搜索這條信息:\"agent\":\"\\xE7\\x99\\xBE\\xE5\\xBA\\xjson
發現是URL連接包含中文後,json的時候對於字符串\\xE7,把x當作須要轉義的字符,問題是,\\不是雙重轉義麼,奇怪!centos
問題解決過程centos 6.7瀏覽器
logstash 1.5elasticsearch
log_format json '{"@timestamp":"$time_iso8601",'ide
'"host":"$server_addr",'url
'"clientip":"$remote_addr",'spa
'"request_method":"$request_method",'日誌
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"complete_url":"$scheme://$host$request_uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
input {
syslog {
port => "12210"
}
}
filter {
json {
source => "message"
}
geoip {
source => "clientip"
}
}
output{
elasticsearch { host => "127.0.0.1"
index => "nginx-logs-%{+YYYY.MM.dd}"
index_type => "logs"
}
}
解決沒法識別的字符轉義 'x'方法抽取logstash配置文件中filter片斷
filter {
mutate {
gsub => ["message", "\\x", "\\\x"]
}
json {
source => "message"
}
geoip {
source => "clientip"
}
}
gsub => ["message", "\\x", "\\\x"]
將message字段中,"\\x"字符串替換爲"\\\x"
logstash再也不輸出錯誤信息,complete_url展現的url連接中中文正常,url沒有解析出來
Trouble parsing json {:source=>"message", :raw=>"{\"@timestamp\":\"2016-05-30T18:21:35+08:00\",\"host\":\"10.139.48.166\",\"clientip\":\"58.250.164.208\",\"request_method\":\"GET\",\"size\":1338,\"responsetime\":0.008,\"upstreamtime\":\"0.008\",\"upstreamhost\":\"10.139.39.45:8801\",\"http_host\":\"www.qhfax.com\",\"url\":\"/aaa/\\xE6\\x88\\x91\\xE6\\x98\\xAF\\xE4\\xB8\\x80\\xE4\\xB8\\xAA\\xE4\\xBA\\xBA\",\"complete_url\":\"https://www.qhfax.com/aaa/%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E4%BA%BA\",\"referer\":\"-\",\"agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36\",\"status\":\"404\"}", :exception=>#<LogStash::Json::ParserError: Unrecognized character escape 'x' (code 120)
\"complete_url\":\"https://www.qhfax.com/aaa/%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E4%BA%BA\"
\"url\":\"/aaa/\\xE6\\x88\\x91\\xE6\\x98\\xAF\\xE4\\xB8\\x80\\xE4\\xB8\\xAA\\xE4\\xBA\\xBA\"
竟然兩條語句輸出的結果都是不一致
'"url":"$uri",'
'"complete_url":"$scheme://$host$request_uri",'
解釋:
$uri請求中的當前URI(不帶請求參數,參數位於$args),不一樣於瀏覽器傳遞的$request_uri的值,它能夠經過內部重定向,或者使用index指令進行修改。不包括協議和主機名,例如/foo/bar.html
$request_uri 這個變量等於包含一些客戶端請求參數的原始URI,它沒法修改,請查看$uri更改或重寫URI。
也就是說:$request_uri是原始請求URL,$uri則是通過nginx處理請求後剔除參數的URL,因此會將漢字表現爲union。
坑點:使用$uri 能夠在nginx對URL進行更改或重寫,可是用於日誌輸出能夠使用$request_uri代替,如無特殊業務需求,徹底能夠替換