Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Perl Server Load Check?

by PipTigger (Hermit)
on Jul 21, 2000 at 06:59 UTC ( #23528=perlquestion: print w/replies, xml ) Need Help??

PipTigger has asked for the wisdom of the Perl Monks concerning the following question:

Does anymonk know of a module or snippit or system command which can be used to best determine the load on a Linux Apache server through Perl? Most valuable would probably be current CPU load as a percentage of capacity and current memory usage as a percentage of available. I'm looking to have particularly intensive CGI scripts determine whether they should even try to execute according to the available resources. Any advice would be highly appreciated. TTFN. -PipTigger

Replies are listed 'Best First'.
Re: Perl Server Load Check?
by btrott (Parson) on Jul 21, 2000 at 07:21 UTC
    Check out Apache::VMonitor, which "emulates the reporting functionalities of top(), mount(), df() and ifconfig() utilities".

    The author, Stas Bekman, has also written a Perl wrapper around ApacheBench (ab). You might check that out, as well. It's called Apache::Benchmark, and it's not yet on CPAN.

    You might also check out the Performance Tuning section of the mod_perl guide. And the Performance section of the Apache docs.

Re: Perl Server Load Check?
by lhoward (Vicar) on Jul 21, 2000 at 07:24 UTC
    Things like "CPU load as a percentage of capacity and current memory usage as a percentage of available" are more of a system thing than an apache thing. The most generic way to gather these kinds of statistics is with SNMP. The server will run an SNMP daemon that will provide all sorts of performance and configuration information to anyone sending an SNMP request (the perl Net::SNMP module is great for polling SNMP daemons).

    Though that is a very generic solution that will run well and work well cross-platform; it does require the instalation of an SNMP daemon on the box to be monitored. Traditionally getting information like you want has required some very tricky kernel IO programming. Howerver Linux (and many other unixes) now have a /proc filesystem that contains various performance metrics and kernel settings. For instance on my linux box the file /proc/meminfo contains the following:

    total: used: free: shared: buffers: cached: Mem: 131321856 99635200 31686656 25956352 3571712 48758784 Swap: 131567616 37847040 93720576 MemTotal: 128244 kB MemFree: 30944 kB MemShared: 25348 kB Buffers: 3488 kB Cached: 47616 kB SwapTotal: 128484 kB SwapFree: 91524 kB
    And the file /proc/loadavg contains:
    0.05 0.05 0.01 2/96 22289
    These aren't "real" files, they are generated virtually by the kernel when you read them. You can open and read these files just like you can any other file on your box. For more info on the /proc filesystem and all the wonders it contains see your system man pages.

    Apache has a mod_status module that you can use to gather performance metrics about the apache processes. See the apache documentation for more details on its use.

(chromatic) Re: Perl Server Load Check?
by chromatic (Archbishop) on Jul 21, 2000 at 07:04 UTC
    Apache comes with a file called 'ab'. That stands for Apache Benchmarking. It can do multiple concurrent requests, authorization, and POST or GET query strings.

    It's not written in Perl, but it's nice, and it has a man page.

Re: Perl Server Load Check?
by lolindrath (Scribe) on Jul 21, 2000 at 17:11 UTC
    If you want to be quick and cheap i suggest finding the pid of the server and and then using the top command

    ps ax | grep httpd
    top -p (pid)

RE: Perl Server Load Check?
by DrManhattan (Chaplain) on Jul 21, 2000 at 19:04 UTC

    Sounds like a job for, which is a Perl interface to libgtop. I haven't used it myself, but it looks like you can write something to this effect:

    #!/usr/bin/perl -w use strict; use GTop (); my $gtop = GTop->new; my $mem = $gtop->mem; my $total = $mem->total; my $free = $mem->free; if ($free / $total > .5) # Or whatever { # If there's enough free memory, execute your # code, etc. }


Re: Perl Server Load Check?
by drujax (Initiate) on Jul 21, 2000 at 20:42 UTC
    if you're just looking for server load, you can use the output from the last command. the first line that it outputs displays uptime and server load. granted it is for the entire server. if you're only worried about web stuff, this might not be so good. but if you're worried about ftp and telnet this gives a value based on ALL of your system resources, not just your web stuff.
    system "last -n 10 > file"; open(filename, "/file"); $serverinfo = <filename>; chomp($serverinfo); close(filename);
    this will give you a line (without the newline character) which will provide server up time as well as current load, etc. -drujax
RE: Perl Server Load Check?
by Anonymous Monk on Jul 21, 2000 at 23:41 UTC
    There is a standard apache configuration that can be loaded to monitor the load of the server. It must be set up in your httpd.conf, somewhat like the following. ExtendedStatus On <Location /server-status> SetHandler server-status Order deny,allow Deny from all Allow from all </Location> The extened status gives a cpu usage. You can check out one I just set up here to see if it's what your looking for. Steve
      Oops! sorry about the spacing, should have put in some html?

      <Location /server-status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from all

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://23528]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-09-17 04:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found