Fix typo
[monitoring-plugins.git] / plugins-scripts / check_oracle.sh
blob664795eb29932526047f3408467ffd3aefdb7600
1 #!/bin/sh
3 # latigid010@yahoo.com
4 # 01/06/2000
6 # This Nagios plugin was created to check Oracle status
9 PROGNAME=`basename $0`
10 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
11 REVISION="@NP_VERSION@"
13 . $PROGPATH/utils.sh
16 print_usage() {
17 echo "Usage:"
18 echo " $PROGNAME --tns <Oracle Sid or Hostname/IP address>"
19 echo " $PROGNAME --db <ORACLE_SID>"
20 echo " $PROGNAME --login <ORACLE_SID>"
21 echo " $PROGNAME --cache <ORACLE_SID> <USER> <PASS> <CRITICAL> <WARNING>"
22 echo " $PROGNAME --tablespace <ORACLE_SID> <USER> <PASS> <TABLESPACE> <CRITICAL> <WARNING>"
23 echo " $PROGNAME --oranames <Hostname>"
24 echo " $PROGNAME --help"
25 echo " $PROGNAME --version"
28 print_help() {
29 print_revision $PROGNAME $REVISION
30 echo ""
31 print_usage
32 echo ""
33 echo "Check Oracle status"
34 echo ""
35 echo "--tns SID/IP Address"
36 echo " Check remote TNS server"
37 echo "--db SID"
38 echo " Check local database (search /bin/ps for PMON process) and check"
39 echo " filesystem for sgadefORACLE_SID.dbf"
40 echo "--login SID"
41 echo " Attempt a dummy login and alert if not ORA-01017: invalid username/password"
42 echo "--cache"
43 echo " Check local database for library and buffer cache hit ratios"
44 echo " ---> Requires Oracle user/password and SID specified."
45 echo " ---> Requires select on v_$sysstat and v_$librarycache"
46 echo "--tablespace"
47 echo " Check local database for tablespace capacity in ORACLE_SID"
48 echo " ---> Requires Oracle user/password specified."
49 echo " ---> Requires select on dba_data_files and dba_free_space"
50 echo "--oranames Hostname"
51 echo " Check remote Oracle Names server"
52 echo "--help"
53 echo " Print this help screen"
54 echo "--version"
55 echo " Print version and license information"
56 echo ""
57 echo "If the plugin doesn't work, check that the ORACLE_HOME environment"
58 echo "variable is set, that ORACLE_HOME/bin is in your PATH, and the"
59 echo "tnsnames.ora file is locatable and is properly configured."
60 echo ""
61 echo "When checking local database status your ORACLE_SID is case sensitive."
62 echo ""
63 echo "If you want to use a default Oracle home, add in your oratab file:"
64 echo "*:/opt/app/oracle/product/7.3.4:N"
65 echo ""
66 support
69 case "$1" in
71 cmd='--tns'
74 cmd='--db'
77 cmd="$1"
79 esac
81 # Information options
82 case "$cmd" in
83 --help)
84 print_help
85 exit $STATE_OK
87 -h)
88 print_help
89 exit $STATE_OK
91 --version)
92 print_revision $PROGNAME $REVISION
93 exit $STATE_OK
95 -V)
96 print_revision $PROGNAME $REVISION
97 exit $STATE_OK
99 esac
101 # Hunt down a reasonable ORACLE_HOME
102 if [ -z "$ORACLE_HOME" ] ; then
103 # Adjust to taste
104 for oratab in /var/opt/oracle/oratab /etc/oratab
106 [ ! -f $oratab ] && continue
107 ORACLE_HOME=`IFS=:
108 while read SID ORACLE_HOME junk;
110 if [ "$SID" = "$2" -o "$SID" = "*" ] ; then
111 echo $ORACLE_HOME;
112 exit;
114 done < $oratab`
115 [ -n "$ORACLE_HOME" ] && break
116 done
118 # Last resort
119 [ -z "$ORACLE_HOME" -a -d $PROGPATH/oracle ] && ORACLE_HOME=$PROGPATH/oracle
121 if [ "$cmd" != "--db" ]; then
122 if [ -z "$ORACLE_HOME" -o ! -d "$ORACLE_HOME" ] ; then
123 echo "Cannot determine ORACLE_HOME for sid $2"
124 exit $STATE_UNKNOWN
127 PATH=$PATH:$ORACLE_HOME/bin
128 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
129 export ORACLE_HOME PATH LD_LIBRARY_PATH
131 case "$cmd" in
132 --tns)
133 tnschk=` tnsping $2`
134 tnschk2=` echo $tnschk | grep -c OK`
135 if [ ${tnschk2} -eq 1 ] ; then
136 tnschk3=` echo $tnschk | sed -e 's/.*(//' -e 's/).*//'`
137 echo "OK - reply time ${tnschk3} from $2"
138 exit $STATE_OK
139 else
140 echo "No TNS Listener on $2"
141 exit $STATE_CRITICAL
144 --oranames)
145 namesctl status $2 | awk '
146 /Server has been running for:/ {
147 msg = "OK: Up"
148 for (i = 6; i <= NF; i++) {
149 msg = msg " " $i
151 status = '$STATE_OK'
153 /error/ {
154 msg = "CRITICAL: " $0
155 status = '$STATE_CRITICAL'
157 END {
158 print msg
159 exit status
162 --db)
163 pmonchk=`ps -ef | grep -v grep | grep -c "ora_pmon_${2}$"`
164 if [ ${pmonchk} -ge 1 ] ; then
165 echo "${2} OK - ${pmonchk} PMON process(es) running"
166 exit $STATE_OK
167 #if [ -f $ORACLE_HOME/dbs/sga*${2}* ] ; then
168 #if [ ${pmonchk} -eq 1 ] ; then
169 #utime=`ls -la $ORACLE_HOME/dbs/sga*$2* | cut -c 43-55`
170 #echo "${2} OK - running since ${utime}"
171 #exit $STATE_OK
173 else
174 echo "${2} Database is DOWN"
175 exit $STATE_CRITICAL
178 --login)
179 loginchk=`sqlplus dummy/user@$2 < /dev/null`
180 loginchk2=` echo $loginchk | grep -c ORA-01017`
181 if [ ${loginchk2} -eq 1 ] ; then
182 echo "OK - dummy login connected"
183 exit $STATE_OK
184 else
185 loginchk3=` echo "$loginchk" | grep "ORA-" | head -1`
186 echo "CRITICAL - $loginchk3"
187 exit $STATE_CRITICAL
190 --cache)
191 if [ ${5} -gt ${6} ] ; then
192 echo "UNKNOWN - Warning level is less then Crit"
193 exit $STATE_UNKNOWN
195 result=`sqlplus -s ${3}/${4}@${2} << EOF
196 set pagesize 0
197 set numf '9999999.99'
198 select (1-(pr.value/(dbg.value+cg.value)))*100
199 from v\\$sysstat pr, v\\$sysstat dbg, v\\$sysstat cg
200 where pr.name='physical reads'
201 and dbg.name='db block gets'
202 and cg.name='consistent gets';
203 EOF`
205 if [ -n "`echo $result | grep ORA-`" ] ; then
206 error=` echo "$result" | grep "ORA-" | head -1`
207 echo "CRITICAL - $error"
208 exit $STATE_CRITICAL
211 buf_hr=`echo "$result" | awk '/^[0-9\. \t]+$/ {print int($1)}'`
212 buf_hrx=`echo "$result" | awk '/^[0-9\. \t]+$/ {print $1}'`
213 result=`sqlplus -s ${3}/${4}@${2} << EOF
214 set pagesize 0
215 set numf '9999999.99'
216 select sum(lc.pins)/(sum(lc.pins)+sum(lc.reloads))*100
217 from v\\$librarycache lc;
218 EOF`
220 if [ -n "`echo $result | grep ORA-`" ] ; then
221 error=` echo "$result" | grep "ORA-" | head -1`
222 echo "CRITICAL - $error"
223 exit $STATE_CRITICAL
226 lib_hr=`echo "$result" | awk '/^[0-9\. \t]+$/ {print int($1)}'`
227 lib_hrx=`echo "$result" | awk '/^[0-9\. \t]+$/ {print $1}'`
229 if [ $buf_hr -le ${5} -o $lib_hr -le ${5} ] ; then
230 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"
231 exit $STATE_CRITICAL
233 if [ $buf_hr -le ${6} -o $lib_hr -le ${6} ] ; then
234 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"
235 exit $STATE_WARNING
237 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"
239 exit $STATE_OK
241 --tablespace)
242 if [ ${6} -lt ${7} ] ; then
243 echo "UNKNOWN - Warning level is more then Crit"
244 exit $STATE_UNKNOWN
246 result=`sqlplus -s ${3}/${4}@${2} << EOF
247 set pagesize 0
248 set numf '9999999.99'
249 select NVL(b.free,0.0),a.total,100 - trunc(NVL(b.free,0.0)/a.total * 1000) / 10 prc
250 from (
251 select tablespace_name,sum(bytes)/1024/1024 total
252 from dba_data_files group by tablespace_name) A
253 LEFT OUTER JOIN
254 ( select tablespace_name,sum(bytes)/1024/1024 free
255 from dba_free_space group by tablespace_name) B
256 ON a.tablespace_name=b.tablespace_name WHERE a.tablespace_name='${5}';
257 EOF`
259 if [ -n "`echo $result | grep ORA-`" ] ; then
260 error=` echo "$result" | grep "ORA-" | head -1`
261 echo "CRITICAL - $error"
262 exit $STATE_CRITICAL
265 ts_free=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($1)}'`
266 ts_total=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($2)}'`
267 ts_pct=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print int($3)}'`
268 ts_pctx=`echo "$result" | awk '/^[ 0-9\.\t ]+$/ {print $3}'`
269 if [ "$ts_free" -eq 0 -a "$ts_total" -eq 0 -a "$ts_pct" -eq 0 ] ; then
270 echo "No data returned by Oracle - tablespace $5 not found?"
271 exit $STATE_UNKNOWN
273 if [ "$ts_pct" -ge ${6} ] ; then
274 echo "${2} : ${5} CRITICAL - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
275 exit $STATE_CRITICAL
277 if [ "$ts_pct" -ge ${7} ] ; then
278 echo "${2} : ${5} WARNING - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
279 exit $STATE_WARNING
281 echo "${2} : ${5} OK - $ts_pctx% used [ $ts_free / $ts_total MB available ]|${5}=$ts_pctx%;${7};${6};0;100"
282 exit $STATE_OK
285 print_usage
286 exit $STATE_UNKNOWN
287 esac