Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting cross platform system information

1 view
Skip to first unread message

Simon Bunker

unread,
Jan 3, 2003, 4:18:24 PM1/3/03
to
Does anyone know how to get processor load, RAM usage and disk usage on
linux/unix and Windows?

Simon

--
http://www.rendermania.com/


Gerhard Häring

unread,
Jan 3, 2003, 4:38:14 PM1/3/03
to
Simon Bunker wrote:
> Does anyone know how to get processor load, RAM usage and disk usage on
> linux/unix and Windows?

I'll answer for Unix:

processor load => uptime
disk usage => df

RAM usage: I don't believe you can do this portably on Unix. On Linux,
you can evaluate the output of 'free' or read /proc/meminfo directly
(with commands.getoutput or os.popen).

For Windows, you'll probably want to download the win32 extensions and
look what goodies there are in the win32api module.

Gerhard
--
Favourite database: http://www.postgresql.org/
Favourite programming language: http://www.python.org/
Combine the two: http://pypgsql.sf.net/
Embedded database for Python: http://pysqlite.sf.net/

Cameron Laird

unread,
Jan 3, 2003, 5:14:17 PM1/3/03
to
In article <slrnb1c0m6.3kp....@lilith.my-fqdn.de>,

Gerhard Häring <gerhard...@gmx.de> wrote:
>Simon Bunker wrote:
>> Does anyone know how to get processor load, RAM usage and disk usage on
>> linux/unix and Windows?
>
>I'll answer for Unix:
>
>processor load => uptime
>disk usage => df
>
>RAM usage: I don't believe you can do this portably on Unix. On Linux,
>you can evaluate the output of 'free' or read /proc/meminfo directly
>(with commands.getoutput or os.popen).
>
>For Windows, you'll probably want to download the win32 extensions and
>look what goodies there are in the win32api module.
.
.
.
We've covered these before in c.l.p; valuable information
is in the archives.

Most troublesome is "memory usage", which is not a portable
concept at all, let alone one implemented with portable
access. In fact, what *do* you mean by "memory usage"? Is
that per process? Or are you asking how much RAM the host
recognizes? Or ...?
--

Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html

Simon Bunker

unread,
Jan 4, 2003, 9:01:31 AM1/4/03
to
by RAM usage I mean total free memory - eg what free gives you under Linux.
Unix/Linux isn't really the problem, but Windows is as I don't know any
win32 programming.

Simon
--
http://www.rendermania.com/

"Cameron Laird" <cla...@lairds.com> wrote in message
news:v1c2ppk...@corp.supernews.com...

David Bolen

unread,
Jan 6, 2003, 11:08:46 AM1/6/03
to
"Simon Bunker" <si...@rendermania.com> writes:

> by RAM usage I mean total free memory - eg what free gives you under Linux.
> Unix/Linux isn't really the problem, but Windows is as I don't know any
> win32 programming.

As others have pointed out, there are a lot of variables in terms of
just what you want to report in terms of memory usage. But if you
want values that are basically what the Windows Task Manager (at least
under NT/2K/XP - but the API is documented for the 9x series too)
reports, you can use the GlobalMemoryStatus Win32 API function (see
MSDN for the API definition). I believe it still remains unwrapped by
the win32all package, but here's some example code accessing it with
the available calldll module. I'm sure you could also use you can
access it with the available ctypes module (and perhaps more simply),
but that didn't exist when I originally wrote this code.

- - - - - - - - - - - - - - - - - - - - - - - - -
import calldll, struct

result = {}
kernel32 = calldll.load_library('kernel32')
memstat = calldll.get_proc_address(kernel32,'GlobalMemoryStatus')
if memstat:
buf = calldll.membuf(32)
buf.write(struct.pack('1L',32))
calldll.call_foreign_function(memstat,'l','',(buf.address(),))
rtuple = struct.unpack('8L',buf.read())
result['MemoryLoad'] = rtuple[1] # [0] is len
result['TotalPhys'] = rtuple[2]
result['AvailPhys'] = rtuple[3]
result['TotalPageFile'] = rtuple[4]
result['AvailPageFile'] = rtuple[5]
result['TotalVirtual'] = rtuple[6]
result['AvailVirtual'] = rtuple[7]
return result
- - - - - - - - - - - - - - - - - - - - - - - - -

The memory load is in percentage - the other values are all bytes.
This API may not be accurate with more than 2GB of memory in a machine
(it rounds some values down to 2GB) and it will overflow at 4GB -
there's a GlobalMemoryStatusEx call that uses 64-bit return values if
you are worried about such cases, but that's unsupported on the Win 9x
series.

--
-- David
--
/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db...@fitlinxx.com /
| FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/

0 new messages