From: Andrew Lorimer Date: Sun, 17 Mar 2019 08:47:43 +0000 (+1100) Subject: fix bug in writing disk usage X-Git-Url: https://git.lorimer.id.au/logparse.git/diff_plain/cd58d65184d97da1a84a3b6871fd2e8c2620fcba?ds=sidebyside fix bug in writing disk usage --- 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")