Results 1 to 8 of 8
All,
Im new to bash and have try to make this work, but am hitting a wall. This script returns the results for all table spaces when the -d flag ...
- 09-29-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 10
Adding a new flag to a bash script
All,
Im new to bash and have try to make this work, but am hitting a wall. This script returns the results for all table spaces when the -d flag isnt used and a specific tablespace when the -d flag is used. I would like to add a flag -o to omit a certain tablespace or spaces when designated with the new flag. So the command would look like:
script.sh -s SID -w 80 -c 90 -o tablespace 1
I added the -o under getopts, but need help with the actual function to make it work. Thanks!
#!/bin/sh
#
#
#
# ------------------------------ SETTINGS --------------------------------------
# Oracle environment settings (could be parametrized)
#ORACLE_ORATAB="/var/opt/oracle/oratab"NLS_LANG=AMERICAN_AMERICA.UTF8
OLDPWD=/home/oracle
OPTERR=1
OPTIND=1
ORACLE_BASE=/opt/oracle10gR2-x86_64
ORACLE_HOME=/opt/oracle10gR2-x86_64/product/10.2.0
ORACLE_SID=niunidb09
ORACLE_TERM=xterm
OSTYPE=linux-gnu
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/oracle/bin:/opt/oracle10gR2-x86_64/product/10.2.0/bin
ORACLE_ORATAB="/etc/oratab"
ORACLE_USER="nagios"
ORACLE_PASS="Nag1Os"# External commands
CMD_AWK="/usr/bin/awk"
CMD_EGREP="/bin/egrep"# Temporary work file (will be removed automatically)
TEMP_FILE="/tmp/check_oracle_tablespace_$$.tmp"# Nagios plugin return values
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3# Default values
WARN_THRESHOLD=-1
WARN_EXCEEDED=0
WARN_STATE_TEXT=""
WARN_TRIGGER=0
CRIT_THRESHOLD=-1
CRIT_EXCEEDED=0
CRIT_STATE_TEXT=""
CRIT_TRIGGER=0
CHECK_AUTOEXTENSION=0
VERBOSE=0
------------------------------Function----------------------------------------------
# Checks command line options (pass $@ as parameter).
checkOptions() {
if [ $# -eq 0 ]; then
printInfo
printHelp
exit $STATE_UNKNOWN
fi while getopts s:d:w:c:alhvV OPT $@; do
case $OPT in
s) # Oracle SID
ORACLE_SID="$OPTARG"
;;
d) # Oracle databases (regular expression for egrep)
DB_REGEXP="$OPTARG"
;;
w) # warning threshold
opt_warn_threshold=$OPTARG
WARN_TRIGGER=1
;;
c) # critical threshold
opt_crit_threshold=$OPTARG
CRIT_TRIGGER=1
;;
a) # check tablespace autoextension
CHECK_AUTOEXTENSION=1
;;
o) # omit tablespace
EXCLUDE_TB="$OPTARG"
;;
l) printInfo
printLicense
exit $STATE_UNKNOWN
;;
h) printInfo
printHelp
exit $STATE_UNKNOWN
;;
v) VERBOSE=1
;;
V) printInfo
printVersion
exit $STATE_UNKNOWN
;;
?) printInfo
printHelp
exit $STATE_UNKNOWN
;;
esac
done TMP=`$CMD_EGREP \^${ORACLE_SID}\: $ORACLE_ORATAB`
if [ -z "$ORACLE_SID" ] || [ "$TMP" = "" ]; then
echo "Error: Invalid Oracle SID (see $ORACLE_ORATAB)."
printInfo
printHelp
exit $STATE_UNKNOWN
else
ORACLE_HOME=`echo $TMP | $CMD_AWK 'BEGIN{FS=":"}{print $2}'`
fi
#export LD_LIBRARY_PATH=/usr/local/ora-instantclient/instantclient_10_2
if [ -z "$DB_REGEXP" ]; then
DB_REGEXP=".*"
fi threshold_error=0
if [ $WARN_TRIGGER -eq 1 ]; then
if [ "`echo $opt_warn_threshold | grep '^[0-9]*\$'`" = "" ]; then
threshold_error=1
elif [ $opt_warn_threshold -gt 100 ]; then
threshold_error=1
else
WARN_THRESHOLD=$opt_warn_threshold
fi
fi
if [ $CRIT_TRIGGER -eq 1 ]; then
if [ "`echo $opt_crit_threshold | grep '^[0-9]*\$'`" = "" ]; then
threshold_error=1
elif [ $opt_crit_threshold -gt 100 ]; then
threshold_error=1
else
CRIT_THRESHOLD=$opt_crit_threshold
fi
fi
if [ $threshold_error -eq 1 ]; then
echo "Error: Invalid threshold values (must be between 0-100)."
printInfo
printHelp
exit $STATE_UNKNOWN
fi
}
# ----------------------------- MAIN PROGRAM -----------------------------------
checkOptions $@if [ $VERBOSE -eq 1 ]; then
echo "ORACLE_SID = $ORACLE_SID"
echo "ORACLE=HOME = $ORACLE_HOME"
fi
export ORACLE_SID
export ORACLE_HOME# Feed SQL as here document to sqlplus to query tablespace information.
# Result is stored into temporary text file with four columns separated
# with whitespace:
#
# 1 tablespace name
# 2 usage % (=used/total) as integer
# 3 usage % considering autoextension (=used/max) as integer
# 4 autoextensible YES/NO
#
# SOMETABLESPACE1 74 58 YES
# SOMETABLESPACE2 90 90 NO
# ...
#
if [ $VERBOSE -eq 1 ]; then
echo "Executing $ORACLE_HOME/bin/sqlplus..."
fi
if [ ! -x "$ORACLE_HOME/bin/sqlplus" ]; then
echo "Error: $ORACLE_HOME/bin/sqlplus not found or not executable."
exit $STATE_UNKNOWN
fi
$ORACLE_HOME/bin/sqlplus $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID <<EOF | $CMD_EGREP -i "$DB_REGEXP" | $CMD_EGREP "YES$|NO$" > $TEMP_FILE
set linesize 80
set pages 500
set head offcolumn tablespace_name format a20
column usage_pct format 999
column max_pct format 999
column autoextensible format a5break on reportselect df.TABLESPACE_NAME,
round(((df.BYTES - fs.BYTES) / df.BYTES) * 100) usage_pct,
round(decode(df.MAXBYTES, 34359721984, 0, (df.BYTES - fs.BYTES) / df.MAXBYTES * 100)) max_pct,
df.AUTOEXTENSIBLE
from
(
select TABLESPACE_NAME,
sum(BYTES) BYTES,
AUTOEXTENSIBLE,
decode(AUTOEXTENSIBLE, 'YES', sum(MAXBYTES), sum(BYTES)) MAXBYTES
from dba_data_files
group by TABLESPACE_NAME,
AUTOEXTENSIBLE
)
df,
(
select TABLESPACE_NAME,
sum(BYTES) BYTES
from dba_free_space
group by TABLESPACE_NAME
)
fs
where df.TABLESPACE_NAME=fs.TABLESPACE_NAME
order by df.TABLESPACE_NAME asc
/
EOF
if [ "`cat $TEMP_FILE`" = "" ]; then
echo "Error: Empty result from sqlplus. Check plugin settings and Oracle status."
exit $STATE_UNKNOWN
fi
if [ $VERBOSE -eq 1 ]; then
cat $TEMP_FILE
fi# Loop through tablespace usage percentages and set a flag if thresholds
# are exceeded.
#
if [ $VERBOSE -eq 1 ]; then
echo "Comparing usage percentages to threshold values..."
fi
column=0
for row in `cat $TEMP_FILE`; do
column=`expr $column + 1`
case $column in
1) # tablespace name
ts=$row
;;
2) # usage percentage
usage=$row
;;
3) # usage percentage considering autoextension
autoext_usage=$row;
;;
4) # autoextensible
autoext=$row# Decide which usage percentage to use.
if [ $CHECK_AUTOEXTENSION -eq 1 ] && [ $autoext = "YES" ]; then
usage=$autoext_usage
aetext="AUTOEXT "
fi
if [ $CRIT_TRIGGER -eq 1 ] && [ $usage -ge $CRIT_THRESHOLD ]; then
# Critical threshold was exceeded. Append tablespace and usage
# to status text (shown in Nagios service status information).
CRIT_EXCEEDED=1
if [ "$CRIT_STATE_TEXT" != "" ]; then
CRIT_STATE_TEXT="${CRIT_STATE_TEXT}; ${ts} ${aetext}${usage}%"
else
CRIT_STATE_TEXT="${CRIT_STATE_TEXT}${ts} ${aetext}${usage}%"
fi
if [ $VERBOSE -eq 1 ]; then
echo "${ts} ${aetext}${usage}% CRITICAL"
fi elif [ $WARN_TRIGGER -eq 1 ] && [ $usage -ge $WARN_THRESHOLD ]; then
# Warning threshold was exceeded. Append tablespace and usage
# to status text (shown in Nagios service status information).
WARN_EXCEEDED=1
if [ "$WARN_STATE_TEXT" != "" ]; then
WARN_STATE_TEXT="${WARN_STATE_TEXT}; ${ts} ${aetext}${usage}%"
else
WARN_STATE_TEXT="${WARN_STATE_TEXT}${ts} ${aetext}${usage}%"
fi
if [ $VERBOSE -eq 1 ]; then
echo "${ts} ${aetext}${usage}% WARNING"
fi
fi
column=0
;;
esac
done# Remove temporary work file.
rm -f $TEMP_FILE# Print check results and exit.
if [ $CRIT_EXCEEDED -eq 1 ]; then
if [ $WARN_EXCEEDED -eq 1 ]; then
echo "TABLESPACE CRITICAL: $CRIT_STATE_TEXT WARNING: $WARN_STATE_TEXT"
else
echo "TABLESPACE CRITICAL: $CRIT_STATE_TEXT"
fi
exit $STATE_CRITICALelif [ $WARN_EXCEEDED -eq 1 ]; then
echo "TABLESPACE WARNING: $WARN_STATE_TEXT" exit $STATE_WARNING
fiecho "TABLESPACE OK"
exit $STATE_OK
- 09-30-2008 #2
So first off, in the future, please wrap your code in [code] tags. That makes it a lot easier to read.
As for your question, you might want to look into the '-v' option to grep. This allows you to match all lines that DON'T match the given pattern. This is a good way to exclude certain input.DISTRO=Arch
Registered Linux User #388732
- 09-30-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 10
Could you be more specific about the grep? I'm a novice when it comes to bash scripting so i don't know what grep command you're talking about or where its place in the script would be so it would work.
Thanks
- 09-30-2008 #4Just Joined!
- Join Date
- Sep 2008
- Posts
- 10
BUMP anyone?
- 10-01-2008 #5Just Joined!
- Join Date
- Sep 2008
- Posts
- 10
And the grep -v suggestion doesnt work,but thanks
- 10-01-2008 #6
If a file (called "filename") contains the word "foo", then
will find the line containing the word "foo".Code:grep foo filename
will print out all lines which don't contain the word "foo".Code:grep -v foo filename
You need to edit your script to use "grep -v" every time it's called with "-o".Registered Linux user #388328 || Registered LFS user #15880
AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
Need instant help? Try us on IRC -- #linuxforums on freenode
- 10-01-2008 #7Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Here's a complete example:
Producing:Code:#!/bin/bash - # @(#) s1 Demonstrate inverse match for grep: option "-v". echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) grep set -o nounset cat >data1 <<EOF aa ab bb ba EOF echo echo " Results for grep:" grep "a" data1 echo echo " Results for grep -v:" grep -v "a" data1 exit 0
Best wishes ... cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 grep (GNU grep) 2.5.1 Results for grep: aa ab ba Results for grep -v: bb
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 10-01-2008 #8Just Joined!
- Join Date
- Sep 2008
- Posts
- 10
[QUOTE=investmentbnker75;629633]All,
Im new to bash and have try to make this work, but am hitting a wall. This script returns the results for all table spaces when the -d flag isnt used and a specific tablespace when the -d flag is used.


Reply With Quote