Skip to content

Instantly share code, notes, and snippets.

@garno
Created August 24, 2010 00:20
Show Gist options
  • Select an option

  • Save garno/546630 to your computer and use it in GitHub Desktop.

Select an option

Save garno/546630 to your computer and use it in GitHub Desktop.

Revisions

  1. garno renamed this gist Aug 24, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. garno created this gist Aug 24, 2010.
    61 changes: 61 additions & 0 deletions Server load - Perl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # FOUND ON http://www.skolnick.org/cgi-bin/list.pl?file=serverload.pl
    #
    #!/usr/bin/perl -w
    # serverload.pl
    # Subroutine to defer execution if server load is high
    # dhs 09/20/02
    #
    # Subroutine is written to be called from a script run as a cron
    # job where specific execution time is much less important than
    # not impacting server performance
    # &serverload( $number_of_attempts, $time_between_attempts, $load_threshold )
    #
    # $number_of_attempts: the number of times to check for low server
    # load before execution (default 10)
    #
    # $time_between_attempts: the number of seconds to wait between attempts
    # (default 30)
    #
    # $load_threshold: the one-minute server load above which execution
    # will not take place (default 6.0)
    #
    # serverload returns 1 if load is okay (proceed) or 0 if load is high

    ## test code
    if ( &serverload( 10, 30, 6.0 ) ) {
    print "okay! \n";
    } else {
    print "wait 'til later! \n";
    }

    sub serverload( $attempts, $wait, $threshold ) {
    my $LOAD_TOO_HIGH = 0;
    my $GO_AHEAD = 1;
    my $return_value = $LOAD_TOO_HIGH;
    my ( $attempts, $wait, $threshold ) = @_;
    my $load;
    unless ( $attempts ) { $attempts = 10; }
    unless ( $wait ) { $wait = 30; }
    unless ( $threshold ) { $threshold = 6.0; }
    for ( my $i = 0; $i < $attempts; $i++ ) {
    # print "."; ## test code
    $load = &loadnow();
    if ( $load <= $threshold ) {
    $return_value = $GO_AHEAD;
    last;
    } else {
    sleep( $wait );
    }
    }
    # print " $load \n"; ## test code
    return $return_value;
    }

    sub loadnow {
    open( LOAD, "/proc/loadavg" )
    or die "Bad juju! serverload couldn't open /proc/loadavg: $! \n";
    my $load_avg = <LOAD>;
    close LOAD;
    my ( $one_min_avg ) = split /\s/, $load_avg;
    return $one_min_avg;
    }