Skip to content

Instantly share code, notes, and snippets.

@juliedavila
Created March 22, 2015 17:35
Show Gist options
  • Save juliedavila/2db5eeeee9da6796dac9 to your computer and use it in GitHub Desktop.
Save juliedavila/2db5eeeee9da6796dac9 to your computer and use it in GitHub Desktop.
facts sample
def get_memory_facts(self):
# Here we declare a new list
memstats = {}
if not os.access("/proc/meminfo", os.R_OK):
return
for line in open("/proc/meminfo").readlines():
data = line.split(":", 1)
key = data[0]
if key in LinuxHardware.MEMORY_FACTS:
val = data[1].strip().split(' ')[0]
self.facts["%s_mb" % key.lower()] = long(val) / 1024
if key in LinuxHardware.MEMORY_FACTS or key in LinuxHardware.EXTRA_MEMORY_FACTS:
val = data[1].strip().split(' ')[0]
# This is where we use the list we created earlier and start populating it with information
memstats[key.lower()] = long(val) / 1024
# Here we simply add another key to the self.facts list which is proper json format and references the memstats key:values with some trivial math included
self.facts['memory_mb'] = {
'real' : {
'total': memstats['memtotal'],
'used': (memstats['memtotal'] - memstats['memfree']),
'free': memstats['memfree']
},
'nocache' : {
'free': memstats['cached'] + memstats['memfree'] + memstats['buffers'],
'used': memstats['memtotal'] - (memstats['cached'] + memstats['memfree'] + memstats['buffers'])
},
'swap' : {
'total': memstats['swaptotal'],
'free': memstats['swapfree'],
'used': memstats['swaptotal'] - memstats['swapfree'],
'cached': memstats['swapcached']
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment