logview: add gzip compression on logfiles

Signed-off-by: Francois Andrieu <darknao@fedoraproject.org>
This commit is contained in:
Francois Andrieu 2020-08-16 22:09:37 +02:00
parent 09d9f204dd
commit a221e0db50
2 changed files with 12 additions and 3 deletions

View file

@ -21,6 +21,7 @@ import os
import time import time
import json import json
import pwd import pwd
import gzip
try: try:
from ansible.utils.hashing import secure_hash from ansible.utils.hashing import secure_hash
@ -156,7 +157,7 @@ class LogMech(object):
name = name.strip() name = name.strip()
sanitize_host = host.replace(' ', '_').replace('>', '-') sanitize_host = host.replace(' ', '_').replace('>', '-')
fd = open(self.logpath_play + '/' + sanitize_host + '.log', 'a') fd = gzip.open(self.logpath_play + '/' + sanitize_host + '.log.gz', 'at')
now = time.strftime(TIME_FORMAT, time.localtime()) now = time.strftime(TIME_FORMAT, time.localtime())
fd.write(MSG_FORMAT % dict(now=now, name=name, count=count, category=category, data=json.dumps(data))) fd.write(MSG_FORMAT % dict(now=now, name=name, count=count, category=category, data=json.dumps(data)))
fd.close() fd.close()

View file

@ -5,6 +5,7 @@ from optparse import OptionParser
import os import os
import glob import glob
from datetime import date, timedelta from datetime import date, timedelta
import gzip
import dateutil.parser as dateparser import dateutil.parser as dateparser
logpath = '/var/log/ansible' logpath = '/var/log/ansible'
@ -53,9 +54,16 @@ def parse_args(args):
def search_logs(opts, logfiles): def search_logs(opts, logfiles):
msg = '' msg = ''
for fn in sorted(logfiles): for fn in sorted(logfiles):
hostname = os.path.basename(fn).replace('.log', '') hostname = os.path.basename(fn).replace('.log', '').replace('.gz', '')
timestamp = os.path.basename(os.path.dirname(fn)) timestamp = os.path.basename(os.path.dirname(fn))
for line in open(fn): try:
with gzip.open(fn) as f:
f.read()
open_f = gzip.open(fn, "rt")
except:
open_f = open(fn)
for line in open_f:
things = line.split('\t') things = line.split('\t')
if len(things) < 5: if len(things) < 5:
msg += "(logview error - unhandled line): %r\n" % line msg += "(logview error - unhandled line): %r\n" % line