Shell Script for Load monitoring!

Reply

Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Shell Script for Load monitoring!

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

 
0
  #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 Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Re: Shell Script for Load monitoring!

 
0
  #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 Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

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

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2.  
  3. # function gen_data
  4. # record performance data, place in a file with each node's
  5. # information all on one line
  6. gen_data()
  7. {
  8. for i in 54 72 114 122 123 139 73
  9. do
  10. ssh 10.42.1.$i "hostname && uptime"
  11. done | \
  12. awk ' BEGIN {cnt=0}
  13. { printf("%s, ", $0)
  14. cnt++
  15. if(cnt % 3 == 0) {printf("\n") }
  16. }' > ./perf_data
  17. }
  18.  
  19. #function check_data
  20. # check for load averages (fields 6,7,8) which are greater than 7
  21.  
  22. check_data()
  23. {
  24. awk -F"," '{ if($6 > 7.0 || $7 > 7.0 || $8 > 7.0)
  25. {print $0 }
  26. }' ./perf_data > ./email.dat
  27.  
  28. # do we have any warnings to email ?
  29.  
  30. if [ -s email.dat ]; then
  31. cat ./email.dat | \
  32. usr/bin/mailx -s 'Performance warning' you@someplace.com
  33. fi
  34. }
  35.  
  36. # main control loop
  37. # run the two functions every ten minutes 60 seconds * 10 = 600
  38.  
  39. sleepy_time=600
  40.  
  41. # loop forever .....
  42. while true
  43. do
  44. gen_data
  45. check_data
  46. cat ./perf_data >> ./perf_data.log
  47. sleep $sleepy_time
  48. done
  49.  
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: rags is an unknown quantity at this point 
Solved Threads: 0
rags rags is offline Offline
Newbie Poster

Re: Shell Script for Load monitoring!

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

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2.  
  3. # function gen_data
  4. # record performance data, place in a file with each node's
  5. # information all on one line
  6. gen_data()
  7. {
  8. for i in 54 72 114 122 123 139 73
  9. do
  10. ssh 10.42.1.$i "hostname && uptime"
  11. done | \
  12. awk ' BEGIN {cnt=0}
  13. { printf("%s, ", $0)
  14. cnt++
  15. if(cnt % 3 == 0) {printf("\n") }
  16. }' > ./perf_data
  17. }
  18.  
  19. #function check_data
  20. # check for load averages (fields 6,7,8) which are greater than 7
  21.  
  22. check_data()
  23. {
  24. awk -F"," '{ if($6 > 7.0 || $7 > 7.0 || $8 > 7.0)
  25. {print $0 }
  26. }' ./perf_data > ./email.dat
  27.  
  28. # do we have any warnings to email ?
  29.  
  30. if [ -s email.dat ]; then
  31. cat ./email.dat | \
  32. usr/bin/mailx -s 'Performance warning' you@someplace.com
  33. fi
  34. }
  35.  
  36. # main control loop
  37. # run the two functions every ten minutes 60 seconds * 10 = 600
  38.  
  39. sleepy_time=600
  40.  
  41. # loop forever .....
  42. while true
  43. do
  44. gen_data
  45. check_data
  46. cat ./perf_data >> ./perf_data.log
  47. sleep $sleepy_time
  48. done
  49.  
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 Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Shell Script for Load monitoring!

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

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC