need bash script to calculate vmstat metrics

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
petersystems engineerAsked:
Who is Participating?
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.

you can try this

while true
    b=`/usr/bin/vmstat 55` | /usr/bin/tail -1 | awk '{ print $2 }'`
    if [ $b -gt 20 ]
           /usr/bin/mail -s "high value $b" username@domain
    /usr/bin/sleep 60
Save this as somehwere and "chmod +x" it

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"

Open in new window

Then add
    0 * * * * /path/to/script/
to your crontab

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

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

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
    b=`/usr/bin/vmstat 55` | /usr/bin/tail -1 | /usr/bin/awk '{ print $2 }'`
    if [ $b -gt 20 ]
           /usr/bin/mail -s "high value $b" username@domain
    /usr/bin/sleep 60
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

petersystems engineerAuthor Commented:
mail isnt working in this form...
mail -s "vmstat limit exceeded"

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

even though I dont have an attachment.....
you can use mailx -s
petersystems engineerAuthor Commented:
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
both mail and mailx support -s for subject. What is not working?
petersystems engineerAuthor Commented:
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' < /dev/null
but the message must be an attachment, doesn't make sense to me either.
do you want it part of subject?


mail -a /dev/null  "server load is high $b"
petersystems engineerAuthor Commented:
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
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

From novice to tech pro — start learning today.