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

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.

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...

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
commented: Excellent! Clean, neat, and tidy!! +0

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

Have you tried it - it looks like it will work, depnding on the shell you use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.