Solved

need bash script to calculate vmstat metrics

Posted on 2014-10-08
11
398 Views
Last Modified: 2014-10-08
Hi everyone,
I need a little assistance with writing a bash script for monitoring a oracle server running red hat 6.5.

I would like a bash script to run vmstat, and email a warning if the b column run higher then a value, say 20 using mail -s.
the b column is the backlog of transactions in queue. Anything over 70 shows up as database latency.

This can be scheduled as cron or always run as a while statement, example, while b is less then 20, do nothing. If b is higher then 20, email once every hour.

Thank you!

vmstat 5 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0  87176  25536 320380    0    0     0     5    9    7  0  0 100  0  0
0
Comment
Question by:Peter Kuczynski
  • 5
  • 4
11 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 40368997
you can try this

while true
do
    b=`/usr/bin/vmstat 55` | /usr/bin/tail -1 | awk '{ print $2 }'`
    if [ $b -gt 20 ]
    then
           /usr/bin/mail -s "high value $b" username@domain
    fi
    /usr/bin/sleep 60
done
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 40369007
Save this as checkvmstat.sh somehwere and "chmod +x" it
#!/bin/bash

LIM=50
CURR=$(vmstat 5 1 | tail -1 | awk '{print $2}')

if [ $CURR -gt $LIM ]; then
  echo "vmstat value $CURR exceeds limit $LIM" | mail -s "vmstat limit exceeded" fred@anywhere.com
fi

Open in new window


Then add
    0 * * * * /path/to/script/checkvmstat.sh
to your crontab

(Edit: Hi omarfarid - this is what I get for working out the script in the EE edit window!)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40369017
@simon3270

if you want to run script as cron job then set PATH or use full path names

In my script, I missed full path name for awk:


while true
do
    b=`/usr/bin/vmstat 55` | /usr/bin/tail -1 | /usr/bin/awk '{ print $2 }'`
    if [ $b -gt 20 ]
    then
           /usr/bin/mail -s "high value $b" username@domain
    fi
    /usr/bin/sleep 60
done
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40369347
mail isnt working in this form...
mail -s "vmstat limit exceeded" fred@anywhere.com

but it does work like this:
mail -a /home/fred/monitor.txt  'oracle01 monitor report' fred@anywhere.com < /dev/null

even though I dont have an attachment.....
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40369360
you can use mailx -s
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40369373
thank you, I know the mail is outside the scope of this issue, it still wont work with mailx -s
seems I can only send mail as an attachment, dont know why.


but I appreciate the code
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40369382
both mail and mailx support -s for subject. What is not working?
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40369416
the message is never sent
the only time I get the message is using this format:
mail -a /home/user/monitor.txt  'server monitor report' fred@somewhere.com < /dev/null
but the message must be an attachment, doesn't make sense to me either.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40369423
do you want it part of subject?

try

mail -a /dev/null  "server load is high $b" fred@somewhere.com
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40369433
that wont work either, can we pipe the whole report out to a report.txt file and send it as an attachment? Attachments work but normal mail does not, weird
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Zimbra on Amazon Linux help 7 115
Samba 4, Users Permission, 5 68
Can't connect to FTP 18 110
How to fix Dual Server Conflict GitLab vs Apache2 3 21
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question