Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How to Monitor System Resources

by barns (Acolyte)
on Oct 25, 2008 at 22:01 UTC ( #719567=perlquestion: print w/replies, xml ) Need Help??
barns has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am trying to integrate simple system resource logging into a fairly complex application I am currently writing. I have already investigated Linux CPU usage monitor, but it doesn't achieve what I am after. If anyone can assist, it will be appreciated.

I wish to monitor cpu usage (I'm really interested in the percentage usage, but the format from the linked node is ok.), memory usage and network throughput. Similar to tools like top, the Gnome System Monitor, the Windows Task Manager, etc.

The biggest problem is that solutions such as parsing /proc/stat are Linux specific. My code will be deployed to Linux and FreeBSD machines, and possibly Windows in the future.

Can anyone suggest where I can get this information from within my perl script?

Thank you.

Replies are listed 'Best First'.
Re: How to Monitor System Resources
by sflitman (Hermit) on Oct 26, 2008 at 04:07 UTC
    Check out Sys::Info, it's a great little module and it is cross-platform. This code is directly from the POD documentation, except the call to $info->device requires just 'CPU', not CPU => %options.
    use strict; use Sys::Info; use Sys::Info::Constants qw( :device_cpu ); my $info = Sys::Info->new; my $cpu = $info->device('CPU'); printf "CPU: %s\n", scalar($cpu->identify) || 'N/A'; printf "CPU speed is %s MHz\n", $cpu->speed || 'N/A'; printf "There are %d CPUs\n" , $cpu->count || 1; printf "CPU load: %s\n" , $cpu->load || 0; exit;
    This prints on my linux box:
    CPU: 2 x Intel(R) Pentium(R) 4 CPU 2.80GHz CPU speed is 2801.015 MHz There are 2 CPUs CPU load: 0.21
    It should run on my windows box, but I found out with ActivePerl 5,the latest version was 0.51 as PPMs tend to lag, and I'm not sure how to install a module with XS code from CPAN on Windows.



Re: How to Monitor System Resources
by BrowserUk (Pope) on Oct 25, 2008 at 22:29 UTC

    It's not clear wether you mean total usage for your system, or just for your own application?

    Either way, on windows you can get the information using for individual processes using Win32::Process::Info or for the system as a whole using Win32::API.

    See google search for some code for both.

    You would obviously have to conditionally combine these with a *nix solution by testing $^O. I unaware of a cross-platform solution for this.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to Monitor System Resources
by smiffy (Pilgrim) on Oct 26, 2008 at 06:28 UTC

    The Sys::Info approach as suggested by sflitman looks cool although I've never tried it myself.

    As Windows is only 'possible future' and for the sake of TIMTOWTDI, run man top on one of your Un*x boxes and have a look at the -b option: batch mode. You could always call this from within Perl and then use the output however you wish.

    I am not an expert at making system calls from within Perl so can't advise which of the various methods would suit this best. If you run top with -n 1 (only iterate once), I'd guess any of them should work. It's when you've got recurring output that things would get a bit more complicated. Of course, you could always call top -b from a shell script and have it piping to Perl.

    But I'd still say that Sys::Info would be best ;-)

    Update - forgot to say that you should probably check the man page for top on your target platforms just in case flags differ. Having just checked on the other (not Linux) OS I have here - FreeBSD - I note that the -n flag has a different meaning. So much for standardisation.

Re: How to Monitor System Resources
by planetscape (Chancellor) on Oct 26, 2008 at 21:32 UTC

    You may also consider having a look at the Script Center Repository: Sample Perl Scripts, particularly the Hardware section. While perhaps not containing exactly what you are after, the scripts there can often point one in the right direction for handling Windows issues.


Re: How to Monitor System Resources
by zentara (Archbishop) on Oct 26, 2008 at 14:04 UTC
    You might be interested in how to put your Sys::Info results into a little Tk window. See linux memory leak monitor for how, ( or wait and I may make a little GUI for it later :-) ) Looks cool.

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://719567]
Approved by oko1
NodeReaper stokes the furnace

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2018-02-18 19:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (256 votes). Check out past polls.