转载

剖析Oracle中oerr命令

Oralce中的命令非常丰富,oerr命令是一个不错的辅助工具,很多看起来没有眉目的错误代码,可以让DBA很快定位问题的缘由,我们根本不需要去记有哪些ORA错误,除非那些错误已经完全和你的工作分不开。
绝大多数的命令都是二进制的形式,比如sqlplus我们可一窥其中的奥妙,oerr是一个shell脚本,而且实现原理也不难,我们来剖析一下,看看这个工具的设计思想。
首先这个工具位于$ORACLE_HOME/bin下,直接看还看不出是个shell脚本。
[oracle@db117 ~]$ ll $ORACLE_HOME/bin/oerr
-rwxr-xr-x 1 oracle oinstall 2567 Apr 23  2014 /U01/app/oracle/product/11.2.0.4/bin/oerr
但是我就是喜欢折腾,所以无意中就打开了这个文件。可以看到这个脚本的注释如下:
# Usage: oerr facility error
#
# This shell script is used to get the description and the cause and action
# of an error from a message text file when a list of error numbers are passed
# to it.  It supports different language environments and errors from different
# facilities.
当然脚本的完整内容也没有多少行,我们来简单看看,有一个配置文件facility.lis
Facilities_File=$ORACLE_HOME/lib/facility.lis
需要从这个配置文件中取得相应的component,比如我们使用命令oerr ora 12345,则ora就是第一个参数,根据这个参数可以得到接下来需要使用的component值。
脚本里面有如下一行类似的内容,我们带入参数ora,当然还可以输入tns等等。
Fac_Info=`grep -i "^${Facility}:" $Facilities_File 2> /dev/null`
我们转换一下,可以看到得到的component是rdbms
[oracle@db117 ~]$ grep -i "^ora:" $ORACLE_HOME/lib/facility.lis
ora:rdbms:*:
至于这一行内容的解释,在文件中也可以轻松找到。
#
# The entries in this file are colon separated defining, for each
# facility (field 1), the component name (field 2), the "real" facility
# name for facilities with aliases (field 3) with a value of "*" for
# facilities without renamings and an optional facility description
# (field 4)
#
#       facility:component:rename:description
#

里面的内容如下:
acfs:usm:*:
acfsk:usm:*:
advm:usm:acfs:
advmk:usm:*:
amd:cwmlite:*:
amdu:rdbms:*:
当然这只是开始,错误信息的文件在一个指定的文件中,比如component是rdbms,则错误信息的文件为$ORACLE_HOME/rdbms/mesg
[oracle@db117 ~]$ cd $ORACLE_HOME/rdbms/mesg
而错误信息的文件也是有规律的,和ora相关的一个文件,oraus.msg也是大体这样的格式。
[oracle@db117 mesg]$ ll ora*us*msg
-rw-r--r-- 1 oracle oinstall 4976647 Apr 23  2014 oraus.msg

比方这个时候我们移步到错误信息的文件中,找到了下面两行错误信息。
64477, 00000, "Multiple token tables are not supported."
// *Cause:   An attempt was made to create a new token table. If encountered
//           during an import, a critical patch was possibly missing on the
//           export database.
// *Action:  Use the default token table. If encountered during an import,
//           apply the appropriate patch on the export database and try the
//           export and import again.

64621, 00000, "SQL statement length (%s) exceeds maximum allowed length (%s)"
// *Cause:  An attempt was made to issue a SQL statement that exceeded the
//          maximum allowed length of a statement.
// *Action: Modify the SQL statement or the views to which it refers to fit
//          within the maximum length or split the SQL statement.
//
这个时候怎么去解析读取这个文件的呢,
首先就是解析错误编码,比如输入错误编码64621,我们也可以输入064621等,这个地方都会使用sed来先转换一下,保证能够找到完全匹配的内容。
Code=`echo 64621|/bin/sed 's/^[0]*//'`
[oracle@db117 bin]$ echo 64621|/bin/sed 's/^[0]*//'
64621
[oracle@db117 bin]$ echo 064621|/bin/sed 's/^[0]*//'
64621
假设这个时候我们就看64621的错误信息,这个时候就可以使用awk来读取文件的内容。Oracle的调用方式类似下面的形式。
Code=64621
[oracle@db117 bin]$ awk "BEGIN { found = 0; }
        /^[0]*$Code/    { found = 1; print ; next;}
        /^/////         { if (found) { print; } next; }
                        { if (found) { exit; } }" $ORACLE_HOME/rdbms/mesg/oraus.msg
64621, 00000, "SQL statement length (%s) exceeds maximum allowed length (%s)"
// *Cause:  An attempt was made to issue a SQL statement that exceeded the
//          maximum allowed length of a statement.
// *Action: Modify the SQL statement or the views to which it refers to fit
//          within the maximum length or split the SQL statement.
//
所以学以致用,我们也可以仿照这种方式简单定制符合自己需求的内容。
正文到此结束
Loading...