前段時間寫了一個監控Oracle表空間百分比的shell腳本,功能是實現了,但是發送的報警郵件內容排版不是很好,今天偷巧稍微改動了下,排版還不錯。不多說了,詳情腳本:
#!/bin/bash # # Description: Check local Oracle database tablespace usage rate, # It will send mail to admin when then usage rate more than 95%. # logfile='/tmp/tablespace_check.log' tmpfile='/tmp/tablespace_check.txt' num=0 . ~oracle/.bash_profile echo "From: szmlserver95_66<oracle@szmlserver95_66.abc.com> To: kavin_yang<[email protected]> Content-type: text/html;charset=UTF-8 Subject: Tablespace Usage Rate more than 95% <pre>" > $tmpfile sqlplus / as sysdba << kavinEOF set feedback off; spool $tmpfile append; select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "Used MB",b.bytes/1024/1024 "Free MB",round(((a.bytes-b.bytes)/a.bytes)*100,2) "Percent_used" from (select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a, (select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name) b where a.tablespace_name=b.tablespace_name order by ((a.bytes-b.bytes)/a.bytes) desc; spool off; set feedback on; exit; kavinEOF if [ $? -ne 0 ];then mail -s "Tablespace Usage Rate run fail-95.66" [email protected] < $logfile exit 1 fi p_zyk_qd=`awk '$1 ~ /ZYK_QD/ {print $5}' $tmpfile` p_stat_data=`awk '$1 ~ /STAT_DATA/ {print $5}' $tmpfile` p_all_mchn_vst=`awk '$1 ~ /ALL_MCHN_VST/ {print $5}' $tmpfile` p_zy_mchn_vst=`awk '$1 ~ /ZY_MCHN_VST/ {print $5}' $tmpfile` p_app_log=`awk '$1 ~ /APP_LOG/ {print $5}' $tmpfile` for i in $p_zyk_qd $p_stat_data $p_all_mchn_vst $p_zy_mchn_vst $p_app_log do if [ $(echo "$i >= 90" | bc ) = 1 ];then num=$((num+1)) fi done if [ $num -gt 0 ];then echo "</pre>" >> $tmpfile cat "$tmpfile" | sendmail -t fi rm -f $tmpfile exit 0
示例圖如下: