[英]Logstash can't (read and) store log data from a file after python write new data into it
我使用python脚本自动将日志写入特殊文件,该文件也是我的日志存储源日志。 可以正确地编写上下文(通过python scrpit),但是在此之后,log-stash无法再读取日志数据,甚至无法重新启动logstash。
主要步骤是:
有人遇到过这个问题吗? 这是我的python代码:
def store(filepath,data):
with open(filepath, 'a') as json_file:
json_file.write(json.dumps(data))
json_file.write("\r")
# json_file.close
def load(filepath):
with open(filepath) as json_file:
data = json.load(json_file)
return data
if __name__ == "__main__":
data = {}
sourceFilePath = "elk_data_source.log"
destFilePath = "elk_data_dest2.log"
for i in range(1,20):
data= load(sourceFilePath)
data["eventType"] = "*ABC"
store(destFilePath, data)
read = open(destFilePath)
line=read.readline()
while line:
print line
'''
context = json.loads(line)
context = context.join([ string.strip().rsplit("}" , 1)[0] , "}"] )
print context
'''
line=read.readline()
# read.close
read.close()
这是我的logstash conf文件,如果在此日志中手动输入数据,该文件可以正常工作:
input {
file {
type => "accounts"
path => "/ELK_Data/elk_data_dest2.log"
start_position => "beginning"
}
}
filter {
json{
source => "message"
}
}
output {
stdout { codec=> rubydebug }
elasticsearch {
hosts => "localhost:9200"
index => "logstash-2016.12.20"
}
}
这是我的elk_data_source.log
{"eventType": ["*icbc"], "prodName": ["LDAP"], "prodFmid": ["HRSL420"], "systemSmfid": ["EIMG"]}
版本:logstash-5.0.0 elasticsearch-2.4.1 Python 2.7.6
我不完全理解您的问题,但是,即使在第一个函数json_file.close()
,最后一行也不应该是read.close()
而不是read.close
,如果必须与with
一起使用,这实际上是多余的
根据我的收集,源文件是elk_data_source.log ,您正在尝试将"eventType" = ["*icbc"]
覆盖为*ABC
。 但是,您错过的是.. "eventType"
值是一个数组,而您正在用一个单个值 - *ABC
编写它。
将data["eventType"] = "*ABC"
更改为data["eventType"] = ["*ABC"]
。
这应该解决。 如果可能,请尝试使用文件比较软件比较两个文件。 另外,在从文件读取和格式化时,请检查可能引起问题的匹配括号或其他空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.