Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

need bash script to calculate vmstat metrics

Posted on 2014-10-08
11
395 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Utilizing an array to gracefully append to a list of EmailAddresses
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

829 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