znc2tmsr_etc.kv 1 #!/usr/bin/env python
znc2tmsr_etc.kv 2
znc2tmsr_etc.kv 3 from os import listdir
znc2tmsr_etc.kv 4 from os.path import isfile, join
znc2tmsr_etc.kv 5 from datetime import datetime
znc2tmsr_etc.kv 6
znc2tmsr_etc.kv 7 # Set the dir you'd like to eat, the suffix of each file, and the filename to shit into
znc2tmsr_etc.kv 8 dir_to_eat = '/path/to/eat/dir/'
znc2tmsr_etc.kv 9 dir_files_end_with = '.log'
znc2tmsr_etc.kv 10 shit_into = '/path/to/shitfile.txt'
znc2tmsr_etc.kv 11
znc2tmsr_etc.kv 12 # Set the top limit of the line index
znc2tmsr_etc.kv 13 archive_top_inx = 999999
znc2tmsr_etc.kv 14
znc2tmsr_etc.kv 15 # Leave these variables alone
znc2tmsr_etc.kv 16 archive_bottom_inx = 0
znc2tmsr_etc.kv 17 dir_data = [f for f in listdir(dir_to_eat) if isfile(join(dir_to_eat, f))]
znc2tmsr_etc.kv 18 logline_array = []
znc2tmsr_etc.kv 19
znc2tmsr_etc.kv 20 # Sort the dir_data in chronological order
znc2tmsr_etc.kv 21 date_format = '%Y-%m-%d'
znc2tmsr_etc.kv 22 dir_data_chrono = []
znc2tmsr_etc.kv 23 for logfile in dir_data:
znc2tmsr_etc.kv 24 if dir_files_end_with in logfile:
znc2tmsr_etc.kv 25 logdate = logfile.replace('.log','')
znc2tmsr_etc.kv 26 dir_data_chrono.append(logdate)
znc2tmsr_etc.kv 27 dir_data_chrono = sorted(dir_data_chrono, key=lambda d:datetime.strptime(d,date_format))
znc2tmsr_etc.kv 28
znc2tmsr_etc.kv 29 # Shit lines into a single file
znc2tmsr_etc.kv 30 def shit_lines(line_inx):
znc2tmsr_etc.kv 31 f = open(shit_into, 'a')
znc2tmsr_etc.kv 32 for line in logline_array:
znc2tmsr_etc.kv 33 indexed_line = ('%s;%s') % (line_inx,line)
znc2tmsr_etc.kv 34 f.write(indexed_line)
znc2tmsr_etc.kv 35 line_inx = line_inx + 1
znc2tmsr_etc.kv 36 f.close()
znc2tmsr_etc.kv 37
znc2tmsr_etc.kv 38 # Eat each file in the dir
znc2tmsr_etc.kv 39 for logfile in dir_data_chrono:
znc2tmsr_etc.kv 40 logfile_full_path = join(dir_to_eat,('%s.log' % logfile))
znc2tmsr_etc.kv 41 with open(logfile_full_path, 'r') as logfile_data:
znc2tmsr_etc.kv 42 for logline in logfile_data:
znc2tmsr_etc.kv 43 znc_timestamp = ('%s %s') % (logfile, logline[1:9])
znc2tmsr_etc.kv 44 znc_datetimestamp = datetime.strptime(znc_timestamp, '%Y-%m-%d %H:%M:%S')
znc2tmsr_etc.kv 45 epoch_datetimestamp = znc_datetimestamp.strftime('%s')
znc2tmsr_etc.kv 46 if '<' in logline[11:12]:
znc2tmsr_etc.kv 47 #normal line
znc2tmsr_etc.kv 48 speaker = logline[12:logline.find('>')]
znc2tmsr_etc.kv 49 logline_array.append(('%s;%s;%s') % (epoch_datetimestamp, speaker, logline[logline.find('>')+2:]))
znc2tmsr_etc.kv 50 elif '*' in logline[12:13]:
znc2tmsr_etc.kv 51 #connectolade; ignore
znc2tmsr_etc.kv 52 pass
znc2tmsr_etc.kv 53 else:
znc2tmsr_etc.kv 54 #action
znc2tmsr_etc.kv 55 logline_array.append(('%s;*;%s') % (epoch_datetimestamp, logline[13:]))
znc2tmsr_etc.kv 56 archive_bottom_inx = archive_top_inx - (len(logline_array) - 1)
znc2tmsr_etc.kv 57 shit_lines(archive_bottom_inx)
znc2tmsr_etc.kv 58 logline_array = []