Skip to content

Instantly share code, notes, and snippets.

@esirK
Created October 7, 2019 12:08
Show Gist options
  • Save esirK/826c65511950375e94d3481e510ec58c to your computer and use it in GitHub Desktop.
Save esirK/826c65511950375e94d3481e510ec58c to your computer and use it in GitHub Desktop.
import subprocess
import re
statistics = {}
matcher = re.compile('\d+')
# Memory usage
total_ram = subprocess.run(['sysctl', 'hw.memsize'], stdout=subprocess.PIPE).stdout.decode('utf-8')
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
vmLines = vm.split('\n')
wired_memory = (int(matcher.search(vmLines[6]).group()) * 4096) / 1024 ** 3
free_memory = (int(matcher.search(vmLines[1]).group()) * 4096) / 1024 ** 3
active_memory = (int(matcher.search(vmLines[2]).group()) * 4096) / 1024 ** 3
inactive_memory = (int(matcher.search(vmLines[3]).group()) * 4096) / 1024 ** 3
# Used memory = wired_memory + inactive + active
statistics['ram'] = dict({
'total_ram': int(matcher.search(total_ram).group())/1024**3,
'used_ram': round(wired_memory+active_memory+inactive_memory, 2),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment