Link to home
Start Free TrialLog in
Avatar of peter
peterFlag for United States of America

asked on

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
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

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
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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
Avatar of peter

ASKER

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.....
you can use mailx -s
Avatar of peter

ASKER

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?
Avatar of peter

ASKER

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.
do you want it part of subject?

try

mail -a /dev/null  "server load is high $b" fred@somewhere.com
Avatar of peter

ASKER

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