LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-27-2006, 01:27 PM   #1
krudupa9
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Rep: Reputation: 0
Help Me its Urgent


Hi,
I am totally new to the world of shell scriping ...I would appreciate if some one guides me with this issue...


I want someone to help me with a shell script to automate load monitoring. This will be a script which ssh to each server (based on a config file) and gives load stats on each server. Apart from doing that it will send email alarms if load on any one server is high.


The script should basically

a) Run 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 if CPU%>25 on any machine.


Thanks
 
Old 06-27-2006, 01:40 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,679
Blog Entries: 54

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
I want someone to help me
Hello and welcome to LQ. Hope you like it here.

The first piece of advice is to use descriptive thread titles. Currently the only thing your thread title describes is the varying states of despair you're in. The second piece of advice would be to be careful with homework assignments as it's against the rules. And the third one would be to come up with something more than "I want". Put some effort in by researching what you think you need. Offer framework code or even pseudo code. Act like you care one bit. So answer these:

a) What does it take to run a script from one machine.
b) What does it take to ssh into all of the other machines.
c) How would one collect the cpu% usage from a machine.
d) What does a scheduler consist of.
 
Old 06-28-2006, 01:24 PM   #3
krudupa9
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Shell Script for Load monitoring

Hi,
First, let me tell you this is not a homework assignment...Iam a java programmer with no exposure to Unix shell scripting...
I am in a job where i need to do this scripting and i cannot plead my ignorance when my superior asks me to do it..I guess it is a matter of survival..

Answering your questions:
1)What does it take to run a script from one machine
Ans:You require to ssh into the other machines which you do by saying ssh hostname.

2)How would one collect the cpu% usage from a machine
I have written a small script with the help of internet and books which goes like this:

#!/bin/sh

typeset -LZ LoadIntVal

top | while read PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND
do
LoadIntVal=`echo $CPU | sed 's/%//g'`
if [[ LoadIntVal -gt 25 ]]
then
mail -s "Load average exceeds 25% for $PID, $COMMAND" root@hostname.com
fi
done
exit


3)What does a scheduler consist of?
Ans: i guess the scheduler in unix is set up in the crontab by specifying the time in ***** where each star represents sec,min,hr and so on...
 
Old 06-28-2006, 07:02 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,679
Blog Entries: 54

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
Post

i cannot plead my ignorance when my superior asks me to do it..
We better come up with something good then. Before that I'll apologise for my harsh phrasing. Now the first question would be if you really need to reinvent the wheel or if there are any existing services that can be built upon. For instance if all boxen would run SNMP it would be very easy to either "walk" each host and get the necessary values or use a central monitoring solution like Nagios, Monit or alike. If not, then it maybe good to know there's tools for getting data from multiple hosts like Clusterit, Clusterssh, Pssh, Dsh, Fanout, Tentakel. Maybe usable in another round of partial wheel reinvention ;-p

Let's cut it and supply some lame and unoptimised code:
Code:
#!/bin/sh
# Name: sshgetstats.sh
# Purpose: show stats
# Args: none
# Deps: Bash, GNU utils, ssh, and /proc plus GNU utils on remote hosts
# Run from: cron
conf="/usr/local/etc/sshgetstats.conf"
if [ -f "$conf" ]; then
        source "$conf"
else
echo -en "# Change values\nhostlist=\"hostOne hostTwo hostZero\"\n\
threshLoad1=\"10\"; threshLoad5=\"4\"; threshProc=\"50\"\n" > "$conf"
echo "Now edit $conf first." > /dev/stderr; exit 127
fi

roundOff() { IFS='.$IFS'; x=($(eval echo "$1")); [ "${x[1]}" -ge "50" ] && x[0]=$[${x[0]}+1]; echo "${x[0]}"; }
threshReach() { [ "$1" -gt "$2" ] && return 1; }

checkStat() { raw=($(ssh -q $1 "hostname; cat /proc/loadavg; /bin/ps axh -eo %cpu,pid --sort=-%cpu|head -1" 2>/dev/null))
if [ "${#raw[@]}" != "8" ]; then echo "$1: [FATAL]: Invalid data for host." > /dev/stderr
else    warnhdr="${raw[0]}: [WARN]:"
        raw[6]=$(roundOff ${raw[6]})
        threshReach "${raw[6]}" "$threshProc" || echo "$warnhdr PID ${raw[7]} over thresh $threshProc."

        raw[1]=$(roundOff ${raw[1]})
        threshReach "${raw[1]}" "$threshLoad1" || echo "$warnhdr 1m loadavg ${raw[1]} over thresh $threshLoad1."

        raw[2]=$(roundOff ${raw[2]})
        threshReach "${raw[2]}" "$threshLoad5" || echo "$warnhdr 5m loadavg ${raw[2]} over thresh $threshLoad5." #\
        #|while read; do mail -s "$REPLY" user@host

fi
 }

for host in $hostlist; do
        checkStat $host #&
done

exit 0
Reading a list of hosts from /usr/local/etc/sshgetstats.conf it ssh's into each host and checks the output against these three thresholds: 1 and 5 minute load average and the process lists first line. Since this is a cronjob any output will be sent to the email address of the user the job is run as. If it needs to go to another address and there's no way to make the MTA take care of that the script would need some small change. An example is given and commented out. The only requirement, except from those mentioned in the script, is a valid and usable ssh account on each box for which you must preauth keys using ssh-agent. It's possible to overcome account problems by for instance using passwordless ssh accounts with keys only accepting connections from a specific host or range and only allow executing one command but for that the script would need some changes.


I guess it is a matter of survival..
This script does work for me, and if it may contribute to your survival there that's nice, but as always: YMMV(VM).
 
Old 06-29-2006, 04:13 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Very nice. I only want to mention that IFS='.$IFS' must have been a mistake, replace the single quotes with double quotes.
 
Old 06-29-2006, 05:31 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,679
Blog Entries: 54

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
IFS='.$IFS' must have been a mistake
Why must it have been?
 
Old 06-29-2006, 07:37 AM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I thought that nobody could possibly want sh's input field separator to consist of the characters ".", "$", "I", "F", "S". To me it seemed more reasonable to add "." to the current content of IFS instead.
Code:
ada@barnabas:~> IFS=".$IFS"
ada@barnabas:~> echo -n "$IFS" | hexdump -c
0000000   .      \t  \n
0000004
Code:
ada@barnabas:~> IFS='.$IFS'
ada@barnabas:~> echo -n "$IFS" | hexdump -c
0000000   .   $   I   F   S
0000005
 
Old 06-29-2006, 07:54 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,679
Blog Entries: 54

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
You're right. Literal vs expansion.
 
Old 06-30-2006, 07:07 AM   #9
krudupa9
LQ Newbie
 
Registered: Jun 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you very much for the help...
I appreciate it...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
boot sector and lilo collapse !!!!! URGENT URGENT URGEN !!!!! frelihm Linux - Software 21 12-02-2009 11:21 AM
Urgent Question Regarding Urgent Questions! Need Help Now! Crashed_Again LQ Suggestions & Feedback 11 10-17-2007 09:07 PM
Urgent!!! luncai Linux - Newbie 3 05-11-2006 03:13 AM
Urgent..Im Having A Nightmare..Urgent!!!! midgcool Linux - Software 41 11-30-2004 11:19 AM
Urgent Urgent !!!! Mozilla Keeps All Your Deleted Emails !!!! odin123 Linux - Software 2 01-31-2004 03:22 AM


All times are GMT -5. The time now is 03:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration