python 将新数据写入文件后,Logstash 无法(读取和)存储文件中的日志数据
Logstash can't (read and) store log data from a file after python write new data into it
我使用了一个python脚本来自动将日志写入一个特殊的文件,
哪个文件也是我的日志存储的源日志。
上下文可以正确写入(通过python scrpit),
但是之后log-stash就不能再读取日志数据了,重启logstash也不能用
主要步骤是:
- 启动elasticsearch(默认配置)和logstash(配置如下)
logstash可以自动将所有数据(在源日志文件中)存储到elasticsearch,并将信息输出到控制台。
- 启动 python 脚本(如下所示)将 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.0
elasticsearch-2.4.1
Python 2.7.6
我没有完全理解你的问题,但是,
最后一行不应该是 read.close()
而不是 read.close
即使在第一个函数 json_file.close()
中,如果必须与 with
一起使用,实际上也是多余的
据我所知,源文件是 elk_data_source.log,您正试图将 "eventType" = ["*icbc"]
覆盖为 *ABC
。但是你错过的是.. "eventType"
的值是一个 array 而你正在用 单个值 编写它 - *ABC
。
将 data["eventType"] = "*ABC"
更改为 data["eventType"] = ["*ABC"]
。
这应该可以解决。如果可能,请尝试使用文件比较软件比较这两个文件。此外,请检查匹配的大括号或其他 space 可能会在从文件读取和格式化时导致问题。
我使用了一个python脚本来自动将日志写入一个特殊的文件, 哪个文件也是我的日志存储的源日志。 上下文可以正确写入(通过python scrpit), 但是之后log-stash就不能再读取日志数据了,重启logstash也不能用
主要步骤是:
- 启动elasticsearch(默认配置)和logstash(配置如下) logstash可以自动将所有数据(在源日志文件中)存储到elasticsearch,并将信息输出到控制台。
- 启动 python 脚本(如下所示)将 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.0 elasticsearch-2.4.1 Python 2.7.6
我没有完全理解你的问题,但是,
最后一行不应该是 read.close()
而不是 read.close
即使在第一个函数 json_file.close()
中,如果必须与 with
据我所知,源文件是 elk_data_source.log,您正试图将 "eventType" = ["*icbc"]
覆盖为 *ABC
。但是你错过的是.. "eventType"
的值是一个 array 而你正在用 单个值 编写它 - *ABC
。
将 data["eventType"] = "*ABC"
更改为 data["eventType"] = ["*ABC"]
。
这应该可以解决。如果可能,请尝试使用文件比较软件比较这两个文件。此外,请检查匹配的大括号或其他 space 可能会在从文件读取和格式化时导致问题。