• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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

0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 4
  • 4
2 Solutions
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now