Created
August 24, 2010 00:20
-
-
Save garno/546630 to your computer and use it in GitHub Desktop.
Run script when server load is below threshold written in Perl.
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 characters
# 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment