Need help with shell script

I'm struggling with a server load that is spiking, not sure what is causing it.

I would like to give this script a try: http://rhcelinuxguide.wordpress.com/2010/08/26/automated-process-monitoring-during-high-server-load/

Doesn't work though, there are some comments saying the quotes are messed up (probably do to being rendered in html on the page?).

I don't know much about this, can you fix the script so it works?

Thank you for any assistance,

Chris
#!/bin/bash

# Define Variables
DT=`date +”%A %b %e %r”`
HOSTNAME=`hostname`

# Create dir to store data
mkdir -p /opt/loadcheck/

# Retrieve the load average of the past 1 minute
LAVG=`uptime | awk {‘print $10}’ | cut -d. -f1`
LCURRENT=`uptime | awk {‘print $10,$11,$12}’`

# Define Threshold. This value will be compared with the current load average. Set the value as per your wish.
LIMIT=-1

# Compare the current load average with Threshold and email the server administrator if threshold is greater.

if [ $LAVG -gt $LIMIT ]
then

#Save the current running processes in a file
/bin/ps -auxf >> /opt/ps_output

echo “Current Time :: $DT. >> /tmp/loadmon.txt
echo “Current Load Average :: $LCURRENT. >> /tmp/loadmon.txt
echo “current processes list attached with the email 1 instance. >> /tmp/loadmon.txt
echo “Also check loadps.txt :: loadtop.txt :: netstat_all.txt :: netstat_port80.txt inside /opt/loadcheck/ on the server” >> /tmp/loadmon.txt
# Send email to support
/usr/bin/mutt -s “Server Load ALERT!!! High 1 minute load average on ‘$HOSTNAME’” -a /opt/ps_output support@somedomain.com > /opt/ps_output

echo “Current Time :: $DT” >> /tmp/loadmon.txt
echo “Current Load Average :: $LCURRENT” >> /tmp/loadmon.txt
echo “current processes list attached with the email 1 instance” >> /tmp/loadmon.txt
echo “Also check loadps.txt :: loadtop.txt :: netstat_all.txt :: netstat_port80.txt inside /opt/loadcheck/ on the server” >> /tmp/loadmon.txt
# Send email to support
/usr/bin/mutt -s ” Server Load ALERT ::: High 1 minute load average on ‘$HOSTNAME’ ” -a /opt/ps_output support@integrityhost.com > /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo ” ######################################################################################################################### ” >> /opt/loadcheck/netstat_port80.txt

fi

# Remove residue logs
/bin/rm -f /tmp/loadmon.txt
/bin/rm -f /opt/ps_output

Open in new window

St_Aug_Beach_BumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Lines 25-27 are missing the closing quotation marks:

echo “Current Time :: $DT." >> /tmp/loadmon.txt
echo “Current Load Average :: $LCURRENT." >> /tmp/loadmon.txt
echo “current processes list attached with the email 1 instance." >> /tmp/loadmon.txt

Lines 30/37: No single quotes around $HOSTNAME!
The output redirect ">" will empty the respective file. Is this desired?

/usr/bin/mutt -s “Server Load ALERT!!! High 1 minute load average on $HOSTNAME” -a /opt/ps_output support@somedomain.com > /opt/ps_output

/usr/bin/mutt -s ” Server Load ALERT ::: High 1 minute load average on $HOSTNAME ” -a /opt/ps_output support@integrityhost.com > /opt/loadcheck/loadps.txt


I didn't check all the netstat commands. Please let me know if you have issues there.

wmp
0
St_Aug_Beach_BumAuthor Commented:
Thank you for your help WMP, this is what I am getting when I run it,


: command not foundonitor.sh: line 2:
date: extra operand `%b'
Try `date --help' for more information.
/etc/load-process-monitor.sh: line 5: jessica.andrews.com: command not found
: command not foundonitor.sh: line 6:
: command not foundonitor.sh: line 9:
awk:
awk:  ^ invalid char '‘' in expression
awk:
awk:  ^ invalid char '‘' in expression
: command not foundonitor.sh: line 13:
: command not foundonitor.sh: line 16:
: command not foundonitor.sh: line 18:
/etc/load-process-monitor.sh: line 27: unexpected EOF while looking for matching `"'
/etc/load-process-monitor.sh: line 87: syntax error: unexpected end of file


Salvageable you think?  

Chris
0
woolmilkporcCommented:
Did you incorporate the changes I suggested?
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

St_Aug_Beach_BumAuthor Commented:
Yes,  :)   That was run after I made the suggested changes.
0
woolmilkporcCommented:
So your "backticks" ( ` ` ) are not working (maybe due to copy-and-paste).

Try to replace them all with $(  ) , e. g. instead of
 
DT='date`

DT=$(date)
0
St_Aug_Beach_BumAuthor Commented:
oh boy, I've tried replacing them as you suggested. I'm still getting:

-bash-3.2# /bin/sh /etc/load-process-monitor.sh
: command not foundonitor.sh: line 2:
date: extra operand `%b'
Try `date --help' for more information.
/etc/load-process-monitor.sh: line 5: jessica.mywebsite.com: command not found
: command not foundonitor.sh: line 6:
: command not foundonitor.sh: line 9:
awk:
awk: ^ invalid char '‘' in expression
awk:
awk: ^ invalid char '‘' in expression
: command not foundonitor.sh: line 13:
: command not foundonitor.sh: line 16:
: command not foundonitor.sh: line 18:
/etc/load-process-monitor.sh: line 87: syntax error: unexpected end of file
-bash-3.2#


The script, with suggested changes, is below

--------------------------------

#!/bin/bash

# Define Variables
DT=$(date +”%A %b %e %r”)
HOSTNAME=$(jessica.mywebsite.com)

# Create dir to store data
mkdir -p /opt/loadcheck/

# Retrieve the load average of the past 1 minute
LAVG=$(uptime | awk ‘{print $10}’ | cut -d. -f1)
LCURRENT=$(uptime | awk ‘{print $10,$11,$12}’)

# Define Threshold. This value will be compared with the current load average. Set the value as per your wish.
LIMIT=-1

# Compare the current load average with Threshold and email the server administrator if threshold is greater.

if [ $LAVG -gt $LIMIT ]
then

#Save the current running processes in a file
/bin/ps -auxf >> /opt/ps_output

echo “Current Time :: $DT.” >> /tmp/loadmon.txt
echo “Current Load Average :: $LCURRENT.” >> /tmp/loadmon.txt
echo “current processes list attached with the email 1 instance.” >> /tmp/loadmon.txt
echo “Also check loadps.txt :: loadtop.txt :: netstat_all.txt :: netstat_port80.txt inside /opt/loadcheck/ on the server” >> /tmp/loadmon.txt
# Send email to support
/usr/bin/mutt -s “Server Load ALERT!!! High 1 minute load average on $HOSTNAME” -a /opt/ps_output chris@mywebsite.com > /opt/ps_output

echo “Current Time :: $DT” >> /tmp/loadmon.txt
echo “Current Load Average :: $LCURRENT” >> /tmp/loadmon.txt
echo “current processes list attached with the email 1 instance” >> /tmp/loadmon.txt
echo “Also check loadps.txt :: loadtop.txt :: netstat_all.txt :: netstat_port80.txt inside /opt/loadcheck/ on the server” >> /tmp/loadmon.txt
# Send email to support
/usr/bin/mutt -s ” Server Load ALERT ::: High 1 minute load average on $HOSTNAME ” -a /opt/ps_output support@integrityhost.com > /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_port80.txt

/bin/ps -auxf >> /opt/loadcheck/loadps.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadps.txt
/bin/top -c -n1 >> /opt/loadcheck/loadtop.txt
echo “#########################################################################################################################” >> /opt/loadcheck/loadtop.txt
/bin/netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_all.txt
echo “#########################################################################################################################” >> /opt/loadcheck/netstat_all.txt
/bin/netstat -alntp | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n >> /opt/loadcheck/netstat_port80.txt
echo ” ######################################################################################################################### ” >> /opt/loadcheck/netstat_port80.txt

fi

# Remove residue logs
/bin/rm -f /tmp/loadmon.txt
/bin/rm -f /opt/ps_output

0
woolmilkporcCommented:
It looks quite as if there were invalid "carriage-return" characters in your script.

Check with "cat -v scriptname". Do you see something like "^M" at the end of each line?

If so, you could try to copy it from your comment above and paste it into a new file (in a PuTTY session or whatever you're using to acces the Linux machine).

An alternative is this:

tr -d '\r' < scriptname > scriptname.new
chmod +x scriptname.new

Now try to run scriptname.new. Does it work?

wmp
0
tel2Commented:
Hi SABB,

I doubt this is a carriage-return probelm.  At first glance your single/double quotes seem to be non-standard.  See how they are angled, instead of vertical?  Have they come from some word processor or something?

Here are a few of the many examples:

Line 2 gives an error for me:
    DT=$(date +”%A %b %e %r”)
but this works:
    DT=$(date +"%A %b %e %r")
or you can use single quotes:
    DT=$(date +'%A %b %e %r')

Line 11 has single quotes angled in both directions:
    LAVG=$(uptime | awk ‘{print $10}’ | cut -d. -f1)
It should be:
    LAVG=$(uptime | awk '{print $10}' | cut -d. -f1)

Line 25 has double quotes angled in both directions:
    echo “Current Time :: $DT.” >> /tmp/loadmon.txt

And for line 5:
    HOSTNAME=$(jessica.mywebsite.com)
that will give an error because jessica.mywebsite.com is not a command.  I suggest you now revert to your original:
    HOSTNAME=`hostname`
or one of these:
    HOSTNAME=$(hostname)
    HOSTNAME='jessica.mywebsite.com'
    HOSTNAME="jessica.mywebsite.com"
    HOSTNAME=jessica.mywebsite.com

I suggest you fix all your single/double quotes to be the standard vertical type, then try again.

If you still get errors, please post your code, with the errors, again.

tel2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
St_Aug_Beach_BumAuthor Commented:
Thank you both very much.

Sorry it took me a while to get back to you, just got swamped here.

Started from the beginning and incorporated solutions from both of you. Went through it one line at a time and fixed it (mostly, I think).

Now that these things are fixed, I found another problem. I have posted it here if you want to take a look :)

It is posted here:  http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_27535337.html

Thanks again for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.