Updated memory facts fix using dict.get() to avoid KeyError
This commit is contained in:
parent
f20967078e
commit
04b2c698ba
1 changed files with 14 additions and 17 deletions
|
@ -626,10 +626,7 @@ class LinuxHardware(Hardware):
|
||||||
if not os.access("/proc/meminfo", os.R_OK):
|
if not os.access("/proc/meminfo", os.R_OK):
|
||||||
return
|
return
|
||||||
|
|
||||||
# No defaultdict in py2.4
|
memstats = {}
|
||||||
memstat_keys = self.MEMORY_FACTS.union(
|
|
||||||
('real:used', 'nocache:free', 'nocache:used', 'swap:used'))
|
|
||||||
memstats = dict(zip(memstat_keys, (None,) * len(memstat_keys)))
|
|
||||||
for line in open("/proc/meminfo").readlines():
|
for line in open("/proc/meminfo").readlines():
|
||||||
data = line.split(":", 1)
|
data = line.split(":", 1)
|
||||||
key = data[0]
|
key = data[0]
|
||||||
|
@ -641,30 +638,30 @@ class LinuxHardware(Hardware):
|
||||||
val = data[1].strip().split(' ')[0]
|
val = data[1].strip().split(' ')[0]
|
||||||
memstats[key.lower()] = long(val) / 1024
|
memstats[key.lower()] = long(val) / 1024
|
||||||
|
|
||||||
if None not in (memstats['memtotal'], memstats['memfree']):
|
if None not in (memstats.get('memtotal'), memstats.get('memfree')):
|
||||||
memstats['real:used'] = memstats['memtotal'] - memstats['memfree']
|
memstats['real:used'] = memstats['memtotal'] - memstats['memfree']
|
||||||
if None not in (memstats['cached'], memstats['memfree'], memstats['buffers']):
|
if None not in (memstats.get('cached'), memstats.get('memfree'), memstats.get('buffers')):
|
||||||
memstats['nocache:free'] = memstats['cached'] + memstats['memfree'] + memstats['buffers']
|
memstats['nocache:free'] = memstats['cached'] + memstats['memfree'] + memstats['buffers']
|
||||||
if None not in (memstats['memtotal'], memstats['nocache:free']):
|
if None not in (memstats.get('memtotal'), memstats.get('nocache:free')):
|
||||||
memstats['nocache:used'] = memstats['memtotal'] - memstats['nocache:free']
|
memstats['nocache:used'] = memstats['memtotal'] - memstats['nocache:free']
|
||||||
if None not in (memstats['swaptotal'], memstats['swapfree']):
|
if None not in (memstats.get('swaptotal'), memstats.get('swapfree')):
|
||||||
memstats['swap:used'] = memstats['swaptotal'] - memstats['swapfree']
|
memstats['swap:used'] = memstats['swaptotal'] - memstats['swapfree']
|
||||||
|
|
||||||
self.facts['memory_mb'] = {
|
self.facts['memory_mb'] = {
|
||||||
'real' : {
|
'real' : {
|
||||||
'total': memstats['memtotal'],
|
'total': memstats.get('memtotal'),
|
||||||
'used': memstats['real:used'],
|
'used': memstats.get('real:used'),
|
||||||
'free': memstats['memfree'],
|
'free': memstats.get('memfree'),
|
||||||
},
|
},
|
||||||
'nocache' : {
|
'nocache' : {
|
||||||
'free': memstats['nocache:free'],
|
'free': memstats.get('nocache:free'),
|
||||||
'used': memstats['nocache:used'],
|
'used': memstats.get('nocache:used'),
|
||||||
},
|
},
|
||||||
'swap' : {
|
'swap' : {
|
||||||
'total': memstats['swaptotal'],
|
'total': memstats.get('swaptotal'),
|
||||||
'free': memstats['swapfree'],
|
'free': memstats.get('swapfree'),
|
||||||
'used': memstats['swap:used'],
|
'used': memstats.get('swap:used'),
|
||||||
'cached': memstats['swapcached'],
|
'cached': memstats.get('swapcached'),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue