[英]Filebeat - Missing opening “{” on first entry of log file only?
我有一個 JSON 日志文件,如下所示:
{"timestamp": "2021-03-20 13:56:07,568", "action": "ws.RegisterUser", "uuid": "e46bfe57-083d-43f9-86fa-4c94626032fe", "application_id": "App_Caller", "ip": "1.2.3.4", "log_level": "INFO", "event": "request", "system": "App_Host", "user_id": "1234567"}
{"timestamp": "2021-03-20 13:56:07,679", "action": "ws.RegisterUser", "uuid": "e46bfe57-083d-43f9-86fa-4c94626032fe", "application_id": "App_Caller", "ip": "1.2.3.4", "log_level": "INFO", "event": "request", "system": "Integration_1", "integration_action": "Create User", "user_id": "1234567"}
{"timestamp": "2021-03-20 13:56:07,789", "action": "ws.RegisterUser", "uuid": "e46bfe57-083d-43f9-86fa-4c94626032fe", "application_id": "App_Caller", "ip": "1.2.3.4", "log_level": "INFO", "event": "response", "system": "Integration_1", "integration_action": "Create User", "result": "01", "user_id": "1234567"}
通過 Ubuntu 20.10 上的 Filebeat (v7.12.0) 運行此程序,我不斷收到此錯誤:
"Error decoding JSON: invalid character 'i' in literal true (expecting 'r')"
始終在第一個 JSON 項目上,其他兩個成功解碼。
我已經更改了項目的順序,但錯誤總是發生在第一個發生的地方。
然后我評論了 JSON 配置,並注意到 Filebeat output 在第一行缺少開頭的“{”:
...
"message": "timestamp\": \"2021-03-20 13:56:07,568\", \"action\": \"ws.RegisterUser\", \"uuid\": \"e46bfe57-083d-43f9-86fa-4c94626032fe\", \"application_id\": \"App_Caller\", \"ip\": \"1.2.3.4\", \"log_level\": \"INFO\", \"event\": \"request\", \"system\": \"App_Host\", \"user_id\": \"1234567\"}",
...
"message": "{\"timestamp\": \"2021-03-20 13:56:07,679\", \"action\": \"ws.RegisterUser\", \"uuid\": \"e46bfe57-083d-43f9-86fa-4c94626032fe\", \"application_id\": \"App_Caller\", \"ip\": \"1.2.3.4\", \"log_level\": \"INFO\", \"event\": \"request\", \"system\": \"Integration_1\", \"integration_action\": \"Create User\", \"user_id\": \"1234567\"}",
...
"message": "{\"timestamp\": \"2021-03-20 13:56:07,789\", \"action\": \"ws.RegisterUser\", \"uuid\": \"e46bfe57-083d-43f9-86fa-4c94626032fe\", \"application_id\": \"App_Caller\", \"ip\": \"1.2.3.4\", \"log_level\": \"INFO\", \"event\": \"response\", \"system\": \"Integration_1\", \"integration_action\": \"Create User\", \"result\": \"01\", \"user_id\": \"1234567\"}",
...
這是我的配置文件:
filebeat:
config:
modules:
path: /etc/filebeat/modules.d/*.yml
reload:
enabled: false
inputs:
- enabled: false
paths:
- /var/log/*.log
type: log
- enabled: false
paths:
- /var/log/*.log
type: filestream
- enabled: true
paths:
- /home/kubs/Documents/fb/test_*.log
# json.message_key: event
# json.add_error_key: true
type: log
output:
# elasticsearch:
# hosts:
# - localhost:9200
console:
pretty: true
path:
config: /etc/filebeat
data: /var/lib/filebeat
home: /usr/share/filebeat
logs: /var/log/filebeat
processors:
- add_host_metadata:
when:
not:
contains:
tags: forwarded
- add_cloud_metadata: null
- add_docker_metadata: null
- add_kubernetes_metadata: null
setup:
kibana: null
template:
settings:
index:
number_of_shards: 1
我做了一些搜索,發現 JSON 解碼錯誤消息的類似情況(但從來沒有像我這樣簡單的場景),但在第一行沒有提到任何丟失的“{”。
我懷疑這些問題可能是相關的,但我不確定。
我是否遺漏了一些可以解釋這種行為的明顯內容,以及 JSON 解碼錯誤的原因?
通常情況下,再花幾分鍾重新閱讀我的問題可能會幫助我想出其他嘗試。
我對 Filebeat 注冊表文件有一些問題(Logstash 要簡單得多),所以我想出了一種復雜的方法來重復處理同一個文件,包括用echo 1 > $file
“清除”它,刪除它並使用新名稱將其恢復到以前的內容,同時停止/啟動 Filebeat。
所以,我剛剛刪除了所有日志文件,創建了一個具有相同內容的新文件,更改了我的配置
paths:
- /home/kubs/Documents/fb/test_*.log
為此(指定實際文件)
paths:
- /home/kubs/Documents/fb/test_20210331153923.log
現在一切正常,包括 JSON 解碼。
我還不知道是什么導致了丟失的“{”,所以如果有人有任何想法,我想聽聽他們的意見。 否則,我會在幾天內接受這個作為答案。
編輯:這肯定與我運行測試的方式有關,Filebeat 保持 state 的方式以及在 Linux 上重用 inode 的方式。
這是注冊表文件中的最后三個條目:
{"op":"set","id":150}
{"k":"filebeat::logs::native::406026-2051","v":{"id":"native::406026-2051","prev_id":"","source":"/home/kubs/Documents/fb/test_20210331162059.log","offset":2,"timestamp":[258565806201,1617204774],"ttl":-1,"type":"log","FileStateOS":{"inode":406026,"device":2051},"identifier_name":"native"}}
{"op":"set","id":151}
{"k":"filebeat::logs::native::406026-2051","v":{"type":"log","FileStateOS":{"inode":406026,"device":2051},"identifier_name":"native","prev_id":"","source":"/home/kubs/Documents/fb/test_20210331163246.log","timestamp":[258567741346,1617204774],"ttl":-1,"id":"native::406026-2051","offset":2}}
{"op":"set","id":152}
{"k":"filebeat::logs::native::406026-2051","v":{"timestamp":[258575697063,1617204775],"FileStateOS":{"inode":406026,"device":2051},"identifier_name":"native","prev_id":"","source":"/home/kubs/Documents/fb/test_20210331163246.log","ttl":-1,"type":"log","id":"native::406026-2051","offset":858}}
第一個條目(id:150)有源“/home/kubs/Documents/fb/test_20210331162059.log”和偏移量2(這是在我“清除”文件之后)。 "FileStateOS" 是 {"inode":406026,"device":2051}。
在我刪除文件並創建新文件后,我們得到下一個條目(id:151),它的源為“/home/kubs/Documents/fb/test_20210331163246.log”(新的日志文件,內容為 JSON) , 但仍然有相同的 "FileStateOS":{"inode":406026,"device":2051},即 inode 被重用,並且具有相同的偏移量。
這就是為什么第一行總是缺少“{”,因為偏移量總是 2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.