ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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...
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.