1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #10129 from RadishTheHut/memfacts-fix

Fix for memory fact gathering
This commit is contained in:
Toshio Kuratomi 2015-01-30 07:44:58 -08:00
commit f355c11a3f

View file

@ -609,9 +609,11 @@ class LinuxHardware(Hardware):
""" """
platform = 'Linux' platform = 'Linux'
MEMORY_FACTS = ['MemTotal', 'SwapTotal', 'MemFree', 'SwapFree']
EXTRA_MEMORY_FACTS = ['Buffers', 'Cached', 'SwapCached']
# Originally only had these four as toplevelfacts
ORIGINAL_MEMORY_FACTS = frozenset(('MemTotal', 'SwapTotal', 'MemFree', 'SwapFree'))
# Now we have all of these in a dict structure
MEMORY_FACTS = ORIGINAL_MEMORY_FACTS.union(('Buffers', 'Cached', 'SwapCached'))
def __init__(self): def __init__(self):
Hardware.__init__(self) Hardware.__init__(self)
@ -628,34 +630,46 @@ class LinuxHardware(Hardware):
return self.facts return self.facts
def get_memory_facts(self): def get_memory_facts(self):
memstats = {}
if not os.access("/proc/meminfo", os.R_OK): if not os.access("/proc/meminfo", os.R_OK):
return return
memstats = {}
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]
if key in LinuxHardware.MEMORY_FACTS: if key in self.ORIGINAL_MEMORY_FACTS:
val = data[1].strip().split(' ')[0] val = data[1].strip().split(' ')[0]
self.facts["%s_mb" % key.lower()] = long(val) / 1024 self.facts["%s_mb" % key.lower()] = long(val) / 1024
if key in LinuxHardware.MEMORY_FACTS or key in LinuxHardware.EXTRA_MEMORY_FACTS:
if key in self.MEMORY_FACTS:
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.get('memtotal'), memstats.get('memfree')):
memstats['real:used'] = memstats['memtotal'] - memstats['memfree']
if None not in (memstats.get('cached'), memstats.get('memfree'), memstats.get('buffers')):
memstats['nocache:free'] = memstats['cached'] + memstats['memfree'] + memstats['buffers']
if None not in (memstats.get('memtotal'), memstats.get('nocache:free')):
memstats['nocache:used'] = memstats['memtotal'] - memstats['nocache:free']
if None not in (memstats.get('swaptotal'), memstats.get('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['memtotal'] - memstats['memfree']), 'used': memstats.get('real:used'),
'free': memstats['memfree'] 'free': memstats.get('memfree'),
}, },
'nocache' : { 'nocache' : {
'free': memstats['cached'] + memstats['memfree'] + memstats['buffers'], 'free': memstats.get('nocache:free'),
'used': memstats['memtotal'] - (memstats['cached'] + memstats['memfree'] + memstats['buffers']) 'used': memstats.get('nocache:used'),
}, },
'swap' : { 'swap' : {
'total': memstats['swaptotal'], 'total': memstats.get('swaptotal'),
'free': memstats['swapfree'], 'free': memstats.get('swapfree'),
'used': memstats['swaptotal'] - memstats['swapfree'], 'used': memstats.get('swap:used'),
'cached': memstats['swapcached'] 'cached': memstats.get('swapcached'),
} },
} }
def get_cpu_facts(self): def get_cpu_facts(self):