Chinaunix首页 | 论坛 | 博客
  • 博客访问: 592737
  • 博文数量: 57
  • 博客积分: 877
  • 博客等级: 准尉
  • 技术积分: 1275
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-24 16:16
文章分类

全部博文(57)

文章存档

2014年(2)

2013年(15)

2012年(20)

2011年(20)

我的朋友

分类: Python/Ruby

2012-06-11 13:51:04

    前几天面试的时候,面试官问我怎么用shell 获取alert_sid.log 中的一段错误信息呢,当时由于有点小紧张,没有理清思路,比较混乱的回答了一下,今天突然又想到了这个问题,就花了些时间把这个脚本写了一下,脚本如下:

点击(此处)折叠或打开

  1. #! /bin/bash
  2. CURRPATH=`pwd`
  3. TMPFILE1=$CURRPATH/tmp1
  4. TMPFILE2=$CURRPATH/tmp2
  5. echo > $TMPFILE1
  6. echo > $TMPFILE2
  7. if [ $# -eq 1 ]
  8. then
  9. /usr/bin/tail -n 1000 $1 >> $TMPFILE2
  10. fi
  11. if [ $# -eq 2 ]
  12. then
  13. /usr/bin/tail -n $2 $1 >> $TMPFILE2
  14. else
  15. exit 1
  16. fi
  17. while read LINE
  18. do
  19. echo $LINE | grep '^[M,T,W,T,F,S][o,u,e,h,r,a][n,e,d,u,i,t,n] ' >/dev/null
  20. FLAG=$?
  21. if [ $FLAG -eq 0 ]
  22. then
  23. echo "$CACHELINE" >> $TMPFILE1
  24. if cat $TMPFILE1 | grep [E,e]rror >> /dev/null
  25. then
  26. cat $TMPFILE1
  27. fi
  28. echo > $TMPFILE1
  29. fi
  30. if [ $FLAG -eq 1 ]
  31. then
  32. echo $CACHELINE >> $TMPFILE1
  33. fi
  34. CACHELINE=$LINE
  35. done < $TMPFILE2
$1 参数是指alert_sid 日志,$2 参数是指要截取 alert_sid.log末尾多少行数据,默认是截取末尾1000行
   
    脚本的大致思路是:把截取的日志先存到TMP2 文件中,然后从TMP2中每一行的获取,判断当前行是不是 日期行(从alert_sid.log 日志中观察,记录每段日志的时候总是有一行是记录当前的日期时间的)如果是,就把前一行重定向到tmp1文件中,并中tmp1文件grep出是否有错误,如果有,就屏幕输出并清空tmp1文件,没有就直接清空tmp1文件;如果当前行不是日期行,就直接把前一行直接重定向到tmp1文件中;
   以下是测试结果:

点击(此处)折叠或打开

  1. [root@ALL geterror]# sh geterror3.sh alert_sbqz.log 50
  2. Sun Jun 03 10:00:51 2012
  3. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j000_10127.trc:
  4. ORA-12012: error on auto execute of job 89514
  5. ORA-00376: file 10 cannot be read at this time
  6. ORA-01110: data file 10: '/oracle/oradata/sbqz/USER_DATA.5.dbf'
  7. ORA-06512: at "SYS.DBMS_ADVISOR", line 201
  8. ORA-06512: at "SYS.DBMS_SPACE", line 1619
  9. ORA-06512: at "SYS.DBMS_SPACE", line 1871
  10. Sun Jun 03 10:05:46 2012
  11. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  12. ORA-00376: file 23 cannot be read at this time
  13. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  14. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  15. ORA-00376: file 23 cannot be read at this time
  16. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  17. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  18. ORA-00376: file 23 cannot be read at this time
  19. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  20. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  21. ORA-00376: file 23 cannot be read at this time
  22. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  23. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  24. ORA-00376: file 23 cannot be read at this time
  25. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  26. Sun Jun 03 10:06:00 2012
  27. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  28. ORA-00376: file 23 cannot be read at this time
  29. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  30. Sun Jun 03 14:01:05 2012
  31. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j002_24418.trc:
  32. ORA-12012: error on auto execute of job 89516
  33. ORA-00376: file 10 cannot be read at this time
  34. ORA-01110: data file 10: '/oracle/oradata/sbqz/USER_DATA.5.dbf'
  35. ORA-06512: at "SYS.DBMS_ADVISOR", line 201
  36. ORA-06512: at "SYS.DBMS_SPACE", line 1619
  37. ORA-06512: at "SYS.DBMS_SPACE", line 1871
  38. Sun Jun 03 14:01:10 2012
  39. GATHER_STATS_JOB encountered errors. Check the trace file.
  40. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_j000_24414.trc:
  41. ORA-00604: error occurred at recursive SQL level 2
  42. ORA-00376: file 23 cannot be read at this time
  43. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  44. Sun Jun 03 14:11:11 2012
  45. Errors in file /oracle/app/oracle/diag/rdbms/sbqz/sbqz/trace/sbqz_smon_6462.trc:
  46. ORA-00376: file 23 cannot be read at this time
  47. ORA-01110: data file 23: '/oracle/oradata/sbqz/undotbs02.dbf'
  48. [root@ALL geterror]#

阅读(5100) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~