Created
August 24, 2010 00:20
-
-
Save garno/546630 to your computer and use it in GitHub Desktop.
Revisions
-
garno renamed this gist
Aug 24, 2010 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
garno created this gist
Aug 24, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; }