From cd58d65184d97da1a84a3b6871fd2e8c2620fcba Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Sun, 17 Mar 2019 19:47:43 +1100 Subject: [PATCH] fix bug in writing disk usage --- logparse.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/logparse.py b/logparse.py index 638d02a..19dba19 100755 --- a/logparse.py +++ b/logparse.py @@ -51,7 +51,10 @@ config = { 'httpd': { 'resolve-domains': '', }, - 'du-paths': ['/', '/etc', '/home'], + 'du': { + 'paths': ['/', '/etc', '/home'], + 'force-write': 'n', + }, 'hostname-path': '/etc/hostname', 'logs': { 'auth': '/var/log/auth.log', @@ -219,8 +222,8 @@ def hostname(): # get the hostname of current server return hn def getlocaldomain(): # get the parent fqdn of current server - domain = socket.getfqdn().split('.', 1) - if len(domain) == 2: + domain = socket.getfqdn().split('.', 1) # Note: if socket.fetfqdn() returns localhost, make sure the first entry in /etc/hosts contains the fqdn + if len(domain) != 2: logger.warning('Could not get domain of this server, only hostname. Please consider updating /etc/hosts') return '' else: @@ -298,6 +301,7 @@ def writelog(path = None, content = "", mode = 'w'): # read file, substituting file = open(path, mode) file.write(content) file.close() + logger.debug("written to file " + path) def getusage(path): # Get disk usage statistics disk = os.statvfs(path) @@ -793,20 +797,20 @@ def du(): out = [] content = readlog('alloc') contentnew = "" - for p in config['du-paths']: - alloc_f = getusage(p).alloc + for path in config['du']['paths']: + alloc_f = getusage(path).alloc delta = None try: - alloc_i = re.search(p + '\t(.*)\n', content).group(1) + alloc_i = re.search(path + '\t(.*)\n', content).group(1) delta = alloc_f - float(alloc_i) except: pass if (delta == None): - out.append([p, "used " + parsesize(alloc_f)]) + out.append([path, "used " + parsesize(alloc_f)]) else: - out.append([p, "used " + parsesize(alloc_f), "delta " + parsesize(delta)]) - contentnew += (p + '\t' + str(alloc_f) + '\n') - if config['rotate'] == 'y': + out.append([path, "used " + parsesize(alloc_f), "delta " + parsesize(delta)]) + contentnew += (path + '\t' + str(alloc_f) + '\n') + if config['rotate'] == 'y' or config['du']['force-write'] == 'y': writelog('alloc', contentnew) writetitle("du") -- 2.43.2