这段代码正在将access.log日志转换为JSON格式。我正在尝试将其上传到elasticsearch,这样索引也会创建。我该如何做?
import json
import re
import logging
import ecs_logging
import time
import sys, requests, os
import time
from datetime import datetime
from elasticsearch import Elasticsearch
from pprint import pprint
res = requests.get('http://localhost:9200')
print (res.content)
es = Elasticsearch([{'host': 'localhost', 'port': '9200'}])
i = 0
result = {}
with open('access.log') as f:
lines = f.readlines()
regex = '(?P<ipaddress>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - - \[(?P<dateandtime>.*)\] \"(?P<httpstatus>(GET|POST) .+ HTTP\/1\.1)\" (?P<returnstatus>\d{3} \d+) (\".*\")(?P<browserinfo>.*)\"'
for line in lines:
r = re.match(regex,line)
if r != None:
result[i] = {'IP address': r.group('ipaddress'), 'Time Stamp': r.group('dateandtime'),
'HTTP status': r.group('httpstatus'), 'Return status':
r.group('returnstatus'), 'Browser Info': r.group('browserinfo')}
i += 1
print(result)
with open('data.json', 'w') as fp:
json.dump(result, fp) ```
1条答案
按热度按时间zlhcx6iw1#
修改了您的代码,希望这对您有所帮助,如果您启用了身份验证,那么我们应该提到相同的内容,并且您还要求创建一个名为“my-index”的索引