LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Run a script if the server load is above a certain value. (http://www.linuxquestions.org/questions/linux-server-73/run-a-script-if-the-server-load-is-above-a-certain-value-853872/)

waha87 01-03-2011 05:09 AM

Run a script if the server load is above a certain value.
 
Hi all,

I want to setup a cron job to run a bash script which which calculates the server load and if the load is above certain value(say 15) then stores highest cpu eating users list in to a file for that instant. I have a small script which lists users according to cpu usage.. But i dont know how to check the server load and if and only if its above 15. I hope you understand what i meant. Please help me guyz.

EricTRA 01-03-2011 05:16 AM

Hello,

There are several ways to go about this. Look into the commands uptime, w, procinfo (if installed) that you can use in a bash script to check for the load average. Tip: Have a look using Google, there's a site that gives you the entire script. If you post what you already have and tell us where it's failing or where you're in doubt we would be able to help you better.

Kind regards,

Eric

syg00 01-03-2011 05:24 AM

Define "server load".
Presuming you mean loadavg, what makes you believe it is related to CPU consumption of individual processes ?.

This is a general question, not directed specifically at you. I suspect all the scripts @EricTRA found are also inappropriate in Linux.
Just a guess of course ...

EricTRA 01-03-2011 05:31 AM

Quote:

Originally Posted by syg00 (Post 4211288)
Define "server load".
Presuming you mean loadavg, what makes you believe it is related to CPU consumption of individual processes ?.

This is a general question, not directed specifically at you. I suspect all the scripts @EricTRA found are also inappropriate in Linux.
Just a guess of course ...

Hi,

I made one of the easiest errors a human can make :cry: I assumed the OP was referring to the Load Average obtained by the commands I referred to.

@syg00: What do you mean by 'inappropriate in Linux'? Can you clarify that a bit please?

Kind regards,

Eric

syg00 01-03-2011 05:49 AM

Loadavg in linux comprises tasks in the runqueue plus tasks in uninterruptible sleep (state "D"). Usually this is taken to mean tasks with non-completed I/O, but isn't exclusively this.
High count of state "D" tasks will elevate the loadavg without any relationship to CPU consumption. Things like apache have been known to do this.

EricTRA 01-03-2011 05:51 AM

Quote:

Originally Posted by syg00 (Post 4211306)
Loadavg in linux comprises tasks in the runqueue plus tasks in uninterruptible wait (state "D"). Usually this is taken to mean tasks with non-completed I/O, but isn't exclusively this.
High count of state "D" tasks will elevate the loadavg without any relationship to CPU consumption. Things like apache have been known to do this.

Hi,

OK, point taken, thanks for the clarification.

Kind regards,

Eric

WhisperiN 01-03-2011 06:13 AM

Hello,

Assuming you are talking about the Load Average, returned by uptime ..

Well, I'm not a super shell scripting geek, but I've made a try and written a little script for you, that may help :)

Code:

#!/bin/bash


# Set the Load Average below where the script should be fired..!!
WhenLoadAverage=15

which bc
if [[ "$?" -eq "0" ]]; then
  bc=`which bc`
fi

load1=`uptime | awk {'print $10'} | cut -d',' -f1`

load2=`uptime | awk {'print $11'} | cut -d',' -f1`

load3=`uptime | awk {'print $12'} | cut -d',' -f1`

echo "Load for 1m = $load1"

echo "Load for 5m = $load2"

echo "Load for 15m = $load3"


load1=`echo $load1*100 | $bc`

load2=`echo $load2*100 | $bc`

load3=`echo $load3*100 | $bc`

load1=`echo $load1 | cut -d'.' -f1`

load2=`echo $load2 | cut -d'.' -f1`

load3=`echo $load3 | cut -d'.' -f1`


echo "Load where I should take action is: $WhenLoadAverage"

WhenLoadAverage=`echo $WhenLoadAverage*100 | $bc`
WhenLoadAverage=`echo $WhenLoadAverage | cut -d'.' -f1`

# Change load1 below, to the load you want to keep eye on..
# load1 = 1 min load.
# load2 = 5 min load.
# load3 = 15 min load.
if [[ "$load1" -ge "$WhenLoadAverage" ]]; then
  # Put your script here, or the script you want to run
  echo 'Script is fired'
else
  echo 'All is Cool..!!'
fi

exit
# DONE


Please, let me know if there is something unclear, or if you need some extra help with that..
I'd like to mention that the code above was tested on CentOS 5, Slackware 13.
I assume, it should run OK, on other distros.

Greetings :)

Slax-Dude 01-03-2011 12:19 PM

Like we say in my country: "there is more than one way to skin a cat"

Code:

#!/bin/bash
EXECUTE_ON_AVERAGE="15" # if cpu load average for last 60 secs is
                        # greater or equal to this value, execute script
                        # change it to whatever you want :-)

while true; do
  if [ $(echo "$(uptime | cut -d " " -f 13 | cut -d "," -f 1) >= $EXECUTE_ON_AVERAGE" | bc) = 1 ]; then
    echo "execute the script"
  else
    echo "do nothing"
  fi
  sleep 60
done

Copy the code to a file named insert_script_name_here and "chmod 755" it.
Execute it with ./insert_script_name_here and it will check cpu load average every 60 secs.
Please note that it will execute in the background until you stop it with "killall insert_script_name_here"

misconfiguration 01-03-2011 12:33 PM

Simply set a cronjob to run at N time, ( where N is any arbitrary limit you want to check ).

In your script make sure you have a condition that checks if the load meets your criteria - if it does not, simply have the script "die" until the next time it's invoked.

* Set cron job to run every minute *

Quote:

#!/usr/bin/perl -w

die unless( $server_load eq N );

# If the server is equal to or above N

( ... )

waha87 01-03-2011 08:40 PM

Hi..

Thank you all for your quick response. I will try out your suggestions and will updates the thread after.

Thanx again.

waha87 01-10-2011 09:22 PM

Hi all...


I modified WhisperiN suggestion and got what i want. I will try others too. Thank you all nad what i wanted is starting and guyz perfevtly gave that.
Love you all &LQ

EricTRA 01-11-2011 12:55 AM

Hello,

Glad you've found what you were looking for. If you consider your problem/question solved, then please mark it as such using the Thread Tools.

Kind regards,

Eric

waha87 01-11-2011 03:27 AM

Hi,

I am sorry. I did forget that.
Thanx for reminding me..


All times are GMT -5. The time now is 01:49 AM.