繁体   English   中英

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。

主要步骤是:

  1. 启动elasticsearch(使用默认conf)和logstash(使用conf显示如下所示),logstash可以自动将所有数据(在源日志文件中)存储到elasticsearch,并将信息输出到控制台。
  2. 启动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

我不完全理解您的问题,但是,即使在第一个函数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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM