views:

503

answers:

4

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).

+2  A: 

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
+2  A: 

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.

dF
+2  A: 

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
A: 

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