我使用python脚本将日志自动写入特殊文件,哪个文件也是我的日志库的源日志。上下文可以正确编写(通过Python Scrpit),但是在那个日志静止之后,无法再读取日志数据,甚至无法正常启动LogStash。
主要步骤是:
- 启动Elasticsearch(默认conf)和logstash(conf显示bellow)LogStash可以将所有数据(在源日志文件中)自动存储到Elasticsearch,并将信息输出到控制台。
- 启动Python脚本(显示Bellow)将JSON数据写入源日志文件。数据成功了。但是LogStash无法再读取新数据了。即使我重新启动了LogStash,它仍然无法在源日志中读取数据或将其存储到Elasticsearch。
以前有人遇到了这个问题吗?这是我的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.0Elasticsearch-2.4.1Python 2.7.6
从我收集的内容中,源文件是 elk_data_source.log ,您正在尝试将"eventType" = ["*icbc"]
覆盖至*ABC
。但是您错过的是.. "eventType"
的值是 array ,并且您正在用单个值而不是*ABC
。
将data["eventType"] = "*ABC"
更改为data["eventType"] = ["*ABC"]
。
这应该解决。如果可能的话,请尝试将两个文件与文件比较软件进行比较。另外,请检查匹配的支撑或其他空间,这些空间可能会在文件中读取和格式化时引起问题。
我不完全理解您的问题,但是,不是最后一行是read.close()
而不是read.close
即使在第一个功能json_file.close()
中,如果必须与with