Code

Git commit notifications via post-receive hook
[nagiosplug.git] / plugins-scripts / check_oracle.sh
index 3064db90bddd46c3c255079d149b6532dd4f979f..664795eb29932526047f3408467ffd3aefdb7600 100755 (executable)
@@ -3,25 +3,12 @@
 # latigid010@yahoo.com
 # 01/06/2000
 #
-#  This Nagios plugin was created to check remote or local TNS
-#  status and check local Database status.
+#  This Nagios plugin was created to check Oracle status
 #
-#  Add the following lines to your object config file (i.e. commands.cfg)
-#         command[check-tns]=/usr/local/nagios/libexec/check_ora 1 $ARG$
-#         command[check-oradb]=/usr/local/nagios/libexec/check_ora 2 $ARG$
-#
-#
-# Usage: 
-#      To check TNS Status:  ./check_ora 1 <Oracle Sid or Hostname/IP address>
-#  To Check local database:  ./check_ora 2 <ORACLE_SID>
-#
-# I have the script checking for the Oracle PMON process and 
-# the sgadefORACLE_SID.dbf file.
-# 
 
 PROGNAME=`basename $0`
 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
-REVISION=`echo '$Revision$' | sed -e 's/[^0-9.]//g'`
+REVISION="@NP_VERSION@"
 
 . $PROGPATH/utils.sh
 
@@ -31,8 +18,8 @@ print_usage() {
   echo "  $PROGNAME --tns <Oracle Sid or Hostname/IP address>"
   echo "  $PROGNAME --db <ORACLE_SID>"
   echo "  $PROGNAME --login <ORACLE_SID>"
-  echo "  $PROGNAME --cache <USER> <PASS> <INST> <CRITICAL> <WARNING>"
-  echo "  $PROGNAME --tablespace <USER> <PASS> <INST> <TABLESPACE> <CRITICAL> <WARNING>"
+  echo "  $PROGNAME --cache <ORACLE_SID> <USER> <PASS> <CRITICAL> <WARNING>"
+  echo "  $PROGNAME --tablespace <ORACLE_SID> <USER> <PASS> <TABLESPACE> <CRITICAL> <WARNING>"
   echo "  $PROGNAME --oranames <Hostname>"
   echo "  $PROGNAME --help"
   echo "  $PROGNAME --version"
@@ -43,24 +30,24 @@ print_help() {
   echo ""
   print_usage
   echo ""
-  echo "Check remote or local TNS status and check local Database status"
+  echo "Check Oracle status"
   echo ""
-  echo "--tns=SID/IP Address"
+  echo "--tns SID/IP Address"
   echo "   Check remote TNS server"
-  echo "--db=SID"
-  echo "   Check local database (search /bin/ps for PMON process and check"
+  echo "--db SID"
+  echo "   Check local database (search /bin/ps for PMON process) and check"
   echo "   filesystem for sgadefORACLE_SID.dbf"
-  echo "--login=SID"
+  echo "--login SID"
   echo "   Attempt a dummy login and alert if not ORA-01017: invalid username/password"
   echo "--cache"
   echo "   Check local database for library and buffer cache hit ratios"
-  echo "       --->  Requires Oracle user/password and SID  specified."
+  echo "       --->  Requires Oracle user/password and SID specified."
   echo "                       --->  Requires select on v_$sysstat and v_$librarycache"
   echo "--tablespace"
   echo "   Check local database for tablespace capacity in ORACLE_SID"
   echo "       --->  Requires Oracle user/password specified."
   echo "                       --->  Requires select on dba_data_files and dba_free_space"
-  echo "--oranames=Hostname"
+  echo "--oranames Hostname"
   echo "   Check remote Oracle Names server"
   echo "--help"
   echo "   Print this help screen"
@@ -71,7 +58,7 @@ print_help() {
   echo "variable is set, that ORACLE_HOME/bin is in your PATH, and the"
   echo "tnsnames.ora file is locatable and is properly configured."
   echo ""
-  echo "When checking Local Database status your ORACLE_SID is case sensitive."
+  echo "When checking local database status your ORACLE_SID is case sensitive."
   echo ""
   echo "If you want to use a default Oracle home, add in your oratab file:"
   echo "*:/opt/app/oracle/product/7.3.4:N"
@@ -102,11 +89,11 @@ case "$cmd" in
     exit $STATE_OK
     ;;
 --version)
-               print_revision $PLUGIN $REVISION
+               print_revision $PROGNAME $REVISION
     exit $STATE_OK
     ;;
 -V)
-               print_revision $PLUGIN $REVISION
+               print_revision $PROGNAME $REVISION
     exit $STATE_OK
     ;;
 esac
@@ -131,9 +118,11 @@ fi
 # Last resort
 [ -z "$ORACLE_HOME" -a -d $PROGPATH/oracle ] && ORACLE_HOME=$PROGPATH/oracle
 
-if [ -z "$ORACLE_HOME" -o ! -d "$ORACLE_HOME" ] ; then
-       echo "Cannot determine ORACLE_HOME for sid $2"
-       exit $STATE_UNKNOWN
+if [ "$cmd" != "--db" ]; then
+       if [ -z "$ORACLE_HOME" -o ! -d "$ORACLE_HOME" ] ; then
+               echo "Cannot determine ORACLE_HOME for sid $2"
+               exit $STATE_UNKNOWN
+       fi
 fi
 PATH=$PATH:$ORACLE_HOME/bin
 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
@@ -171,7 +160,7 @@ case "$cmd" in
     }'
     ;;
 --db)
-    pmonchk=`ps -ef | grep -v grep | grep ${2} | grep -c pmon`
+    pmonchk=`ps -ef | grep -v grep | grep -c "ora_pmon_${2}$"`
     if [ ${pmonchk} -ge 1 ] ; then
        echo "${2} OK - ${pmonchk} PMON process(es) running"
        exit $STATE_OK
@@ -203,33 +192,49 @@ case "$cmd" in
        echo "UNKNOWN - Warning level is less then Crit"
        exit $STATE_UNKNOWN
     fi
-    result=`sqlplus -s ${2}/${3}@${4} << EOF
-      set pagesize 0
-     
-select (1-(pr.value/(dbg.value+cg.value)))*100 \
-from v\\$sysstat pr, v\\$sysstat dbg, v\\$sysstat cg \
-where pr.name = 'physical reads'  \
-        and dbg.name='db block gets' \
-        and cg.name='consistent gets'; `
+    result=`sqlplus -s ${3}/${4}@${2} << EOF
+set pagesize 0
+set numf '9999999.99'
+select (1-(pr.value/(dbg.value+cg.value)))*100
+from v\\$sysstat pr, v\\$sysstat dbg, v\\$sysstat cg
+where pr.name='physical reads'
+and dbg.name='db block gets'
+and cg.name='consistent gets';
+EOF`
 
-    buf_hr=`echo $result | awk '{print int($1)}'` 
-    result=`sqlplus -s ${2}/${3}@${4} << EOF
-      set pagesize 0
+    if [ -n "`echo $result | grep ORA-`" ] ; then
+      error=` echo "$result" | grep "ORA-" | head -1`
+      echo "CRITICAL - $error"
+      exit $STATE_CRITICAL
+    fi
 
-select sum(lc.pins)/(sum(lc.pins)+sum(lc.reloads))*100 \
-from v\\$librarycache lc ; `
+    buf_hr=`echo "$result" | awk '/^[0-9\. \t]+$/ {print int($1)}'` 
+    buf_hrx=`echo "$result" | awk '/^[0-9\. \t]+$/ {print $1}'` 
+    result=`sqlplus -s ${3}/${4}@${2} << EOF
+set pagesize 0
+set numf '9999999.99'
+select sum(lc.pins)/(sum(lc.pins)+sum(lc.reloads))*100
+from v\\$librarycache lc;
+EOF`
        
-    lib_hr=`echo $result | awk '{print int($1)}'`
+    if [ -n "`echo $result | grep ORA-`" ] ; then
+      error=` echo "$result" | grep "ORA-" | head -1`
+      echo "CRITICAL - $error"
+      exit $STATE_CRITICAL
+    fi
+
+    lib_hr=`echo "$result" | awk '/^[0-9\. \t]+$/ {print int($1)}'`
+    lib_hrx=`echo "$result" | awk '/^[0-9\. \t]+$/ {print $1}'`
 
     if [ $buf_hr -le ${5} -o $lib_hr -le ${5} ] ; then
-       echo "${3} : ${4} CRITICAL - Cache Hit Rates: $lib_hr % Lib -- $buf_hr % Buff"
+       echo "${2} CRITICAL - Cache Hit Rates: $lib_hrx% Lib -- $buf_hrx% Buff|lib=$lib_hrx%;${6};${5};0;100 buffer=$buf_hrx%;${6};${5};0;100"
        exit $STATE_CRITICAL
     fi
     if [ $buf_hr -le ${6} -o $lib_hr -le ${6} ] ; then
-       echo "${3} : ${4} WARNING  - Cache Hit Rates: $lib_hr % Lib -- $buf_hr % Buff"
+       echo "${2} WARNING  - Cache Hit Rates: $lib_hrx% Lib -- $buf_hrx% Buff|lib=$lib_hrx%;${6};${5};0;100 buffer=$buf_hrx%;${6};${5};0;100"
        exit $STATE_WARNING
     fi
-    echo "${3} : ${4} OK - Cache Hit Rates: $lib_hr % Lib -- $buf_hr % Buff"
+    echo "${2} OK - Cache Hit Rates: $lib_hrx% Lib -- $buf_hrx% Buff|lib=$lib_hrx%;${6};${5};0;100 buffer=$buf_hrx%;${6};${5};0;100"
 
     exit $STATE_OK
     ;;
@@ -238,29 +243,42 @@ from v\\$librarycache lc ; `
        echo "UNKNOWN - Warning level is more then Crit"
        exit $STATE_UNKNOWN
     fi
-    result=`sqlplus -s ${2}/${3}@${4} << EOF
-      set pagesize 0
-     
-select b.free,a.total,100 - trunc(b.free/a.total * 1000) / 10 prc \
-from ( \
-select tablespace_name,sum(bytes)/1024/1024 total \
-from dba_data_files group by tablespace_name) A, \
-( select tablespace_name,sum(bytes)/1024/1024 free \
-from dba_Free_space group by tablespace_name) B \
-where a.tablespace_name=b.tablespace_name and a.tablespace_name='${5}';  ` 
+    result=`sqlplus -s ${3}/${4}@${2} << EOF
+set pagesize 0
+set numf '9999999.99'
+select NVL(b.free,0.0),a.total,100 - trunc(NVL(b.free,0.0)/a.total * 1000) / 10 prc
+from (
+select tablespace_name,sum(bytes)/1024/1024 total
+from dba_data_files group by tablespace_name) A
+LEFT OUTER JOIN
+( select tablespace_name,sum(bytes)/1024/1024 free
+from dba_free_space group by tablespace_name) B
+ON a.tablespace_name=b.tablespace_name WHERE a.tablespace_name='${5}';
+EOF`
 
-    ts_free=`echo $result | awk '{print int($1)}'` 
-    ts_total=`echo $result | awk '{print int($2)}'` 
-    ts_pct=`echo $result | awk '{print int($3)}'` 
-    if [ $ts_pct -ge ${6} ] ; then
-       echo "${4} : ${5} CRITICAL - $ts_pct% used [ $ts_free / $ts_total MB available ]"
+    if [ -n "`echo $result | grep ORA-`" ] ; then
+      error=` echo "$result" | grep "ORA-" | head -1`
+      echo "CRITICAL - $error"
+      exit $STATE_CRITICAL
+    fi
+
+    ts_free=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($1)}'` 
+    ts_total=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($2)}'` 
+    ts_pct=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($3)}'` 
+    ts_pctx=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print $3}'` 
+    if [ "$ts_free" -eq 0 -a "$ts_total" -eq 0 -a "$ts_pct" -eq 0 ] ; then
+        echo "No data returned by Oracle - tablespace $5 not found?"
+        exit $STATE_UNKNOWN
+    fi
+    if [ "$ts_pct" -ge ${6} ] ; then
+       echo "${2} : ${5} CRITICAL - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
        exit $STATE_CRITICAL
     fi
-    if [ $ts_pct -ge ${7} ] ; then
-       echo "${4} : ${5} WARNING  - $ts_pct% used [ $ts_free / $ts_total MB available ]"
+    if [ "$ts_pct" -ge ${7} ] ; then
+       echo "${2} : ${5} WARNING  - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
        exit $STATE_WARNING
     fi
-    echo "${4} : ${5} OK - $ts_pct% used [ $ts_free / $ts_total MB available ]"
+    echo "${2} : ${5} OK - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
     exit $STATE_OK
     ;;
 *)