system: rhel 5.5
db: oracle 10.2.0.5.0
本人对shell脚本一窍不通,顶多只认识echo,if语句,连语法是怎样都不清楚,没关系,我就直接拿AIX上现有的系统监控脚本模仿修改了一下,修改后的脚本如下:
$ cat /oradata/db_monitor/hr_monitor.sh
#!/usr/bin/ksh
#check file system
echo "========`date '+%Y-%m-%d %H:%M:%S'`=========="
echo "========BEGIN-OF-THE-SCRIPT-OUTPUT=========="
i=0
df -h|grep -v "tmpfs"|grep -v "Use"|awk '{print $5,$6}'|while read Use Filesystem
do
if [ "`echo ${Use}|awk -F% '{print $1}'`" -gt "80" ]
then
echo "Filesystem ${Filesystem} have use ${Use}!"
i=1
fi
done
if [ ${i} -eq "0" ]
then
echo "Filesystem usage ok!"
fi
echo "========================="
if [ "`ps -ef|grep pmon|grep -v grep|wc -l`" -eq "1" ]
then
echo "Oracle is Running!"
echo "========================="
else
echo "Oracle is Down!"
echo "========================="
fi
#check hr_tset login
sqlplus -s hr_test/hr_test << ! > /tmp/hr_test_db_connect
set heading off
set term off
set feedback off
select 1000/40 from dual
/
Exit
!
if [ "`cat /tmp/hr_test_db_connect|grep -v "^$"|awk '{print $1}'`" = "25" ]
then
echo "Instance ytohrdb connect is OK!"
echo "========================="
else
echo "Instance ytohrdb connect is FAIL!"
echo "========================="
fi
#check db tablespace
sqlplus -s system/system << ! > /tmp/tablespace_used
set heading off
set feedback off
set pagesize 0
@/oradata/db_monitor/shell_scripts/check_tablespace.sql
exit
!
if [ "`cat /tmp/tablespace_used`" != "" ]
then
cat /tmp/tablespace_used |grep -v "^$"| while read line
do
tablespace=`echo $line | awk '{print $1}'`
usage=`echo $line | awk '{print $4}'`
free_mb=`echo $line |awk '{print $5}'`
echo "Tablespace ${tablespace} has used ${usage}%,there are ${free_m}M Free Space!"
done
echo "==============END-OF-THE-SCRIPT-OUTPUT================"
else
echo "All the tablespace was been checked! It's OK! "
echo "==============END-OF-THE-SCRIPT-OUTPUT================"
fi
其中,/oradata/db_monitor/shell_script/check_tablespace.sql的脚本如下:
column TABLESPACE_NAME format a30 HEADING "TABLESPACE_NAME"
column MEGS_ALLOC format 9999,999,999,999 HEADING "MEGS_ALLOC"
column USED_OF_MAX format 9999,999,999,999 HEADING "USED_OF_MAX"
column FREE_OF_MAX format 9999,999,999,999 HEADING "FREE_OF_MAX"
column MAX format 9999,999,999,999 HEADING "MAX"
SELECT SIZE_INFO.TABLESPACE_NAME,
SIZE_INFO.MEGS_ALLOC,
SIZE_INFO.MEGS_USED,
SIZE_INFO.USED_OF_MAX,
SIZE_INFO.FREE_OF_MAX,
SIZE_INFO.MAX
FROM (
SELECT A.TABLESPACE_NAME,
ROUND(A.BYTES_ALLOC / 1024 / 1024) MEGS_ALLOC,
ROUND((A.BYTES_ALLOC - NVL(B.BYTES_FREE, 0)) / 1024 / 1024) MEGS_USED,
ROUND((A.BYTES_ALLOC - NVL(B.BYTES_FREE, 0))*100/A.MAXBYTES) USED_OF_MAX,
ROUND((A.MAXBYTES - A.BYTES_ALLOC + NVL(B.BYTES_FREE, 0))/1048576) FREE_OF_MAX,
ROUND(A.MAXBYTES / 1048576) MAX
FROM (SELECT F.TABLESPACE_NAME,
SUM(F.BYTES) BYTES_ALLOC,
SUM(DECODE(F.AUTOEXTENSIBLE, 'YES', F.MAXBYTES, 'NO', F.BYTES)) MAXBYTES
FROM DBA_DATA_FILES F
GROUP BY TABLESPACE_NAME) A,
(SELECT F.TABLESPACE_NAME, SUM(F.BYTES) BYTES_FREE
FROM DBA_FREE_SPACE F
GROUP BY TABLESPACE_NAME) B
WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME(+)
) SIZE_INFO
WHERE SIZE_INFO.USED_OF_MAX > 80;
上面SHELL脚本的一些代码,可以在HP-UX下执行原始SCRIPT看看:
$ date "+%Y-%m-%d %H:%M:%S"
2012-10-18 08:55:19
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/cciss/c0d0p10 4.9G 453M 4.2G 10% /
/dev/cciss/c0d0p9 4.9G 1.6G 3.1G 34% /home
/dev/cciss/c0d0p8 4.9G 392M 4.3G 9% /var
/dev/cciss/c0d0p7 9.7G 151M 9.1G 2% /tmp
/dev/cciss/c0d0p6 9.7G 4.4G 4.9G 48% /usr
/dev/cciss/c0d0p5 30G 3.5G 25G 13% /opt
/dev/cciss/c0d0p2 436G 259G 154G 63% /oradata
/dev/cciss/c0d0p1 996M 40M 905M 5% /boot
tmpfs 16G 0 16G 0% /dev/shm
$ df -h|grep -v "tmpfs"|grep -v "Use"
/dev/cciss/c0d0p10 4.9G 453M 4.2G 10% /
/dev/cciss/c0d0p9 4.9G 1.6G 3.1G 34% /home
/dev/cciss/c0d0p8 4.9G 392M 4.3G 9% /var
/dev/cciss/c0d0p7 9.7G 151M 9.1G 2% /tmp
/dev/cciss/c0d0p6 9.7G 4.4G 4.9G 48% /usr
/dev/cciss/c0d0p5 30G 3.5G 25G 13% /opt
/dev/cciss/c0d0p2 436G 259G 154G 63% /oradata
/dev/cciss/c0d0p1 996M 40M 905M 5% /boot
$ df -h|grep -v "tmpfs"|grep -v "Use"|awk '{print $5,$6}'
10% /
34% /home
9% /var
2% /tmp
48% /usr
13% /opt
63% /oradata
5% /boot
$ df -h|grep -v "tmpfs"|grep -v "Use"|awk '{print $5}'|awk -F% '{print $1}'
10
34
9
2
48
13
63
5
$ps -ef|grep pmon|grep -v grep|wc -l
1
由AIX中的脚本修改过程中,连猜带分段测试,看每一步的执行结果,最终改写成在HP-UX下的监控脚本,各位朋友也可以根据本人HP-UX下的脚本来测试改写,相信不一需要把SHELL全部学完,才会写SHELL;另外,在改写的过程中,有些管道命令还是不太清楚,[例如awk -F%,-F后面跟上域分隔符%]其实免不了BAIDU一下了。(当然,并不是鼓励大家不要去学习SHELL基础,我这是赶鸭子上架,没办法了,才这样干的!大家不要误会哦!)。
总结:
其中比较有意思的是df -h|grep -v "tmpfs"|grep -v "Use"|awk '{print $5,$6}'|while read Use Filesystem,
根据df -h|grep -v "tmpfs"|grep -v "Use"|awk '{print $5,$6}' 的结果集,输出第5、6个参数,while read到Use、Filesystem两个参数里
do与done是循环,一起结合使用;
其中在后面的cat /tmp/tablespace_used |grep -v "^$"| while read line也有使用,同理,这里是将一行的值读入参数line;
另外,注意if与then使用时,必须分行,像本人以前是做开发的,习惯性地把if [ "`cat /tmp/tablespace_used`" != "" ] 后面直接接then,结果报错;