1

Following from this OS-agnostic question, specifically this response, similar to data available from the likes of /proc/meminfo on Linux, how can I read system information from OS X using Python (including, but not limited to memory usage).

Community
  • 1
  • 1
davidmytton
  • 38,604
  • 37
  • 87
  • 93

5 Answers5

3

You can get a large amount of system information from the command line utilities sysctl and vm_stat (as well as ps, as in this question.)

If you don't find a better way, you could always call these using subprocess.

Community
  • 1
  • 1
dF.
  • 74,139
  • 30
  • 130
  • 136
3

The only stuff that's really nicely accesible is available from the platform module, but it's extremely limited (cpu, os version, architecture, etc). For cpu usage and uptime I think you will have to wrap the command line utilities 'uptime' and 'vm_stat'.

I built you one for vm_stat, the other one is up to you ;-)

import os, sys

def memoryUsage():

    result = dict()

    for l in [l.split(':') for l in os.popen('vm_stat').readlines()[1:8]]:
        result[l[0].strip(' "').replace(' ', '_').lower()] = int(l[1].strip('.\n '))

    return result

print memoryUsage()
Koen Bok
  • 3,234
  • 3
  • 29
  • 42
2

I did some more googling (looking for "OS X /proc") -- it looks like the sysctl command might be what you want, although I'm not sure if it will give you all the information you need. Here's the manpage: http://developer.apple.com/DOCUMENTATION/Darwin/Reference/ManPages/man8/sysctl.8.html

Also, wikipedia.

John Fouhy
  • 41,203
  • 19
  • 62
  • 77
1

i was searching for this same thing, and i noticed there was no accepted answer for this question. in the intervening time since the question was originally asked, a python module called psutil was released:

https://github.com/giampaolo/psutil

for memory utilization, you can use the following:

>>> psutil.virtual_memory()
svmem(total=8374149120L, available=2081050624L, percent=75.1, used=8074080256L, free=300068864L, active=3294920704, inactive=1361616896, buffers=529895424L, cached=1251086336)
>>> psutil.swap_memory()
sswap(total=2097147904L, used=296128512L, free=1801019392L, percent=14.1, sin=304193536, sout=677842944)
>>>

there are functions for cpu utilization, process management, disk, and network as well. the only omission from the module is a function for retrieving load average, but the python stdlib has os.getloadavg() if you are on a UNIX-like system.

psutil claims to support Linux, Windows, OSX, FreeBSD and Sun Solaris, but i have only tried OSX mavericks and fedora 20.

Giampaolo RodolĂ 
  • 12,488
  • 6
  • 68
  • 60
Michael Frank
  • 483
  • 1
  • 5
  • 7
0

Here's a MacFUSE-based /proc fs:

http://www.osxbook.com/book/bonus/chapter11/procfs

If you have control of the boxes you're running your python program on it might be a reasonable solution. At any rate it's nice to have a /proc to look at!

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465