Solved

need bash script to calculate vmstat metrics

Posted on 2014-10-08
11
378 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now