tags:

views:

224

answers:

3

I need to read system information like CPU/RAM/disks usage in C++. Maybe swap, network and process too but that's less important.

It has probably been done thousand of times before so I first tried to search for a library. Someone here suggested SIGAR, which seems to fit my needs but it has a GPL license and it is for inclusion in a proprietary product. So it's not an option here.

I feel like it's something not that easy to implement, as it'll need testing on several platforms. So a library would be welcome.

If you don't know of any library, could you point me in the right direction for both platforms?

A: 

The short answer is it's not very difficult to roll your own implementation.

For a more complete answer take a look at the following topic on QT forum. It's from 2006, but I think it addresses your problem:

http://lists.trolltech.com/qt-interest/2006-05/thread00922-0.html

UPDATE:

You could try:

#if defined(WINDOWS)
  // either macro format
  #define CPU_INFO (<your cpu macro>)
  // or function format
  void fs_info()
  ...
#elif defined(LINXU)
  ...
#elif defined(MAC)
  ...
#endif

and then use those macros/functions in your code.

I'm sure there's a way to create a C++ Template-based solution that would be cleaner then the C mess above.

Misha M
thx for the link but they seem to talk about cpu count not usage, nor ram and filesystem usage. I'm particularly interested in the last one actually (filesystem)
f4
I gave the link as an example of detecting multiple operating systems.You could try to roll SIGAR into your own custom dynamic library, which would be GPL and then use it with your proprietary code. I've heard of this approach used for proprietary Linux drivers, but I don't know what the legal ramifications would be.
Misha M
I doubt that's legal. this sentence is from gnu.org : "using the Lesser GPL permits use of the library in proprietary programs; using the ordinary GPL for a library makes it available only for free programs." and from wikipedia : "The main difference between the GPL and the LGPL is that the latter can be linked to (in the case of a library, 'used by') a non-(L)GPLed program, regardless of whether it is free software or proprietary software."
f4
A: 

Your best bet is to create something yourself.

On Windows, you would be looking at something like this: http://www.codeproject.com/KB/system/Using_WMI_in_Visual_C__.aspx and this: http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/

George Edison
+3  A: 

On Windows, try GetDiskFreeSpaceEx and GlobalMemoryStatusEx.

Linux is a tad more complicated, due to the way it allows you to mount volumes. You can always system() out to "df", but that's horrid. Since Linux is open source, simply look at the source code to "df" to find out how it works! :)

If you don't have the time: for UNIX variants (including Linux), you can try libstatgrab. It's LGPL / proprietary friendly. You'll probably need to #ifdef some code specifically for Windows but, fortunately, the Windows calls are straightforward. Worst case: 200 lines. If you're feeling generous, you can contribute a patch for full-blown Windows support :)

Good luck!

Pestilence
looks interesting I'll have a look ;)
f4
quick question: let's say I decide to use the (very dirty) way of system("df"). How could I capture the results?
Bruno Brant
Pestilence