User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 422,977 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,934 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 6064 | Replies: 5
Reply
Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Question Shell Script for Load monitoring!

  #1  
Jun 27th, 2006
I am working as a java programmer and have no experience writing shell scripts or prior experience in UNIX...I would appreciate if someone helps me write a Shell script that:

a) Runs from one machine.
b) ssh into all of the other machines.
c) Collect the cpu% usage from each machine.
d) Write a scheduler which will send this data as mail if CPU%>25 on any machine

Right now iam logging into each serverbox and monitoring the load and killing process manually..using the kill command...
Experts-exchange helped me in writing a script which would automate this killing process....and it works fine.

The script should ssh to each server (based on a config file) and gives load stats on each server
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2004
Posts: 177
Reputation: jim mcnamara is on a distinguished road 
Rep Power: 5
Solved Threads: 9
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

  #2  
Jun 28th, 2006
For starters we need to know what flavor(s) of unix are involved.

And, to keep to you busy, you'll need to be sure to have set up authentication keys between the main host and the remotes so that ssh will work in a script..... they may well be set up already.
Reply With Quote  
Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Re: Shell Script for Load monitoring!

  #3  
Jun 28th, 2006
we are using Linux AS3 and the ssh is already set up...

I need to monitor load on servers and it should not exceed 7 i guess..
Right now iam using

for i in 54 72 114 122 123 139 73;
do ssh 10.42.1.$i "hostname && uptime" ; done;


It displays the following:we.localdomain
12:22:39 up 27 days, 13:17, 16 users, load average:
0.05, 0.05, 0.13
we.localdomain
12:23:12 up 28 days, 21:25, 9 users, load average:
0.39, 0.24, 0.23
wer.localdomain
12:22:20 up 11 days, 19:03, 9 users, load average:
0.00, 0.03, 0.00
we
12:23:02 up 20 days, 13:25, 9 users, load average:
1.16, 1.36, 1.57
wej
12:23:09 up 20 days, 13:23, 9 users, load average:
0.20, 0.31, 0.26
wey.localdomain
12:21:47 up 6 days, 18:59, 9 users, load average:
1.00, 0.42, 0.19
web.localdomain
12:22:52 up 11 days, 18:54, 14 users, load average:
0.00, 0.05, 0.02



The above displays the load processes of each machine and now i need to code the logic to write a mail or scheduler when this load process exceeds 7...
Reply With Quote  
Join Date: May 2004
Posts: 177
Reputation: jim mcnamara is on a distinguished road 
Rep Power: 5
Solved Threads: 9
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

  #4  
Jun 29th, 2006
Here is a start - you need to read up on shell scripting

#!/bin/ksh

# function gen_data
# record performance data, place in a file with each node's
#    information all on one line
gen_data()
{
    for i in 54 72 114 122 123 139 73
    do 
        ssh 10.42.1.$i "hostname && uptime"  
    done | \
    awk ' BEGIN {cnt=0}
        { printf("%s, ", $0)
          cnt++
          if(cnt % 3 == 0) {printf("\n") }
        }' > ./perf_data
}

#function check_data
# check for load averages (fields 6,7,8) which are greater than 7

check_data()
{
	awk -F"," '{ if($6 > 7.0 || $7 > 7.0 || $8 > 7.0) 
	                 {print $0 } 
	           }' ./perf_data > ./email.dat
	           
# do we have any warnings to email ?	           

	if [ -s email.dat ]; then
	   cat ./email.dat | \
	       usr/bin/mailx -s 'Performance warning' you@someplace.com
	fi
}

# main control loop
# run the two functions every ten minutes  60 seconds * 10 = 600

sleepy_time=600

# loop forever .....
while true
do
	gen_data
	check_data
	cat ./perf_data >> ./perf_data.log
	sleep $sleepy_time
done 
Reply With Quote  
Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Re: Shell Script for Load monitoring!

  #5  
Jun 29th, 2006
Originally Posted by jim mcnamara
Here is a start - you need to read up on shell scripting

#!/bin/ksh

# function gen_data
# record performance data, place in a file with each node's
#    information all on one line
gen_data()
{
    for i in 54 72 114 122 123 139 73
    do 
        ssh 10.42.1.$i "hostname && uptime"  
    done | \
    awk ' BEGIN {cnt=0}
        { printf("%s, ", $0)
          cnt++
          if(cnt % 3 == 0) {printf("\n") }
        }' > ./perf_data
}

#function check_data
# check for load averages (fields 6,7,8) which are greater than 7

check_data()
{
	awk -F"," '{ if($6 > 7.0 || $7 > 7.0 || $8 > 7.0) 
	                 {print $0 } 
	           }' ./perf_data > ./email.dat
	           
# do we have any warnings to email ?	           

	if [ -s email.dat ]; then
	   cat ./email.dat | \
	       usr/bin/mailx -s 'Performance warning' you@someplace.com
	fi
}

# main control loop
# run the two functions every ten minutes  60 seconds * 10 = 600

sleepy_time=600

# loop forever .....
while true
do
	gen_data
	check_data
	cat ./perf_data >> ./perf_data.log
	sleep $sleepy_time
done 
Thanks for the reply...
Actually i wrote this script after doing some research in some unix material...
The requirement changed a little bit and they wanted me to see that the server load process does not exceed 8
It goes like this:

#!/bin/ksh
# here 54,72,114,122,123,139,73 are hostnames or IPs

typeset -LZ CPU_LOAD_VAL
for host in 54 72 114 122 123 139 73
do CPU_LOAD_VAL=`ssh 10.42.1.$host uptime | cut -d,
-f4 | cut -d: -f2 | cut -d. -f1`

# if load average > 8 send a message
# then must be be below if
if [[ ${CPU_LOAD_VAL} -gt 8 ]]
then
echo "Load average is above 8 on $host" | mail
root
fi

done

Please let me know if it is ok...
Thanks
Reply With Quote  
Join Date: May 2004
Posts: 177
Reputation: jim mcnamara is on a distinguished road 
Rep Power: 5
Solved Threads: 9
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

  #6  
Jun 30th, 2006
Have you tried it - it looks like it will work, depnding on the shell you use.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 3:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC