prevent KeyError by checking whether partition even exists

This commit is contained in:
Arnold Bechtoldt 2015-09-18 19:03:17 +02:00
parent c542cd49d0
commit 0511f611bb

View file

@ -58,9 +58,9 @@ def usage(args=None):
'''
flags = _clean_flags(args, 'disk.usage')
if not os.path.isfile('/etc/mtab') and __grains__['kernel'] == 'Linux':
log.warn('df cannot run without /etc/mtab')
log.error('df cannot run without /etc/mtab')
if __grains__.get('virtual_subtype') == 'LXC':
log.warn('df command failed and LXC detected. If you are running '
log.error('df command failed and LXC detected. If you are running '
'a Docker container, consider linking /proc/mounts to '
'/etc/mtab or consider running Docker with -privileged')
return {}
@ -112,7 +112,7 @@ def usage(args=None):
'capacity': comps[4],
}
except IndexError:
log.warn('Problem parsing disk usage information')
log.error('Problem parsing disk usage information')
ret = {}
return ret
@ -159,7 +159,7 @@ def inodeusage(args=None):
'filesystem': comps[0],
}
except (IndexError, ValueError):
log.warn('Problem parsing inode usage information')
log.error('Problem parsing inode usage information')
ret = {}
return ret
@ -197,18 +197,21 @@ def percent(args=None):
else:
ret[comps[5]] = comps[4]
except IndexError:
log.warn('Problem parsing disk usage information')
log.error('Problem parsing disk usage information')
ret = {}
if args:
if args and args not in ret:
log.error('Problem parsing disk usage information: Partition \'{0}\' does not exist!'.format(args))
ret = {}
elif args:
return ret[args]
else:
return ret
return ret
@decorators.which('blkid')
def blkid(device=None):
'''
Return block device attributes: UUID, LABEL, etc. This function only works
Return block device attributes: UUID, LABEL, etc. This function only works
on systems where blkid is available.
CLI Example: