943,520 Members | Top Members by Rank

Ad:
Jun 27th, 2006
0

Shell Script for Load monitoring!

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rags is offline Offline
3 posts
since Jun 2006
Jun 28th, 2006
0

Re: Shell Script for Load monitoring!

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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Jun 28th, 2006
0

Re: Shell Script for Load monitoring!

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rags is offline Offline
3 posts
since Jun 2006
Jun 29th, 2006
1

Re: Shell Script for Load monitoring!

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.  
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Jun 29th, 2006
0

Re: Shell Script for Load monitoring!

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rags is offline Offline
3 posts
since Jun 2006
Jun 30th, 2006
0

Re: Shell Script for Load monitoring!

Have you tried it - it looks like it will work, depnding on the shell you use.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: exiting the shell within a shell script
Next Thread in Shell Scripting Forum Timeline: Can anyone tell me only if this is valid?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC