Code:Logparser

25 mar. 2016

Et enkelt script for å pakke ut mine komprimerte logfiler. Scriptet finner alle "tar.gz" filer under en spesifisert mappe. Hver fil blir pakket opp, og innholdet blir skrevet (lagt til) en samlet logfil.

#!/usr/bin/python
import os
import fnmatch
import tarfile
import sys # use sys.exit(0) for debugging

root_path = "C:\\Users\\andre\\Desktop\\compressedlogs\\"
matching_files = "fe_access_andynor_secure"
access_log = "%s%s.log" % (root_path, matching_files)
count = 0

def open_compressed(fname):
if (fname.endswith("tar.gz")):
tar = tarfile.open(fname, "r:gz")
for member in tar.getmembers():
# the archive is assumed to only contain one compressed apache log file
f=tar.extractfile(member)
content=f.read()
append_to_file(access_log, content)
#print content
tar.close()

def append_to_file(fname, content):
with open(fname, "a") as myfile:
myfile.write(content)

for root, dir, files in os.walk(root_path):
for item in fnmatch.filter(files, "*"):
if matching_files in item:
full_path = ("%s\%s") % (root, item)
#print full_path
open_compressed(full_path)
count += 1

print "processed %s days of log files" % count