CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Linux Server Load in Perl ?

    Does anyone know of a GPL script or how I would go about creating a script that checks the current server load on a Linux/Apache webserver

    Basically I am trying to modify a GPL Perl backup script like this pseudo-code

    if ( ServerLoad < 15% ) {

    DoSomething();

    }
    else
    {

    DoNothing();

    }

  2. #2

    Re: Linux Server Load in Perl ?

    It all really depends on what you mean by load. If you're referring to load average, you can simply parse the output of uptime and use the value that's the most important to you.

    For example:

    Code:
    $uptime = `uptime`;
    $uptime =~ /load average:(.*)$/;
    @avg = split /\,/,$uptime;
    foreach (@avg)
    {
       print "Load: $_\n";
    }
    I'm sure there are also perl modules that will give you different metrics...

  3. #3
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Re: Linux Server Load in Perl ?

    OK what I mean by "load" is that I want to determine if the current CPU usuage is above a given percentage ? Does that make sense ?

  4. #4

    Re: Linux Server Load in Perl ?

    Then parse the output of vmstat. It will give you the percentage of CPU used for user requests, system requests, and idle %. I'd subtract the idle stat from 100 and you'll have the total % utilization.

    Code:
    @output = `vmstat`;
    $idle = (split ' ', $output[-1])[14] # Get the last line of the vmstat output, get the idle column
    print "Idle: $idle%\n";
    print "Total Utilization: " . (100 - $idle) . "%\n";
    I'd run vmstat a few times and get the average depending on what you're trying to do...

    Again, there is likely a perl module that does this for you...

  5. #5
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Re: Linux Server Load in Perl ?

    If there is a perl module that does this already, how would you suggest that I go about finding out which one that it is ?

    thanks for the help

  6. #6

    Re: Linux Server Load in Perl ?

    Check on CPAN / etc. Or you can use the above script.

  7. #7
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Re: Linux Server Load in Perl ?

    Thanks - I decided to go ahead and use the script you provided.

    I had to change a little to account for the fact that on my architecture the idle time is not in column 16.

    Here's what I ended up with

    Code:
    @output = `vmstat`;
    @cols = (split ' ', $output[1]);
    $colcount = 0;
    $col = 0;
    
    foreach(@cols) {
     if ($_ == "id" ) {
       $col = $colcount;
     } 
     $colcount++;
    }
    
    $idle = (split ' ', $output[-1])[$col]; 
    $load = 100 - $idle;
    works like a charm!

    Thanks so much for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured