Solved

need bash script to calculate vmstat metrics

Posted on 2014-10-08
11
390 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

785 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