前几天面试的时候,面试官问我怎么用shell 获取alert_sid.log 中的一段错误信息呢,当时由于有点小紧张,没有理清思路,比较混乱的回答了一下,今天突然又想到了这个问题,就花了些时间把这个脚本写了一下,脚本如下:
-
#! /bin/bash
-
CURRPATH=`pwd`
-
TMPFILE1=$CURRPATH/tmp1
-
TMPFILE2=$CURRPATH/tmp2
-
-
echo > $TMPFILE1
-
echo > $TMPFILE2
-
-
if [ $# -eq 1 ]
-
then
-
/usr/bin/tail -n 1000 $1 >> $TMPFILE2
-
fi
-
if [ $# -eq 2 ]
-
then
-
/usr/bin/tail -n $2 $1 >> $TMPFILE2
-
else
-
exit 1
-
fi
-
-
while read LINE
-
do
-
echo $LINE | grep '^[M,T,W,T,F,S][o,u,e,h,r,a][n,e,d,u,i,t,n] ' >/dev/null
-
FLAG=$?
-
if [ $FLAG -eq 0 ]
-
then
-
echo "$CACHELINE" >> $TMPFILE1
-
if cat $TMPFILE1 | grep [E,e]rror >> /dev/null
-
then
-
cat $TMPFILE1
-
fi
-
echo > $TMPFILE1
-
fi
-
if [ $FLAG -eq 1 ]
-
then
-
echo $CACHELINE >> $TMPFILE1
-
fi
-
CACHELINE=$LINE
-
done < $TMPFILE2
$1 参数是指alert_sid 日志,$2 参数是指要截取 alert_sid.log末尾多少行数据,默认是截取末尾1000行
脚本的大致思路是:把截取的日志先存到TMP2 文件中,然后从TMP2中每一行的获取,判断当前行是不是 日期行(从alert_sid.log 日志中观察,记录每段日志的时候总是有一行是记录当前的日期时间的)如果是,就把前一行重定向到tmp1文件中,并中tmp1文件grep出是否有错误,如果有,就屏幕输出并清空tmp1文件,没有就直接清空tmp1文件;如果当前行不是日期行,就直接把前一行直接重定向到tmp1文件中;
以下是测试结果:
-
[root@ALL geterror]# sh geterror3.sh alert_sbqz.log 50
-
-
Sun Jun 03 10:00:51 2012
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j000_10127.trc:
-
ORA-12012: error on auto execute of job 89514
-
ORA-00376: file 10 cannot be read at this time
-
ORA-01110: data file 10: '/oracle/oradata/sbqz/USER_DATA.5.dbf'
-
ORA-06512: at "SYS.DBMS_ADVISOR", line 201
-
ORA-06512: at "SYS.DBMS_SPACE", line 1619
-
ORA-06512: at "SYS.DBMS_SPACE", line 1871
-
-
Sun Jun 03 10:05:46 2012
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
-
Sun Jun 03 10:06:00 2012
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
-
Sun Jun 03 14:01:05 2012
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j002_24418.trc:
-
ORA-12012: error on auto execute of job 89516
-
ORA-00376: file 10 cannot be read at this time
-
ORA-01110: data file 10: '/oracle/oradata/sbqz/USER_DATA.5.dbf'
-
ORA-06512: at "SYS.DBMS_ADVISOR", line 201
-
ORA-06512: at "SYS.DBMS_SPACE", line 1619
-
ORA-06512: at "SYS.DBMS_SPACE", line 1871
-
-
Sun Jun 03 14:01:10 2012
-
GATHER_STATS_JOB encountered errors. Check the trace file.
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j000_24414.trc:
-
ORA-00604: error occurred at recursive SQL level 2
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
-
Sun Jun 03 14:11:11 2012
-
Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
-
ORA-00376: file 23 cannot be read at this time
-
ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
-
[root@ALL geterror]#
阅读(5100) | 评论(0) | 转发(0) |