mirror of
git://git.acid.vegas/IRCP.git
synced 2024-11-26 09:56:42 +00:00
Simplified parser for easy usage with grep
This commit is contained in:
parent
45100452da
commit
c6c746117c
47
parser.py
47
parser.py
@ -5,64 +5,35 @@ import json
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def parse(line, raw): # TODO: finish adding custom outputs for certain fields
|
def parse(data, raw=True):
|
||||||
if not raw:
|
if not raw:
|
||||||
args = line.split()
|
data = ' '.join(line.split()[3:])
|
||||||
numeric = args[1]
|
|
||||||
data = ' '.join(args[3:])
|
|
||||||
if data[:1] == ':':
|
if data[:1] == ':':
|
||||||
data = data[1:]
|
data = data[1:]
|
||||||
if numeric == '001' and len(args) >= 7 and data.lower().startswith('welcome to the '):
|
print(data)
|
||||||
return args[6]
|
return data
|
||||||
elif numeric == '002' and len(line.split('running version ')) == 2:
|
|
||||||
return line.split('running version ')[1]
|
|
||||||
elif numeric == '003':
|
|
||||||
check = [item for item in ('This server was cobbled together ','This server was created ','This server has been started ','This server was last re(started) on ','This server was last (re)started on ') if data.startswith(item)]
|
|
||||||
if check:
|
|
||||||
return data.replace(check[0],'')
|
|
||||||
elif numeric == '004' and len(args) >= 5:
|
|
||||||
return args[4]
|
|
||||||
elif numeric == '005':
|
|
||||||
return data.split(' :')[0]
|
|
||||||
elif numeric == '006':
|
|
||||||
while data[:1] in ('-','|',' ','`'):
|
|
||||||
data = data[1:]
|
|
||||||
return data.split()[0]
|
|
||||||
return line if raw else data
|
|
||||||
|
|
||||||
# Main
|
# Main
|
||||||
if len(sys.argv) >= 2:
|
if len(sys.argv) >= 2:
|
||||||
check = sys.argv[1]
|
option = sys.argv[1]
|
||||||
raw = True
|
raw = True
|
||||||
stats = False
|
|
||||||
if len(sys.argv) == 3:
|
if len(sys.argv) == 3:
|
||||||
if sys.argv[2] == 'clean':
|
if sys.argv[2] == 'clean':
|
||||||
raw = False
|
raw = False
|
||||||
elif sys.argv == 'stats':
|
|
||||||
stats = True
|
|
||||||
logs = os.listdir('logs')
|
logs = os.listdir('logs')
|
||||||
found = list()
|
found = list()
|
||||||
for log in logs:
|
for log in logs:
|
||||||
with open('logs/'+log) as logfile:
|
with open('logs/'+log) as logfile:
|
||||||
data = json.loads(logfile.read())
|
data = json.loads(logfile.read())
|
||||||
if check in data:
|
if option in data:
|
||||||
data = data[check]
|
data = data[option]
|
||||||
if type(data) == str:
|
if type(data) == str:
|
||||||
print(parse(data, raw))
|
|
||||||
found.append(parse(data, raw))
|
found.append(parse(data, raw))
|
||||||
elif type(data) == list:
|
elif type(data) == list:
|
||||||
for item in data:
|
for item in data:
|
||||||
print(parse(item, raw))
|
|
||||||
found.append(parse(item, raw))
|
found.append(parse(item, raw))
|
||||||
if stats:
|
if found:
|
||||||
database = dict()
|
print(f'\nfound {len(found)} results in {len(logs)} logs')
|
||||||
for item in found:
|
|
||||||
if item not in database:
|
|
||||||
database[item] = 1
|
|
||||||
else:
|
|
||||||
database[item] +=1
|
|
||||||
print(database)
|
|
||||||
print(f'\nFound {len(found)} results in {len(logs)} logs')
|
|
||||||
else:
|
else:
|
||||||
print('usage: python parser.py <field> [clean]\n')
|
print('usage: python parser.py <field> [clean]\n')
|
||||||
print(' <field> may be any item in the snapshots (001, NOTICE, 464, etc)')
|
print(' <field> may be any item in the snapshots (001, NOTICE, 464, etc)')
|
||||||
|
Loading…
Reference in New Issue
Block a user