views:

851

answers:

7

Using Python, how can information such as CPU usage, memory usage (free, used, etc), process count, etc be returned in a generic manner so that the same code can be run on Linux, Windows, BSD, etc?

Alternatively, how could this information be returned on all the above systems with the code specific to that OS being run only if that OS is indeed the operating environment?

A: 

take a look to the os module...

Giancarlo
A: 

Yes - the OS package is the way to go but it doesn't give you everything you are looking for.

There's an example here.

nzpcmad
+1  A: 

It looks like you want to get a lot more information than the standard Python library offers. If I were you, I would download the source code for 'ps' or 'top', or the Gnome/KDE version of the same, or any number of system monitoring/graphing programs which are more likely to have all the necessary Unix cross platform bits, see what they do, and then make the necessary native calls with ctypes.

It's trivial to detect the platform. For example with ctypes you might try to load libc.so, if that throws an exception try to load 'msvcrt.dll' and so on. Not to mention simply checking the operating system's name with os.name. Then just delegate calls to your new cross-platform API to the appropriate platform-specific (sorry) implementation.

When you're done, don't forget to upload the resulting package to pypi.

joeforker
+2  A: 

In a Linux environment you could read from the /proc file system.

~$ cat /proc/meminfo
MemTotal:      2076816 kB
MemFree:        130284 kB
Buffers:        192664 kB
Cached:        1482760 kB
SwapCached:          0 kB
Active:         206584 kB
Inactive:      1528608 kB
HighTotal:     1179484 kB
HighFree:       120768 kB
LowTotal:       897332 kB
LowFree:          9516 kB
SwapTotal:     2650684 kB
SwapFree:      2650632 kB
Dirty:              64 kB
Writeback:          12 kB
AnonPages:       59668 kB
Mapped:          22008 kB
Slab:           200744 kB
PageTables:       1220 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   3689092 kB
Committed_AS:   263892 kB
VmallocTotal:   114680 kB
VmallocUsed:      3604 kB
VmallocChunk:   110752 kB
Anthony D
+1  A: 

I recommend the platform module:

http://doc.astro-wise.org/platform.html

http://docs.python.org/library/platform.html

http://www.doughellmann.com/PyMOTW/platform/index.html

kylebrooks
The platform module doesn't return any of the things he's looking for.
DNS
A: 

There's the PSI (Python System Information) project with that aim, but they don't cover Windows yet.

You can probably use PSI and recpies like this one and create a basic library that meets your needs.

orip
+4  A: 

Regarding cross-platform: your best bet is probably to write platform-specific code, and then import it conditionally. e.g.

import sys
if sys.platform == 'win32':
  import win32_sysinfo as sysinfo
elif sys.platform == 'darwin':
  import mac_sysinfo as sysinfo
elif 'linux' in sys.platform:
  import linux_sysinfo as sysinfo
#etc

print 'Memory available:', sysinfo.memory_available()

For specific resources, as Anthony points out you can access /proc under linux. For Windows, you could have a poke around at the Microsoft Script Repository. I'm not sure where to get that kind of information on Macs, but I can think of a great website where you could ask :-)

John Fouhy
+1: Each OS *is* different. Some concepts do not map. It can't be made OS-agnostic. It must be polymorphic with a unique implementation for each OS.
S.Lott
I agree this is probably the best way. I have created 2 new questions for OS X / Windows specifics. http://stackoverflow.com/questions/467600/how-can-i-read-system-information-in-python-on-os-x and http://stackoverflow.com/questions/467602/how-can-i-read-system-information-in-python-on-windows
DavidM